Allow currency and block explorer details for custom provider

pull/4060/head
aniket-engg 1 year ago committed by Aniket
parent 573dfa93c9
commit 839f318054
  1. 25
      apps/remix-ide/src/app/providers/injected-custom-provider.tsx
  2. 15
      apps/remix-ide/src/app/providers/injected-ephemery-testnet-provider.tsx

@ -4,23 +4,27 @@ export class InjectedCustomProvider extends InjectedProviderDefaultBase {
chainName: string chainName: string
chainId: string chainId: string
rpcUrls: Array<string> rpcUrls: Array<string>
nativeCurrency: Record<string, any>
blockExplorerUrls: Array<string>
constructor(profile: any, chainName: string, chainId: string, rpcUrls: Array<string>) { constructor(profile: any, chainName: string, chainId: string, rpcUrls: Array<string>, nativeCurrency?: Record<string, any>, blockExplorerUrls?: Array<string>) {
super(profile) super(profile)
this.chainName = chainName this.chainName = chainName
this.chainId = chainId this.chainId = chainId
this.rpcUrls = rpcUrls this.rpcUrls = rpcUrls
this.nativeCurrency = nativeCurrency
this.blockExplorerUrls = blockExplorerUrls
} }
async init() { async init() {
await super.init() await super.init()
if (this.chainName && this.rpcUrls && this.rpcUrls.length > 0) await addCustomNetwork(this.chainName, this.chainId, this.rpcUrls) if (this.chainName && this.rpcUrls && this.rpcUrls.length > 0) await addCustomNetwork(this.chainName, this.chainId, this.rpcUrls, this.nativeCurrency, this.blockExplorerUrls)
else throw new Error('Cannot add the custom network to main injected provider') else throw new Error('Cannot add the custom network to main injected provider')
return {} return {}
} }
} }
export const addCustomNetwork = async (chainName: string, chainId: string, rpcUrls: Array<string>) => { export const addCustomNetwork = 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',
@ -30,15 +34,16 @@ 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 {
let 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({ await (window as any).ethereum.request({
method: 'wallet_addEthereumChain', method: 'wallet_addEthereumChain',
params: [ params: [ paramsObj ]
{
chainId: chainId,
chainName: chainName,
rpcUrls: rpcUrls
}
]
}) })
await (window as any).ethereum.request({ await (window as any).ethereum.request({

@ -12,6 +12,19 @@ const profile = {
export class InjectedEphemeryTestnetProvider extends InjectedCustomProvider { export class InjectedEphemeryTestnetProvider extends InjectedCustomProvider {
constructor() { constructor() {
super(profile, 'Ephemery Testnet', '0x259C709', ['https://otter.bordel.wtf/erigon', 'https://eth.ephemeral.zeus.fyi']) super(profile,
'Ephemery Testnet',
'',
['https://otter.bordel.wtf/erigon', 'https://eth.ephemeral.zeus.fyi'],
{
"name": "Ephemery ETH",
"symbol": "ETH",
"decimals": 18
},
[
'https://otter.bordel.wtf/',
'https://explorer.ephemery.dev/'
]
)
} }
} }

Loading…
Cancel
Save