dagger-contracts/test/evm.js

58 lines
1.2 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()
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])
}
async function mineBlock() {
await ethers.provider.send("evm_mine")
}
async function minedBlockNumber() {
return await ethers.provider.getBlockNumber()
}
2022-03-09 10:21:19 +00:00
async function ensureMinimumBlockHeight(height) {
while ((await minedBlockNumber()) < height) {
await mineBlock()
}
}
async function currentTime() {
let block = await ethers.provider.getBlock("latest")
return block.timestamp
}
async function advanceTime(seconds) {
ethers.provider.send("evm_increaseTime", [seconds])
await mineBlock()
}
async function advanceTimeTo(timestamp) {
if ((await currentTime()) !== timestamp) {
ethers.provider.send("evm_setNextBlockTimestamp", [timestamp])
await mineBlock()
}
}
module.exports = {
snapshot,
revert,
mineBlock,
minedBlockNumber,
2022-03-09 10:21:19 +00:00
ensureMinimumBlockHeight,
currentTime,
advanceTime,
advanceTimeTo,
}