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

fix: decimals and fiat conversion crashes in simulation #24422

Merged
merged 2 commits into from
May 8, 2024

Conversation

dbrans
Copy link
Contributor

@dbrans dbrans commented May 8, 2024

Description

This PR fixes 2 bugs in useBalanceChanges

Fix 1: decimals parsing in fetchErc20Decimals sometimes returns 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.

Fix 2: fiat rates for ETH or tokens may contain more than 15 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: 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

Related issues

Fixes: #24336

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • 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 format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). 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.

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 added the team-confirmations Push issues to confirmations team label May 8, 2024
@dbrans dbrans marked this pull request as ready for review May 8, 2024 01:44
@dbrans dbrans requested review from a team as code owners May 8, 2024 01:44
@dbrans dbrans added the team-transactions Transactions team label May 8, 2024
Copy link

codecov bot commented May 8, 2024

Codecov Report

Attention: Patch coverage is 91.66667% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 67.36%. Comparing base (39e7512) to head (9972070).

Files Patch % Lines
...components/simulation-details/useBalanceChanges.ts 91.67% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           develop   #24422   +/-   ##
========================================
  Coverage    67.36%   67.36%           
========================================
  Files         1285     1285           
  Lines        50088    50096    +8     
  Branches     12996    12997    +1     
========================================
+ Hits         33738    33745    +7     
- Misses       16350    16351    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@metamaskbot
Copy link
Collaborator

Builds ready [9972070]
Page Load Metrics (805 ± 609 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint592371024220
domContentLoaded96618147
load4733418051269609
domInteractive96618147
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 89 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@sleepytanya
Copy link
Contributor

Fiat rates and ERC20 decimals are accurately parsed:

Screenshot 2024-05-07 at 23 58 17
panc.mov
uni.mov

@dbrans dbrans merged commit 603b1b5 into develop May 8, 2024
78 checks passed
@dbrans dbrans deleted the dbrans/fix-decimals-amounts-simulation branch May 8, 2024 10:15
@github-actions github-actions bot locked and limited conversation to collaborators May 8, 2024
@metamaskbot metamaskbot added release-11.16.0 Issue or pull request that will be included in release 11.16.0 and removed release-11.18.0 labels May 21, 2024
@metamaskbot
Copy link
Collaborator

Missing release label release-11.16.0 on PR. Adding release label release-11.16.0 on PR and removing other release labels(release-11.18.0), 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
release-11.16.0 Issue or pull request that will be included in release 11.16.0 team-confirmations Push issues to confirmations team team-transactions Transactions team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: MetaMask encounters errors when processing transactions initiated from dapp
5 participants