-
Notifications
You must be signed in to change notification settings - Fork 10
/
BrowserEnvironment.js
37 lines (31 loc) · 1.07 KB
/
BrowserEnvironment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const { ethers } = require('ethers')
const sleep = require('../../helpers/sleep')
const Environment = require('./Environment')
const JsonRpcSigner = require('../providers/JsonRpcSigner')
const StaticArtifacts = require('../artifacts/StaticArtifacts')
class BrowserEnvironment extends Environment {
constructor(network) {
super(network)
}
async isEnabled() {
await sleep(2)
const { web3 } = window
return !!(web3 && web3.currentProvider && web3.currentProvider.selectedAddress)
}
async _getProvider() {
const isEnabled = await this.isEnabled()
if (!isEnabled) throw Error('Could not access to a browser web3 provider, please make sure to allow one.')
const provider = window.web3.currentProvider
provider.setMaxListeners(300)
return new ethers.providers.Web3Provider(provider)
}
async _getSigner() {
const provider = await this.getProvider()
return new JsonRpcSigner(provider)
}
async _getArtifacts() {
const signer = await this.getSigner()
return new StaticArtifacts(signer)
}
}
module.exports = BrowserEnvironment