Skip to content

Commit 485dcc6

Browse files
fix: Add chain fallback for wallet switching (#5668)
1 parent a41e423 commit 485dcc6

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

.changeset/tasty-emus-promise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix add chain not triggering for certain wallets

packages/thirdweb/src/wallets/injected/index.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -348,25 +348,20 @@ async function switchChain(provider: EIP1193Provider, chain: Chain) {
348348
method: "wallet_switchEthereumChain",
349349
params: [{ chainId: hexChainId }],
350350
});
351-
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
352-
} catch (e: any) {
351+
} catch {
353352
// if chain does not exist, add the chain
354-
if (e?.code === 4902 || e?.data?.originalError?.code === 4902) {
355-
const apiChain = await getChainMetadata(chain);
356-
await provider.request({
357-
method: "wallet_addEthereumChain",
358-
params: [
359-
{
360-
chainId: hexChainId,
361-
chainName: apiChain.name,
362-
nativeCurrency: apiChain.nativeCurrency,
363-
rpcUrls: getValidPublicRPCUrl(apiChain), // no client id on purpose here
364-
blockExplorerUrls: apiChain.explorers?.map((x) => x.url),
365-
},
366-
],
367-
});
368-
} else {
369-
throw e;
370-
}
353+
const apiChain = await getChainMetadata(chain);
354+
await provider.request({
355+
method: "wallet_addEthereumChain",
356+
params: [
357+
{
358+
chainId: hexChainId,
359+
chainName: apiChain.name,
360+
nativeCurrency: apiChain.nativeCurrency,
361+
rpcUrls: getValidPublicRPCUrl(apiChain), // no client id on purpose here
362+
blockExplorerUrls: apiChain.explorers?.map((x) => x.url),
363+
},
364+
],
365+
});
371366
}
372367
}

0 commit comments

Comments
 (0)