Skip to content

Ensure exchange rate is stringified before passing to bignumber in token confirm component - #12550

Merged
danjm merged 1 commit into
developfrom
fix-token-exchange-rates
Nov 2, 2021
Merged

danjm merged 1 commit into
developfrom
fix-token-exchange-rates

Conversation

@danjm

@danjm danjm commented Nov 1, 2021

Copy link
Copy Markdown
Contributor

With this change, we introduced the possibility of exchange rate numbers having more than 15 significant digits: MetaMask/core#585

Our current version of BigNumber does not handle that well, but does if the number is stringified.

Currently in prod, users will hit an error screen when this happens. Here is the associated error record in sentry https://sentry.io/organizations/metamask/issues/2745103173/?project=273505&query=is%3Aunresolved+firstRelease%3Alatest&statsPeriod=14d

@danjm
danjm requested a review from a team as a code owner November 1, 2021 15:26
@danjm
danjm requested a review from brad-decker November 1, 2021 15:26
@github-actions

github-actions Bot commented Nov 1, 2021

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.

@Gudahtt Gudahtt left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we know why bignumber.js throws on numbers with more than 15 significant digits? If it's a matter of significant digits beyond 15 being unreliable due to floating point error, maybe we're better off truncating our conversion rate to 15 (e.g. using toPrecision(15)) rather than keeping this additional precision.

Longer-term we should consider using BigInt to perform these calculations in the controllers repo so we don't lose precision from floating point errors, and truncating the calculated value to reflect the amount of precious that was input, and returning the value as a BigInt or at least a string.

@metamaskbot

Copy link
Copy Markdown
Collaborator
Builds ready [d5d50a1]
Page Load Metrics (329 ± 37 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint2755423297837
domContentLoaded2665323187837
load2745403297837
domInteractive2665323187837

highlights:

storybook

@Gudahtt Gudahtt left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll approve for now since this seems like a quick and relatively harmless way to fix the crash, but we should follow up with a better solution that doesn't include spurious digits that give a false impression of how precise this value is.

For more information on that, see https://en.wikipedia.org/wiki/Significant_figures#Rules_to_identify_significant_figures_in_a_number
and https://en.wikipedia.org/wiki/Significant_figures#Multiplication_and_division

@danjm
danjm merged commit 8ae90ae into develop Nov 2, 2021
@danjm
danjm deleted the fix-token-exchange-rates branch November 2, 2021 08:23
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 2, 2021
@danjm

danjm commented Nov 2, 2021

Copy link
Copy Markdown
Contributor Author

@Gudahtt A couple notes regarding big number. In the latest versions, it does not throw when significant digits are greater than 15, unless the DEBUG flag is set to true.

Before the latest version, stringifying before passing to bignumber was the recommended approach: MikeMcl/bignumber.js#148 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants