68 lines
1.6 KiB
JavaScript
Raw Normal View History

const { ethers } = require("hardhat")
let snapshots = []
async function snapshot() {
const id = await ethers.provider.send("evm_snapshot")
const time = await currentTime()
const automine = await ethers.provider.send("hardhat_getAutomine")
snapshots.push({ id, time, automine })
}
async function revert() {
const { id, time, automine } = snapshots.pop()
await ethers.provider.send("evm_revert", [id])
await ethers.provider.send("evm_setNextBlockTimestamp", [time])
await ethers.provider.send("evm_setAutomine", [automine])
}
2025-01-30 13:41:42 +01:00
/**
* Enables or disables Hardhat's automine mode.
*
* When automine mode is disabled, transactions that revert are silently ignored!
*/
async function setAutomine(enabled) {
await ethers.provider.send("evm_setAutomine", [enabled])
}
async function mine() {
await ethers.provider.send("evm_mine")
}
2022-03-09 11:21:19 +01:00
async function ensureMinimumBlockHeight(height) {
2022-03-09 11:29:41 +01:00
while ((await ethers.provider.getBlockNumber()) < height) {
await mine()
2022-03-09 11:21:19 +01:00
}
}
async function currentTime() {
let block = await ethers.provider.getBlock("latest")
return block.timestamp
}
async function advanceTime(seconds) {
2022-03-09 11:29:41 +01:00
await ethers.provider.send("evm_increaseTime", [seconds])
await mine()
}
async function advanceTimeTo(timestamp) {
await setNextBlockTimestamp(timestamp)
await mine()
}
async function setNextBlockTimestamp(timestamp) {
await ethers.provider.send("evm_setNextBlockTimestamp", [timestamp])
}
module.exports = {
snapshot,
revert,
setAutomine,
mine,
2022-03-09 11:21:19 +01:00
ensureMinimumBlockHeight,
currentTime,
advanceTime,
advanceTimeTo,
setNextBlockTimestamp,
}