Skip to content

Commit

Permalink
fix: #4118
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Jul 17, 2024
1 parent 1e797ad commit 8d81df5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-pots-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/connectors": patch
---

Fixed `metaMask` connector switch chain issue.
2 changes: 1 addition & 1 deletion packages/connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"dependencies": {
"@coinbase/wallet-sdk": "4.0.4",
"@metamask/sdk": "0.26.4",
"@metamask/sdk": "0.26.5",
"@safe-global/safe-apps-provider": "0.18.3",
"@safe-global/safe-apps-sdk": "9.1.0",
"@walletconnect/ethereum-provider": "2.13.0",
Expand Down
19 changes: 15 additions & 4 deletions packages/connectors/src/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,21 @@ export function metaMask(parameters: MetaMaskParameters = {}) {

try {
await Promise.all([
provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: numberToHex(chainId) }],
}),
provider
.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: numberToHex(chainId) }],
})
// 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 })
}),
new Promise<void>((resolve) =>
config.emitter.once('change', ({ chainId: currentChainId }) => {
if (currentChainId === chainId) resolve()
Expand Down
62 changes: 40 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d81df5

Please sign in to comment.