Skip to content

Commit

Permalink
Prepare for next hardhat version (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio authored May 26, 2023
1 parent 8d49be0 commit e1416d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
9 changes: 8 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,17 @@ class API {
}

// Hardhat
attachToHardhatVM(provider){
async attachToHardhatVM(provider){
const self = this;
this.collector = new DataCollector(this.instrumenter.instrumentationData);

if ('init' in provider) {
// Newer versions of Hardhat initialize the provider lazily, so we need to
// call provider.init() explicitly. This is a no-op if the provider is
// already initialized.
await provider.init();
}

let cur = provider;

// Go down to core HardhatNetworkProvider
Expand Down
8 changes: 4 additions & 4 deletions plugins/hardhat.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,19 @@ task("coverage", "Generates a code coverage report for tests")
// ==============
// Server launch
// ==============
let network = nomiclabsUtils.setupHardhatNetwork(env, api, ui);
let network = await nomiclabsUtils.setupHardhatNetwork(env, api, ui);

if (network.isHardhatEVM){
accounts = await utils.getAccountsHardhat(network.provider);
nodeInfo = await utils.getNodeInfoHardhat(network.provider);

// Note: this only works if the reset block number is before any transactions have fired on the fork.
// e.g you cannot fork at block 1, send some txs (blocks 2,3,4) and reset to block 2
env.network.provider.on(HARDHAT_NETWORK_RESET_EVENT, () => {
api.attachToHardhatVM(env.network.provider);
env.network.provider.on(HARDHAT_NETWORK_RESET_EVENT, async () => {
await api.attachToHardhatVM(env.network.provider);
});

api.attachToHardhatVM(network.provider);
await api.attachToHardhatVM(network.provider);

ui.report('hardhat-network', [
nodeInfo.split('/')[1],
Expand Down
33 changes: 25 additions & 8 deletions plugins/resources/nomiclabs.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ function setupBuidlerNetwork(env, api, ui){
)
}

function setupHardhatNetwork(env, api, ui){
async function setupHardhatNetwork(env, api, ui){
const hardhatPackage = require('hardhat/package.json');
const { createProvider } = require("hardhat/internal/core/providers/construction");
const { HARDHAT_NETWORK_NAME } = require("hardhat/plugins")

// after 2.15.0, the internal createProvider function has a different signature
const newCreateProviderSignature = semver.satisfies(hardhatPackage.version, "^2.15.0");

let provider, networkName, networkConfig;
let isHardhatEVM = false;

Expand All @@ -91,12 +95,20 @@ function setupHardhatNetwork(env, api, ui){
networkConfig = env.network.config;
configureHardhatEVMGas(networkConfig, api);

provider = createProvider(
networkName,
networkConfig,
env.config.paths,
env.artifacts,
)
if (newCreateProviderSignature) {
provider = await createProvider(
env.config,
networkName,
env.artifacts,
)
} else {
provider = createProvider(
networkName,
networkConfig,
env.config.paths,
env.artifacts,
)
}

// HttpProvider
} else {
Expand All @@ -106,7 +118,12 @@ function setupHardhatNetwork(env, api, ui){
networkConfig = env.config.networks[networkName]
configureNetworkGas(networkConfig, api);
configureHttpProvider(networkConfig, api, ui)
provider = createProvider(networkName, networkConfig);

if (newCreateProviderSignature) {
provider = await createProvider(env.config, networkName);
} else {
provider = createProvider(networkName, networkConfig);
}
}

return configureNetworkEnv(
Expand Down

0 comments on commit e1416d9

Please sign in to comment.