7

I see that is it is possible to define the timestamp of the next block (see here) in this way:

await network.provider.send("evm_setNextBlockTimestamp", [1625097600]);
await network.provider.send("evm_mine");

but what about setting the block number?

My contracts use block.number to perform operations and I need to verify that they act correctly by mining a block with a greater height.

3
  • 2
    You can use the evm_mine method, but if you want to mine multiple blocks you'll need to call it as many times as you need. But this is not practical if you want to advance thousands of blocks. We are going to add a new method that adds support for this. May 31, 2021 at 11:48
  • @FrancoVictorio is there an update on this?
    – TommyF
    Jun 23, 2021 at 5:40
  • 3
    You can subscribe to this issue to get notified when this is implemented: github.com/nomiclabs/hardhat/issues/1112 Jul 2, 2021 at 18:14

3 Answers 3

5

Most up to date method is with hardhat-network-helpers

yarn add --dev @nomicfoundation/hardhat-network-helpers

const { mine } = require("@nomicfoundation/hardhat-network-helpers");

async function main() {
  // instantly mine 1000 blocks
  await mine(1000);
}
3

I ended up modifying the block.number by mining multiple blocks as it was suggested in the comment by Franco Victorio.

This is the code:

async function mineNBlocks(n) {
  for (let index = 0; index < n; index++) {
    await ethers.provider.send('evm_mine');
  }
}

Then it is called with:

await mineNBlocks(750);

It was a bit slow, but it worked quite well for what I needed it.

1

There's now a Hardhat method exactly for that:

From hardhat docs:

// mine 256 blocks
await hre.network.provider.send("hardhat_mine", ["0x100"]);

// mine 1000 blocks with an interval of 1 minute
await hre.network.provider.send("hardhat_mine", ["0x3e8", "0x3c"]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.