From 485dcc6020089a80d994c24882f389c24a0af039 Mon Sep 17 00:00:00 2001 From: Joaquim Verges Date: Tue, 10 Dec 2024 11:02:35 +1300 Subject: [PATCH] fix: Add chain fallback for wallet switching (#5668) --- .changeset/tasty-emus-promise.md | 5 +++ .../thirdweb/src/wallets/injected/index.ts | 33 ++++++++----------- 2 files changed, 19 insertions(+), 19 deletions(-) create mode 100644 .changeset/tasty-emus-promise.md diff --git a/.changeset/tasty-emus-promise.md b/.changeset/tasty-emus-promise.md new file mode 100644 index 00000000000..8c8ddbcf760 --- /dev/null +++ b/.changeset/tasty-emus-promise.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Fix add chain not triggering for certain wallets diff --git a/packages/thirdweb/src/wallets/injected/index.ts b/packages/thirdweb/src/wallets/injected/index.ts index a803af02467..9f343d074bc 100644 --- a/packages/thirdweb/src/wallets/injected/index.ts +++ b/packages/thirdweb/src/wallets/injected/index.ts @@ -348,25 +348,20 @@ async function switchChain(provider: EIP1193Provider, chain: Chain) { method: "wallet_switchEthereumChain", params: [{ chainId: hexChainId }], }); - // biome-ignore lint/suspicious/noExplicitAny: TODO: fix any - } catch (e: any) { + } catch { // if chain does not exist, add the chain - if (e?.code === 4902 || e?.data?.originalError?.code === 4902) { - const apiChain = await getChainMetadata(chain); - await provider.request({ - method: "wallet_addEthereumChain", - params: [ - { - chainId: hexChainId, - chainName: apiChain.name, - nativeCurrency: apiChain.nativeCurrency, - rpcUrls: getValidPublicRPCUrl(apiChain), // no client id on purpose here - blockExplorerUrls: apiChain.explorers?.map((x) => x.url), - }, - ], - }); - } else { - throw e; - } + const apiChain = await getChainMetadata(chain); + await provider.request({ + method: "wallet_addEthereumChain", + params: [ + { + chainId: hexChainId, + chainName: apiChain.name, + nativeCurrency: apiChain.nativeCurrency, + rpcUrls: getValidPublicRPCUrl(apiChain), // no client id on purpose here + blockExplorerUrls: apiChain.explorers?.map((x) => x.url), + }, + ], + }); } }