Skip to content

Commit

Permalink
fix: balance push (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
apotdevin authored Sep 12, 2023
1 parent 1703a46 commit c5842a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
24 changes: 24 additions & 0 deletions src/server/modules/api/amboss/amboss.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GetChannelsResult } from 'lightning';
import { EdgeInfo, NodeAlias } from './amboss.types';

export const mapNodeResult = (
Expand All @@ -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 + '',
};
});
};
47 changes: 11 additions & 36 deletions src/server/modules/api/amboss/amboss.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down

0 comments on commit c5842a6

Please sign in to comment.