diff --git a/apps/remix-ide/src/app/providers/injected-custom-provider.tsx b/apps/remix-ide/src/app/providers/injected-custom-provider.tsx index c050496698..153efcd1bb 100644 --- a/apps/remix-ide/src/app/providers/injected-custom-provider.tsx +++ b/apps/remix-ide/src/app/providers/injected-custom-provider.tsx @@ -23,15 +23,12 @@ export class InjectedCustomProvider extends InjectedProviderDefault { this.chainId = `0x${chainId.toString(16)}` } await super.init() - if (this.chainName && this.rpcUrls && this.rpcUrls.length > 0) await addCustomNetwork(this.chainName, this.chainId, this.rpcUrls, this.nativeCurrency, this.blockExplorerUrls) - else { - console.log('The custom network is added without rpcUrls, the network need to be already installed in the injected provider.') - } + await setCustomNetwork(this.chainName, this.chainId, this.rpcUrls, this.nativeCurrency, this.blockExplorerUrls) return {} } } -export const addCustomNetwork = async (chainName: string, chainId: string, rpcUrls: Array, nativeCurrency?: Record, blockExplorerUrls?: Array) => { +export const setCustomNetwork = async (chainName: string, chainId: string, rpcUrls: Array, nativeCurrency?: Record, blockExplorerUrls?: Array) => { try { await (window as any).ethereum.request({ method: 'wallet_switchEthereumChain', @@ -41,22 +38,24 @@ export const addCustomNetwork = async (chainName: string, chainId: string, rpcUr // This error code indicates that the chain has not been added to MetaMask. if (switchError.code === 4902) { try { - const paramsObj: Record = { - chainId: chainId, - chainName: chainName, - rpcUrls: rpcUrls, - } - if (nativeCurrency) paramsObj.nativeCurrency = nativeCurrency - if (blockExplorerUrls) paramsObj.blockExplorerUrls = blockExplorerUrls - await (window as any).ethereum.request({ - method: 'wallet_addEthereumChain', - params: [ paramsObj ] - }) - - await (window as any).ethereum.request({ - method: 'wallet_switchEthereumChain', - params: [{chainId: chainId}] - }) + if (chainName && rpcUrls && rpcUrls.length > 0) { + const paramsObj: Record = { + chainId: chainId, + chainName: chainName, + rpcUrls: rpcUrls, + } + if (nativeCurrency) paramsObj.nativeCurrency = nativeCurrency + if (blockExplorerUrls) paramsObj.blockExplorerUrls = blockExplorerUrls + await (window as any).ethereum.request({ + method: 'wallet_addEthereumChain', + params: [ paramsObj ] + }) + + await (window as any).ethereum.request({ + method: 'wallet_switchEthereumChain', + params: [{chainId: chainId}] + }) + } } catch (addError) { // handle "add" error }