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)}`
}
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<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 {
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<string, any> = {
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<string, any> = {
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
}

Loading…
Cancel
Save