From 12c6302fa4719a47eab2c750ca66dae55481d1a4 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 8 Aug 2025 08:24:46 -0700 Subject: [PATCH] Utilize convert function for block balance calculations. --- src/lib/block.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/lib/block.ts b/src/lib/block.ts index 6a05014..8a2d342 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -8,6 +8,7 @@ import { BURN_PUBLIC_KEY, PREAMBLE, DIFFICULTY_RECEIVE, DIFFICULTY_SEND, UNITS } import { bytes, dec, hex } from './convert' import { NanoNaCl } from './nano-nacl' import { Rpc } from './rpc' +import { convert } from './tools' import { Wallet } from './wallet' /** @@ -80,7 +81,7 @@ export class Block { * the other parameters instead of passing them into the constructor. * * @param {(string|Account)} account - Target of the transaction; can include `balance`, `frontier`, `representative` - * @param {(bigint|number|string)} [balance] - Current balance of the target account + * @param {(bigint|number|string)} [balance] - Current balance of the target account in raw * @param {string} [previous] - Current frontier block hash of the target account * @param {(string|Account)} [representative] - Current representative of the target account */ @@ -106,7 +107,7 @@ export class Block { throw new TypeError('Account frontier is unknown') } this.account = account - this.balance = BigInt(balance) + this.balance = convert(balance, 'raw', 'raw', 'bigint') this.previous = previous if (representative instanceof Account) { this.representative = representative @@ -306,7 +307,7 @@ export class Block { if (typeof amount !== 'bigint' && typeof amount !== 'number' && typeof amount !== 'string') { throw new TypeError('Invalid amount') } - this.balance += BigInt(amount) * (10n ** BigInt(UNITS[unit.toUpperCase()])) + this.balance += convert(amount, unit, 'raw', 'bigint') if (typeof sendBlock !== 'string' && !(sendBlock instanceof Block)) { throw new TypeError('Invalid send block') @@ -343,13 +344,13 @@ export class Block { unit ??= 'RAW' if (typeof unit !== 'string' || typeof UNITS[unit] !== 'number') { - throw new TypeError('Invalid unit') + throw new TypeError('Invalid unit', { cause: unit }) } if (typeof amount !== 'bigint' && typeof amount !== 'number' && typeof amount !== 'string') { - throw new TypeError('Invalid amount') + throw new TypeError(`Invalid amount ${amount}`, { cause: typeof amount }) } - this.balance -= BigInt(amount) * (10n ** BigInt(UNITS[unit.toUpperCase()])) + this.balance -= convert(amount, unit, 'raw', 'bigint') if (this.balance < 0) { throw new RangeError('Insufficient funds', { cause: this.balance }) -- 2.47.3