]> git.codecow.com Git - libnemo.git/commitdiff
Define account_balance RPC shape and reuse in accounts_balances.
authorChris Duncan <chris@codecow.com>
Fri, 7 Aug 2026 05:15:27 +0000 (22:15 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 7 Aug 2026 05:15:27 +0000 (22:15 -0700)
src/lib/rpc/account_balance.ts [new file with mode: 0644]
src/lib/rpc/accounts_balances.ts

diff --git a/src/lib/rpc/account_balance.ts b/src/lib/rpc/account_balance.ts
new file mode 100644 (file)
index 0000000..988ce06
--- /dev/null
@@ -0,0 +1,29 @@
+//! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
+//! 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
+}
index 2769fbcd4c464882e2d9f3a33df83f4f0f3f877c..30bacf1ec8c917897bd6943c68bbffa0842c2ca3 100644 (file)
@@ -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