From fbd5a7089b2cd75c276b83d1fa8553dcceac7802 Mon Sep 17 00:00:00 2001 From: Victor Elias Date: Wed, 11 Oct 2023 18:38:06 -0300 Subject: [PATCH] Add the task to attempt to increase blocks --- tasks/evm-increase-blocks.ts | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tasks/evm-increase-blocks.ts diff --git a/tasks/evm-increase-blocks.ts b/tasks/evm-increase-blocks.ts new file mode 100644 index 00000000..1a80083a --- /dev/null +++ b/tasks/evm-increase-blocks.ts @@ -0,0 +1,37 @@ +import {task} from "hardhat/config" +import {ethers} from "ethers" + +const BLOCK_PERIOD = 12 // seconds + +task( + "evm-increase-blocks", + "Helper task to increase the block number in the current EVM" +) + .addOptionalPositionalParam( + "blocks", + "How many blocks to increase by (defaults to 1)" + ) + .setAction(async (taskArgs, hre) => { + const {network} = hre + + const provider = network.provider + const blocks = parseInt(taskArgs.blocks ?? "1") + + const currBlock = ethers.BigNumber.from( + await provider.send("eth_blockNumber") + ) + console.log(`Previous block pre-update: ${currBlock}`) + + await provider.send("evm_increaseBlocks", [ + ethers.utils.hexValue(blocks) // hex encoded number of blocks to increase + ]) + // helpfully increase the time by 12s per block as well + await provider.send("evm_increaseTime", [ + ethers.utils.hexValue(BLOCK_PERIOD * blocks) // hex encoded number of seconds + ]) + + const newBlock = ethers.BigNumber.from( + await provider.send("eth_blockNumber") + ) + console.log(`New block post-update: ${newBlock}`) + })