From 1f0c3c5efa8ad0222cb3e85b7ecb6ddd05e27658 Mon Sep 17 00:00:00 2001 From: Krr Prr <24500376+krrprr@users.noreply.github.com> Date: Sun, 3 Nov 2019 20:48:43 +0000 Subject: [PATCH] fix(rpc/cli): separate inactive channel balance in getbalance --- docs/api.md | 9 ++- lib/cli/commands/getbalance.ts | 20 +++-- lib/grpc/GrpcService.ts | 1 + lib/lndclient/LndClient.ts | 10 ++- lib/proto/xudrpc.swagger.json | 5 ++ lib/proto/xudrpc_pb.d.ts | 4 + lib/proto/xudrpc_pb.js | 49 ++++++++--- lib/raidenclient/RaidenClient.ts | 4 +- lib/service/Service.ts | 7 +- lib/swaps/SwapClient.ts | 2 + proto/xudrpc.proto | 6 +- test/jest/Service.spec.ts | 85 ++++++++++++++++++++ test/jest/__snapshots__/Service.spec.ts.snap | 14 ++++ 13 files changed, 186 insertions(+), 30 deletions(-) diff --git a/docs/api.md b/docs/api.md index a5aecce0d..8bf34b0bb 100644 --- a/docs/api.md +++ b/docs/api.md @@ -138,6 +138,7 @@ | total_balance | [uint64](#uint64) | | Total balance denominated in satoshis. | | channel_balance | [uint64](#uint64) | | Sum of confirmed channel balances denominated in satoshis. | | pending_channel_balance | [uint64](#uint64) | | Sum of pending channel balances denominated in satoshis. | +| inactive_channel_balance | [uint64](#uint64) | | Sum of inactive channel balances denominated in satoshis. | | wallet_balance | [uint64](#uint64) | | Confirmed wallet balance in satoshis. | | unconfirmed_wallet_balance | [uint64](#uint64) | | Unconfirmed wallet balance in satoshis. | @@ -1177,16 +1178,16 @@ | ListPairs | [ListPairsRequest](#xudrpc.ListPairsRequest) | [ListPairsResponse](#xudrpc.ListPairsResponse) | Gets a list of this nodes suported trading pairs. | | ListPeers | [ListPeersRequest](#xudrpc.ListPeersRequest) | [ListPeersResponse](#xudrpc.ListPeersResponse) | Gets a list of connected peers. | | ListTrades | [ListTradesRequest](#xudrpc.ListTradesRequest) | [ListTradesResponse](#xudrpc.ListTradesResponse) | | -| PlaceOrder | [PlaceOrderRequest](#xudrpc.PlaceOrderRequest) | [PlaceOrderEvent](#xudrpc.PlaceOrderEvent) | Adds an order to the order book. If price is zero or unspecified a market order will get added. | +| PlaceOrder | [PlaceOrderRequest](#xudrpc.PlaceOrderRequest) | [PlaceOrderEvent](#xudrpc.PlaceOrderEvent) stream | Adds an order to the order book. If price is zero or unspecified a market order will get added. | | PlaceOrderSync | [PlaceOrderRequest](#xudrpc.PlaceOrderRequest) | [PlaceOrderResponse](#xudrpc.PlaceOrderResponse) | The synchronous non-streaming version of PlaceOrder. | | ExecuteSwap | [ExecuteSwapRequest](#xudrpc.ExecuteSwapRequest) | [SwapSuccess](#xudrpc.SwapSuccess) | Execute a swap on a maker peer order | | RemoveCurrency | [RemoveCurrencyRequest](#xudrpc.RemoveCurrencyRequest) | [RemoveCurrencyResponse](#xudrpc.RemoveCurrencyResponse) | Removes a currency from the list of supported currencies. Only currencies that are not in use for any currently supported trading pairs may be removed. Once removed, the currency can no longer be used for any supported trading pairs. | | RemovePair | [RemovePairRequest](#xudrpc.RemovePairRequest) | [RemovePairResponse](#xudrpc.RemovePairResponse) | Removes a trading pair from the list of currently supported trading pair. This call will effectively cancel any standing orders for that trading pair. Peers are informed when a pair is no longer supported so that they will know to stop sending orders for it. | | DiscoverNodes | [DiscoverNodesRequest](#xudrpc.DiscoverNodesRequest) | [DiscoverNodesResponse](#xudrpc.DiscoverNodesResponse) | Discover nodes from a specific peer and apply new connections | | Shutdown | [ShutdownRequest](#xudrpc.ShutdownRequest) | [ShutdownResponse](#xudrpc.ShutdownResponse) | Begin gracefully shutting down xud. | -| SubscribeOrders | [SubscribeOrdersRequest](#xudrpc.SubscribeOrdersRequest) | [OrderUpdate](#xudrpc.OrderUpdate) | Subscribes to orders being added to and removed from the order book. This call allows the client to maintain an up-to-date view of the order book. For example, an exchange that wants to show its users a real time view of the orders available to them would subscribe to this streaming call to be alerted as new orders are added and expired orders are removed. | -| SubscribeSwaps | [SubscribeSwapsRequest](#xudrpc.SubscribeSwapsRequest) | [SwapSuccess](#xudrpc.SwapSuccess) | Subscribes to completed swaps. By default, only swaps that are initiated by a remote peer are transmitted unless a flag is set to include swaps initiated by the local node. This call allows the client to get real-time notifications when its orders are filled by a peer. It can be used for tracking order executions, updating balances, and informing a trader when one of their orders is settled through the Exchange Union network. | -| SubscribeSwapFailures | [SubscribeSwapsRequest](#xudrpc.SubscribeSwapsRequest) | [SwapFailure](#xudrpc.SwapFailure) | Subscribes to failed swaps. By default, only swaps that are initiated by a remote peer are transmitted unless a flag is set to include swaps initiated by the local node. This call allows the client to get real-time notifications when swap attempts are failing. It can be used for status monitoring, debugging, and testing purposes. | +| SubscribeOrders | [SubscribeOrdersRequest](#xudrpc.SubscribeOrdersRequest) | [OrderUpdate](#xudrpc.OrderUpdate) stream | Subscribes to orders being added to and removed from the order book. This call allows the client to maintain an up-to-date view of the order book. For example, an exchange that wants to show its users a real time view of the orders available to them would subscribe to this streaming call to be alerted as new orders are added and expired orders are removed. | +| SubscribeSwaps | [SubscribeSwapsRequest](#xudrpc.SubscribeSwapsRequest) | [SwapSuccess](#xudrpc.SwapSuccess) stream | Subscribes to completed swaps. By default, only swaps that are initiated by a remote peer are transmitted unless a flag is set to include swaps initiated by the local node. This call allows the client to get real-time notifications when its orders are filled by a peer. It can be used for tracking order executions, updating balances, and informing a trader when one of their orders is settled through the Exchange Union network. | +| SubscribeSwapFailures | [SubscribeSwapsRequest](#xudrpc.SubscribeSwapsRequest) | [SwapFailure](#xudrpc.SwapFailure) stream | Subscribes to failed swaps. By default, only swaps that are initiated by a remote peer are transmitted unless a flag is set to include swaps initiated by the local node. This call allows the client to get real-time notifications when swap attempts are failing. It can be used for status monitoring, debugging, and testing purposes. | diff --git a/lib/cli/commands/getbalance.ts b/lib/cli/commands/getbalance.ts index 41545c868..0058bbe11 100644 --- a/lib/cli/commands/getbalance.ts +++ b/lib/cli/commands/getbalance.ts @@ -19,7 +19,7 @@ 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); @@ -27,11 +27,21 @@ const formatBalances = (balances: GetBalanceResponse.AsObject) => { 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 = () => { diff --git a/lib/grpc/GrpcService.ts b/lib/grpc/GrpcService.ts index 40511d7f5..68925a855 100644 --- a/lib/grpc/GrpcService.ts +++ b/lib/grpc/GrpcService.ts @@ -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); diff --git a/lib/lndclient/LndClient.ts b/lib/lndclient/LndClient.ts index ab8883bce..757f3ee76 100644 --- a/lib/lndclient/LndClient.ts +++ b/lib/lndclient/LndClient.ts @@ -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'; @@ -590,7 +590,7 @@ class LndClient extends SwapClient { return walletBalanceResponse.toObject(); } - public channelBalance = async (): Promise => { + public channelBalance = async (): Promise => { const channelBalanceResponse = await this.unaryCall( 'channelBalance', new lndrpc.ChannelBalanceRequest(), ); @@ -598,7 +598,11 @@ class LndClient extends SwapClient { 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 () => { diff --git a/lib/proto/xudrpc.swagger.json b/lib/proto/xudrpc.swagger.json index b4d5acf40..6c375aa4e 100644 --- a/lib/proto/xudrpc.swagger.json +++ b/lib/proto/xudrpc.swagger.json @@ -707,6 +707,11 @@ "format": "uint64", "description": "Sum of pending channel balances denominated in satoshis." }, + "inactive_channel_balance": { + "type": "string", + "format": "uint64", + "description": "Sum of inactive channel balances denominated in satoshis." + }, "wallet_balance": { "type": "string", "format": "uint64", diff --git a/lib/proto/xudrpc_pb.d.ts b/lib/proto/xudrpc_pb.d.ts index 8ef0960d8..1b19dce71 100644 --- a/lib/proto/xudrpc_pb.d.ts +++ b/lib/proto/xudrpc_pb.d.ts @@ -279,6 +279,9 @@ export class Balance extends jspb.Message { getPendingChannelBalance(): number; setPendingChannelBalance(value: number): void; + getInactiveChannelBalance(): number; + setInactiveChannelBalance(value: number): void; + getWalletBalance(): number; setWalletBalance(value: number): void; @@ -301,6 +304,7 @@ export namespace Balance { totalBalance: number, channelBalance: number, pendingChannelBalance: number, + inactiveChannelBalance: number, walletBalance: number, unconfirmedWalletBalance: number, } diff --git a/lib/proto/xudrpc_pb.js b/lib/proto/xudrpc_pb.js index 99208b6d0..0b759a57a 100644 --- a/lib/proto/xudrpc_pb.js +++ b/lib/proto/xudrpc_pb.js @@ -1896,8 +1896,9 @@ proto.xudrpc.Balance.toObject = function(includeInstance, msg) { totalBalance: jspb.Message.getFieldWithDefault(msg, 1, 0), channelBalance: jspb.Message.getFieldWithDefault(msg, 2, 0), pendingChannelBalance: jspb.Message.getFieldWithDefault(msg, 3, 0), - walletBalance: jspb.Message.getFieldWithDefault(msg, 4, 0), - unconfirmedWalletBalance: jspb.Message.getFieldWithDefault(msg, 5, 0) + inactiveChannelBalance: jspb.Message.getFieldWithDefault(msg, 4, 0), + walletBalance: jspb.Message.getFieldWithDefault(msg, 5, 0), + unconfirmedWalletBalance: jspb.Message.getFieldWithDefault(msg, 6, 0) }; if (includeInstance) { @@ -1948,9 +1949,13 @@ proto.xudrpc.Balance.deserializeBinaryFromReader = function(msg, reader) { break; case 4: var value = /** @type {number} */ (reader.readUint64()); - msg.setWalletBalance(value); + msg.setInactiveChannelBalance(value); break; case 5: + var value = /** @type {number} */ (reader.readUint64()); + msg.setWalletBalance(value); + break; + case 6: var value = /** @type {number} */ (reader.readUint64()); msg.setUnconfirmedWalletBalance(value); break; @@ -2004,20 +2009,27 @@ proto.xudrpc.Balance.serializeBinaryToWriter = function(message, writer) { f ); } - f = message.getWalletBalance(); + f = message.getInactiveChannelBalance(); if (f !== 0) { writer.writeUint64( 4, f ); } - f = message.getUnconfirmedWalletBalance(); + f = message.getWalletBalance(); if (f !== 0) { writer.writeUint64( 5, f ); } + f = message.getUnconfirmedWalletBalance(); + if (f !== 0) { + writer.writeUint64( + 6, + f + ); + } }; @@ -2067,35 +2079,50 @@ proto.xudrpc.Balance.prototype.setPendingChannelBalance = function(value) { /** - * optional uint64 wallet_balance = 4; + * optional uint64 inactive_channel_balance = 4; * @return {number} */ -proto.xudrpc.Balance.prototype.getWalletBalance = function() { +proto.xudrpc.Balance.prototype.getInactiveChannelBalance = function() { return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 4, 0)); }; /** @param {number} value */ -proto.xudrpc.Balance.prototype.setWalletBalance = function(value) { +proto.xudrpc.Balance.prototype.setInactiveChannelBalance = function(value) { jspb.Message.setProto3IntField(this, 4, value); }; /** - * optional uint64 unconfirmed_wallet_balance = 5; + * optional uint64 wallet_balance = 5; * @return {number} */ -proto.xudrpc.Balance.prototype.getUnconfirmedWalletBalance = function() { +proto.xudrpc.Balance.prototype.getWalletBalance = function() { return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 5, 0)); }; /** @param {number} value */ -proto.xudrpc.Balance.prototype.setUnconfirmedWalletBalance = function(value) { +proto.xudrpc.Balance.prototype.setWalletBalance = function(value) { jspb.Message.setProto3IntField(this, 5, value); }; +/** + * optional uint64 unconfirmed_wallet_balance = 6; + * @return {number} + */ +proto.xudrpc.Balance.prototype.getUnconfirmedWalletBalance = function() { + return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 6, 0)); +}; + + +/** @param {number} value */ +proto.xudrpc.Balance.prototype.setUnconfirmedWalletBalance = function(value) { + jspb.Message.setProto3IntField(this, 6, value); +}; + + /** * Generated by JsPbCodeGenerator. diff --git a/lib/raidenclient/RaidenClient.ts b/lib/raidenclient/RaidenClient.ts index 828c37b31..9ae14e528 100644 --- a/lib/raidenclient/RaidenClient.ts +++ b/lib/raidenclient/RaidenClient.ts @@ -453,7 +453,7 @@ class RaidenClient extends SwapClient { public channelBalance = async (currency?: string): Promise => { if (!currency) { - return { balance: 0, pendingOpenBalance: 0 }; + return { balance: 0, pendingOpenBalance: 0, inactiveBalance: 0 }; } const channels = await this.getChannels(this.tokenAddresses.get(currency)); @@ -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 }; } /** diff --git a/lib/service/Service.ts b/lib/service/Service.ts index 632634b84..9c69babc4 100644 --- a/lib/service/Service.ts +++ b/lib/service/Service.ts @@ -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(); + const channelBalances = new Map(); const walletBalances = new Map(); if (currency) { @@ -159,13 +159,13 @@ class Service { await Promise.all(balancePromises); } const balances = new Map(); 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, @@ -173,6 +173,7 @@ class Service { totalBalance, channelBalance: channelBalance.balance, pendingChannelBalance: channelBalance.pendingOpenBalance, + inactiveChannelBalance: channelBalance.inactiveBalance, walletBalance: walletBalance.confirmedBalance, unconfirmedWalletBalance: walletBalance.unconfirmedBalance, }); diff --git a/lib/swaps/SwapClient.ts b/lib/swaps/SwapClient.ts index e7fbb3b01..cd6faf434 100644 --- a/lib/swaps/SwapClient.ts +++ b/lib/swaps/SwapClient.ts @@ -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 = { diff --git a/proto/xudrpc.proto b/proto/xudrpc.proto index 1642691dd..716cb06de 100644 --- a/proto/xudrpc.proto +++ b/proto/xudrpc.proto @@ -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 { diff --git a/test/jest/Service.spec.ts b/test/jest/Service.spec.ts index 6722c7e16..5ede15f4e 100644 --- a/test/jest/Service.spec.ts +++ b/test/jest/Service.spec.ts @@ -4,6 +4,7 @@ import Swaps from '../../lib/swaps/Swaps'; import SwapClientManager from '../../lib/swaps/SwapClientManager'; import Pool from '../../lib/p2p/Pool'; import Peer from '../../lib/p2p/Peer'; +import SwapClient from '../../lib/swaps/SwapClient'; jest.mock('../../lib/orderbook/OrderBook'); const mockedOrderbook = >Orderbook; @@ -11,6 +12,8 @@ jest.mock('../../lib/swaps/Swaps'); const mockedSwaps = >Swaps; jest.mock('../../lib/swaps/SwapClientManager'); const mockedSwapClientManager = >SwapClientManager; +jest.mock('../../lib/swaps/SwapClient'); +const mockedSwapClient = >SwapClient; jest.mock('../../lib/p2p/Pool'); const mockedPool = >Pool; jest.mock('../../lib/p2p/Peer'); @@ -90,4 +93,86 @@ describe('Service', () => { }); }); + describe('getBalance', () => { + const setup = () => { + service = new Service(components); + components.swapClientManager.swapClients = new Map(); + components.swapClientManager.get = jest.fn().mockImplementation((arg) => { + return components.swapClientManager.swapClients.get(arg); + }); + + const btcClient = new mockedSwapClient(); + btcClient.isConnected = jest.fn().mockImplementation(() => true); + btcClient.channelBalance = jest.fn().mockImplementation(() => { + return Promise.resolve({ balance: 70000, inactiveBalance: 18817, pendingOpenBalance: 190191 }); + }); + btcClient.walletBalance = jest.fn().mockImplementation(() => { + return Promise.resolve({ totalBalance: 10000, confirmedBalance: 10000, unconfirmedBalance: 0 }); + }); + components.swapClientManager.swapClients.set('BTC', btcClient); + + const ltcClient = new mockedSwapClient(); + ltcClient.isConnected = jest.fn().mockImplementation(() => true); + ltcClient.channelBalance = jest.fn().mockImplementation(() => { + return Promise.resolve({ balance: 0, inactiveBalance: 12345, pendingOpenBalance: 0 }); + }); + ltcClient.walletBalance = jest.fn().mockImplementation(() => { + return Promise.resolve({ totalBalance: 2000, confirmedBalance: 1500, unconfirmedBalance: 500 }); + }); + components.swapClientManager.swapClients.set('LTC', ltcClient); + + const bchClient = new mockedSwapClient(); + bchClient.isConnected = jest.fn().mockImplementation(() => false); + components.swapClientManager.swapClients.set('BCH', bchClient); + }; + + test('returns balance for all connected clients when currency is not specified', async () => { + setup(); + const result = await service.getBalance({ currency: '' }); + expect(result.size).toEqual(2); + + const btcBalance = result.get('BTC')!; + expect(btcBalance).toBeTruthy(); + expect(btcBalance.channelBalance).toEqual(70000); + expect(btcBalance.pendingChannelBalance).toEqual(190191); + expect(btcBalance.inactiveChannelBalance).toEqual(18817); + expect(btcBalance.walletBalance).toEqual(10000); + expect(btcBalance.unconfirmedWalletBalance).toEqual(0); + expect(btcBalance.totalBalance).toEqual(289008); + + const ltcBalance = result.get('LTC')!; + expect(ltcBalance).toBeTruthy(); + expect(ltcBalance.channelBalance).toEqual(0); + expect(ltcBalance.pendingChannelBalance).toEqual(0); + expect(ltcBalance.inactiveChannelBalance).toEqual(12345); + expect(ltcBalance.walletBalance).toEqual(1500); + expect(ltcBalance.unconfirmedWalletBalance).toEqual(500); + expect(ltcBalance.totalBalance).toEqual(14345); + }); + + test('returns balance for specified currency', async () => { + setup(); + const result = await service.getBalance({ currency: 'BTC' }); + expect(result.size).toEqual(1); + + const btcBalance = result.get('BTC')!; + expect(btcBalance).toBeTruthy(); + expect(btcBalance.channelBalance).toEqual(70000); + expect(btcBalance.pendingChannelBalance).toEqual(190191); + expect(btcBalance.inactiveChannelBalance).toEqual(18817); + expect(btcBalance.walletBalance).toEqual(10000); + expect(btcBalance.unconfirmedWalletBalance).toEqual(0); + expect(btcBalance.totalBalance).toEqual(289008); + }); + + test('throws in case of invalid currency', async () => { + setup(); + await expect(service.getBalance({ currency: 'A' })).rejects.toMatchSnapshot(); + }); + + test('throws when swap client is not found', async () => { + setup(); + await expect(service.getBalance({ currency: 'BBB' })).rejects.toMatchSnapshot(); + }); + }); }); diff --git a/test/jest/__snapshots__/Service.spec.ts.snap b/test/jest/__snapshots__/Service.spec.ts.snap index b0028c982..647844f4e 100644 --- a/test/jest/__snapshots__/Service.spec.ts.snap +++ b/test/jest/__snapshots__/Service.spec.ts.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Service getBalance throws in case of invalid currency 1`] = ` +Object { + "code": "6.1", + "message": "currency must consist of 2 to 5 upper case English letters or numbers", +} +`; + +exports[`Service getBalance throws when swap client is not found 1`] = ` +Object { + "code": "7.1", + "message": "swapClient for currency BBB not found", +} +`; + exports[`Service openChannel throws when failure from swapClientManager 1`] = ` Object { "code": "6.5",