|
|
@ -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,6 +38,7 @@ 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 { |
|
|
|
|
|
|
|
if (chainName && rpcUrls && rpcUrls.length > 0) { |
|
|
|
const paramsObj: Record<string, any> = { |
|
|
|
const paramsObj: Record<string, any> = { |
|
|
|
chainId: chainId, |
|
|
|
chainId: chainId, |
|
|
|
chainName: chainName, |
|
|
|
chainName: chainName, |
|
|
@ -57,6 +55,7 @@ export const addCustomNetwork = async (chainName: string, chainId: string, rpcUr |
|
|
|
method: 'wallet_switchEthereumChain', |
|
|
|
method: 'wallet_switchEthereumChain', |
|
|
|
params: [{chainId: chainId}] |
|
|
|
params: [{chainId: chainId}] |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
}
|
|
|
|
} catch (addError) { |
|
|
|
} catch (addError) { |
|
|
|
// handle "add" error
|
|
|
|
// handle "add" error
|
|
|
|
} |
|
|
|
} |
|
|
|