Skip to content

Commit

Permalink
Improves the ability to change networks in hardhat
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Mar 25, 2024
1 parent 1347aa0 commit 8f36b9b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 9 deletions.
57 changes: 57 additions & 0 deletions packages/validator/hardhat-change-network.ts
Original file line number Diff line number Diff line change
@@ -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<void>;
getProvider(newNetwork: string): Promise<EthereumProvider>;
}
}

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<EthereumProvider> {
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;
}
}
};
});
2 changes: 1 addition & 1 deletion packages/validator/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 0 additions & 1 deletion packages/validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/validator/src/scheduler/EventCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class EventCollector {
}

public async work() {
hre.changeNetwork(this.network);
await hre.changeNetwork(this.network);

const latestBlockNumber = await this.getLatestBlockNumber();

Expand Down
2 changes: 1 addition & 1 deletion packages/validator/src/scheduler/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8f36b9b

Please sign in to comment.