From 9eecff456531efc31e345f8a777f8baea2b92f06 Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 23 Apr 2024 16:11:45 +0200 Subject: [PATCH] fix adding test net --- .../providers/injected-custom-provider.tsx | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) 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 }