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: handle low numbers when saving default advanced gas fees #22790

Merged
merged 7 commits into from Feb 19, 2024

Conversation

vinistevam
Copy link
Contributor

@vinistevam vinistevam commented Feb 2, 2024

Description

This PR aims to fix a bug when a user saves very low default values, in the example 0.000000001 number was turned into 1e-9 string and thrown when converting it down the line in the next transaction.

Related issues

Fixes: #22605

Manual testing steps

  1. Go to send flow
  2. Edit gas fee > Advanced
  3. Change Max Base Fee 1 and Priority Fee to 0.000000001
  4. Check Save these values as my default for the <chain name> network.

I was able to reproduce the issue using Ganache with default gas zero.

Screenshots/Recordings

Before

before_fix_gas.webm

After

advance-gas.webm

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've clearly explained what problem this PR is solving and how it is solved.
  • I've linked related issues
  • I've included manual testing steps
  • I've included screenshots/recordings if applicable
  • 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.
  • I’ve properly set the pull request status:
    • In case it's not yet "ready for review", I've set it to "draft".
    • In case it's "ready for review", I've changed it from "draft" to "non-draft".

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 Feb 2, 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.

@vinistevam vinistevam changed the title fix: handle small numbers when saving default advance gas fees fix: handle low numbers when saving default advanced gas fees Feb 2, 2024
@vinistevam vinistevam added the team-confirmations-system DEPRECATED: please use "team-confirmations" label instead label Feb 2, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [38d63f8]
Page Load Metrics (830 ± 48 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint951781242813
domContentLoaded95623168
load688101283010048
domInteractive95623168
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 418 Bytes (0.01%)
  • common: 0 Bytes (0.00%)

Copy link

codecov bot commented Feb 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (8ab0637) 68.53% compared to head (bdaca06) 68.53%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #22790      +/-   ##
===========================================
- Coverage    68.53%   68.53%   -0.00%     
===========================================
  Files         1089     1089              
  Lines        42966    42972       +6     
  Branches     11440    11440              
===========================================
+ Hits         29446    29448       +2     
- Misses       13520    13524       +4     

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

@vinistevam vinistevam marked this pull request as ready for review February 2, 2024 10:29
@vinistevam vinistevam requested a review from a team as a code owner February 2, 2024 10:29
DDDDDanica
DDDDDanica previously approved these changes Feb 2, 2024
@DDDDDanica
Copy link
Contributor

LGTM!

@sleepytanya
Copy link
Contributor

sleepytanya commented Feb 2, 2024

@vinistevam Build from 38d63f8a9cf05be24c39d1495a0236b0b7e71b96] - it won't let me type 0 after decimal point:

decimal.mov

I can cut and paste 0.000000001 but it results in failed transaction.

@@ -19,6 +19,7 @@ import { bnGreaterThan, bnLessThan } from '../../../../../helpers/utils/util';
import { useAdvancedGasFeePopoverContext } from '../../context';
import AdvancedGasFeeInputSubtext from '../../advanced-gas-fee-input-subtext';
import { decGWEIToHexWEI } from '../../../../../../shared/modules/conversion.utils';
import { Numeric } from '../../../../../../shared/modules/Numeric';

const validatePriorityFee = (value, gasFeeEstimates) => {
if (value < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If I read this right, value is actually string here both before and after this change - would it make sense to treat it as a Numeric here for consistency, either by transforming it here or in the calling scope?

Then the comparison functions from Numeric can be used and the import of bnGreaterThan, bnLessThan could be removed from this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeap apart from the FormField setting the value as number the rest of the code treating as a string. I'm pushing the changes to treat it as Numeric in both of the components for consistency as you suggested and remove the bnGreaterThan, bnLessThan.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I missed the part where the input is still getting transformed via number through the FormField. This means the resulting value can be different than entered by the user through loss of precision from inherent limitations of floating-point representation. If we don't want to introduce rounding errors, it would have to go straight from string to Numeric without intermediary parseFloat.
Some examples of value that will be transformed: 1234.00000000000030, 1234.00000000000080.

Seems to be relevant for all places that accept decimal input via the numeric input component.

onChange?.(parseFloat(newValue || 0, 10));

// detect discrepancy introduced through `parseFloat`
new Numeric(parseFloat(s), 10).toFixed(16) !== new Numeric(s, 10).toFixed(18)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should be ok with rounding numbers up to 1 Wei (0.000000001 Gwei).
I agree this is relevant for every piece that uses this component with decimals. I need to check maybe the FormTextField component doesn't have this limitation. The only issue I saw when changing to string in the component is the visual, so UX would also need to be involved.
Here is the ticket to track the precision issue.
This fix ensures that we can handle low numbers when checking the "Save these values as my default..." option.

@metamaskbot
Copy link
Collaborator

Builds ready [dabd239]
Page Load Metrics (861 ± 25 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint94161122199
domContentLoaded9471694
load7509638615325
domInteractive9471694
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 241 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [9e0a0ae]
Page Load Metrics (783 ± 19 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint88140109157
domContentLoaded9421784
load7078597833919
domInteractive9421784
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 346 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [49435c8]
Page Load Metrics (770 ± 19 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint86137107136
domContentLoaded9461784
load7098717704019
domInteractive9461784
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 346 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@vinistevam vinistevam force-pushed the fix/handle-small-number-save-advance-gas-fees branch from b3f28fb to af27baf Compare February 6, 2024 17:29
@metamaskbot
Copy link
Collaborator

Builds ready [01352bb]
Page Load Metrics (794 ± 27 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint92165118178
domContentLoaded105821115
load7109177945527
domInteractive105821115
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 375 Bytes (0.01%)
  • common: 0 Bytes (0.00%)

@sleepytanya
Copy link
Contributor

Low values can be entered and saved correctly.
Chrome 121.0.6167.139

123.mov

Firefox 122.0

456.mov

matthewwalsh0
matthewwalsh0 previously approved these changes Feb 9, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [4491d22]
Page Load Metrics (1235 ± 168 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1444002176531
domContentLoaded1296372512
load80920701235350168
domInteractive1296372512
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 381 Bytes (0.01%)
  • common: 0 Bytes (0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [728b59c]
Page Load Metrics (936 ± 76 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1252711803115
domContentLoaded970322311
load818147593615976
domInteractive970322311
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 381 Bytes (0.01%)
  • common: 0 Bytes (0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [bdaca06]
Page Load Metrics (1072 ± 86 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1024292116230
domContentLoaded1595352512
load8901665107217886
domInteractive1595352512
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 381 Bytes (0.01%)
  • common: 0 Bytes (0.00%)

@vinistevam vinistevam merged commit ca0f122 into develop Feb 19, 2024
65 checks passed
@vinistevam vinistevam deleted the fix/handle-small-number-save-advance-gas-fees branch February 19, 2024 09:50
@github-actions github-actions bot locked and limited conversation to collaborators Feb 19, 2024
@metamaskbot metamaskbot added the release-11.12.0 Issue or pull request that will be included in release 11.12.0 label Feb 19, 2024
@gauthierpetetin gauthierpetetin added the Sev1-high High severity; partial loss of service with severe impact upon users, with no workaround. label Mar 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-11.12.0 Issue or pull request that will be included in release 11.12.0 Sev1-high High severity; partial loss of service with severe impact upon users, with no workaround. team-confirmations-system DEPRECATED: please use "team-confirmations" label instead
Projects
None yet
9 participants