]> git.codecow.com Git - libnemo.git/commitdiff
Fix block unit conversion.
authorChris Duncan <chris@zoso.dev>
Fri, 8 Aug 2025 12:44:26 +0000 (05:44 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 8 Aug 2025 12:44:26 +0000 (05:44 -0700)
src/lib/block.ts

index 5912593d32d133e2159e7ba0060f10102c55581e..6a050142800c05740cc2fa1abaf347e95f63172e 100644 (file)
@@ -286,7 +286,7 @@ export class Block {
        *
        * @param {(string|Block)} sendBlock - Corresponding send block or its hash
        * @param {(bigint|number|string)} amount - Amount to be received from sender
-       * @param {string} [unit] - Unit of measure for amount. Default: "NANO" (10³⁰ RAW)
+       * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
        * @returns {Block} This block with balance, link, and subtype configured
        */
        receive (sendBlock: string | Block, amount: bigint | number | string, unit?: string): Block
@@ -306,7 +306,7 @@ export class Block {
                        if (typeof amount !== 'bigint' && typeof amount !== 'number' && typeof amount !== 'string') {
                                throw new TypeError('Invalid amount')
                        }
-                       this.balance += BigInt(amount) * (1n << BigInt(UNITS[unit.toUpperCase()]))
+                       this.balance += BigInt(amount) * (10n ** BigInt(UNITS[unit.toUpperCase()]))
 
                        if (typeof sendBlock !== 'string' && !(sendBlock instanceof Block)) {
                                throw new TypeError('Invalid send block')
@@ -329,7 +329,7 @@ export class Block {
        *
        * @param {(string|Account)} account - Account to target or its address or public key
        * @param {(bigint|number|string)} amount - Amount to send to recipient
-       * @param {string} [unit] - Unit of measure for amount. Default: "NANO" (10³⁰ RAW)
+       * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
        * @returns {Block} This block with balance, link, and subtype configured
        */
        send (account: string | Account, amount: bigint | number | string, unit?: string): Block
@@ -349,7 +349,7 @@ export class Block {
                        if (typeof amount !== 'bigint' && typeof amount !== 'number' && typeof amount !== 'string') {
                                throw new TypeError('Invalid amount')
                        }
-                       this.balance -= BigInt(amount) * (1n << BigInt(UNITS[unit.toUpperCase()]))
+                       this.balance -= BigInt(amount) * (10n ** BigInt(UNITS[unit.toUpperCase()]))
 
                        if (this.balance < 0) {
                                throw new RangeError('Insufficient funds', { cause: this.balance })