Skip to content

fix: calcTokenAmount BigNumber more than 15 digits error#25799

Merged
digiwand merged 9 commits intodevelopfrom
fix-calcTokenAmount-bignumber-more-than-15-digits-error
Jul 17, 2024
Merged

fix: calcTokenAmount BigNumber more than 15 digits error#25799
digiwand merged 9 commits intodevelopfrom
fix-calcTokenAmount-bignumber-more-than-15-digits-error

Conversation

@digiwand
Copy link
Copy Markdown
Contributor

@digiwand digiwand commented Jul 12, 2024

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 → 9.999999999999999e+35. 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

new BigNumber(10).pow(decimals) does not have this rounding issue and can be passed to BigNumber div() without the error.

We can see the bignumber.js#div code here

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)

Open in GitHub Codespaces

Related issues

Fixes: #13738
Related: #25741 (comment)

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

(divisor was previously called multiplier)
CleanShot 2024-07-16 at 01 07 09@2x

After

CleanShot 2024-07-16 at 01 19 06@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.

@github-actions
Copy link
Copy Markdown
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.

@digiwand digiwand added team-snaps-deprecated DEPRECATED: please use "team-core-platform" instead team-confirmations Push issues to confirmations team labels Jul 12, 2024
: totalEthCost;

const decimalAdjustedDestinationAmount = calcTokenAmount(
destinationAmount,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's expected that the destinationAmount of a Quote is either a string or null. If it was a null, null would be passed to calcTokenAmount. If it was undefined, it would have been a number type. This fixes the type and expected null handling

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [d960c70]
Page Load Metrics (262 ± 244 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint774861288541
domContentLoaded9101382211
load501950262508244
domInteractive9101382211
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 4 Bytes (0.00%)

@codecov
Copy link
Copy Markdown

codecov bot commented Jul 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.77%. Comparing base (ced1db1) to head (dbfb8b8).
Report is 66 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop   #25799   +/-   ##
========================================
  Coverage    69.77%   69.77%           
========================================
  Files         1398     1398           
  Lines        49187    49187           
  Branches     13580    13580           
========================================
+ Hits         34318    34319    +1     
+ Misses       14869    14868    -1     

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

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [229a5d5]
Page Load Metrics (227 ± 208 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint772811174723
domContentLoaded999352411
load461576227433208
domInteractive999352411
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 1 Bytes (0.00%)

@digiwand digiwand added the needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) label Jul 16, 2024
@sonarqubecloud
Copy link
Copy Markdown

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [dbfb8b8]
Page Load Metrics (301 ± 314 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint711731182512
domContentLoaded108733189
load552480301654314
domInteractive108733189
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 1 Bytes (0.00%)

@digiwand digiwand merged commit 5f0949b into develop Jul 17, 2024
@digiwand digiwand deleted the fix-calcTokenAmount-bignumber-more-than-15-digits-error branch July 17, 2024 17:23
@github-actions github-actions bot locked and limited conversation to collaborators Jul 17, 2024
@github-actions github-actions bot removed the needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) label Jul 17, 2024
@metamaskbot metamaskbot added the release-12.3.0 Issue or pull request that will be included in release 12.3.0 label Jul 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-12.3.0 Issue or pull request that will be included in release 12.3.0 team-confirmations Push issues to confirmations team team-snaps-deprecated DEPRECATED: please use "team-core-platform" instead type-bug Something isn't working

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[Sentry] [Bug] Bug/Error: div() number type has more than 15 significant digits

4 participants