|
1 | 1 | import type { AppMetadata, Preference, ProviderInterface } from "@coinbase/wallet-sdk"; |
| 2 | +import type { ProviderRpcError } from "@coinbase/wallet-sdk/dist/core/provider/interface"; |
2 | 3 |
|
3 | 4 | import { |
4 | 5 | BaseConnectorLoginParams, |
@@ -68,7 +69,6 @@ class CoinbaseConnector extends BaseEvmConnector<void> { |
68 | 69 | const chainConfig = this.coreOptions.chains.find((x) => x.chainId === options.chainId); |
69 | 70 | super.checkInitializationRequirements({ chainConfig }); |
70 | 71 | const { createCoinbaseWalletSDK } = await import("@coinbase/wallet-sdk"); |
71 | | - |
72 | 72 | const coinbaseInstance = createCoinbaseWalletSDK({ |
73 | 73 | ...this.coinbaseOptions, |
74 | 74 | preference: { options: this.coinbaseOptions.options || "smartWalletOnly" }, |
@@ -156,7 +156,31 @@ class CoinbaseConnector extends BaseEvmConnector<void> { |
156 | 156 |
|
157 | 157 | public async switchChain(params: { chainId: string }, init = false): Promise<void> { |
158 | 158 | super.checkSwitchChainRequirements(params, init); |
159 | | - await this.coinbaseProvider.request({ method: "wallet_switchEthereumChain", params: [{ chainId: params.chainId }] }); |
| 159 | + try { |
| 160 | + await this.coinbaseProvider.request({ method: "wallet_switchEthereumChain", params: [{ chainId: params.chainId }] }); |
| 161 | + } catch (switchError: unknown) { |
| 162 | + // 4902 indicates that the client does not recognize the Harmony One network |
| 163 | + if ((switchError as ProviderRpcError).code === 4902) { |
| 164 | + const chainConfig = this.coreOptions.chains.find((x) => x.chainId === params.chainId); |
| 165 | + if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available"); |
| 166 | + await this.coinbaseProvider.request({ |
| 167 | + method: "wallet_addEthereumChain", |
| 168 | + params: [ |
| 169 | + { |
| 170 | + chainId: chainConfig.chainId, |
| 171 | + rpcUrls: [chainConfig.rpcTarget], |
| 172 | + chainName: chainConfig.displayName, |
| 173 | + nativeCurrency: { name: chainConfig.tickerName, symbol: chainConfig.ticker, decimals: chainConfig.decimals || 18 }, |
| 174 | + blockExplorerUrls: [chainConfig.blockExplorerUrl], |
| 175 | + iconUrls: [chainConfig.logo], |
| 176 | + }, |
| 177 | + ], |
| 178 | + }); |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + throw switchError; |
| 183 | + } |
160 | 184 | } |
161 | 185 |
|
162 | 186 | public async enableMFA(): Promise<void> { |
|
0 commit comments