Skip to content

Commit

Permalink
fix: 🐛 forward report channels
Browse files Browse the repository at this point in the history
  • Loading branch information
apotdevin committed Sep 3, 2020
1 parent 2f2da04 commit 7dddb9a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
38 changes: 23 additions & 15 deletions server/helpers/getNodeFromChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LndObject,
GetChannelType,
GetNodeType,
ChannelType,
} from 'server/types/ln-service.types';

const errorNode = {
Expand All @@ -15,24 +16,31 @@ const errorNode = {
export const getNodeFromChannel = async (
id: string,
publicKey: string,
lnd: LndObject | null
lnd: LndObject | null,
closedChannel?: ChannelType
) => {
const [channelInfo, channelError] = await toWithError(
getChannel({
lnd,
id,
})
);
let partnerPublicKey: string;

if (channelError || !channelInfo) {
logger.verbose(`Error getting channel with id ${id}: %o`, channelError);
return errorNode;
}
if (closedChannel?.partner_public_key) {
partnerPublicKey = closedChannel.partner_public_key;
} else {
const [channelInfo, channelError] = await toWithError(
getChannel({
lnd,
id,
})
);

const partnerPublicKey =
(channelInfo as GetChannelType).policies[0].public_key !== publicKey
? (channelInfo as GetChannelType).policies[0].public_key
: (channelInfo as GetChannelType).policies[1].public_key;
if (channelError || !channelInfo) {
logger.verbose(`Error getting channel with id ${id}: %o`, channelError);
return errorNode;
}

partnerPublicKey =
(channelInfo as GetChannelType).policies[0].public_key !== publicKey
? (channelInfo as GetChannelType).policies[0].public_key
: (channelInfo as GetChannelType).policies[1].public_key;
}

const [nodeInfo, nodeError] = await toWithError(
getNode({
Expand Down
19 changes: 14 additions & 5 deletions server/schema/widgets/resolvers/getForwardChannelsReport.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getForwards, getWalletInfo } from 'ln-service';
import { getForwards, getWalletInfo, getClosedChannels } from 'ln-service';
import { subHours, subDays } from 'date-fns';
import { sortBy } from 'underscore';
import { ContextType } from 'server/types/apiTypes';
import { getNodeFromChannel } from 'server/helpers/getNodeFromChannel';
import { requestLimiter } from 'server/helpers/rateLimiter';

import { to } from 'server/helpers/async';
import {
GetForwardsType,
GetWalletInfoType,
GetClosedChannelsType,
} from 'server/types/ln-service.types';
import { countArray, countRoutes } from './helpers';

Expand Down Expand Up @@ -38,18 +38,26 @@ export const getForwardChannelsReport = async (
startDate = subHours(endDate, 24);
}

const closedChannels = await to<GetClosedChannelsType>(
getClosedChannels({
lnd,
})
);

const getRouteAlias = (array: any[], publicKey: string) =>
Promise.all(
array.map(async channel => {
const nodeAliasIn = await getNodeFromChannel(
channel.in,
publicKey,
lnd
lnd,
closedChannels?.channels.find(c => c.id === channel.in)
);
const nodeAliasOut = await getNodeFromChannel(
channel.out,
publicKey,
lnd
lnd,
closedChannels?.channels.find(c => c.id === channel.out)
);

return {
Expand All @@ -68,7 +76,8 @@ export const getForwardChannelsReport = async (
const nodeAlias = await getNodeFromChannel(
channel.name,
publicKey,
lnd
lnd,
closedChannels?.channels.find(c => c.id === channel.name)
);
return {
alias: nodeAlias.alias,
Expand Down
2 changes: 2 additions & 0 deletions server/types/ln-service.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export type GetChannelType = {
}[];
};

export type GetClosedChannelsType = { channels: ChannelType[] };

export type GetChannelsType = { channels: ChannelType[] };

export type GetForwardsType = { forwards: ForwardType[]; next?: string };
Expand Down

0 comments on commit 7dddb9a

Please sign in to comment.