diff --git a/packages/validator/hardhat-change-network.ts b/packages/validator/hardhat-change-network.ts new file mode 100644 index 0000000..f2dba31 --- /dev/null +++ b/packages/validator/hardhat-change-network.ts @@ -0,0 +1,57 @@ +import { extendEnvironment } from "hardhat/config"; +import { createProvider } from "hardhat/internal/core/providers/construction"; +import { HttpNetworkConfig } from "hardhat/src/types"; +import { JsonRpcProvider } from "@ethersproject/providers"; +import { EthereumProvider } from "hardhat/types/provider"; + +// This import is needed to let the TypeScript compiler know that it should include your type +// extensions in your npm package's types file. +import "hardhat/types/runtime"; +declare module "hardhat/types/runtime" { + interface HardhatRuntimeEnvironment { + changeNetwork(newNetwork: string): Promise; + getProvider(newNetwork: string): Promise; + } +} + +extendEnvironment((hre) => { + // We add a field to the Hardhat Runtime Environment here. + // We use lazyObject to avoid initializing things until they are actually + // needed. + const providers: { [name: string]: EthereumProvider } = {}; + + hre.getProvider = async function getProvider(name: string): Promise { + if (!providers[name]) { + providers[name] = await createProvider(hre.config, name, this.artifacts); + } + return providers[name]; + }; + + hre.changeNetwork = async function changeNetwork(newNetwork: string) { + if (!this.config.networks[newNetwork]) { + throw new Error(`changeNetwork: Couldn't find network '${newNetwork}'`); + } + + if (!providers[this.network.name]) { + providers[this.network.name] = this.network.provider; + } + + this.network.name = newNetwork; + this.network.config = this.config.networks[newNetwork]; + this.network.provider = await this.getProvider(newNetwork); + + if ((this as any).ethers) { + if (newNetwork === "hardhat") { + const { EthersProviderWrapper } = require("@nomiclabs/hardhat-ethers/internal/ethers-provider-wrapper"); + (this as any).ethers.provider = new EthersProviderWrapper(this.network.provider); + } else { + const httpNetConfig = this.config.networks[newNetwork] as HttpNetworkConfig; + const chainId = httpNetConfig.chainId || 0; + (this as any).ethers.provider = new JsonRpcProvider(httpNetConfig.url, { + name: newNetwork, + chainId, + }) as JsonRpcProvider; + } + } + }; +}); diff --git a/packages/validator/hardhat.config.ts b/packages/validator/hardhat.config.ts index e8ec028..822fc39 100644 --- a/packages/validator/hardhat.config.ts +++ b/packages/validator/hardhat.config.ts @@ -3,7 +3,7 @@ import "@nomiclabs/hardhat-waffle"; import "@nomiclabs/hardhat-web3"; import "@openzeppelin/hardhat-upgrades"; import "@typechain/hardhat"; -import "hardhat-change-network"; +import "./hardhat-change-network"; import "hardhat-gas-reporter"; import "solidity-coverage"; import "solidity-docgen"; diff --git a/packages/validator/package.json b/packages/validator/package.json index 7cc14fe..dc620e3 100644 --- a/packages/validator/package.json +++ b/packages/validator/package.json @@ -85,7 +85,6 @@ "express-validator": "^6.14.0", "extend": "^3.0.2", "hardhat": "^2.12.7", - "hardhat-change-network": "^0.0.7", "hardhat-gas-reporter": "^1.0.7", "ip": "^1.1.5", "loyalty-tokens": "^1.0.5", diff --git a/packages/validator/src/scheduler/EventCollector.ts b/packages/validator/src/scheduler/EventCollector.ts index 7e58bb2..e660b19 100644 --- a/packages/validator/src/scheduler/EventCollector.ts +++ b/packages/validator/src/scheduler/EventCollector.ts @@ -37,7 +37,7 @@ export class EventCollector { } public async work() { - hre.changeNetwork(this.network); + await hre.changeNetwork(this.network); const latestBlockNumber = await this.getLatestBlockNumber(); diff --git a/packages/validator/src/scheduler/Executor.ts b/packages/validator/src/scheduler/Executor.ts index 46ae37c..ab07c64 100644 --- a/packages/validator/src/scheduler/Executor.ts +++ b/packages/validator/src/scheduler/Executor.ts @@ -46,7 +46,7 @@ export class Executor { ); if (events.length === 0) return; - hre.changeNetwork(this.targetNetwork); + await hre.changeNetwork(this.targetNetwork); const signer = new NonceManager(new GasPriceManager(this.wallet.connect(hre.ethers.provider))); const contract = new hre.ethers.Contract( diff --git a/yarn.lock b/yarn.lock index 6c88bf8..5581895 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4110,11 +4110,6 @@ har-validator@~5.1.3: ajv "^6.12.3" har-schema "^2.0.0" -hardhat-change-network@^0.0.7: - version "0.0.7" - resolved "https://registry.yarnpkg.com/hardhat-change-network/-/hardhat-change-network-0.0.7.tgz#9f9b7943ff966515658b70bf5e44bc2f073af402" - integrity sha512-Usp9fJan9SOJnOlVcv/jMJDchseE7bIDA5ZsBnracgVk4MiBwkvMqpmLWn5G1aDBvnUCthvS2gO3odfahgkV0Q== - hardhat-gas-reporter@^1.0.7: version "1.0.10" resolved "https://registry.yarnpkg.com/hardhat-gas-reporter/-/hardhat-gas-reporter-1.0.10.tgz#ebe5bda5334b5def312747580cd923c2b09aef1b"