fix: use BigInt to prevent overflow when parsing large token amounts#305
Open
agentmila wants to merge 1 commit into
Open
fix: use BigInt to prevent overflow when parsing large token amounts#305agentmila wants to merge 1 commit into
agentmila wants to merge 1 commit into
Conversation
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 helius-labs#280
|
Someone is attempting to deploy a commit to the Helius Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
The token amount parser in
account-data.tsusesparseInt()to parse raw token amounts. When token amounts exceedNumber.MAX_SAFE_INTEGER(~9.0 × 10¹⁵), JavaScript loses precision, producing incorrect or negative values.This causes the overflow issue reported in #280, where a minting transaction shows a negative amount instead of the correct minted value.
Root Cause
parseInt(tokenAmount)returns a JavaScriptNumber, which only has 53 bits of integer precision. Token amounts in lamports or with high decimals can easily exceed this.Fix
Use
BigInt()for intermediate arithmetic, converting toNumberonly after dividing to a safe range:Number(BigInt(tokenAmount))This preserves precision for large amounts while maintaining the existing
numbertype interface.Fixes #280