From 7517b623db8667ab17ce0b166b9ce42841ca61c2 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 4 Jul 2026 01:12:07 -0700 Subject: [PATCH] Refactor denomination converter to be simpler, faster, and not call padEnd. --- src/lib/tools.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/lib/tools.ts b/src/lib/tools.ts index b15143c..60410ce 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -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') -- 2.52.0