From: Chris Duncan Date: Fri, 7 Aug 2026 04:20:53 +0000 (-0700) Subject: Start refactoring RPC method response parsing. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=64c537ca4be8140ead945a65de966bd048b86a8b;p=libnemo.git Start refactoring RPC method response parsing. --- diff --git a/src/lib/rpc/accounts_balances.ts b/src/lib/rpc/accounts_balances.ts index ecf49b1..274ab0b 100644 --- a/src/lib/rpc/accounts_balances.ts +++ b/src/lib/rpc/accounts_balances.ts @@ -1,16 +1,71 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -export const accounts_balances = Object.freeze({ - fields: [ - 'balances', - ], - balances: { - address: { - fields: [ - 'balance', - 'receivable', - ], - }, +import { Account } from '../account' + +type AccountsBalances = { + balances?: { + [address: string]: { + balance: string, + receivable: string + } }, -}) + errors?: { + [address: string]: string + } +} + +export function accounts_balances (body: unknown): AccountsBalances { + const response: AccountsBalances = {} + if (body == null || typeof body !== 'object') { + return response + } + + const balances = 'balances' in body ? body.balances : null + const errors = 'errors' in body ? body.errors : null + if (balances == null && errors == null) { + return response + } + + if (balances != null) { + if (typeof balances === 'object') { + for (const [address, data] of Object.entries(balances) as [string, unknown][]) { + try { + Account.validate(address) + 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 + } + } catch (err: any) { + response.errors ??= {} + response.errors[address] = err?.message + } + } + } + } + + if (errors != null) { + if (typeof errors === 'object') { + for (const [address, error] of Object.entries(errors) as [string, unknown][]) { + try { + Account.validate(address) + if (typeof error === 'string') { + throw new Error(error) + } + } catch (err: any) { + response.errors ??= {} + response.errors[address] = err?.message + } + } + } + } + + return response +} diff --git a/src/lib/rpc/index.ts b/src/lib/rpc/index.ts index 6bc3700..aed417b 100644 --- a/src/lib/rpc/index.ts +++ b/src/lib/rpc/index.ts @@ -74,7 +74,7 @@ export class Rpc { * @param {object} [data] - JSON to send to the node as defined by the action * @returns {Promise} JSON-formatted RPC results from the node */ - async post (action: T, data?: Record): Promise + async post (action: T, data?: Record): Promise> async post (action: unknown, data: unknown): Promise { this.#validate(action) const aborter = new AbortController() @@ -97,7 +97,7 @@ export class Rpc { if (error) { throw new Error(`${code}${error}${message}`, { cause: JSON.stringify(body) }) } - return body + return Schema[action](body) } catch (err) { console.error(err) throw new Error(`RPC ${action} request failed`, { cause: err }) @@ -131,7 +131,7 @@ export class Rpc { }) } - #validate (action: unknown): asserts action is string { + #validate (action: unknown): asserts action is keyof typeof Schema { if (!action) { throw new ReferenceError('Action is required for RPCs') } diff --git a/src/lib/rpc/schema.ts b/src/lib/rpc/schema.ts index d4655b7..6d4c444 100644 --- a/src/lib/rpc/schema.ts +++ b/src/lib/rpc/schema.ts @@ -10,12 +10,12 @@ import { blocks_info } from './blocks_info' import { process } from './process' export const Schema = Object.freeze({ - account_info, - account_representative, + // account_info, + // account_representative, accounts_balances, - accounts_frontiers, - accounts_representatives, - block_info, - blocks_info, - process, + // accounts_frontiers, + // accounts_representatives, + // block_info, + // blocks_info, + // process, }) diff --git a/src/lib/wallet/refresh.ts b/src/lib/wallet/refresh.ts index 53fd2c3..4ef3d43 100644 --- a/src/lib/wallet/refresh.ts +++ b/src/lib/wallet/refresh.ts @@ -40,7 +40,7 @@ export async function _refresh (wallet: Wallet, rpc: unknown, from: unknown, to: json_block: true, accounts: addresses } - const { balances } = await rpc.post('accounts_balances', data) as { balances?: { [address: string]: { balance: string, receivable: string } } } + const { balances } = await rpc.post('accounts_balances', data) const { frontiers } = await rpc.post('accounts_frontiers', data) as { frontiers: { [address: string]: string } } const { blocks } = await rpc.post('blocks_info', { json_block: true, hashes: Object.values(frontiers) }) as BlockInfo for (const account of accounts.values()) {