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.

Alvin Sartor
  • 275
  • 2
  • 14
  • 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. – Franco Victorio May 31 '21 at 11:48
  • @FrancoVictorio is there an update on this? – TommyF Jun 23 '21 at 05:40
  • 3
    You can subscribe to this issue to get notified when this is implemented: https://github.com/nomiclabs/hardhat/issues/1112 – Franco Victorio Jul 02 '21 at 18:14

3 Answers3

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); }

alpo
  • 164
  • 2
  • 8
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.

Alvin Sartor
  • 275
  • 2
  • 14
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"]);