diff --git a/adapters/wagmi-connector/src/connector.ts b/adapters/wagmi-connector/src/connector.ts index 96a83f4c..7cddfb36 100644 --- a/adapters/wagmi-connector/src/connector.ts +++ b/adapters/wagmi-connector/src/connector.ts @@ -133,23 +133,23 @@ class BloctoConnector extends Connector { } async switchChain(chainId: number): Promise { - const provider = await this.getProvider(); - const id = numberToHex(chainId); - const chain = this.chains.find((x) => x.id === chainId); - const { networks } = await fetch( - 'https://api.blocto.app/networks/evm' - ).then((response) => response.json()); - const evmSupportMap = networks.reduce( - (a: any, v: any) => ({ ...a, [v.chain_id]: v }), - {} - ); - const isBloctoSupportChain = evmSupportMap[`${chainId}`]; - - if (!chain || !isBloctoSupportChain) { - throw new SwitchChainError(new Error(`Blocto unsupported chain: ${id}`)); - } - try { + const provider = await this.getProvider(); + const id = numberToHex(chainId); + const chain = this.chains.find((x) => x.id === chainId); + const networks = await provider.supportChainList(); + const evmSupportMap = networks.reduce( + (a: any, v: any) => ({ ...a, [v.chain_id]: v }), + {} + ); + const isBloctoSupportChain = evmSupportMap[`${chainId}`]; + + if (!chain || !isBloctoSupportChain) { + throw new SwitchChainError( + new Error(`Blocto unsupported chain: ${id}`) + ); + } + await provider.request({ method: 'wallet_addEthereumChain', params: [{ chainId: id, rpcUrls: chain?.rpcUrls.default.http }],