From acdf43da20426869f445de5632fb8185c86f526f Mon Sep 17 00:00:00 2001 From: Anthony Potdevin Date: Wed, 30 Nov 2022 23:11:10 +0100 Subject: [PATCH] chore: balance changes --- src/server/modules/api/amboss/amboss.gql.ts | 6 +- .../modules/api/amboss/amboss.service.ts | 91 +++++++++++++++---- 2 files changed, 76 insertions(+), 21 deletions(-) diff --git a/src/server/modules/api/amboss/amboss.gql.ts b/src/server/modules/api/amboss/amboss.gql.ts index b358b1f3..b969afa0 100644 --- a/src/server/modules/api/amboss/amboss.gql.ts +++ b/src/server/modules/api/amboss/amboss.gql.ts @@ -91,8 +91,8 @@ export const pingHealthCheckMutation = gql` } `; -export const pushBalancesMutation = gql` - mutation PushBalances($input: BalancePushInput!) { - pushBalances(input: $input) +export const pushNodeBalancesMutation = gql` + mutation PushNodeBalances($input: ChannelBalancePushInput!) { + pushNodeBalances(input: $input) } `; diff --git a/src/server/modules/api/amboss/amboss.service.ts b/src/server/modules/api/amboss/amboss.service.ts index 90043251..63c3ffa2 100644 --- a/src/server/modules/api/amboss/amboss.service.ts +++ b/src/server/modules/api/amboss/amboss.service.ts @@ -9,13 +9,14 @@ import { AccountsService } from '../../accounts/accounts.service'; import { FetchService } from '../../fetch/fetch.service'; import { pingHealthCheckMutation, - pushBalancesMutation, + pushNodeBalancesMutation, saveBackupMutation, } from './amboss.gql'; import { auto, map, each } from 'async'; import { NodeService } from '../../node/node.service'; import { UserConfigService } from '../userConfig/userConfig.service'; import { getSHA256Hash } from 'src/server/utils/crypto'; +import { orderBy } from 'lodash'; const ONE_MINUTE = 60 * 1000; @@ -26,6 +27,24 @@ type NodeType = { lnd: any; }; +type ChannelBalanceInputType = { + signature: string; + timestamp: string; + pendingChannelBalance?: { + local: string; + total: string; + }; + onchainBalance?: { + confirmed: string; + pending: string; + }; + channelBalance?: { + local: string; + total: string; + }; + channels: { balance: string; capacity: string; chan_id: string }[]; +}; + @Injectable() export class AmbossService { constructor( @@ -68,16 +87,11 @@ export class AmbossService { } } - async pushBalancesToAmboss( - timestamp: string, - signature: string, - onchainBalance: string, - channels: { balance: string; capacity: string; chan_id: string }[] - ) { + async pushBalancesToAmboss(input: ChannelBalanceInputType) { const { data, error } = await this.fetchService.graphqlFetchWithProxy( this.ambossUrl, - pushBalancesMutation, - { input: { signature, timestamp, channels, onchainBalance } } + pushNodeBalancesMutation, + { input } ); if (!data?.pushBalances || error) { @@ -335,8 +349,45 @@ export class AmbossService { const { pending_chain_balance } = await this.nodeService.getPendingChainBalance(node.id); - onchain = (chain_balance + pending_chain_balance).toString(); - message += onchain; + onchain = { + confirmed: chain_balance + '', + pending: pending_chain_balance + '', + }; + + message += `${chain_balance}${pending_chain_balance}`; + } + + let pendingChannelBalance; + + if (channelPushEnabled) { + pendingChannelBalance = { + local: '0', + total: '0', + }; + + const { pending_channels } = + await this.nodeService.getPendingChannels(node.id); + + if (pending_channels.length) { + const amounts = pending_channels.reduce( + (p, c) => { + if (!c) return p; + + const local = p.local + c.local_balance; + const total = p.total + c.capacity; + + return { local, total }; + }, + { local: 0, total: 0 } + ); + + pendingChannelBalance = { + local: amounts.local + '', + total: amounts.total + '', + }; + + message += `${amounts.local}${amounts.total}`; + } } const allChannels = []; @@ -374,8 +425,10 @@ export class AmbossService { allChannels.push(...mapped); } - if (allChannels.length) { - const infoString = allChannels.reduce((p, c) => { + 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 || ''}`; }, ''); @@ -392,17 +445,19 @@ export class AmbossService { this.logger.info('Push Info', { onchainBalance: !!onchain, - amountOfChannels: allChannels.length, + pendingChannelBalance: !!pendingChannelBalance, + amountOfChannels: sortedChannels.length, finalMessage, signature, }); - await this.pushBalancesToAmboss( + await this.pushBalancesToAmboss({ timestamp, signature, - onchain, - allChannels - ); + pendingChannelBalance, + onchainBalance: onchain, + channels: sortedChannels, + }); }); }, ],