diff --git a/src/core/assets.mjs b/src/core/assets.mjs index a959f80..f4d4627 100644 --- a/src/core/assets.mjs +++ b/src/core/assets.mjs @@ -40,7 +40,31 @@ export function numberToUnits(value, decimals, { mode = 'round' } = {}) { } export function bigintAmount(amount) { - return BigInt(String(amount || '0')); + if (typeof amount === 'bigint') return amount; + const str = String(amount ?? '0').trim(); + if (!str) return 0n; + // BigInt() cannot parse scientific notation (e.g. "6.03e+25"). + // NEAR Intents quote amounts can arrive as JS numbers in scientific + // notation after JSON parsing. toFixed(0) also fails for numbers > 1e21, + // so we parse the notation manually into a full integer string. + if (str.includes('e') || str.includes('E')) { + const [mantissa, expStr] = str.toLowerCase().split('e'); + const exp = parseInt(expStr, 10); + const negative = mantissa.startsWith('-'); + const positive = negative ? mantissa.slice(1) : mantissa; + const [intPart, decPart] = positive.split('.'); + const digits = intPart + (decPart || ''); + const decLen = (decPart || '').length; + const targetLen = digits.length - decLen + exp; + if (targetLen <= 0) return 0n; + const padded = digits.padEnd(targetLen, '0'); + const result = BigInt(padded.slice(0, targetLen) || '0'); + return negative ? -result : result; + } + if (str.includes('.')) { + return BigInt(str.split('.')[0]); + } + return BigInt(str); } export function formatNumber(value, digits = 8) {