Skip to content

Commit 350096d

Browse files
authored
Merge pull request #229 from apotdevin/refactor-forwards
refactor: forwards query
2 parents 7c3cb6c + 449f8bc commit 350096d

File tree

98 files changed

+782
-413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+782
-413
lines changed

pages/forwards.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const ForwardsView = () => {
101101
<Separation />
102102
<ForwardChannelsReport days={days} order={infoType} />
103103
</Card>
104-
<SubTitle>Grouped by Peer</SubTitle>
104+
<SubTitle>Grouped by Channel</SubTitle>
105105
<Card>
106106
<ForwardTable days={days} order={infoType} />
107107
</Card>

server/helpers/rateLimiter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const RateConfig: RateConfigProps = {
1414
chainBalance: { max: 10, window: '5s' },
1515
pendingChainBalance: { max: 10, window: '5s' },
1616
channelBalance: { max: 10, window: '5s' },
17+
getChannel: { max: 1000, window: '5s' },
1718
};
1819

1920
const rateLimiter = getGraphQLRateLimiter({

server/schema/channel/resolvers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { logger } from 'server/helpers/logger';
22
import { toWithError } from 'server/helpers/async';
3-
import { getChannel } from 'ln-service';
3+
import { getChannel as getLnChannel } from 'ln-service';
44
import { ChannelType, GetChannelType } from 'server/types/ln-service.types';
55
import { openChannel } from './resolvers/mutation/openChannel';
66
import { closeChannel } from './resolvers/mutation/closeChannel';
@@ -10,6 +10,7 @@ import { getChannelBalance } from './resolvers/query/getChannelBalance';
1010
import { getChannels } from './resolvers/query/getChannels';
1111
import { getClosedChannels } from './resolvers/query/getClosedChannels';
1212
import { getPendingChannels } from './resolvers/query/getPendingChannels';
13+
import { getChannel } from './resolvers/query/getChannel';
1314

1415
type ParentType = {
1516
id: string;
@@ -21,6 +22,7 @@ type ParentType = {
2122

2223
export const channelResolvers = {
2324
Query: {
25+
getChannel,
2426
getChannelBalance,
2527
getChannels,
2628
getClosedChannels,
@@ -48,7 +50,7 @@ export const channelResolvers = {
4850
}
4951

5052
const [channel, error] = await toWithError<GetChannelType>(
51-
getChannel({ lnd, id })
53+
getLnChannel({ lnd, id })
5254
);
5355

5456
if (error) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getChannel as getLnChannel } from 'ln-service';
2+
import { ContextType } from 'server/types/apiTypes';
3+
import { to } from 'server/helpers/async';
4+
import { requestLimiter } from 'server/helpers/rateLimiter';
5+
import { GetChannelType } from 'server/types/ln-service.types';
6+
7+
export const getChannel = async (
8+
_: undefined,
9+
{ id, pubkey }: { id: string; pubkey?: string },
10+
{ ip, lnd }: ContextType
11+
) => {
12+
await requestLimiter(ip, 'getChannel');
13+
14+
const channel = await to<GetChannelType>(getLnChannel({ lnd, id }));
15+
16+
if (!pubkey) {
17+
return channel;
18+
}
19+
20+
let node_policies = null;
21+
let partner_node_policies = null;
22+
23+
channel.policies.forEach(policy => {
24+
if (pubkey && pubkey === policy.public_key) {
25+
node_policies = {
26+
...policy,
27+
node: { lnd, publicKey: policy.public_key },
28+
};
29+
} else {
30+
partner_node_policies = {
31+
...policy,
32+
node: { lnd, publicKey: policy.public_key },
33+
};
34+
}
35+
});
36+
37+
return {
38+
...(channel as GetChannelType),
39+
node_policies,
40+
partner_node_policies,
41+
};
42+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { subDays } from 'date-fns';
2+
import { to } from 'server/helpers/async';
3+
import { requestLimiter } from 'server/helpers/rateLimiter';
4+
import { ContextType } from 'server/types/apiTypes';
5+
import { GetForwardsType } from 'server/types/ln-service.types';
6+
import { getForwards as getLnForwards } from 'ln-service';
7+
import { sortBy } from 'lodash';
8+
9+
export const forwardsResolver = {
10+
Query: {
11+
getForwards: async (
12+
_: undefined,
13+
{ days }: { days: number },
14+
context: ContextType
15+
) => {
16+
await requestLimiter(context.ip, 'getForwardsPastDays');
17+
18+
const { lnd } = context;
19+
20+
const today = new Date();
21+
const startDate = subDays(today, days);
22+
23+
const forwardsList = await to<GetForwardsType>(
24+
getLnForwards({
25+
lnd,
26+
after: startDate,
27+
before: today,
28+
})
29+
);
30+
31+
let forwards = forwardsList.forwards;
32+
let next = forwardsList.next;
33+
34+
let finishedFetching = false;
35+
36+
if (!next || !forwards || forwards.length <= 0) {
37+
finishedFetching = true;
38+
}
39+
40+
while (!finishedFetching) {
41+
if (next) {
42+
const moreForwards = await to<GetForwardsType>(
43+
getLnForwards({ lnd, token: next })
44+
);
45+
forwards = [...forwards, ...moreForwards.forwards];
46+
next = moreForwards.next;
47+
} else {
48+
finishedFetching = true;
49+
}
50+
}
51+
52+
return sortBy(forwards, 'created_at').reverse();
53+
},
54+
},
55+
};

server/schema/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { lnMarketsResolvers } from './lnmarkets/resolvers';
4444
import { lnMarketsTypes } from './lnmarkets/types';
4545
import { boltzResolvers } from './boltz/resolvers';
4646
import { boltzTypes } from './boltz/types';
47+
import { forwardsResolver } from './forwards/resolvers';
4748

4849
const typeDefs = [
4950
generalTypes,
@@ -94,7 +95,8 @@ const resolvers = merge(
9495
tbaseResolvers,
9596
lnUrlResolvers,
9697
lnMarketsResolvers,
97-
boltzResolvers
98+
boltzResolvers,
99+
forwardsResolver
98100
);
99101

100102
export const schema = makeExecutableSchema({ typeDefs, resolvers });
Lines changed: 2 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import {
2-
getPayments,
3-
getInvoices,
4-
getForwards as getLnForwards,
5-
getWalletInfo,
6-
getClosedChannels,
7-
} from 'ln-service';
8-
import { compareDesc, subDays } from 'date-fns';
1+
import { getPayments, getInvoices } from 'ln-service';
2+
import { compareDesc } from 'date-fns';
93
import { sortBy } from 'underscore';
104
import { ContextType } from 'server/types/apiTypes';
115
import { requestLimiter } from 'server/helpers/rateLimiter';
@@ -15,12 +9,7 @@ import {
159
GetPaymentsType,
1610
InvoiceType,
1711
PaymentType,
18-
GetForwardsType,
19-
GetWalletInfoType,
20-
GetClosedChannelsType,
2112
} from 'server/types/ln-service.types';
22-
import { logger } from 'server/helpers/logger';
23-
import { getNodeFromChannel } from './helpers';
2413

2514
type TransactionType = InvoiceType | PaymentType;
2615
type TransactionWithType = { isTypeOf: string } & TransactionType;
@@ -105,89 +94,10 @@ export const transactionResolvers = {
10594
resume,
10695
};
10796
},
108-
109-
getForwardsPastDays: async (
110-
_: undefined,
111-
{ days }: { days: number },
112-
context: ContextType
113-
) => {
114-
await requestLimiter(context.ip, 'getForwardsPastDays');
115-
116-
const { lnd } = context;
117-
118-
const today = new Date();
119-
const startDate = subDays(today, days);
120-
121-
const walletInfo = await to<GetWalletInfoType>(getWalletInfo({ lnd }));
122-
123-
const closedChannels = await to<GetClosedChannelsType>(
124-
getClosedChannels({ lnd })
125-
);
126-
127-
const forwardsList = await to<GetForwardsType>(
128-
getLnForwards({
129-
lnd,
130-
after: startDate,
131-
before: today,
132-
})
133-
);
134-
135-
let forwards = forwardsList.forwards;
136-
let next = forwardsList.next;
137-
138-
let finishedFetching = false;
139-
140-
if (!next || !forwards || forwards.length <= 0) {
141-
finishedFetching = true;
142-
}
143-
144-
while (!finishedFetching) {
145-
if (next) {
146-
const moreForwards = await to<GetForwardsType>(
147-
getLnForwards({ lnd, token: next })
148-
);
149-
forwards = [...forwards, ...moreForwards.forwards];
150-
next = moreForwards.next;
151-
} else {
152-
finishedFetching = true;
153-
}
154-
}
155-
156-
const final = forwards.map(f => ({
157-
...f,
158-
lnd,
159-
public_key: walletInfo.public_key,
160-
closed_channels: closedChannels.channels || [],
161-
}));
162-
163-
logger.debug(
164-
`Got a total of ${final.length} forwards for the past ${days} days`
165-
);
166-
167-
return sortBy(final, 'created_at').reverse();
168-
},
16997
},
17098
Transaction: {
17199
__resolveType(parent: TransactionWithType) {
172100
return parent.isTypeOf;
173101
},
174102
},
175-
Forward: {
176-
incoming_node(parent: any) {
177-
return getNodeFromChannel(
178-
parent.lnd,
179-
parent.incoming_channel,
180-
parent.public_key,
181-
parent.closed_channels
182-
);
183-
},
184-
outgoing_node(parent: any) {
185-
return getNodeFromChannel(
186-
parent.lnd,
187-
parent.outgoing_channel,
188-
parent.public_key,
189-
parent.closed_channels
190-
);
191-
},
192-
},
193103
};

server/schema/transactions/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ export const transactionTypes = gql`
99
mtokens: String!
1010
outgoing_channel: String!
1111
tokens: Int!
12-
incoming_node: ForwardNodeType
13-
outgoing_node: ForwardNodeType
1412
}
1513
1614
type ForwardNodeType {

server/schema/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const queryTypes = gql`
5151
getTimeHealth: channelsTimeHealth
5252
getFeeHealth: channelsFeeHealth
5353
getChannelBalance: channelBalanceType
54+
getChannel(id: String!, pubkey: String): singleChannelType!
5455
getChannels(active: Boolean): [channelType]!
5556
getClosedChannels(type: String): [closedChannelType]
5657
getPendingChannels: [pendingChannelType]
@@ -62,7 +63,7 @@ export const queryTypes = gql`
6263
decodeRequest(request: String!): decodeType
6364
getWalletInfo: walletInfoType
6465
getResume(token: String): getResumeType
65-
getForwardsPastDays(days: Int!): [Forward]!
66+
getForwards(days: Int!): [Forward]!
6667
getBitcoinPrice(logger: Boolean, currency: String): String
6768
getBitcoinFees(logger: Boolean): bitcoinFeeType
6869
getForwardChannelsReport(time: String, order: String, type: String): String

src/graphql/mutations/__generated__/addPeer.generated.tsx

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)