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 accountsChanged callback loop #248

Merged
merged 3 commits into from
Mar 29, 2023
Merged

fix accountsChanged callback loop #248

merged 3 commits into from
Mar 29, 2023

Conversation

Louis-kupo
Copy link
Contributor

Reason

Change the accountsChanged callback object to a new object , or will cause loop.This is because the callback function of accountsChanged returns the reference of the accounts, so request eth_accounts after externally modifying the reference of the account will cause accountsChanged to be triggered again, then it keeps looping

Reproducibility

If you change the result returned by accountsChanged and request eth_accounts again, you will enter a loop .

  window.ethereum.on("accountsChanged", async (newAccounts) => {
    newAccounts[0] = "1234...";
    const nextAccounts = await window.ethereum.request({
      method: "eth_accounts"
    });
    console.log("nextAccounts", nextAccounts);
  });

Solution

In the callback of accountsChanged, return the deep copy object

Change the accountsChanged callback object to a new object , or will cause loop
@Louis-kupo Louis-kupo requested a review from a team as a code owner March 28, 2023 12:36
@Gudahtt
Copy link
Member

Gudahtt commented Mar 28, 2023

Thanks, great find! This seems like the right solution to me.

Could you fix the lint error? yarn lint:fix should fix the lint error being highlighted by CI.

@Louis-kupo
Copy link
Contributor Author

Thanks, great find! This seems like the right solution to me.

Could you fix the lint error? yarn lint:fix should fix the lint error being highlighted by CI.

ok

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

I pushed one additional commit to adjust the code coverage requirements to get CI to pass. While it would be ideal to have a unit test for this error, the file being changed is not yet tested.

@Gudahtt Gudahtt merged commit 9415d67 into MetaMask:main Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants