To write time-dependent tests with hardhat, the "evm_increasetime" and "evm_mine" functions of the hardhat ethers plugin are used.
Steps:
-
Import Hardhat and Hardhat Ethers plugins
const { ethers } = require("hardhat") const hre = require("hardhat") -
Define the below mentioned functions
-
Call the functions in tests
-
Manipulating EVM via Increasing Block Timestamp
async function increaseTime(amount) { await hre.network.provider.send("evm_increaseTime", [amount]) console.log("EVM time " + amount + " seconds increased!") }
- Manipulating EVM via Mining Blocks
async function mineBlocks(amount) { for (let i = 0; i < amount; i++) { await hre.network.provider.send("evm_mine") } console.log(amount + " Blocks Mined!") }