add RPC to wallet on unrecognized chains

This commit is contained in:
hueso 2024-10-09 21:56:33 -03:00
parent e483fd537c
commit 42fcae0465

View File

@ -73,15 +73,20 @@ const listenToNetworkChange = (connection: any) => {
const requestNetworkChange = async (network: NetworkEnum): Promise<boolean> => { const requestNetworkChange = async (network: NetworkEnum): Promise<boolean> => {
const etherStore = useEtherStore(); const etherStore = useEtherStore();
if (!etherStore.walletAddress) return true; if (!etherStore.walletAddress) return true;
const window_ = window as any;
try { try {
const window_ = window as any;
await window_.ethereum.request({ await window_.ethereum.request({
method: "wallet_switchEthereumChain", method: "wallet_switchEthereumChain",
params: [{ chainId: Networks[network].chainId }], // chainId must be in hexadecimal numbers params: [{ chainId: Networks[network].chainId }], // chainId must be in hexadecimal numbers
}); });
} catch { } catch (e:any){
return false; if (e.code == 4902){ // Unrecognized chain ID
await window_.ethereum.request({
"method": "wallet_addEthereumChain",
"params": [ Networks[network] ],
});
}
} }
return true; return true;