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: selector getKnownMethodData should return empty object if user has opted out for using 4Byte Resolution #27203

Merged
merged 6 commits into from
Sep 17, 2024

Conversation

jpuri
Copy link
Contributor

@jpuri jpuri commented Sep 17, 2024

Description

If user has toggled off Decode smart contracts setting he is not able to approve ERC20. This is regression introduced recently.

Related issues

Fixes: #27188

Manual testing steps

  1. Toggle off setting "Decode smart contracts"
  2. Try to approve an ERC 20
  3. It should not throw error

Screenshots/Recordings

Screen.Recording.2024-09-17.at.2.20.17.PM.mov

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.

@jpuri jpuri added the team-confirmations Push issues to confirmations team label Sep 17, 2024
@jpuri jpuri requested a review from a team as a code owner September 17, 2024 08:52
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.

@jpuri jpuri changed the title [bug]: selector getKnownMethodData should return empty object if user has opted out for using 4Byte Resolution fix: selector getKnownMethodData should return empty object if user has opted out for using 4Byte Resolution Sep 17, 2024
cryptotavares
cryptotavares previously approved these changes Sep 17, 2024
@jpuri jpuri requested a review from a team as a code owner September 17, 2024 09:51
matthewwalsh0
matthewwalsh0 previously approved these changes Sep 17, 2024
@@ -34,7 +34,7 @@ describe('useFourByte', () => {
expect(result.current.params).toEqual([]);
});

it('returns undefined if resolution is turned off', () => {
it('returns rmpty object if resolution is turned off', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Minor, typo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -1262,7 +1262,7 @@ export function getKnownMethodData(state, data) {
const fourBytePrefix = prefixedData.slice(0, 10);
const { knownMethodData, use4ByteResolution } = state.metamask;
// If 4byte setting is off, we do not want to return the knownMethodData
return use4ByteResolution ? knownMethodData?.[fourBytePrefix] : undefined;
return use4ByteResolution ? knownMethodData?.[fourBytePrefix] : {};
Copy link
Member

Choose a reason for hiding this comment

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

I appreciate this is the quicker fix for resolution, but for audit sake it's less explicit since the caller has to check the individual properties to determine if there is actual method data present.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. It would be nice to create a follow-up refactor to handle when no methodData is found with null or undefined instead of {}

Copy link
Contributor

Choose a reason for hiding this comment

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

One thing to have in mind though is that currently you can have an empty object in the state. In the state file shared by CS in the bug thread, you can see entries in the KnownMethodData like this:

"0xf305d719": {},

This means that even if use4ByteResolution
is true, and there's an entry for the fourBytePrefix we might still get an empty object, and the caller still will have to check individual properties to determine if there's actual data present.

The only thing that I would flag in the above fix, is that it is still possible to get undefined if there's no entry for the fourBytePrefix.

Copy link
Contributor Author

@jpuri jpuri Sep 17, 2024

Choose a reason for hiding this comment

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

Good catch @cryptotavares , I changed it to

return use4ByteResolution ? knownMethodData?.[fourBytePrefix] ?? {} : {};

@metamaskbot
Copy link
Collaborator

Builds ready [b47f50b]
Page Load Metrics (1713 ± 126 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint33626881587468225
domContentLoaded144026311695251121
load144826851713262126
domInteractive21100442311
Bundle size diffs [🚀 Bundle size reduced!]
  • background: 0 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: -7 Bytes (-0.00%)

Copy link

codecov bot commented Sep 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.01%. Comparing base (33f430a) to head (4450a71).
Report is 5 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop   #27203   +/-   ##
========================================
  Coverage    70.00%   70.01%           
========================================
  Files         1445     1445           
  Lines        50195    50194    -1     
  Branches     14041    14041           
========================================
  Hits         35139    35139           
+ Misses       15056    15055    -1     

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

@@ -1262,7 +1262,7 @@ export function getKnownMethodData(state, data) {
const fourBytePrefix = prefixedData.slice(0, 10);
const { knownMethodData, use4ByteResolution } = state.metamask;
// If 4byte setting is off, we do not want to return the knownMethodData
return use4ByteResolution ? knownMethodData?.[fourBytePrefix] : undefined;
return use4ByteResolution ? knownMethodData?.[fourBytePrefix] : {};
Copy link
Contributor

Choose a reason for hiding this comment

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

for consistency, at the start of the method we could now return {} instead of null as well.

  if (!data) {
    return null;
  }
const { name } = null; 
// VM334:1 Uncaught TypeError: Cannot destructure property 'name' of 'null' as it is null.
    at <anonymous>:1:9

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@digiwand since this is hotfix, I will prefer to keep scope minimum. More detailed changes can go in a followup PR.

Copy link
Contributor

@digiwand digiwand left a comment

Choose a reason for hiding this comment

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

Requesting to update the !data return value too in case this may also cause similar destructing issues

https://github.com/MetaMask/metamask-extension/pull/27203/files#diff-45ae84764c1d49a07813a91adc3351d6ed96bf9ee8fa2ae4c9a116d1e0cba06cR1259

digiwand
digiwand previously approved these changes Sep 17, 2024
digiwand
digiwand previously approved these changes Sep 17, 2024
Copy link

@jpuri jpuri requested a review from digiwand September 17, 2024 12:03
@metamaskbot
Copy link
Collaborator

Builds ready [4450a71]
Page Load Metrics (1746 ± 110 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint152823421747228110
domContentLoaded148223211722224108
load149123451746229110
domInteractive166941157
Bundle size diffs [🚀 Bundle size reduced!]
  • background: 0 Bytes (0.00%)
  • ui: 14 Bytes (0.00%)
  • common: -1 Bytes (-0.00%)

digiwand

This comment was marked as outdated.

@jpuri jpuri merged commit 270d2ad into develop Sep 17, 2024
78 checks passed
@jpuri jpuri deleted the approve_token_fix branch September 17, 2024 12:30
@github-actions github-actions bot locked and limited conversation to collaborators Sep 17, 2024
@metamaskbot metamaskbot added the release-12.6.0 Issue or pull request that will be included in release 12.6.0 label Sep 17, 2024
@metamaskbot metamaskbot added release-12.5.0 Issue or pull request that will be included in release 12.5.0 and removed release-12.6.0 Issue or pull request that will be included in release 12.6.0 labels Sep 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-12.5.0 Issue or pull request that will be included in release 12.5.0 team-confirmations Push issues to confirmations team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: user is unable to approve tokens for trading on decentralised exchange
6 participants