async switchChain(chainId: number) {
try {
const chain = this.chains.find((x) => x.id === chainId);
if (!chain) throw new SwitchChainError(new Error("chain not found on connector."));
await this.web3AuthInstance.addChain({
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: `0x${chain.id.toString(16)}`,
rpcTarget: chain.rpcUrls.default.http[0],
displayName: chain.name,
// This returns the first character of the string URL since type change
// chain.blockExplorers?.default is not an array. it is a string
blockExplorer: chain.blockExplorers?.default.url[0] || "",
// blockExplorer: chain.blockExplorers?.default.url || "", // should be instead
ticker: chain.nativeCurrency?.symbol || "ETH",
tickerName: chain.nativeCurrency?.name || "Ethereum",
decimals: chain.nativeCurrency.decimals || 18,
});
log.info("Chain Added: ", chain.name);
await this.web3AuthInstance.switchChain({ chainId: `0x${chain.id.toString(16)}` });
log.info("Chain Switched to ", chain.name);
return chain;
} catch (error: unknown) {
log.error("Error: Cannot change chain", error);
throw new SwitchChainError(error as Error);
}
}
On a side and related note, Wagmi's switchChain no longer works (at least in testnet) with Web3AuthConnector on versions:
Issue
On a side and related note, Wagmi's
switchChain
no longer works (at least intestnet
) withWeb3AuthConnector
on versions: