From 19736fce78f43f97225b1a686777a7b148ca20d9 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 19 Aug 2025 11:12:32 -0700 Subject: [PATCH] Add more account info. Include confirmed blocks in account-specific refresh. Alter name of frontier property for consistency with other block-type account properties. Alter name of block_count and confirmed_height to be consisten with each other. --- src/lib/account.ts | 67 +++++++++++++++++++++++++++++----- src/lib/block.ts | 2 +- src/lib/tools.ts | 2 +- src/lib/wallet/refresh.ts | 2 +- test/test.refresh-accounts.mjs | 14 +++---- 5 files changed, 67 insertions(+), 20 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index 294d8e8..21b0554 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -20,24 +20,58 @@ export class Account { #index?: number #publicKey: Uint8Array + #confirmed_balance?: bigint + #confirmed_block_height?: number + #confirmed_frontier_block?: string + #confirmed_receivable?: bigint + #confirmed_representative?: Account + #balance?: bigint - #frontier?: string + #block_height?: number + #frontier_block?: string + #open_block?: string #receivable?: bigint #representative?: Account + #representative_block?: string #weight?: bigint get address (): string { return `${PREFIX}${this.#address}` } get index (): number | undefined { return this.#index } get publicKey (): string { return bytes.toHex(this.#publicKey) } + get confirmed_balance (): bigint | undefined { return this.#confirmed_balance } + get confirmed_block_height (): number | undefined { return this.#confirmed_block_height } + get confirmed_frontier_block (): string | undefined { return this.#confirmed_frontier_block } + get confirmed_receivable (): bigint | undefined { return this.#confirmed_receivable } + get confirmed_representative (): Account | undefined { return this.#confirmed_representative } + get balance (): bigint | undefined { return this.#balance } - get frontier (): string | undefined { return this.#frontier } + get block_height (): number | undefined { return this.#block_height } + get frontier_block (): string | undefined { return this.#frontier_block } + get open_block (): string | undefined { return this.#open_block } get receivable (): bigint | undefined { return this.#receivable } get representative (): Account | undefined { return this.#representative } + get representative_block (): string | undefined { return this.#representative_block } get weight (): bigint | undefined { return this.#weight } + set confirmed_balance (v: bigint | number | string) { this.#confirmed_balance = BigInt(v) } + set confirmed_block_height (v: number | undefined) { if (v !== undefined) this.#confirmed_block_height = v | 0 } + set confirmed_frontier_block (v: string | undefined) { this.#confirmed_frontier_block = v } + set confirmed_receivable (v: bigint | number | string) { this.#confirmed_receivable = BigInt(v) } + set confirmed_representative (v: unknown) { + if (v instanceof Account) { + this.#confirmed_representative = v + } else if (typeof v === 'string') { + this.#confirmed_representative = Account.load(v) + } else { + throw new TypeError(`Invalid argument for account confirmed representative: ${v}`) + } + } + set balance (v: bigint | number | string) { this.#balance = BigInt(v) } - set frontier (v: string | undefined) { this.#frontier = v } + set block_height (v: number | undefined) { if (v !== undefined) this.#block_height = v | 0 } + set frontier_block (v: string | undefined) { this.#frontier_block = v } + set open_block (v: string | undefined) { this.#open_block = v } set receivable (v: bigint | number | string) { this.#receivable = BigInt(v) } set representative (v: unknown) { if (v instanceof Account) { @@ -48,6 +82,7 @@ export class Account { throw new TypeError(`Invalid argument for account representative: ${v}`) } } + set representative_block (v: string | undefined) { this.#representative_block = v } set weight (v: bigint | number | string) { this.#weight = BigInt(v) } private constructor (address: string, publicKey: Key, index?: number) { @@ -69,7 +104,7 @@ export class Account { * allow garbage collection. */ async destroy (): Promise { - this.#frontier = undefined + this.#frontier_block = undefined this.#balance = undefined this.#receivable = undefined this.#representative = undefined @@ -171,19 +206,31 @@ export class Account { } const data = { account: this.address, + include_confirmed: true, receivable: true, representative: true, weight: true } - const { balance, frontier, receivable, representative, weight } = await rpc.call('account_info', data) + const { confirmed_balance, confirmed_frontier, confirmed_height, confirmed_receivable, confirmed_representative, + balance, block_count, frontier, open_block, receivable, representative, representative_block, weight, + } = await rpc.call('account_info', data) + if (frontier == null) { throw new Error('Account not found') } - this.#balance = BigInt(balance) - this.#frontier = frontier - this.#receivable = BigInt(receivable) - this.#representative = await Account.load(representative) - this.#weight = BigInt(weight) + this.confirmed_balance = confirmed_balance + this.confirmed_block_height = confirmed_height + this.confirmed_frontier_block = confirmed_frontier + this.confirmed_representative = confirmed_representative + this.confirmed_receivable = confirmed_receivable + this.balance = balance + this.block_height = block_count + this.frontier_block = frontier + this.open_block = open_block + this.receivable = receivable + this.representative = representative + this.representative_block = representative_block + this.weight = weight } /** diff --git a/src/lib/block.ts b/src/lib/block.ts index aa8a31c..37c7fb7 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -101,7 +101,7 @@ export class Block { throw new TypeError('Invalid account') } balance ??= account.balance - previous ??= account.frontier + previous ??= account.frontier_block representative ??= account.representative if (typeof balance !== 'bigint' && typeof balance !== 'number' && typeof balance !== 'string') { throw new TypeError('Account balance is unknown') diff --git a/src/lib/tools.ts b/src/lib/tools.ts index f457018..39d5d8d 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -152,7 +152,7 @@ export async function sweep ( const recipientAccount = Account.load(recipient) const accounts = await wallet.refresh(rpc, from, to) for (const account of accounts) { - if (account.representative?.address && account.frontier && account.index != null) { + if (account.representative?.address && account.frontier_block && account.index != null) { const block = await new Block(account) .send(recipientAccount, account.balance ?? 0n) .sign(wallet, account.index) diff --git a/src/lib/wallet/refresh.ts b/src/lib/wallet/refresh.ts index 40373fc..1c8ac68 100644 --- a/src/lib/wallet/refresh.ts +++ b/src/lib/wallet/refresh.ts @@ -33,7 +33,7 @@ export async function _refresh (wallet: Wallet, rpc: unknown, from: unknown, to: for (const account of accounts) { account.balance = balances[account.address]?.balance account.receivable = balances[account.address]?.receivable - account.frontier = frontiers[account.address] + account.frontier_block = frontiers[account.address] } return accounts } catch (err) { diff --git a/test/test.refresh-accounts.mjs b/test/test.refresh-accounts.mjs index 6f3291c..89b30a8 100644 --- a/test/test.refresh-accounts.mjs +++ b/test/test.refresh-accounts.mjs @@ -39,12 +39,12 @@ await Promise.all([ assert.notEqual(account.balance, '') assert.equal(typeof account.balance, 'bigint') //@ts-expect-error - assert.ok(account.balance >= 0) + assert.ok(account.balance >= 0n) - assert.exists(account.frontier) - assert.equal(typeof account.frontier, 'string') - assert.notEqual(account.frontier, '') - assert.ok(/^[A-F0-9]{64}$/i.test(account.frontier ?? '')) + assert.exists(account.frontier_block) + assert.equal(typeof account.frontier_block, 'string') + assert.notEqual(account.frontier_block, '') + assert.ok(/^[A-F0-9]{64}$/i.test(account.frontier_block ?? '')) assert.exists(account.representative) assert.notEqual(account.representative, '') @@ -173,8 +173,8 @@ await Promise.all([ assert.ok(account instanceof Account) assert.equal(typeof account.balance, 'bigint') - assert.exists(account.frontier) - assert.equal(typeof account.frontier, 'string') + assert.exists(account.frontier_block) + assert.equal(typeof account.frontier_block, 'string') await assert.resolves(wallet.destroy()) }) -- 2.47.3