]> git.codecow.com Git - libnemo.git/commitdiff
Refactor denomination converter to be simpler, faster, and not call padEnd.
authorChris Duncan <chris@codecow.com>
Sat, 4 Jul 2026 08:12:07 +0000 (01:12 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 4 Jul 2026 08:12:07 +0000 (01:12 -0700)
src/lib/tools.ts

index b15143cdf909ab28d247d53a8fc2cd731ae85941..60410cef563790b6c204d2c1b1919f1655285fa3 100644 (file)
@@ -60,25 +60,24 @@ export class Tools {
                        throw new Error('Invalid output format', { cause: format })
                }
 
+               const inUnit = UNITS[inputUnit]
                let [i, f] = typeof amount === 'string'
                        ? amount.split('.')
                        : dec.toString(amount).split('.')
                i = i.replace(/^0*/g, '')
                f = f?.replace(/0*$/g, '')
-
-               const inUnit = UNITS[inputUnit]
-               const intShift = i.length + inUnit
                if (f?.length > inUnit) {
                        throw new RangeError('Amount contains fractional raw')
                }
 
                // convert to raw
-               let int = BigInt(i.padEnd(intShift, '0') || '0')
+               let shift = 0
+               while (shift++ < inUnit) i += '0'
+               let int = BigInt(i || '0')
                if (f != null) {
-                       const fracShift = f?.length + inUnit
-                       let frac = BigInt(f?.padEnd(fracShift, '0') || '0')
-                       frac /= 10n ** BigInt(f.length)
-                       int += frac
+                       shift = f.length
+                       while (shift++ < inUnit) f += '0'
+                       int += BigInt(f || '0')
                }
                if (int > MAX_SUPPLY) {
                        throw new Error('Amount exceeds available supply')