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 the switchChain error encountered when using the "metaMask" connector #4227

Merged
merged 3 commits into from
Sep 16, 2024

Conversation

xianchenxc
Copy link
Contributor

fix #4223

Copy link

changeset-bot bot commented Aug 27, 2024

🦋 Changeset detected

Latest commit: 8a0e3b1

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

This PR includes changesets to release 3 packages
Name Type
@wagmi/connectors Patch
wagmi Patch
@wagmi/vue 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 Aug 27, 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 Sep 16, 2024 5:18pm

provider.request({
method: 'wallet_addEthereumChain',
params: [addEthereumChain],
}),
Copy link

@DNR500 DNR500 Sep 3, 2024

Choose a reason for hiding this comment

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

Should this also have the additional then block to fire the change event if its needed?

// During `'wallet_switchEthereumChain'`, MetaMask makes a `'net_version'` RPC call to the target chain.
// If this request fails, MetaMask does not emit the `'chainChanged'` event, but will still switch the chain.
// To counter this behavior, we request and emit the current chain ID to confirm the chain switch either via
// this callback or an externally emitted `'chainChanged'` event.
// https://github.com/MetaMask/metamask-extension/issues/24247
.then(async () => {
const currentChainId = await this.getChainId()
if (currentChainId === chainId)
config.emitter.emit('change', { chainId })
}),

@tmm tmm force-pushed the main branch 2 times, most recently from 026feab to 9b7cc38 Compare September 11, 2024 21:57
@tmm
Copy link
Member

tmm commented Sep 16, 2024

Calling 'eth_chainId' directly solves it. It seems provider.getChainId does not always update this.state.chainId immediately.

@tmm tmm merged commit d07ad7f into wevm:main Sep 16, 2024
18 of 23 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.

switchChainAsync throw an error when user approved
3 participants