Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: decimals and fiat conversion crashes in simulation (#24422)
<!-- 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. --> This PR fixes 2 bugs in `useBalanceChanges` `NaN` `fetchErc20Decimals` uses `parseInt` to parse the return from getTokenStandardAndDetails. `parseInt` will return `NaN` if it cannot parse the string: ``` > parseInt('0x') NaN ``` This PR patches `fetchErc20Decimals` to try a couple methods of parsing and fall back to the default if none of them work. decimals The constructor and many methods on `BigNumber` will throw an error if you attempt to call them with a javascript number that has more than 15 decimal places. An [explanation can be found here, along with a workaround](MikeMcl/bignumber.js#11 (comment)): Converting these numbers to a string may loose some precision, but is a safe. **New tests added and fixed** ``` ● useBalanceChanges › with token balance changes › uses default decimals when token details are not valid numbers expect(received).toBe(expected) // Object.is equality Expected: 18 Received: 0 271 | await waitForNextUpdate(); 272 | > 273 | expect(result.current.value[0].amount.decimalPlaces()).toBe(18); | ^ 274 | }); 275 | }); 276 | ● useBalanceChanges › with token balance changes › handles token fiat rate with more than 15 significant digits BigNumber Error: times() number type has more than 15 significant digits: 0.1234567890123456 141 | const fiatRate = erc20FiatRates[tokenBc.address]; 142 | const fiatAmount = fiatRate > 143 | ? amount.times(fiatRate).toNumber() | ^ 144 | : FIAT_UNAVAILABLE; 145 | ``` [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24422?quickstart=1) Fixes: #24336 1. Go to this page... 2. 3. <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> <!-- [screenshots/recordings] --> <!-- [screenshots/recordings] --> - [ ] I’ve followed [MetaMask 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. - [ ] 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