mirror of
https://github.com/apotdevin/thunderhub.git
synced 2025-02-23 06:35:05 +01:00
chore: balance changes
This commit is contained in:
parent
c97826cc76
commit
acdf43da20
2 changed files with 76 additions and 21 deletions
|
@ -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)
|
||||
}
|
||||
`;
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
});
|
||||
},
|
||||
],
|
||||
|
|
Loading…
Add table
Reference in a new issue