From: Chris Duncan Date: Fri, 7 Aug 2026 05:15:27 +0000 (-0700) Subject: Define account_balance RPC shape and reuse in accounts_balances. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=bb47226f16e8cf41f9090e5d65041d0be37ba303;p=libnemo.git Define account_balance RPC shape and reuse in accounts_balances. --- diff --git a/src/lib/rpc/account_balance.ts b/src/lib/rpc/account_balance.ts new file mode 100644 index 0000000..988ce06 --- /dev/null +++ b/src/lib/rpc/account_balance.ts @@ -0,0 +1,29 @@ +//! SPDX-FileCopyrightText: 2026 Chris Duncan +//! SPDX-License-Identifier: GPL-3.0-or-later + +export type AccountBalance = { + balance: string + pending: string + receivable: string +} + +export function account_balance (body: unknown): AccountBalance { + const response: AccountBalance = { + balance: '0', + pending: '0', + receivable: '0', + } + if (body == null || typeof body !== 'object') { + return response + } + if ('balance' in body && typeof body.balance === 'string') { + response.balance = body.balance + } + if ('pending' in body && typeof body.pending === 'string') { + response.pending = body.pending + } + if ('receivable' in body && typeof body.receivable === 'string') { + response.receivable = body.receivable + } + return response +} diff --git a/src/lib/rpc/accounts_balances.ts b/src/lib/rpc/accounts_balances.ts index 2769fbc..30bacf1 100644 --- a/src/lib/rpc/accounts_balances.ts +++ b/src/lib/rpc/accounts_balances.ts @@ -2,14 +2,12 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { Account } from '../account' +import { AccountBalance, account_balance } from './account_balance' type AccountsBalances = { balances?: { - [address: string]: { - balance: string, - receivable: string - } - }, + [address: string]: AccountBalance + } errors?: { [address: string]: string } @@ -31,14 +29,8 @@ export function accounts_balances (body: unknown): AccountsBalances { if (data == null || typeof data !== 'object') { throw new Error('Invalid account balance data') } - if ('balance' in data && typeof data.balance === 'string') { - response.balances ??= {} - response.balances[address].balance = data.balance - } - if ('receivable' in data && typeof data.receivable === 'string') { - response.balances ??= {} - response.balances[address].receivable = data.receivable - } + response.balances ??= {} + response.balances[address] = account_balance(data) } catch (err: any) { response.errors ??= {} response.errors[address] = err?.message