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

accountsChanged fired two times #2162

Closed
ochikov opened this issue Jan 25, 2021 · 16 comments
Closed

accountsChanged fired two times #2162

ochikov opened this issue Jan 25, 2021 · 16 comments
Labels
community Issues or PRs opened by the MM community Sev2-normal An issue that may lead to users misunderstanding some limited risks they are taking stale Issues that have not had activity in the last 90 days team-mobile-platform type-bug Something isn't working

Comments

@ochikov
Copy link

ochikov commented Jan 25, 2021

accountsChanged fired two times when the account is changed on Mobile App when the page is opened from the integrated browser and account is changed.

  • Device: Iphone 7 Plus
  • OS: 13.5.1
  • App Version: 1.0.9
@ochikov ochikov added the type-bug Something isn't working label Jan 25, 2021
@ochikov ochikov changed the title chainChanged does not return chainId accountsChanged fired two times Jan 25, 2021
@omnat
Copy link
Contributor

omnat commented Jan 25, 2021

Hi @ochikov To understand impact of this on users, could you elaborate more on what problems this causes for you?

@ochikov
Copy link
Author

ochikov commented Jan 26, 2021

Hi @ochikov To understand impact of this on users, could you elaborate more on what problems this causes for you?

Two events are fired. First the event with the new account address and then once again the event with the old address.

@omnat
Copy link
Contributor

omnat commented Jan 27, 2021

@ochikov And how is it impacting your experience?

@ochikov
Copy link
Author

ochikov commented Jan 27, 2021

@ochikov And how is it impacting your experience?

I am listening for the event and after the account is changed, I am reloading the page, and make some other App-specific tasks. Dispatching two events one after another with wrong data is affecting my experience as I cannot load the right data for the user.

@Upperfoot
Copy link

Same exact issue as described by the other users.

This also happens on Android (Samsung S20+), the event fires twice with different data in both calls.

First Event - Correct Account Address
Second Event - Incorrect Account Address

Now, this doesn't happen at all on the desktop version and is a specific issue to Metamask Mobile.

@deepseafishing
Copy link

deepseafishing commented Jun 22, 2021

Having the same issue here. For me, it doesn't even give me the 2 different events. They're always returning the account that was originally logged in. They're not returning the newly switched account in the accountChanged callback. Is this still an issue for you too? How did you fix it? @ochikov

@deepseafishing
Copy link

[MetaMask DEBUG]: Analytics 'trackEvent' - {category: "Accounts", action: "Account Modal", name: "Switched Accounts"} {}proto: Object undefined undefined

@dev-johnny-gh
Copy link

same issue here on android metamask app

@andrepimenta andrepimenta added Sev2-normal An issue that may lead to users misunderstanding some limited risks they are taking and removed close issue candidate labels Aug 17, 2021
@gantunesr gantunesr added the community Issues or PRs opened by the MM community label Sep 14, 2021
@maximpn
Copy link

maximpn commented Oct 13, 2021

Same for me. accountsChanged is fired two times after accounts switching. First time with a proper account and the second time with an initial account (an account which had been selected upon page loading). When switching back accountsChanged isn't fired at al.

The only solution now is full page reload either manual by users or automatic. Anyway it doesn't give the best user experience.

Metamask browser's extension behaves as expected. Fires accountsChanged only once with the correct value after accounts switching.

@Markkop
Copy link

Markkop commented Jan 25, 2022

Same bug happening for me.
The best workaround I found was setting a cooldown variable to avoid triggering the event's method again.
However, when the user changes the account back to the one he loaded the page, the accountsChanged event is not triggered.

let onAccountsChangedCooldown = false

function onAccountsChanged (accounts) {
  if (onAccountsChangedCooldown) return
  onAccountsChangedCooldown = true
  setTimeout(() => { onAccountsChangedCooldown = false }, 1000)
  setAccount(accounts[0])
}

connection.on('accountsChanged', onAccountsChanged)

I tried using throttle functions, but they only delayed the second event firing.

@Markkop
Copy link

Markkop commented Jan 25, 2022

I was not able to try the build locally yet, but looking at the code perhaps this forEach is the culprit?

const notifyAllConnections = useCallback(
(payload, restricted = true) => {
const fullHostname = new URL(url.current).hostname;
// TODO:permissions move permissioning logic elsewhere
backgroundBridges.current.forEach((bridge) => {
if (
bridge.hostname === fullHostname &&
(!props.privacyMode || !restricted || approvedHosts[bridge.hostname])
) {
bridge.sendNotification(payload);
}
});
},
[props.privacyMode]
);

notifyAllConnections is being called here when changing accounts

if (numApprovedHosts > 0) {
notifyAllConnections({
method: NOTIFICATION_NAMES.accountsChanged,
params: [selectedAddress],
});
}

@wyaricky
Copy link

wyaricky commented Apr 22, 2022

I've been experiencing similar behavior but it triggers at least 8 times.

I listen for accountsChanged and have event put my code block in a conditional to check whether the new account matches the account stored in a state variable. Still results in 8+ triggers.

I am also not using any loops in my file

@thelastinuit
Copy link

For me, accountsChanged is being triggered every page I switch to.

Any solution or hacks so far? 😔

@thelastinuit
Copy link

I've been experiencing similar behavior but it triggers at least 8 times.

I listen for accountsChanged and have event put my code block in a conditional to check whether the new account matches the account stored in a state variable. Still results in 8+ triggers.

I am also not using any loops in my file

@wyaricky
did you manage to solve it / hack it?

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity in the last 90 days. It will be closed in 7 days. Thank you for your contributions.

@github-actions github-actions bot added the stale Issues that have not had activity in the last 90 days label Jul 17, 2023
@github-actions
Copy link
Contributor

This issue was closed because it has been stalled for 7 days with no activity. If you feel this was closed in error please reopen and provide evidence on the current production app. Thank you for your contributions.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Issues or PRs opened by the MM community Sev2-normal An issue that may lead to users misunderstanding some limited risks they are taking stale Issues that have not had activity in the last 90 days team-mobile-platform type-bug Something isn't working
Projects
None yet
Development

No branches or pull requests

15 participants