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: metamask chain switch with missing net_version #3859

Merged
merged 4 commits into from Apr 26, 2024

Conversation

holic
Copy link
Sponsor Contributor

@holic holic commented Apr 25, 2024

Description

This is a workaround for the issue described in MetaMask/metamask-extension#24247.

I've left a big inline comment explaining everything.

Copy link

changeset-bot bot commented Apr 25, 2024

🦋 Changeset detected

Latest commit: 2045037

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@wagmi/core Patch
@wagmi/connectors Patch
wagmi Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Apr 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wagmi ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 26, 2024 2:55pm

config.emitter.once('change', ({ chainId: currentChainId }) => {
if (currentChainId === chainId) resolve()
}),
),
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I switched this to use the internal emitter rather than binding to the provider's emitter so that we can emit our own changed event in the workaround case. This also matches the MetaMask connector:

new Promise<void>((resolve) =>
config.emitter.once('change', ({ chainId: currentChainId }) => {
if (currentChainId === chainId) resolve()
}),
),

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I believe that either approach can leave this listener open indefinitely if we never get a chain switch event. Should we time out after some time and remove the listener?

@holic holic force-pushed the holic/metamask-chain-switch-workaround branch 2 times, most recently from 0cd6af9 to 230a2b4 Compare April 25, 2024 22:39
const currentChainId = await this.getChainId()
if (currentChainId === chainId)
config.emitter.emit('change', { chainId })
}),
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

If this doesn't have the target chain ID right away, we could wait and try again, maybe a couple times?

@tmm tmm merged commit 400c960 into wevm:main Apr 26, 2024
15 of 19 checks passed
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.

None yet

2 participants