diff --git a/lib/raidenclient/RaidenClient.ts b/lib/raidenclient/RaidenClient.ts index f4ab6ca23..23e71c9f3 100644 --- a/lib/raidenclient/RaidenClient.ts +++ b/lib/raidenclient/RaidenClient.ts @@ -41,6 +41,12 @@ class RaidenClient extends SwapClient { private host: string; private disable: boolean; + // TODO: Populate the mapping from the database (Currency.decimalPlaces). + private static readonly UNITS_PER_CURRENCY: { [key: string]: number } = { + WETH: 10 ** 10, + DAI: 10 ** 10, + }; + /** * Creates a raiden client. */ @@ -271,14 +277,17 @@ class RaidenClient extends SwapClient { } /** - * Returns the total balance available across all channels. + * Returns the total balance available across all channels for a specified currency. */ - public channelBalance = async (): Promise => { - // TODO: refine logic to determine balance per token rather than all combined - const channels = await this.getChannels(); + public channelBalance = async (currency?: string): Promise => { + if (!currency) { + return { balance: 0, pendingOpenBalance: 0 }; + } + + const channels = await this.getChannels(this.tokenAddresses.get(currency)); const balance = channels.filter(channel => channel.state === 'opened') .map(channel => channel.balance) - .reduce((acc, sum) => sum + acc, 0); + .reduce((acc, sum) => sum + (acc / RaidenClient.UNITS_PER_CURRENCY[currency]), 0); return { balance, pendingOpenBalance: 0 }; } diff --git a/lib/service/Service.ts b/lib/service/Service.ts index 3c758f3cd..bc2186894 100644 --- a/lib/service/Service.ts +++ b/lib/service/Service.ts @@ -119,23 +119,27 @@ class Service { public channelBalance = async (args: { currency: string }) => { const { currency } = args; const balances = new Map(); - const getBalance = async (currency: string) => { + + if (currency) { + argChecks.VALID_CURRENCY(args); + const swapClient = this.swapClientManager.get(currency.toUpperCase()); if (swapClient) { - const channelBalance = await swapClient.channelBalance(); - return channelBalance; + const channelBalance = await swapClient.channelBalance(currency); + balances.set(currency, channelBalance); } else { throw swapsErrors.SWAP_CLIENT_NOT_FOUND(currency); } - }; - - if (currency) { - argChecks.VALID_CURRENCY(args); - balances.set(currency, await getBalance(currency)); } else { - for (const currency of this.orderBook.currencies) { - balances.set(currency, await getBalance(currency)); - } + const balancePromises: Promise[] = []; + this.swapClientManager.swapClients.forEach((swapClient, currency) => { + if (swapClient.isConnected()) { + balancePromises.push(swapClient.channelBalance().then((channelBalance) => { + balances.set(currency, channelBalance); + })); + } + }); + await Promise.all(balancePromises); } return balances; diff --git a/lib/swaps/SwapClient.ts b/lib/swaps/SwapClient.ts index 503fc0221..2031dfbcd 100644 --- a/lib/swaps/SwapClient.ts +++ b/lib/swaps/SwapClient.ts @@ -44,8 +44,10 @@ abstract class SwapClient extends EventEmitter { /** * Returns the total balance available across all channels. + * @param currency the currency whose balance to query for, otherwise all/any + * currencies supported by this client are included in the balance. */ - public abstract channelBalance(): Promise; + public abstract channelBalance(currency?: string): Promise; protected setStatus = async (status: ClientStatus): Promise => { this.logger.info(`${this.constructor.name} status: ${ClientStatus[status]}`);