]> git.codecow.com Git - libnemo.git/commitdiff
Utilize convert function for block balance calculations.
authorChris Duncan <chris@zoso.dev>
Fri, 8 Aug 2025 15:24:46 +0000 (08:24 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 8 Aug 2025 15:24:46 +0000 (08:24 -0700)
src/lib/block.ts

index 6a050142800c05740cc2fa1abaf347e95f63172e..8a2d342f841a46667cb94c98ab55772c2d050769 100644 (file)
@@ -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 })