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 gas calculation checking wrong account balance #21174

Merged
merged 3 commits into from
Oct 13, 2023

Conversation

bergeron
Copy link
Contributor

@bergeron bergeron commented Oct 3, 2023

Description

Dapps can connect to / prompt transactions from a different account than the one currently selected within the wallet itself.

When this occurs, useGasFeeErrors was doing a balance check against the wrong account. It was using the current account within the wallet, instead of the account issuing the transaction. This can cause balance errors even when the account has sufficient funds.

This shows in 2 places in the UI, which are the only places checking balanceError:

  • Token approvals
  • Customizing gas popup

Solution: Check balance of the account issuing the transaction.

Manual testing steps

  1. Within the wallet, select an empty account.
  2. On the E2E Test Dapp, connect to a funded account.
  3. Send ETH
  4. Click "🌐 Site suggested" to customize gas
  5. A balance error used to appear, but no longer should.

Screenshots/Recordings

Before

gas-check-wrong-account.mov

Related issues

Fixes: #20770

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've clearly explained:
    • What problem this PR is solving.
    • How this problem was solved.
    • How reviewers can test my changes.
  • I’ve indicated what issue this PR is linked to: Fixes #???
  • I’ve included tests if applicable.
  • I’ve documented any added code.
  • I’ve applied the right labels on the PR (see labeling guidelines).
  • 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.

@bergeron bergeron requested a review from a team as a code owner October 3, 2023 17:56
@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2023

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.

@@ -122,7 +121,7 @@ export const generateUseSelectorRouter =
},
};
}
if (selector === getSelectedAccount) {
if (selector.toString().includes('getTargetAccount')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hack because the selector is actually a lambda, to allow getTargetAccount to accept a second parameter. Therefore it can't use function equality like the others.

A better approach to this kind of mocking, would keep the selectors from the product code and mock the state instead. Better to mock the lowest level thing, so that the product selectors are tested too if they change.

Comment on lines +277 to +281
// Balance check is only relevant for outgoing + pending transactions
const balanceError =
account !== undefined &&
transaction?.type !== TransactionType.incoming &&
transaction?.status in PENDING_STATUS_HASH
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found that this balance check was being called in cases it should not. Like for incoming transactions, and transactions that have already confirmed. Because we won't have the from account in our state for incoming transactions, this guard is needed.

A better fix may be to further split these gas operations, so that callers can request only the necessary gas information for their contexts.

@bergeron bergeron added the team-confirmations-secure-ux-PR PRs from the confirmations team label Oct 3, 2023
@metamaskbot
Copy link
Collaborator

Builds ready [25a9129]
Page Load Metrics (1118 ± 355 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint91145112178
domContentLoaded78138106188
load9019681118740355
domInteractive78138106188
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 486 Bytes (0.01%)
  • common: 0 Bytes (0.00%)

brad-decker
brad-decker previously approved these changes Oct 4, 2023
salimtb
salimtb previously approved these changes Oct 11, 2023
sahar-fehri
sahar-fehri previously approved these changes Oct 12, 2023
@bergeron bergeron dismissed stale reviews from sahar-fehri, salimtb, and brad-decker via 506f20a October 12, 2023 14:09
@metamaskbot
Copy link
Collaborator

Builds ready [506f20a]
Page Load Metrics (640 ± 356 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint84150101168
domContentLoaded67155902210
load781869640742356
domInteractive67155902210
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 486 Bytes (0.01%)
  • common: 0 Bytes (0.00%)

@codecov
Copy link

codecov bot commented Oct 12, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (d6d324b) 68.61% compared to head (506f20a) 68.61%.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop   #21174   +/-   ##
========================================
  Coverage    68.61%   68.61%           
========================================
  Files         1020     1020           
  Lines        40929    40934    +5     
  Branches     10920    10925    +5     
========================================
+ Hits         28080    28085    +5     
  Misses       12849    12849           
Files Coverage Δ
ui/hooks/gasFeeInput/test-utils.js 76.60% <100.00%> (ø)
ui/hooks/gasFeeInput/useGasFeeErrors.js 95.10% <100.00%> (+0.25%) ⬆️

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

@bergeron bergeron requested review from brad-decker and a team October 12, 2023 16:01
@bergeron bergeron merged commit 0bec1df into develop Oct 13, 2023
10 checks passed
@bergeron bergeron deleted the brian/gas-check-wrong-account branch October 13, 2023 16:07
@github-actions github-actions bot locked and limited conversation to collaborators Oct 13, 2023
@metamaskbot metamaskbot added the release-11.5.0 Issue or pull request that will be included in release 11.5.0 label Oct 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-11.5.0 Issue or pull request that will be included in release 11.5.0 team-confirmations-secure-ux-PR PRs from the confirmations team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Incorrect gas balance validation at ERC20 approval
5 participants