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')