-
-
Notifications
You must be signed in to change notification settings - Fork 15
Feat/migrate units conversion utils #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/migrate units conversion utils #255
Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub. |
|
Caution Review the following alerts detected in dependencies. According to your organization's Security Policy, you must resolve all "Block" alerts before proceeding. It is recommended to resolve "Warn" alerts too. Learn more about Socket for GitHub.
Ignoring alerts on:
|
src/unitsConversion.test.ts
Outdated
| @@ -0,0 +1,709 @@ | |||
| // web3 dependency is not used in the codebase, but only in tests | |||
| // eslint-disable-next-line import/no-extraneous-dependencies | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ignore comment shouldn't be needed, since this is a test file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! At some point we might want to investigate porting the Numeric class from the extension into this repo, using BigInt instead of BN. But this is sufficient for now.
I had a few comments but they are all pretty minor. Thanks for backfilling the tests!
5cecf26 to
d489240
Compare
|
@mcmire All comments should be fixed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
@SocketSecurity ignore npm/cross-fetch@4.1.0
|
## **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: - [x] merge MetaMask/utils#255 - [x] merge #18949 and then rebase - [x] 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 `toHexadecima`l 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. - [x] How to gracefully migrate from old BN? I would suggest keeping old BN implementation and only that functions deprecated. ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 4. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> 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** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **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. <!-- CURSOR_SUMMARY --> --- > [!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). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 6ea5e81. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
This PR is migrating state of
@metamask/ethjs-unitfrom this PR MetaMask/ethjs-unit#24 to Typescript and moving it to this repo.I migrated all existing test cases and also added bunch of new ones.
Just migration to
bigintimproved performance already quite a bit and I also added few more optimizations to make it even faster.Here are some results:
1.
getValueOfUnit- 92.4% faster (13.1x speedup)2.
numberToString- Mixed Results3.
fromWei- 41.8% to 71.9% faster4.
toWei- 55.2% to 79.0% faster