Skip to content

refactor(assets-controller): replace bignumber.js with native BigInt - #11864

Open
Kriys94 wants to merge 2 commits into
mainfrom
refactor/assets-controller-native-bigint
Open

Kriys94 wants to merge 2 commits into
mainfrom
refactor/assets-controller-native-bigint

Conversation

@Kriys94

@Kriys94 Kriys94 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explanation

Current state: @metamask/assets-controller uses bignumber.js to sum, divide, and round balance amounts. ASSETS-4143 flags it as brittle (it turns bad input into NaN and can fail silently) and slow on mobile. The ticket names UnifiedAssetsController, which no longer exists in the repo. The BigNumber usage lives in AssetsController and its data sources and selectors, so that is the scope here.

Solution: Native BigInt covers the exact decimal arithmetic these call sites need.

  • New src/utils/decimal.ts. It represents a value as { units: bigint; scale: number } and provides parseDecimal, formatDecimal, shiftDecimal, addDecimal, and setDecimalPlaces. Values are parsed from strings, so no float is involved. Exponent form ("1e-18") and hex ("0x1f") are still accepted, as before.
  • Call sites moved over: balance aggregation in selectors/balance.ts, raw-to-human conversion in RpcDataSource and AccountActivityDataSource, weiToHumanReadable, normalizeAmountString, and the fiat value in AssetsController.
  • bignumber.js is removed from this package's dependencies. Other workspace packages still use it, so yarn.lock changes by one line.

Behavior notes (intentional):

  • An invalid amount now becomes "0". Previously AccountActivityDataSource could write "NaN" into state.
  • weiToHumanReadable(x, 0) no longer drops the last digit of whole numbers: "100" used to give "1".
  • A negative amount that rounds to zero gives "0", not "-0".
  • Division by 10^decimals is now exact. BigNumber rounded to 20 decimal places, so this only shows up for tokens with more than 20 decimals.
  • The fiat value multiplies two Numbers, since the balance is already human-readable and the result is a Number. It can differ from the old exact product by one float ulp.

Verification: A temporary differential test compared the new helpers against bignumber.js on 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

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

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.js in @metamask/assets-controller with a new src/utils/decimal.ts layer built on BigInt (parseDecimal, formatDecimal, shiftDecimal, addDecimal, setDecimalPlaces). Balance parsing, human-readable conversion, aggregation, normalization, and fiat display now go through these helpers; bignumber.js is removed from package.json.

Call sites updated: AccountActivityDataSource and RpcDataSource raw→human conversion; weiToHumanReadable; normalizeAmountString; aggregated balance selectors in selectors/balance.ts; fiat value in AssetsController (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 cover decimal.ts and added weiToHumanReadable edge cases.

Reviewed by Cursor Bugbot for commit 96b523e. Configure here.

Kriys94 and others added 2 commits December 1, 2026 22:53
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>
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

🛑 Files requiring codeowner review

To merge this PR, you'll need to get approvals from the following owners.

@MetaMask/metamask-assets (12 files)
  • packages/assets-controller/CHANGELOG.md
  • packages/assets-controller/package.json
  • packages/assets-controller/src/__fixtures__/stale-balance/rpcAmounts.ts
  • packages/assets-controller/src/AssetsController.ts
  • packages/assets-controller/src/data-sources/AccountActivityDataSource.ts
  • packages/assets-controller/src/data-sources/evm-rpc-services/utils/parsing.test.ts
  • packages/assets-controller/src/data-sources/evm-rpc-services/utils/parsing.ts
  • packages/assets-controller/src/data-sources/RpcDataSource.ts
  • packages/assets-controller/src/selectors/balance.ts
  • packages/assets-controller/src/utils/decimal.test.ts
  • packages/assets-controller/src/utils/decimal.ts
  • packages/assets-controller/src/utils/normalizeAmountString.ts

ℹ️ Files co-owned by @MetaMask/core-platform

For convenience in maintaining this repo, this team co-owns the following files and thus may approve changes to them.

Note

You do not need to get approval from @MetaMask/core-platform to merge this PR.

(Expand to view list of files)
  • packages/assets-controller/CHANGELOG.md
  • packages/assets-controller/package.json

@Kriys94
Kriys94 marked this pull request as ready for review December 1, 2026 22:14
@Kriys94
Kriys94 requested review from a team as code owners December 1, 2026 22:14

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mcmire

mcmire commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

The PR description also states that bignumber.js is "brittle" because it rejects bad input or represents it as NaN, and the new implementation is better because it implicitly converts invalid input into "0". It seems to me that the problem is not that bignumber.js is doing the wrong thing, it is that code which has to do with representing big numbers is not being tested properly and that parsing errors are not being handled correctly.

This branch was previously deployed

1 inactive deployment
default-branch — 96b523ea Deployed Dec 1, 2026 by Kriys94 via Determine whether this PR is a release PR #7847
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants