Skip to content
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

Cherry-pick fix: decimals and fiat conversion crashes in simulation (#24422) into Version-v11.16.0 #24434

Merged

Conversation

dbrans
Copy link
Contributor

@dbrans dbrans commented May 8, 2024

Cherry-pick 603b1b5 (#24422) into Version-v11.16.0

Merge conflict resolved:

// Compiles the balance change for the native asset
function getNativeBalanceChange(
  nativeBalanceChange: SimulationBalanceChange | undefined,
  nativeFiatRate: number,
): BalanceChange | undefined {
  if (!nativeBalanceChange) {
    return undefined;
  }
  const asset = NATIVE_ASSET_IDENTIFIER;
  const amount = getAssetAmount(nativeBalanceChange, NATIVE_DECIMALS);
<<<<<<< HEAD - REJECTED
  const fiatAmount = amount.times(nativeFiatRate).toNumber();
======= ACCEPTED:
  const fiatAmount = amount
    .times(convertNumberToStringWithPrecisionWarning(nativeFiatRate))
    .toNumber();
>>>>>>> 603b1b5900 (fix: decimals and fiat conversion crashes in simulation (#24422))
  return { asset, amount, fiatAmount };
}

Copy link
Contributor

github-actions bot commented May 8, 2024

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.

@dbrans dbrans marked this pull request as ready for review May 8, 2024 13:07
@dbrans dbrans requested review from a team as code owners May 8, 2024 13:07
@metamaskbot metamaskbot added the INVALID-PR-TEMPLATE PR's body doesn't match template label May 8, 2024
@danjm
Copy link
Contributor

danjm commented May 8, 2024

@metamaskbot update-policies

@metamaskbot
Copy link
Collaborator

Policies updated

@dbrans dbrans requested review from a team as code owners May 8, 2024 13:24
<!--
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.
@danjm danjm force-pushed the Version-v11.16.0-cherry-pick-simulation-crash branch from 6a527ac to cb6e599 Compare May 20, 2024 21:42
@metamaskbot
Copy link
Collaborator

Builds ready [cb6e599]
Page Load Metrics (1367 ± 508 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint731851163115
domContentLoaded107027136
load60248813671058508
domInteractive107027136

@danjm danjm merged commit 4e45738 into Version-v11.16.0 May 20, 2024
66 of 67 checks passed
@danjm danjm deleted the Version-v11.16.0-cherry-pick-simulation-crash branch May 20, 2024 23:43
@github-actions github-actions bot locked and limited conversation to collaborators May 20, 2024
@metamaskbot metamaskbot added the release-11.16.0 Issue or pull request that will be included in release 11.16.0 label May 21, 2024
@metamaskbot
Copy link
Collaborator

No release label on PR. Adding release label release-11.16.0 on PR, as PR was cherry-picked in branch 11.16.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
INVALID-PR-TEMPLATE PR's body doesn't match template release-11.16.0 Issue or pull request that will be included in release 11.16.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants