UNPKG

ln-sync

Version:
88 lines (70 loc) 2.24 kB
const percentile = require('./percentile'); const above = 0.9; const {round} = Math; /** Liquidity tokens { channels: [{ is_active: <Channel is Active Bool> local_balance: <Local Balance Tokens Number> partner_public_key: <Public Key Hex String> remote_balance: <Remote Balance Tokens Number> }] [is_outbound]: <Count Outbound Liquidity Bool> [is_top]: <Return Top Liquidity Bool> [max_fee_rate]: <Eliminate Inbound Liquidity With Fee Rate Above Number> [min_node_score]: <Eliminate Liquidity With Score Below Score Number> policies: [[{ fee_rate: <Fee Rate Parts Per Million Number> public_key: <Public Key Hex String> }]] public_key: <Public Key Hex String> with: [<With Public Key Hex String>] } @returns { tokens: [<Tokens Number>] } */ module.exports = args => { const inboundFeeRates = args.policies.reduce((sum, policies) => { const peer = policies.find(n => n.public_key !== args.public_key); // Exit early when there is no known peer policy if (!peer) { return sum; } // Exit early when there is an existing higher fee rate if (!!sum[peer.public_key] && peer.fee_rate > sum[peer.public_key]) { return sum; } sum[peer.public_key] = peer.fee_rate; return sum; }, {}); const activeChannels = args.channels .filter(n => !!n.is_active) .filter(channel => { // Exit early when there are no peers specified if (!args.with) { return true; } return args.with.includes(channel.partner_public_key); }) .filter(n => { // Exit early when considering outbound liquidity if (!!args.is_outbound) { return true; } // Exit early when there is no max fee rate if (args.max_fee_rate === undefined) { return true; } const feeRate = inboundFeeRates[n.partner_public_key]; return !!feeRate && feeRate <= args.max_fee_rate; }); const balanceType = !!args.is_outbound ? 'local' : 'remote'; const tokens = activeChannels.map(n => n[`${balanceType}_balance`]); if (!!args.is_top) { return {tokens: [round(percentile({above, tokens}).top)]}; } return {tokens}; };