parent
140d8a394d
commit
d7619601f6
@ -0,0 +1,18 @@ |
||||
import * as packageJson from '../../../../../package.json' |
||||
import { InjectedProvider } from './injected-provider' |
||||
|
||||
const profile = { |
||||
name: 'injected', |
||||
displayName: 'Injected Provider', |
||||
kind: 'provider', |
||||
description: 'injected Provider', |
||||
methods: ['sendAsync', 'init'], |
||||
version: packageJson.version |
||||
} |
||||
|
||||
export class BasicInjectedProvider extends InjectedProvider {
|
||||
|
||||
constructor () { |
||||
super(profile) |
||||
} |
||||
} |
@ -0,0 +1,54 @@ |
||||
import { InjectedProvider } from './injected-provider' |
||||
|
||||
export class InjectedL2Provider extends InjectedProvider {
|
||||
chainName: string |
||||
chainId: string |
||||
rpcUrls: Array<string> |
||||
|
||||
constructor (profile: any, chainName: string, chainId: string, rpcUrls: Array<string>) { |
||||
super(profile) |
||||
this.chainName = chainName |
||||
this.chainId = chainId |
||||
this.rpcUrls = rpcUrls |
||||
} |
||||
|
||||
async init () { |
||||
await super.init() |
||||
if (this.chainName && this.rpcUrls && this.rpcUrls.length > 0) await addL2Network(this.chainName, this.chainId, this.rpcUrls) |
||||
else |
||||
throw new Error('Cannot add the L2 network to main injected provider') |
||||
} |
||||
} |
||||
|
||||
export const addL2Network = async (chainName: string, chainId: string, rpcUrls: Array<string>) => { |
||||
try { |
||||
await (window as any).ethereum.request({ |
||||
method: 'wallet_switchEthereumChain', |
||||
params: [{ chainId: chainId }], |
||||
}); |
||||
} catch (switchError) { |
||||
// This error code indicates that the chain has not been added to MetaMask.
|
||||
if (switchError.code === 4902) { |
||||
try { |
||||
await (window as any).ethereum.request({ |
||||
method: 'wallet_addEthereumChain', |
||||
params: [ |
||||
{ |
||||
chainId: chainId, |
||||
chainName: chainName, |
||||
rpcUrls: rpcUrls, |
||||
}, |
||||
], |
||||
}); |
||||
|
||||
await (window as any).ethereum.request({ |
||||
method: 'wallet_switchEthereumChain', |
||||
params: [{ chainId: chainId }], |
||||
}); |
||||
} catch (addError) { |
||||
// handle "add" error
|
||||
} |
||||
} |
||||
// handle other "switch" errors
|
||||
} |
||||
} |
@ -1,21 +1,18 @@ |
||||
import * as packageJson from '../../../../../package.json' |
||||
import { InjectedProvider } from './injected-provider' |
||||
import { InjectedL2Provider } from './injected-L2-provider' |
||||
|
||||
const profile = { |
||||
name: 'injected-arbitrum-one-provider', |
||||
displayName: 'Injected Arbitrum One Provider', |
||||
kind: 'provider', |
||||
description: 'injected Arbitrum One Provider', |
||||
methods: ['sendAsync'], |
||||
methods: ['sendAsync', 'init'], |
||||
version: packageJson.version |
||||
} |
||||
|
||||
export class InjectedArbitrumOneProvider extends InjectedProvider { |
||||
export class InjectedArbitrumOneProvider extends InjectedL2Provider { |
||||
|
||||
constructor () { |
||||
super(profile) |
||||
this.chainName = 'Arbitrum One' |
||||
this.chainId = '0xa4b1' |
||||
this.rpcUrls = ['https://arb1.arbitrum.io/rpc'] |
||||
super(profile, 'Arbitrum One', '0xa4b1', ['https://arb1.arbitrum.io/rpc']) |
||||
} |
||||
} |
@ -1,21 +1,18 @@ |
||||
import * as packageJson from '../../../../../package.json' |
||||
import { InjectedProvider } from './injected-provider' |
||||
import { InjectedL2Provider } from './injected-L2-provider' |
||||
|
||||
const profile = { |
||||
name: 'injected-optimism-provider', |
||||
displayName: 'Injected Optimism Provider', |
||||
kind: 'provider', |
||||
description: 'injected Optimism Provider', |
||||
methods: ['sendAsync'], |
||||
methods: ['sendAsync', 'init'], |
||||
version: packageJson.version |
||||
} |
||||
|
||||
export class Injected0ptimismProvider extends InjectedProvider { |
||||
export class Injected0ptimismProvider extends InjectedL2Provider { |
||||
|
||||
constructor () { |
||||
super(profile) |
||||
this.chainName = 'Optimism' |
||||
this.chainId = '0xa' |
||||
this.rpcUrls = ['https://mainnet.optimism.io'] |
||||
super(profile, 'Optimism', '0xa', ['https://mainnet.optimism.io']) |
||||
} |
||||
} |
Loading…
Reference in new issue