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: Error: new BigNumber() more than 15 digits feat: apply ellipsis #25741

Merged
merged 18 commits into from
Jul 12, 2024

Conversation

digiwand
Copy link
Contributor

@digiwand digiwand commented Jul 10, 2024

Description

Plan is to cherry-pick this PR for V12.1

  • Adds ellipsis to Permit value
  • Fixes error:
BigNumber Error: new BigNumber() number type has more than 15 significant digits

Note that the precision is lost through formatAmount. Fix will be added #25755

Most file updates are snapshot updates

Open in GitHub Codespaces

Related issues

Fixes: https://github.com/MetaMask/MetaMask-planning/issues/2730 (ellipsis)
Fixes: #25751 (Error)

Technical Details

  • JSON.parse coerces number values into scientific notation if they are greater than Number.MAX_SAFE_INTEGER, which is the max safe integer in JavaScript.
    e.g. 30001231231212312138768 → 3.0001231231212312e+22
  • JSON.parse reviver cannot help since the value will be coerced by the time it
    reaches the reviver function.
  • We preserve the value by extracting the value with a regex and overwriting the JSON.parse value. The preserved value should never be converted to a JavaScript number since it will lose its precision. Instead, we can preserve the value as a string, BigNumber, or Numeric type. BigInt also has rounding limitations.
  • BigNumber errors out when constructed with numbers with 15+ digits. It can accept strings to preserve digits.
  • formatAmount in ui/pages/confirmations/components/simulation-details/formatAmount.ts utilizes Intl.NumberFormat, which requires a BigInt or number passed to it causing a loss in precision when computing large numbers. It does not accept strings. Currently, it will display 0s for the digits after 15 digits.
    e.g. 30001231231212312138768 → 30,001,231,231,212,312,000,000
    Issue ticket to address this [Bug]: Precision is lost displaying Permit value in simulation and data tree #25755

Manual testing steps

  1. run yarn storybook
  2. go to http://localhost:6006/?path=/story/pages-confirmations-confirm-signatures-signedtypeddatav3orv4--permit-story&args=msgParams.data:3.0001231231212312e+22
  3. copy and paste the following into data
"{\"types\":{\"EIP712Domain\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"version\",\"type\":\"string\"},{\"name\":\"chainId\",\"type\":\"uint256\"},{\"name\":\"verifyingContract\",\"type\":\"address\"}],\"Permit\":[{\"name\":\"owner\",\"type\":\"address\"},{\"name\":\"spender\",\"type\":\"address\"},{\"name\":\"value\",\"type\":\"uint256\"},{\"name\":\"nonce\",\"type\":\"uint256\"},{\"name\":\"deadline\",\"type\":\"uint256\"}]},\"primaryType\":\"Permit\",\"domain\":{\"name\":\"MyToken\",\"version\":\"1\",\"verifyingContract\":\"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC\",\"chainId\":1},\"message\":{\"owner\":\"0x935e73edb9ff52e23bac7f7e043a1ecd06d05477\",\"spender\":\"0x5B38Da6a701c568545dCfcB03FcB875f56beddC4\",\"value\":30001231231212312138768,\"nonce\":0,\"deadline\":50000000000}}"

Screenshots/Recordings

Before

CleanShot 2024-07-10 at 01 14 52

After

CleanShot 2024-07-11 at 00 37 25@2x
CleanShot 2024-07-11 at 00 37 37@2x

Pre-merge author checklist

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.

digiwand added 4 commits July 10, 2024 14:52
- calculation passed to BigNumber may result in over 15 digits which errors
- preserve the precision up to formatAmount
- currently formatAmount takes away precision after it transforms the value using toNumber to pass to Intl.Number
@digiwand digiwand requested review from a team as code owners July 10, 2024 14:05
Copy link
Contributor

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.

@metamaskbot metamaskbot added the team-confirmations Push issues to confirmations team label Jul 10, 2024
@digiwand digiwand changed the title fix: Error: new BigNumber() more than 15 digits feat: apply fix: Error: new BigNumber() more than 15 digits feat: apply ellipsis Jul 10, 2024
@@ -52,10 +53,15 @@ const PermitSimulation: React.FC<{
}, [exchangeRate, value]);

const { tokenValue, tokenValueMaxPrecision } = useMemo(() => {
const valueBN = new BigNumber(value / Math.pow(10, tokenDecimals));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to split the BigNumber constants to use built in pow method since the result of value / Math.pow(10, tokenDecimals) could lead to an unexpected lengthy number. If it is over 15 digits it would cause the error.

We are also not applying to string on the result of the equation to pass to BigNumber directly because this could also result in precision loss.

@metamaskbot
Copy link
Collaborator

Builds ready [1182bb5]
Page Load Metrics (235 ± 236 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint733761136230
domContentLoaded118331189
load501924235492236
domInteractive118331189
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 0 Bytes (0.00%)
  • ui: 202 Bytes (0.00%)
  • common: 276 Bytes (0.00%)

jpuri
jpuri previously approved these changes Jul 11, 2024
Copy link
Contributor

@jpuri jpuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 changes look good

matthewwalsh0
matthewwalsh0 previously approved these changes Jul 11, 2024
@digiwand digiwand dismissed stale reviews from matthewwalsh0, pedronfigueiredo, and jpuri via ab747cf July 11, 2024 14:20
@metamaskbot
Copy link
Collaborator

Builds ready [d82f208]
Page Load Metrics (153 ± 146 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint714221187134
domContentLoaded10125332411
load451458153305146
domInteractive10125332411
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 0 Bytes (0.00%)
  • ui: 202 Bytes (0.00%)
  • common: 276 Bytes (0.00%)

Copy link

@metamaskbot
Copy link
Collaborator

Builds ready [fceba54]
Page Load Metrics (71 ± 10 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint611531012010
domContentLoaded97130167
load39127712010
domInteractive97130167
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 0 Bytes (0.00%)
  • ui: 202 Bytes (0.00%)
  • common: 276 Bytes (0.00%)

Copy link

codecov bot commented Jul 11, 2024

Codecov Report

Attention: Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.

Project coverage is 69.97%. Comparing base (5ee57a6) to head (fceba54).
Report is 32 commits behind head on develop.

Files Patch % Lines
shared/modules/transaction.utils.ts 90.91% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #25741      +/-   ##
===========================================
+ Coverage    69.77%   69.97%   +0.19%     
===========================================
  Files         1376     1390      +14     
  Lines        48403    48910     +507     
  Branches     13348    13456     +108     
===========================================
+ Hits         33773    34221     +448     
- Misses       14630    14689      +59     

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

Copy link
Contributor

@legobeat legobeat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@digiwand
Copy link
Contributor Author

applying a similar fix for 15 digits [issue] PR:(#13738) #25799

later, we can reuse the calcTokenAmount that is being updated in the PR

@digiwand digiwand merged commit 528c653 into develop Jul 12, 2024
76 checks passed
@digiwand digiwand deleted the feat-permit-simulation-ellipsis-format branch July 12, 2024 13:09
@github-actions github-actions bot locked and limited conversation to collaborators Jul 12, 2024
@metamaskbot metamaskbot added the release-12.3.0 Issue or pull request that will be included in release 12.3.0 label Jul 12, 2024
@digiwand digiwand added release-12.1.0 Issue or pull request that will be included in release 12.1.0 and removed release-12.3.0 Issue or pull request that will be included in release 12.3.0 labels Jul 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-12.1.0 Issue or pull request that will be included in release 12.1.0 team-confirmations Push issues to confirmations team
Projects
None yet
6 participants