Testing – Modifying Block Number with Hardhat

hardhattesting

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.

Best Answer

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.