2022-03-03 10:59:01 +00:00
|
|
|
const { ethers } = require("hardhat")
|
|
|
|
|
|
|
|
let snapshots = []
|
|
|
|
|
|
|
|
async function snapshot() {
|
|
|
|
const id = await ethers.provider.send("evm_snapshot")
|
|
|
|
const time = await currentTime()
|
|
|
|
snapshots.push({ id, time })
|
|
|
|
}
|
|
|
|
|
|
|
|
async function revert() {
|
|
|
|
const { id, time } = snapshots.pop()
|
|
|
|
await ethers.provider.send("evm_revert", [id])
|
|
|
|
await ethers.provider.send("evm_setNextBlockTimestamp", [time + 1])
|
|
|
|
}
|
|
|
|
|
2022-03-09 10:21:19 +00:00
|
|
|
async function ensureMinimumBlockHeight(height) {
|
2022-03-09 10:29:41 +00:00
|
|
|
while ((await ethers.provider.getBlockNumber()) < height) {
|
|
|
|
await ethers.provider.send("evm_mine")
|
2022-03-09 10:21:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 10:59:01 +00:00
|
|
|
async function currentTime() {
|
|
|
|
let block = await ethers.provider.getBlock("latest")
|
|
|
|
return block.timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
async function advanceTime(seconds) {
|
2022-03-09 10:29:41 +00:00
|
|
|
await ethers.provider.send("evm_increaseTime", [seconds])
|
|
|
|
await ethers.provider.send("evm_mine")
|
2022-03-03 10:59:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 14:58:08 +00:00
|
|
|
async function advanceTimeTo(timestamp) {
|
|
|
|
if ((await currentTime()) !== timestamp) {
|
2022-03-09 10:29:41 +00:00
|
|
|
await ethers.provider.send("evm_setNextBlockTimestamp", [timestamp])
|
|
|
|
await ethers.provider.send("evm_mine")
|
2022-03-08 14:58:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 10:59:01 +00:00
|
|
|
module.exports = {
|
|
|
|
snapshot,
|
|
|
|
revert,
|
2022-03-09 10:21:19 +00:00
|
|
|
ensureMinimumBlockHeight,
|
2022-03-03 10:59:01 +00:00
|
|
|
currentTime,
|
|
|
|
advanceTime,
|
2022-03-08 14:58:08 +00:00
|
|
|
advanceTimeTo,
|
2022-03-03 10:59:01 +00:00
|
|
|
}
|