Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: calcTokenAmount BigNumber more than 15 digits error (#25799)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** #### This PR: - Fixes a BigNumber Error on calcTokenAmount - Fixes Swaps destinationAmount type. '0' should take place of null occurrences - Adds calcTokenAmount tests #### Explanation: When certain large params are passed, e.g. when the decimal passed is 36, the calculation of `Math.pow(10, Number(decimals || 0))` is expected → 100000000. Then, when we pass this to BigNumber div() we get the error: ``` BigNumber Error: div() number type has more than 15 significant digits: 9.999999999999999e+35 ``` however, if we pass the same value as a BigNumber, we do not get the error. That is, instead of passing the result of `Math.pow(10, Number(decimals || 0))`, we pass the result of `new BigNumber(10).pow(decimals)` honestly, I don't quite understand why this is. We can see the [bignumber.js#div code here](https://github.com/MikeMcl/bignumber.js/blob/v4.1.0/bignumber.js#L750) #### Other notes: - It turns out that updating the bignumber.js package from 4.1.0 → 9.1.2 could also fix this issue - There is another, less-used BigNumber class that exists in our code ([this BigNumber div code](https://github.com/ethers-io/ethers.js/blob/f97b92bbb1bde22fcc44100af78d7f31602863ab/packages/bignumber/src.ts/bignumber.ts#L81)) --- [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/25799?quickstart=1) ## **Related issues** Fixes: #13738 Related: #25741 (comment) ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** (divisor was previously called multiplier) ![CleanShot 2024-07-16 at 01 07 09@2x](https://github.com/user-attachments/assets/f1444f58-ca02-404a-aba9-153455bb177c) ### **After** ![CleanShot 2024-07-16 at 01 19 06@2x](https://github.com/user-attachments/assets/c5251516-f8b1-459a-be5f-06f99d66b3f7) ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.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-extension/blob/develop/.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.
- Loading branch information