From 26a1386da7ff9d12452cdcd45763ce7a8dec4ce1 Mon Sep 17 00:00:00 2001 From: Pedro Piccino Date: Fri, 20 Feb 2026 00:22:07 -0300 Subject: [PATCH] fix: use BigInt to prevent overflow when parsing large token amounts parseInt() on large token amounts (>2^53) causes JavaScript Number overflow, resulting in incorrect or negative values. This affects tokens with large supplies or high-precision decimals. Use BigInt for intermediate arithmetic, converting to Number only after dividing to a safe range. This preserves precision for amounts that exceed Number.MAX_SAFE_INTEGER. Fixes #280 --- src/lib/xray/lib/parser/utils/account-data.ts | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/lib/xray/lib/parser/utils/account-data.ts b/src/lib/xray/lib/parser/utils/account-data.ts index 5406ac27..d4a00cd9 100644 --- a/src/lib/xray/lib/parser/utils/account-data.ts +++ b/src/lib/xray/lib/parser/utils/account-data.ts @@ -49,15 +49,24 @@ export const traverseAccountData = ( let amount; if (tokenBalanceData.rawTokenAmount.decimals === 0) { - amount = parseInt( - tokenBalanceData.rawTokenAmount.tokenAmount + amount = Number( + BigInt( + tokenBalanceData.rawTokenAmount.tokenAmount + ) ); } else { + const raw = BigInt( + tokenBalanceData.rawTokenAmount.tokenAmount + ); + const divisor = BigInt( + 10 ** tokenBalanceData.rawTokenAmount.decimals + ); + const whole = raw / divisor; + const remainder = raw % divisor; amount = - parseInt( - tokenBalanceData.rawTokenAmount.tokenAmount - ) / - 10 ** tokenBalanceData.rawTokenAmount.decimals; + Number(whole) + + Number(remainder) / + 10 ** tokenBalanceData.rawTokenAmount.decimals; } if (