From c5842a6b06f26995ffd7080d916a39c298ccf6a9 Mon Sep 17 00:00:00 2001 From: Anthony Potdevin <31413433+apotdevin@users.noreply.github.com> Date: Tue, 12 Sep 2023 16:42:11 +0200 Subject: [PATCH] fix: balance push (#567) --- .../modules/api/amboss/amboss.helpers.ts | 24 ++++++++++ .../modules/api/amboss/amboss.service.ts | 47 +++++-------------- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/server/modules/api/amboss/amboss.helpers.ts b/src/server/modules/api/amboss/amboss.helpers.ts index 8189be9d..c5fa242a 100644 --- a/src/server/modules/api/amboss/amboss.helpers.ts +++ b/src/server/modules/api/amboss/amboss.helpers.ts @@ -1,3 +1,4 @@ +import { GetChannelsResult } from 'lightning'; import { EdgeInfo, NodeAlias } from './amboss.types'; export const mapNodeResult = ( @@ -15,3 +16,26 @@ export const mapEdgeResult = ( pk => edges.find(edge => edge.short_channel_id === pk) || null ); }; + +export const getMappedChannelInfo = ( + info: GetChannelsResult +): { + chan_id: string; + balance: string; + capacity: string; +}[] => { + if (!info?.channels?.length) return []; + return info.channels.map(c => { + const heldAmount = c.pending_payments.reduce((p, pp) => { + if (!pp) return p; + if (!pp.is_outgoing) return p; + return p + pp.tokens; + }, 0); + + return { + chan_id: c.id, + balance: (c.local_balance + heldAmount).toString(), + capacity: c.capacity + '', + }; + }); +}; diff --git a/src/server/modules/api/amboss/amboss.service.ts b/src/server/modules/api/amboss/amboss.service.ts index 7b7a8818..ae6e41fe 100644 --- a/src/server/modules/api/amboss/amboss.service.ts +++ b/src/server/modules/api/amboss/amboss.service.ts @@ -19,7 +19,11 @@ import { NodeService } from '../../node/node.service'; import { UserConfigService } from '../userConfig/userConfig.service'; import { getSHA256Hash } from 'src/server/utils/crypto'; import { orderBy } from 'lodash'; -import { mapEdgeResult, mapNodeResult } from './amboss.helpers'; +import { + getMappedChannelInfo, + mapEdgeResult, + mapNodeResult, +} from './amboss.helpers'; import { EdgeInfo, NodeAlias } from './amboss.types'; const ONE_MINUTE = 60 * 1000; @@ -457,22 +461,7 @@ export class AmbossService { is_public: true, }); - if (!channels.channels.length) return; - - const mapped = channels.channels.map(c => { - const heldAmount = c.pending_payments.reduce((p, pp) => { - if (!pp) return p; - if (!pp.is_outgoing) return p; - return p + pp.tokens; - }, 0); - - return { - chan_id: c.id, - balance: (c.local_balance + heldAmount).toString(), - capacity: c.capacity + '', - }; - }); - + const mapped = getMappedChannelInfo(channels); allChannels.push(...mapped); } @@ -482,31 +471,17 @@ export class AmbossService { { is_private: true } ); - if (!privateChannels.channels.length) return; - - const mapped = privateChannels.channels.map(c => { - const heldAmount = c.pending_payments.reduce((p, pp) => { - if (!pp) return p; - if (!pp.is_outgoing) return p; - return p + pp.tokens; - }, 0); - - return { - chan_id: c.id, - balance: (c.local_balance + heldAmount).toString(), - capacity: c.capacity + '', - }; - }); - + const mapped = getMappedChannelInfo(privateChannels); allChannels.push(...mapped); } const sortedChannels = orderBy(allChannels, ['chan_id'], ['desc']); if (sortedChannels.length) { - const infoString = sortedChannels.reduce((p, c) => { - return p + `${c.chan_id}${c.balance}${c.capacity || ''}`; - }, ''); + const infoString = sortedChannels.reduce( + (p, c) => p + `${c.chan_id}${c.balance}${c.capacity || ''}`, + '' + ); message += getSHA256Hash(infoString); }