chore: balance changes

This commit is contained in:
Anthony Potdevin 2022-11-30 23:11:10 +01:00
parent c97826cc76
commit acdf43da20
No known key found for this signature in database
GPG key ID: 4403F1DFBE779457
2 changed files with 76 additions and 21 deletions

View file

@ -91,8 +91,8 @@ export const pingHealthCheckMutation = gql`
} }
`; `;
export const pushBalancesMutation = gql` export const pushNodeBalancesMutation = gql`
mutation PushBalances($input: BalancePushInput!) { mutation PushNodeBalances($input: ChannelBalancePushInput!) {
pushBalances(input: $input) pushNodeBalances(input: $input)
} }
`; `;

View file

@ -9,13 +9,14 @@ import { AccountsService } from '../../accounts/accounts.service';
import { FetchService } from '../../fetch/fetch.service'; import { FetchService } from '../../fetch/fetch.service';
import { import {
pingHealthCheckMutation, pingHealthCheckMutation,
pushBalancesMutation, pushNodeBalancesMutation,
saveBackupMutation, saveBackupMutation,
} from './amboss.gql'; } from './amboss.gql';
import { auto, map, each } from 'async'; import { auto, map, each } from 'async';
import { NodeService } from '../../node/node.service'; import { NodeService } from '../../node/node.service';
import { UserConfigService } from '../userConfig/userConfig.service'; import { UserConfigService } from '../userConfig/userConfig.service';
import { getSHA256Hash } from 'src/server/utils/crypto'; import { getSHA256Hash } from 'src/server/utils/crypto';
import { orderBy } from 'lodash';
const ONE_MINUTE = 60 * 1000; const ONE_MINUTE = 60 * 1000;
@ -26,6 +27,24 @@ type NodeType = {
lnd: any; 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() @Injectable()
export class AmbossService { export class AmbossService {
constructor( constructor(
@ -68,16 +87,11 @@ export class AmbossService {
} }
} }
async pushBalancesToAmboss( async pushBalancesToAmboss(input: ChannelBalanceInputType) {
timestamp: string,
signature: string,
onchainBalance: string,
channels: { balance: string; capacity: string; chan_id: string }[]
) {
const { data, error } = await this.fetchService.graphqlFetchWithProxy( const { data, error } = await this.fetchService.graphqlFetchWithProxy(
this.ambossUrl, this.ambossUrl,
pushBalancesMutation, pushNodeBalancesMutation,
{ input: { signature, timestamp, channels, onchainBalance } } { input }
); );
if (!data?.pushBalances || error) { if (!data?.pushBalances || error) {
@ -335,8 +349,45 @@ export class AmbossService {
const { pending_chain_balance } = const { pending_chain_balance } =
await this.nodeService.getPendingChainBalance(node.id); await this.nodeService.getPendingChainBalance(node.id);
onchain = (chain_balance + pending_chain_balance).toString(); onchain = {
message += 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 = []; const allChannels = [];
@ -374,8 +425,10 @@ export class AmbossService {
allChannels.push(...mapped); allChannels.push(...mapped);
} }
if (allChannels.length) { const sortedChannels = orderBy(allChannels, ['chan_id'], ['desc']);
const infoString = allChannels.reduce((p, c) => {
if (sortedChannels.length) {
const infoString = sortedChannels.reduce((p, c) => {
return p + `${c.chan_id}${c.balance}${c.capacity || ''}`; return p + `${c.chan_id}${c.balance}${c.capacity || ''}`;
}, ''); }, '');
@ -392,17 +445,19 @@ export class AmbossService {
this.logger.info('Push Info', { this.logger.info('Push Info', {
onchainBalance: !!onchain, onchainBalance: !!onchain,
amountOfChannels: allChannels.length, pendingChannelBalance: !!pendingChannelBalance,
amountOfChannels: sortedChannels.length,
finalMessage, finalMessage,
signature, signature,
}); });
await this.pushBalancesToAmboss( await this.pushBalancesToAmboss({
timestamp, timestamp,
signature, signature,
onchain, pendingChannelBalance,
allChannels onchainBalance: onchain,
); channels: sortedChannels,
});
}); });
}, },
], ],