Skip to content

Commit 5aee556

Browse files
committedJun 22, 2023
2 parents 9d24835 + e1416d9 commit 5aee556

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed
 

‎lib/api.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,17 @@ class API {
279279
}
280280

281281
// Hardhat
282-
attachToHardhatVM(provider){
282+
async attachToHardhatVM(provider){
283283
const self = this;
284284
this.collector = new DataCollector(this.instrumenter.instrumentationData);
285285

286+
if ('init' in provider) {
287+
// Newer versions of Hardhat initialize the provider lazily, so we need to
288+
// call provider.init() explicitly. This is a no-op if the provider is
289+
// already initialized.
290+
await provider.init();
291+
}
292+
286293
let cur = provider;
287294

288295
// Go down to core HardhatNetworkProvider

‎plugins/hardhat.plugin.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,19 @@ task("coverage", "Generates a code coverage report for tests")
198198
// ==============
199199
// Server launch
200200
// ==============
201-
let network = nomiclabsUtils.setupHardhatNetwork(env, api, ui);
201+
let network = await nomiclabsUtils.setupHardhatNetwork(env, api, ui);
202202

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

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

213-
api.attachToHardhatVM(network.provider);
213+
await api.attachToHardhatVM(network.provider);
214214

215215
ui.report('hardhat-network', [
216216
nodeInfo.split('/')[1],

‎plugins/resources/nomiclabs.utils.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ function setupBuidlerNetwork(env, api, ui){
7575
)
7676
}
7777

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

83+
// after 2.15.0, the internal createProvider function has a different signature
84+
const newCreateProviderSignature = semver.satisfies(hardhatPackage.version, "^2.15.0");
85+
8286
let provider, networkName, networkConfig;
8387
let isHardhatEVM = false;
8488

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

94-
provider = createProvider(
95-
networkName,
96-
networkConfig,
97-
env.config.paths,
98-
env.artifacts,
99-
)
98+
if (newCreateProviderSignature) {
99+
provider = await createProvider(
100+
env.config,
101+
networkName,
102+
env.artifacts,
103+
)
104+
} else {
105+
provider = createProvider(
106+
networkName,
107+
networkConfig,
108+
env.config.paths,
109+
env.artifacts,
110+
)
111+
}
100112

101113
// HttpProvider
102114
} else {
@@ -106,7 +118,12 @@ function setupHardhatNetwork(env, api, ui){
106118
networkConfig = env.config.networks[networkName]
107119
configureNetworkGas(networkConfig, api);
108120
configureHttpProvider(networkConfig, api, ui)
109-
provider = createProvider(networkName, networkConfig);
121+
122+
if (newCreateProviderSignature) {
123+
provider = await createProvider(env.config, networkName);
124+
} else {
125+
provider = createProvider(networkName, networkConfig);
126+
}
110127
}
111128

112129
return configureNetworkEnv(

0 commit comments

Comments
 (0)
Please sign in to comment.