Skip to content

Commit

Permalink
fix: Add chain fallback for wallet switching (#5668)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges authored Dec 9, 2024
1 parent a41e423 commit 485dcc6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-emus-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix add chain not triggering for certain wallets
33 changes: 14 additions & 19 deletions packages/thirdweb/src/wallets/injected/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
],
});
}
}

0 comments on commit 485dcc6

Please sign in to comment.