From 8d81df5cc884d0a210dedd3c1ea0e2e9e52b83c5 Mon Sep 17 00:00:00 2001 From: Tom Meagher Date: Wed, 17 Jul 2024 16:08:21 -0400 Subject: [PATCH] fix: #4118 --- .changeset/serious-pots-rescue.md | 5 +++ packages/connectors/package.json | 2 +- packages/connectors/src/metaMask.ts | 19 +++++++-- pnpm-lock.yaml | 62 +++++++++++++++++++---------- 4 files changed, 61 insertions(+), 27 deletions(-) create mode 100644 .changeset/serious-pots-rescue.md diff --git a/.changeset/serious-pots-rescue.md b/.changeset/serious-pots-rescue.md new file mode 100644 index 0000000000..48c4230a2d --- /dev/null +++ b/.changeset/serious-pots-rescue.md @@ -0,0 +1,5 @@ +--- +"@wagmi/connectors": patch +--- + +Fixed `metaMask` connector switch chain issue. diff --git a/packages/connectors/package.json b/packages/connectors/package.json index 8a7e70b943..83262a226c 100644 --- a/packages/connectors/package.json +++ b/packages/connectors/package.json @@ -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", diff --git a/packages/connectors/src/metaMask.ts b/packages/connectors/src/metaMask.ts index bce6efb9d0..a54e9b8c45 100644 --- a/packages/connectors/src/metaMask.ts +++ b/packages/connectors/src/metaMask.ts @@ -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((resolve) => config.emitter.once('change', ({ chainId: currentChainId }) => { if (currentChainId === chainId) resolve() diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a32fae9377..5ba52580fd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -157,8 +157,8 @@ importers: specifier: 4.0.4 version: 4.0.4 '@metamask/sdk': - specifier: 0.26.4 - version: 0.26.4(bufferutil@4.0.8)(encoding@0.1.13)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(rollup@4.17.2)(utf-8-validate@5.0.10) + specifier: 0.26.5 + version: 0.26.5(bufferutil@4.0.8)(encoding@0.1.13)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(rollup@4.17.2)(utf-8-validate@5.0.10) '@safe-global/safe-apps-provider': specifier: 0.18.3 version: 0.18.3(bufferutil@4.0.8)(encoding@0.1.13)(typescript@5.5.2)(utf-8-validate@5.0.10)(zod@3.22.4) @@ -1531,8 +1531,12 @@ packages: resolution: {integrity: sha512-dwZPq8wx9yV3IX2caLi9q9xZBw2XeIoYqdyihDDDpuHVCEiqadJLwqM3zy+uwf6F1QYQ65A8aOMQg1Uw7LMLNg==} engines: {node: '>=16.0.0'} - '@metamask/json-rpc-middleware-stream@6.0.2': - resolution: {integrity: sha512-jtyx3PRfc1kqoLpYveIVQNwsxYKefc64/LCl9h9Da1m3nUKEvypbYuXSIwi237qvOjKmNHQKsDOZg6f4uBf62Q==} + '@metamask/json-rpc-engine@8.0.2': + resolution: {integrity: sha512-IoQPmql8q7ABLruW7i4EYVHWUbF74yrp63bRuXV5Zf9BQwcn5H9Ww1eLtROYvI1bUXwOiHZ6qT5CWTrDc/t/AA==} + engines: {node: '>=16.0.0'} + + '@metamask/json-rpc-middleware-stream@7.0.2': + resolution: {integrity: sha512-yUdzsJK04Ev98Ck4D7lmRNQ8FPioXYhEUZOMS01LXW8qTvPGiRVXmVltj2p4wrLkh0vW7u6nv0mNl5xzC5Qmfg==} engines: {node: '>=16.0.0'} '@metamask/object-multiplex@2.0.0': @@ -1542,8 +1546,8 @@ packages: '@metamask/onboarding@1.0.1': resolution: {integrity: sha512-FqHhAsCI+Vacx2qa5mAFcWNSrTcVGMNjzxVgaX8ECSny/BJ9/vgXP9V7WF/8vb9DltPeQkxr+Fnfmm6GHfmdTQ==} - '@metamask/providers@15.0.0': - resolution: {integrity: sha512-FXvL1NQNl6I7fMOJTfQYcBlBZ33vSlm6w80cMpmn8sJh0Lb7wcBpe02UwBsNlARnI+Qsr26XeDs6WHUHQh8CuA==} + '@metamask/providers@16.1.0': + resolution: {integrity: sha512-znVCvux30+3SaUwcUGaSf+pUckzT5ukPRpcBmy+muBLC0yaWnBcvDqGfcsw6CBIenUdFrVoAFa8B6jsuCY/a+g==} engines: {node: ^18.18 || >=20} '@metamask/rpc-errors@6.2.1': @@ -1557,6 +1561,10 @@ packages: resolution: {integrity: sha512-j6Z47VOmVyGMlnKXZmL0fyvWfEYtKWCA9yGZkU3FCsGZUT5lHGmvaV9JA5F2Y+010y7+ROtR3WMXIkvl/nVzqQ==} engines: {node: '>=12.0.0'} + '@metamask/safe-event-emitter@3.1.1': + resolution: {integrity: sha512-ihb3B0T/wJm1eUuArYP4lCTSEoZsClHhuWyfo/kMX3m/odpqNcPfsz5O2A3NT7dXCAgWPGDQGPqygCpgeniKMw==} + engines: {node: '>=12.0.0'} + '@metamask/sdk-communication-layer@0.26.4': resolution: {integrity: sha512-+X4GEc5mV1gWK4moSswVlKsUh+RsA48qPlkxBLTUxQODSnyBe0TRMxE6mH+bSrfponnTzvBkGUXyEjvDwDjDHw==} peerDependencies: @@ -1566,10 +1574,10 @@ packages: readable-stream: ^3.6.2 socket.io-client: ^4.5.1 - '@metamask/sdk-install-modal-web@0.26.4': - resolution: {integrity: sha512-7Cx7ZsaExbMwghlRrUWWI0Ksg0m7K60LtMjfuDpjvjWqoZa9MoPxitGDEXNbLaqvKn39ebPvNcPzQ6czA4ilTw==} + '@metamask/sdk-install-modal-web@0.26.5': + resolution: {integrity: sha512-qVA9Nk+NorGx5hXyODy5wskptE8R7RNYTYt49VbQpJogqbbVe1dnJ98+KaA43PBN4XYMCXmcIhULNiEHGsLynA==} peerDependencies: - i18next: 23.2.3 + i18next: 23.11.5 react: ^18.2.0 react-dom: ^18.2.0 react-native: '*' @@ -1581,8 +1589,8 @@ packages: react-native: optional: true - '@metamask/sdk@0.26.4': - resolution: {integrity: sha512-9Yh41KJkD9RhW0lRijnQzPV0ptblLorLdTsf5GnAl3yE72QIfaPBtsDxzLtX+0QLppiFfj7o8vRBYvBApG9k+Q==} + '@metamask/sdk@0.26.5': + resolution: {integrity: sha512-HS/MPQCCYRS+m3dDdGLcAagwYHiPv9iUshDMBjINUywCtfUN4P2BH8xdvPOgtnzRIuRSMXqMWBbZnTvEvBeQvA==} peerDependencies: react: ^18.2.0 react-dom: ^18.2.0 @@ -9451,10 +9459,18 @@ snapshots: transitivePeerDependencies: - supports-color - '@metamask/json-rpc-middleware-stream@6.0.2': + '@metamask/json-rpc-engine@8.0.2': dependencies: - '@metamask/json-rpc-engine': 7.3.3 - '@metamask/safe-event-emitter': 3.0.0 + '@metamask/rpc-errors': 6.2.1 + '@metamask/safe-event-emitter': 3.1.1 + '@metamask/utils': 8.4.0 + transitivePeerDependencies: + - supports-color + + '@metamask/json-rpc-middleware-stream@7.0.2': + dependencies: + '@metamask/json-rpc-engine': 8.0.2 + '@metamask/safe-event-emitter': 3.1.1 '@metamask/utils': 8.4.0 readable-stream: 3.6.2 transitivePeerDependencies: @@ -9469,13 +9485,13 @@ snapshots: dependencies: bowser: 2.11.0 - '@metamask/providers@15.0.0': + '@metamask/providers@16.1.0': dependencies: - '@metamask/json-rpc-engine': 7.3.3 - '@metamask/json-rpc-middleware-stream': 6.0.2 + '@metamask/json-rpc-engine': 8.0.2 + '@metamask/json-rpc-middleware-stream': 7.0.2 '@metamask/object-multiplex': 2.0.0 '@metamask/rpc-errors': 6.2.1 - '@metamask/safe-event-emitter': 3.0.0 + '@metamask/safe-event-emitter': 3.1.1 '@metamask/utils': 8.4.0 detect-browser: 5.3.0 extension-port-stream: 3.0.0 @@ -9497,6 +9513,8 @@ snapshots: '@metamask/safe-event-emitter@3.0.0': {} + '@metamask/safe-event-emitter@3.1.1': {} + '@metamask/sdk-communication-layer@0.26.4(cross-fetch@4.0.0(encoding@0.1.13))(eciesjs@0.3.18)(eventemitter2@6.4.9)(readable-stream@3.6.2)(socket.io-client@4.7.5(bufferutil@4.0.8)(utf-8-validate@5.0.10))': dependencies: bufferutil: 4.0.8 @@ -9512,7 +9530,7 @@ snapshots: transitivePeerDependencies: - supports-color - '@metamask/sdk-install-modal-web@0.26.4(i18next@23.11.5)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': + '@metamask/sdk-install-modal-web@0.26.5(i18next@23.11.5)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': dependencies: i18next: 23.11.5 qr-code-styling: 1.6.0-rc.1 @@ -9520,12 +9538,12 @@ snapshots: react: 18.3.1 react-dom: 18.3.1(react@18.3.1) - '@metamask/sdk@0.26.4(bufferutil@4.0.8)(encoding@0.1.13)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(rollup@4.17.2)(utf-8-validate@5.0.10)': + '@metamask/sdk@0.26.5(bufferutil@4.0.8)(encoding@0.1.13)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(rollup@4.17.2)(utf-8-validate@5.0.10)': dependencies: '@metamask/onboarding': 1.0.1 - '@metamask/providers': 15.0.0 + '@metamask/providers': 16.1.0 '@metamask/sdk-communication-layer': 0.26.4(cross-fetch@4.0.0(encoding@0.1.13))(eciesjs@0.3.18)(eventemitter2@6.4.9)(readable-stream@3.6.2)(socket.io-client@4.7.5(bufferutil@4.0.8)(utf-8-validate@5.0.10)) - '@metamask/sdk-install-modal-web': 0.26.4(i18next@23.11.5)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@metamask/sdk-install-modal-web': 0.26.5(i18next@23.11.5)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@types/dom-screen-wake-lock': 1.0.3 bowser: 2.11.0 cross-fetch: 4.0.0(encoding@0.1.13)