fix adding test net

pull/4745/head
yann300 7 months ago committed by Aniket
parent 95423dc06a
commit 9eecff4565
  1. 41
      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)}` this.chainId = `0x${chainId.toString(16)}`
} }
await super.init() await super.init()
if (this.chainName && this.rpcUrls && this.rpcUrls.length > 0) await addCustomNetwork(this.chainName, this.chainId, this.rpcUrls, this.nativeCurrency, this.blockExplorerUrls) await setCustomNetwork(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.')
}
return {} return {}
} }
} }
export const addCustomNetwork = async (chainName: string, chainId: string, rpcUrls: Array<string>, nativeCurrency?: Record<string, any>, blockExplorerUrls?: Array<string>) => { export const setCustomNetwork = async (chainName: string, chainId: string, rpcUrls: Array<string>, nativeCurrency?: Record<string, any>, blockExplorerUrls?: Array<string>) => {
try { try {
await (window as any).ethereum.request({ await (window as any).ethereum.request({
method: 'wallet_switchEthereumChain', 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. // This error code indicates that the chain has not been added to MetaMask.
if (switchError.code === 4902) { if (switchError.code === 4902) {
try { try {
const paramsObj: Record<string, any> = { if (chainName && rpcUrls && rpcUrls.length > 0) {
chainId: chainId, const paramsObj: Record<string, any> = {
chainName: chainName, chainId: chainId,
rpcUrls: rpcUrls, chainName: chainName,
} rpcUrls: rpcUrls,
if (nativeCurrency) paramsObj.nativeCurrency = nativeCurrency }
if (blockExplorerUrls) paramsObj.blockExplorerUrls = blockExplorerUrls if (nativeCurrency) paramsObj.nativeCurrency = nativeCurrency
await (window as any).ethereum.request({ if (blockExplorerUrls) paramsObj.blockExplorerUrls = blockExplorerUrls
method: 'wallet_addEthereumChain', await (window as any).ethereum.request({
params: [ paramsObj ] method: 'wallet_addEthereumChain',
}) params: [ paramsObj ]
})
await (window as any).ethereum.request({
method: 'wallet_switchEthereumChain', await (window as any).ethereum.request({
params: [{chainId: chainId}] method: 'wallet_switchEthereumChain',
}) params: [{chainId: chainId}]
})
}
} catch (addError) { } catch (addError) {
// handle "add" error // handle "add" error
} }

Loading…
Cancel
Save