fix(transaction-pay-controller): convert exchange rate numbers to strings before passing to BigNumber#8808
Open
OGPoyraz wants to merge 4 commits into
Open
fix(transaction-pay-controller): convert exchange rate numbers to strings before passing to BigNumber#8808OGPoyraz wants to merge 4 commits into
OGPoyraz wants to merge 4 commits into
Conversation
…ings before passing to BigNumber BigNumber throws when a JS number has more than 15 significant digits. In getTokenFiatRate, three number values from external sources are passed to BigNumber constructor and .multipliedBy(): - tokenToNativeRate (from marketData) -> new BigNumber() - nativeToUsdRate (from CurrencyRateController) -> .multipliedBy() - nativeToFiatRate (from CurrencyRateController) -> .multipliedBy() All three can exceed 15 significant digits, especially for weak-currency locales where conversion rates are large numbers like 40115252.21304121 (16 significant digits). Wrapping in String() avoids the crash since BigNumber's string inputs have no precision limit.
pedronfigueiredo
previously approved these changes
May 14, 2026
vinistevam
previously approved these changes
May 14, 2026
47f26de
pedronfigueiredo
approved these changes
May 14, 2026
vinistevam
approved these changes
May 15, 2026
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.
Explanation
Fixes a crash in
transaction-pay-controllercaused byBigNumberthrowing when receiving a JavaScriptnumberwith more than 15 significant digits.Root Cause
BigNumberenforces a strict 15 significant digit limit onnumberinputs — both in the constructor (new BigNumber(number)) and in math methods (.multipliedBy(number),.times(number),.plus(number), etc.) — because IEEE 754 floats lose precision beyond that.In
getTokenFiatRate(token.ts), three rawnumbervalues from external sources are passed directly to BigNumber:When does this crash?
Case 1: Large conversion rate (weak-currency locales like VND, IDR)
Case 2: Very precise small price (micro-cap tokens)
Case 3: Near-peg stablecoin with API floating point noise
Real user state logs confirm CurrencyRateController stores values exceeding 15 significant digits for weak-currency locales:
This happens because
CurrencyRateController.boundedPrecisionNumberusestoFixed(9)which bounds decimal digits, not significant digits. For large numbers, 8+ integer digits + 9 decimal digits = 17+ significant digits.The Fix
Wrap all three values in
String()before passing to BigNumber. BigNumber's string constructor and string method arguments have no precision limit.getTokenFiatRateis the single entry point where rawnumbervalues from CurrencyRateController and marketData enter the transaction-pay-controller. Everything downstream operates onFiatRates({ fiatRate: string, usdRate: string }), so fixing this one function protects the entire controller.Related
CurrencyRateController.boundedPrecisionNumbershould usetoPrecision(15)instead oftoFixed(9)(separate issue for @metamask/assets-controllers)Changelog
@metamask/transaction-pay-controllerNote
Low Risk
Low risk, localized change to exchange-rate arithmetic that only alters input coercion to
BigNumberto prevent runtime exceptions; outputs remain string rates.Overview
Prevents
transaction-pay-controllercrashes when fiat/exchange rates exceed BigNumber’s 15-significant-digit limit by coercingtokenToNativeRate,nativeToUsdRate, andnativeToFiatRateto strings beforeBigNumberconstruction/multiplication ingetTokenFiatRate.Updates the package changelog to document the fix.
Reviewed by Cursor Bugbot for commit 1ec802c. Bugbot is set up for automated code reviews on this repo. Configure here.