Add EVM snapshots including time for tests

This commit is contained in:
Mark Spanbroek 2022-03-03 11:59:01 +01:00 committed by markspanbroek
parent ee4683e50c
commit 22e8ea50e2
4 changed files with 44 additions and 13 deletions

View File

@ -1,6 +1,6 @@
const { expect } = require("chai")
const { ethers } = require("hardhat")
const { mineBlock, minedBlockNumber } = require("./mining")
const { mineBlock, minedBlockNumber } = require("./evm")
describe("Proofs", function () {
const id = ethers.utils.randomBytes(32)

View File

@ -1,7 +1,7 @@
const { expect } = require("chai")
const { ethers, deployments } = require("hardhat")
const { exampleRequest, exampleOffer } = require("./examples")
const { mineBlock, minedBlockNumber } = require("./mining")
const { mineBlock, minedBlockNumber } = require("./evm")
const { requestId, offerId } = require("./ids")
describe("Storage", function () {

42
test/evm.js Normal file
View File

@ -0,0 +1,42 @@
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()
}
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()
}
module.exports = {
snapshot,
revert,
mineBlock,
minedBlockNumber,
currentTime,
advanceTime,
}

View File

@ -1,11 +0,0 @@
const { ethers } = require("hardhat")
async function mineBlock() {
await ethers.provider.send("evm_mine")
}
async function minedBlockNumber() {
return await ethers.provider.getBlockNumber()
}
module.exports = { mineBlock, minedBlockNumber }