Skip to content

Conversation

@Nodonisko
Copy link
Contributor

@Nodonisko Nodonisko commented Aug 19, 2025

Description

This draft of migration from BN.js to native BigInt. It needs MetaMask/utils#255 to be merged and released first.

Native BigInt is much much faster than JS only BN implementation. This could improve app performance in many places like account list, login, dashboard etc. I already measured useAccounts hook and it made it 7x faster 220ms => 30ms.

Proposal for handling migration:

I moved cloned previous file index.js to legacy.js and updated imports everywhere to use this old implementation. Then I migrated whole old index.js to TS and to use BigInt. I also marked all legacy functions as deprecated so they could be spotted more easily during future refactoring and replaced with new implementation.

I think this approach is best in current situation because:

  1. Even that I tried my best I could have introduced some bugs it's better to introduce new functions slowly
  2. We are on tight schedule with performance improvements and updating these functions across whole codebase could take quite a long time and I am not even talking about testing and PR review
  3. I also added TS types which is causing type errors in some places where new functions are used because previous versions were typed as any.

TODOs:

  • merge Feat/migrate units conversion utils utils#255
  • merge chore: improve Engine tests stability #18949 and then rebase
  • in Engine.ts, around line ~1916 there is function toHexadecimal and while migrating it to new implementation with bigint I noticed that i receives params like solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp . In old implementation it causes toHexadecimal return NaN and in my new implementation it throws error. I guess this might be causing bug even in current implementation like total balance not being correct if there is more than one Solana account or something like that.
  • How to gracefully migrate from old BN? I would suggest keeping old BN implementation and only that functions deprecated.

Changelog

CHANGELOG entry:

Related issues

Fixes:

Manual testing steps

  1. Check that total balance across the app is correct for different SRPs (combinations of coins/tokens/networks etc.)

Screenshots/Recordings

Before

After

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Note

Introduce BigInt-backed number utilities while re-pointing existing code to legacy implementations, updating balance/fee logic and tests accordingly.

  • Core number utils (BigInt):
    • Add new util/number/index.ts with BigInt-based conversions/formatting (hexToBigInt, bigIntToHex, toWei, fromWei, fiat/token helpers, etc.).
    • Preserve previous BN.js utils in util/number/legacy.js (marked deprecated) and add comprehensive legacy.test.ts.
    • Update ESLint globals to include BigInt.
  • Codebase migration (temporary legacy shim):
    • Replace imports of util/number with util/number/legacy across app (Swaps, Ramp, Earn, Stake, Bridge, Transactions, selectors, hooks, components, tests).
    • Adjust typings/usages (e.g., remove unnecessary Hex cast, tweak chainId handling for images/selectors).
  • Engine and calculations:
    • Update balance/fiat computations to use BigInt paths (hexToBigInt, bigInt math) and avoid string BN math; fix chainId map lookups (use raw chainId).
    • Modify tests and data to store balances as hex strings with 0x prefix.
  • Tests and minor fixes:
    • Add new tests for BigInt utils and adapt existing tests/mocks to legacy paths.
    • Small test logic refinements (e.g., async wait/press order, debounced assertions).

Written by Cursor Bugbot for commit 6ea5e81. This will update automatically on new commits. Configure here.

@github-actions
Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@sethkfman sethkfman added area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. team-accounts-framework Accounts Framework team labels Aug 26, 2025
@Nodonisko Nodonisko force-pushed the feat/replace-bn-with-bigint branch from 8042d5e to d6ddc81 Compare August 29, 2025 19:07
@Nodonisko Nodonisko marked this pull request as ready for review September 2, 2025 14:28
@Nodonisko Nodonisko requested review from a team as code owners September 2, 2025 14:28
cursor[bot]

This comment was marked as outdated.

salimtb
salimtb previously approved these changes Sep 12, 2025
Copy link
Contributor

@salimtb salimtb left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@georgewrmarshall georgewrmarshall left a comment

Choose a reason for hiding this comment

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

Approving changes to component-library components on behalf of @MetaMask/design-system-engineers ✅

@Nodonisko Nodonisko dismissed stale reviews from georgewrmarshall and salimtb via 1909901 September 16, 2025 18:00
@Nodonisko Nodonisko force-pushed the feat/replace-bn-with-bigint branch from d6ddc81 to 1909901 Compare September 16, 2025 18:00
@sethkfman sethkfman changed the base branch from main to feat/replace-bn-with-bigint-margelo September 19, 2025 22:42
@Nodonisko Nodonisko force-pushed the feat/replace-bn-with-bigint branch from 1909901 to f9738d5 Compare October 2, 2025 17:08
@Nodonisko Nodonisko requested review from a team as code owners October 2, 2025 17:08
@Nodonisko Nodonisko requested review from a team as code owners October 2, 2025 17:08
@socket-security
Copy link

socket-security bot commented Oct 2, 2025

No dependency changes detected. Learn more about Socket for GitHub.

👍 No dependency changes detected in pull request

cursor[bot]

This comment was marked as outdated.

@Nodonisko Nodonisko force-pushed the feat/replace-bn-with-bigint branch from 27c55f8 to 6ea5e81 Compare October 6, 2025 12:45
@Nodonisko Nodonisko requested a review from a team as a code owner October 6, 2025 12:45
@sethkfman sethkfman added the skip-sonar-cloud Only used for bypassing sonar cloud when failures are not relevant to the changes. label Oct 7, 2025
@sethkfman sethkfman merged commit 64d2a07 into MetaMask:feat/replace-bn-with-bigint-margelo Oct 8, 2025
111 of 126 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. external-contributor skip-sonar-cloud Only used for bypassing sonar cloud when failures are not relevant to the changes. team-accounts-framework Accounts Framework team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants