Repository navigation
Conversation
Balance amounts were summed, divided and rounded with `bignumber.js`. Add a small exact-decimal helper built on `BigInt` and use it in place of BigNumber, so `bignumber.js` is no longer a dependency of `@metamask/assets-controller`. `bignumber.js` is heavy and fragile on mobile, and the controller only needs exact decimal arithmetic on strings. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
🛑 Files requiring codeowner reviewTo merge this PR, you'll need to get approvals from the following owners. @MetaMask/metamask-assets (12 files)
ℹ️ Files co-owned by
|
There was a problem hiding this comment.
This seems questionable 😕 . We're re-implementing bignumber.js here effectively, with a bespoke implementation not tested in prod, not shared by the rest of our codebase.
I don't understand the motivation here.
|
The PR description also states that |
Explanation
Current state:
@metamask/assets-controllerusesbignumber.jsto sum, divide, and round balance amounts. ASSETS-4143 flags it as brittle (it turns bad input intoNaNand can fail silently) and slow on mobile. The ticket namesUnifiedAssetsController, which no longer exists in the repo. The BigNumber usage lives inAssetsControllerand its data sources and selectors, so that is the scope here.Solution: Native
BigIntcovers the exact decimal arithmetic these call sites need.src/utils/decimal.ts. It represents a value as{ units: bigint; scale: number }and providesparseDecimal,formatDecimal,shiftDecimal,addDecimal, andsetDecimalPlaces. Values are parsed from strings, so no float is involved. Exponent form ("1e-18") and hex ("0x1f") are still accepted, as before.selectors/balance.ts, raw-to-human conversion inRpcDataSourceandAccountActivityDataSource,weiToHumanReadable,normalizeAmountString, and the fiat value inAssetsController.bignumber.jsis removed from this package's dependencies. Other workspace packages still use it, soyarn.lockchanges by one line.Behavior notes (intentional):
"0". PreviouslyAccountActivityDataSourcecould write"NaN"into state.weiToHumanReadable(x, 0)no longer drops the last digit of whole numbers:"100"used to give"1"."0", not"-0".10^decimalsis now exact. BigNumber rounded to 20 decimal places, so this only shows up for tokens with more than 20 decimals.Verification: A temporary differential test compared the new helpers against
bignumber.json 60k random inputs (normalization, division, sums, and half-up and truncating rounding). The only mismatch was the"-0"case above. The test was not kept.References
Checklist
Note
Medium Risk
Touches balance strings and aggregated fiat math across core asset paths; mostly behavior-preserving but with documented edge-case and float-ulp differences for displayed totals.
Overview
Replaces
bignumber.jsin@metamask/assets-controllerwith a newsrc/utils/decimal.tslayer built onBigInt(parseDecimal,formatDecimal,shiftDecimal,addDecimal,setDecimalPlaces). Balance parsing, human-readable conversion, aggregation, normalization, and fiat display now go through these helpers;bignumber.jsis removed frompackage.json.Call sites updated:
AccountActivityDataSourceandRpcDataSourceraw→human conversion;weiToHumanReadable;normalizeAmountString; aggregated balance selectors inselectors/balance.ts; fiat value inAssetsController(Number multiply for human-readable amounts); stale-balance test fixtures.Intentional behavior shifts: invalid amounts resolve to
"0"instead of possible"NaN"in state;weiToHumanReadable('100', 0)keeps the full integer; negatives that round to zero format as"0"not"-0"; fiat totals may differ slightly from the old path where two Numbers are multiplied. Unit tests coverdecimal.tsand addedweiToHumanReadableedge cases.Reviewed by Cursor Bugbot for commit 96b523e. Configure here.