]> git.codecow.com Git - libnemo.git/commitdiff
Add more account info.
authorChris Duncan <chris@zoso.dev>
Tue, 19 Aug 2025 18:12:32 +0000 (11:12 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 19 Aug 2025 18:12:32 +0000 (11:12 -0700)
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
src/lib/block.ts
src/lib/tools.ts
src/lib/wallet/refresh.ts
test/test.refresh-accounts.mjs

index 294d8e888cb998952060fe033bf9376d16210999..21b055492483657f27225410249a6b91e96830d2 100644 (file)
@@ -20,24 +20,58 @@ export class Account {
        #index?: number\r
        #publicKey: Uint8Array<ArrayBuffer>\r
 \r
+       #confirmed_balance?: bigint\r
+       #confirmed_block_height?: number\r
+       #confirmed_frontier_block?: string\r
+       #confirmed_receivable?: bigint\r
+       #confirmed_representative?: Account\r
+\r
        #balance?: bigint\r
-       #frontier?: string\r
+       #block_height?: number\r
+       #frontier_block?: string\r
+       #open_block?: string\r
        #receivable?: bigint\r
        #representative?: Account\r
+       #representative_block?: string\r
        #weight?: bigint\r
 \r
        get address (): string { return `${PREFIX}${this.#address}` }\r
        get index (): number | undefined { return this.#index }\r
        get publicKey (): string { return bytes.toHex(this.#publicKey) }\r
 \r
+       get confirmed_balance (): bigint | undefined { return this.#confirmed_balance }\r
+       get confirmed_block_height (): number | undefined { return this.#confirmed_block_height }\r
+       get confirmed_frontier_block (): string | undefined { return this.#confirmed_frontier_block }\r
+       get confirmed_receivable (): bigint | undefined { return this.#confirmed_receivable }\r
+       get confirmed_representative (): Account | undefined { return this.#confirmed_representative }\r
+\r
        get balance (): bigint | undefined { return this.#balance }\r
-       get frontier (): string | undefined { return this.#frontier }\r
+       get block_height (): number | undefined { return this.#block_height }\r
+       get frontier_block (): string | undefined { return this.#frontier_block }\r
+       get open_block (): string | undefined { return this.#open_block }\r
        get receivable (): bigint | undefined { return this.#receivable }\r
        get representative (): Account | undefined { return this.#representative }\r
+       get representative_block (): string | undefined { return this.#representative_block }\r
        get weight (): bigint | undefined { return this.#weight }\r
 \r
+       set confirmed_balance (v: bigint | number | string) { this.#confirmed_balance = BigInt(v) }\r
+       set confirmed_block_height (v: number | undefined) { if (v !== undefined) this.#confirmed_block_height = v | 0 }\r
+       set confirmed_frontier_block (v: string | undefined) { this.#confirmed_frontier_block = v }\r
+       set confirmed_receivable (v: bigint | number | string) { this.#confirmed_receivable = BigInt(v) }\r
+       set confirmed_representative (v: unknown) {\r
+               if (v instanceof Account) {\r
+                       this.#confirmed_representative = v\r
+               } else if (typeof v === 'string') {\r
+                       this.#confirmed_representative = Account.load(v)\r
+               } else {\r
+                       throw new TypeError(`Invalid argument for account confirmed representative: ${v}`)\r
+               }\r
+       }\r
+\r
        set balance (v: bigint | number | string) { this.#balance = BigInt(v) }\r
-       set frontier (v: string | undefined) { this.#frontier = v }\r
+       set block_height (v: number | undefined) { if (v !== undefined) this.#block_height = v | 0 }\r
+       set frontier_block (v: string | undefined) { this.#frontier_block = v }\r
+       set open_block (v: string | undefined) { this.#open_block = v }\r
        set receivable (v: bigint | number | string) { this.#receivable = BigInt(v) }\r
        set representative (v: unknown) {\r
                if (v instanceof Account) {\r
@@ -48,6 +82,7 @@ export class Account {
                        throw new TypeError(`Invalid argument for account representative: ${v}`)\r
                }\r
        }\r
+       set representative_block (v: string | undefined) { this.#representative_block = v }\r
        set weight (v: bigint | number | string) { this.#weight = BigInt(v) }\r
 \r
        private constructor (address: string, publicKey: Key, index?: number) {\r
@@ -69,7 +104,7 @@ export class Account {
        * allow garbage collection.\r
        */\r
        async destroy (): Promise<void> {\r
-               this.#frontier = undefined\r
+               this.#frontier_block = undefined\r
                this.#balance = undefined\r
                this.#receivable = undefined\r
                this.#representative = undefined\r
@@ -171,19 +206,31 @@ export class Account {
                }\r
                const data = {\r
                        account: this.address,\r
+                       include_confirmed: true,\r
                        receivable: true,\r
                        representative: true,\r
                        weight: true\r
                }\r
-               const { balance, frontier, receivable, representative, weight } = await rpc.call('account_info', data)\r
+               const { confirmed_balance, confirmed_frontier, confirmed_height, confirmed_receivable, confirmed_representative,\r
+                       balance, block_count, frontier, open_block, receivable, representative, representative_block, weight,\r
+               } = await rpc.call('account_info', data)\r
+\r
                if (frontier == null) {\r
                        throw new Error('Account not found')\r
                }\r
-               this.#balance = BigInt(balance)\r
-               this.#frontier = frontier\r
-               this.#receivable = BigInt(receivable)\r
-               this.#representative = await Account.load(representative)\r
-               this.#weight = BigInt(weight)\r
+               this.confirmed_balance = confirmed_balance\r
+               this.confirmed_block_height = confirmed_height\r
+               this.confirmed_frontier_block = confirmed_frontier\r
+               this.confirmed_representative = confirmed_representative\r
+               this.confirmed_receivable = confirmed_receivable\r
+               this.balance = balance\r
+               this.block_height = block_count\r
+               this.frontier_block = frontier\r
+               this.open_block = open_block\r
+               this.receivable = receivable\r
+               this.representative = representative\r
+               this.representative_block = representative_block\r
+               this.weight = weight\r
        }\r
 \r
        /**\r
index aa8a31c8aadc4ed3e725173f87477ac0aea4b421..37c7fb70df4455c6eff3e0315f8cf6dc6c193b55 100644 (file)
@@ -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')
index f45701836ef906d7c59a40ee9e836b1d1f8ceb79..39d5d8dafb1739823f36f0aabd79096d1ad7c522 100644 (file)
@@ -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)
index 40373fce04ea1dc22115fe018488c675b6a51e92..1c8ac68f3403bfa5306492b14f997f39d7c40766 100644 (file)
@@ -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) {
index 6f3291c2d84e24b111b55269d1bd893bd270193f..89b30a82d2f6b6a820ef16930d7d3c547fe09c2e 100644 (file)
@@ -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())
                })