Last active
February 7, 2019 18:29
-
-
Save fzeoli/9cdd9c1182b9636829bf71bfacb82c43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ContractFactory, ethers, Signer } = require("ethers"); | |
const {JsonRpcProvider} = require("ethers/providers"); | |
function createJsonRpcRequest(method, params) { | |
return { | |
id: nextId++, | |
jsonrpc: "2.0", | |
method, | |
params | |
}; | |
} | |
class EthersProviderWrapper extends JsonRpcProvider { | |
constructor(provider) { | |
super(); | |
} | |
async send(method, params) { | |
const result = await this.provider.send(method, params); | |
// We replicate ethers' behavior. | |
this.emit("debug", { | |
action: "send", | |
request: createJsonRpcRequest(method, params), | |
response: result, | |
provider: this | |
}); | |
return result; | |
} | |
} | |
extendEnvironment((env) => { | |
const wrapper = new EthersProviderWrapper(env.provider); | |
env.ethers = { | |
provider: wrapper, | |
getContract: async function (name) { | |
const artifact = await readArtifact(env.config.paths.artifacts, name); | |
const bytecode = artifact.bytecode; | |
const signers = await env.ethers.signers(); | |
return new ethers.ContractFactory(artifact.abi, bytecode, signers[0]); | |
}, | |
signers: async function () { | |
const accounts = await env.provider.send("eth_accounts"); | |
return accounts.map((account) => wrapper.getSigner(account)); | |
} | |
}; | |
}); | |
module.exports = {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment