Skip to content

Commit

Permalink
fix(rpc/cli): separate inactive channel balance in getbalance
Browse files Browse the repository at this point in the history
  • Loading branch information
krrprr committed Nov 5, 2019
1 parent d0ff854 commit 1f0c3c5
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 30 deletions.
9 changes: 5 additions & 4 deletions docs/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions lib/cli/commands/getbalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ const formatBalances = (balances: GetBalanceResponse.AsObject) => {
element.push(
balance[0],
`${satsToCoinsStr(balance[1].totalBalance)}`,
formatBalance(balance[1].channelBalance, balance[1].pendingChannelBalance),
formatBalance(balance[1].channelBalance, balance[1].pendingChannelBalance, balance[1].inactiveChannelBalance),
formatBalance(balance[1].walletBalance, balance[1].unconfirmedWalletBalance),
);
formatted.push(element);
});
return formatted;
};

const formatBalance = (confirmedBalance: number, unconfirmedBalance: number) => {
const formatBalance = (confirmedBalance: number, unconfirmedBalance: number, inactiveBalance = 0) => {
const confirmedBalanceStr = satsToCoinsStr(confirmedBalance);
return unconfirmedBalance > 0 ?
`${confirmedBalanceStr} (${satsToCoinsStr(unconfirmedBalance)} pending)` :
confirmedBalanceStr;
const unconfirmedBalanceStr = unconfirmedBalance > 0 ? `${satsToCoinsStr(unconfirmedBalance)} pending` : undefined;
const inactiveBalanceStr = inactiveBalance > 0 ? `${satsToCoinsStr(inactiveBalance)} inactive` : undefined;
if (unconfirmedBalanceStr || inactiveBalanceStr) {
let str = `${confirmedBalanceStr} (`;
if (unconfirmedBalanceStr) {
str += inactiveBalanceStr ? `${inactiveBalanceStr} | ${unconfirmedBalanceStr}` : unconfirmedBalanceStr;
} else {
str += inactiveBalanceStr;
}
str += ')';
return str;
}
return confirmedBalanceStr;
};

const createTable = () => {
Expand Down
1 change: 1 addition & 0 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class GrpcService {
balance.setTotalBalance(balanceObj.totalBalance);
balance.setChannelBalance(balanceObj.channelBalance);
balance.setPendingChannelBalance(balanceObj.pendingChannelBalance);
balance.setInactiveChannelBalance(balanceObj.inactiveChannelBalance);
balance.setWalletBalance(balanceObj.walletBalance);
balance.setUnconfirmedWalletBalance(balanceObj.unconfirmedWalletBalance);
balancesMap.set(currency, balance);
Expand Down
10 changes: 7 additions & 3 deletions lib/lndclient/LndClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import grpc, { ChannelCredentials, ClientReadableStream } from 'grpc';
import Logger from '../Logger';
import SwapClient, { ClientStatus, SwapClientInfo, PaymentState } from '../swaps/SwapClient';
import SwapClient, { ClientStatus, SwapClientInfo, PaymentState, ChannelBalance } from '../swaps/SwapClient';
import errors from './errors';
import swapErrors from '../swaps/errors';
import { LightningClient, WalletUnlockerClient } from '../proto/lndrpc_grpc_pb';
Expand Down Expand Up @@ -590,15 +590,19 @@ class LndClient extends SwapClient {
return walletBalanceResponse.toObject();
}

public channelBalance = async (): Promise<lndrpc.ChannelBalanceResponse.AsObject> => {
public channelBalance = async (): Promise<ChannelBalance> => {
const channelBalanceResponse = await this.unaryCall<lndrpc.ChannelBalanceRequest, lndrpc.ChannelBalanceResponse>(
'channelBalance', new lndrpc.ChannelBalanceRequest(),
);
if (this.maximumOutboundAmount !== channelBalanceResponse.getBalance()) {
this.maximumOutboundAmount = channelBalanceResponse.getBalance();
this.logger.debug(`new outbound capacity: ${this.maximumOutboundAmount}`);
}
return channelBalanceResponse.toObject();
const channels = await this.listChannels();
const balance = channels.toObject().channelsList.reduce((sum, channel) => sum + (channel.active ? channel.localBalance : 0), 0);
const inactiveBalance = channelBalanceResponse.getBalance() - balance;

return { balance, inactiveBalance, pendingOpenBalance: channelBalanceResponse.getPendingOpenBalance() };
}

public getHeight = async () => {
Expand Down
5 changes: 5 additions & 0 deletions lib/proto/xudrpc.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/proto/xudrpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 38 additions & 11 deletions lib/proto/xudrpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/raidenclient/RaidenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class RaidenClient extends SwapClient {

public channelBalance = async (currency?: string): Promise<ChannelBalance> => {
if (!currency) {
return { balance: 0, pendingOpenBalance: 0 };
return { balance: 0, pendingOpenBalance: 0, inactiveBalance: 0 };
}

const channels = await this.getChannels(this.tokenAddresses.get(currency));
Expand All @@ -469,7 +469,7 @@ class RaidenClient extends SwapClient {
this.logger.debug(`new outbound capacity for ${currency}: ${balance}`);
}

return { balance, pendingOpenBalance: 0 };
return { balance, pendingOpenBalance: 0, inactiveBalance: 0 };
}

/**
Expand Down
7 changes: 4 additions & 3 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Service {
/** Gets the total lightning network balance for a given currency. */
public getBalance = async (args: { currency: string }) => {
const { currency } = args;
const channelBalances = new Map<string, { balance: number, pendingOpenBalance: number }>();
const channelBalances = new Map<string, { balance: number, pendingOpenBalance: number, inactiveBalance: number }>();
const walletBalances = new Map<string, { confirmedBalance: number, unconfirmedBalance: number }>();

if (currency) {
Expand Down Expand Up @@ -159,20 +159,21 @@ class Service {
await Promise.all(balancePromises);
}
const balances = new Map<string, {
channelBalance: number, pendingChannelBalance: number,
channelBalance: number, pendingChannelBalance: number, inactiveChannelBalance: number,
walletBalance: number, unconfirmedWalletBalance: number,
totalBalance: number,
}>();
channelBalances.forEach((channelBalance, currency) => {
const walletBalance = walletBalances.get(currency) as { confirmedBalance: number, unconfirmedBalance: number };
const totalBalance = channelBalance.balance + channelBalance.pendingOpenBalance +
const totalBalance = channelBalance.balance + channelBalance.pendingOpenBalance + channelBalance.inactiveBalance +
walletBalance.confirmedBalance + walletBalance.unconfirmedBalance;
balances.set(
currency,
{
totalBalance,
channelBalance: channelBalance.balance,
pendingChannelBalance: channelBalance.pendingOpenBalance,
inactiveChannelBalance: channelBalance.inactiveBalance,
walletBalance: walletBalance.confirmedBalance,
unconfirmedWalletBalance: walletBalance.unconfirmedBalance,
});
Expand Down
2 changes: 2 additions & 0 deletions lib/swaps/SwapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type ChannelBalance = {
balance: number,
/** The cumulative balance of pending channels denominated in satoshis. */
pendingOpenBalance: number,
/** The cumulative balance of inactive channels denominated in satoshis. */
inactiveBalance: number,
};

type WalletBalance = {
Expand Down
6 changes: 4 additions & 2 deletions proto/xudrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,12 @@ message Balance {
uint64 channel_balance = 2 [json_name = "channel_balance"];
// Sum of pending channel balances denominated in satoshis.
uint64 pending_channel_balance = 3 [json_name = "pending_channel_balance"];
// Sum of inactive channel balances denominated in satoshis.
uint64 inactive_channel_balance = 4 [json_name = "inactive_channel_balance"];
// Confirmed wallet balance in satoshis.
uint64 wallet_balance = 4 [json_name = "wallet_balance"];
uint64 wallet_balance = 5 [json_name = "wallet_balance"];
// Unconfirmed wallet balance in satoshis.
uint64 unconfirmed_wallet_balance = 5 [json_name = "unconfirmed_wallet_balance"];
uint64 unconfirmed_wallet_balance = 6 [json_name = "unconfirmed_wallet_balance"];
}

message GetBalanceRequest {
Expand Down
Loading

0 comments on commit 1f0c3c5

Please sign in to comment.