vault: allow automine to be disabled in time sensitive tests

This commit is contained in:
Mark Spanbroek 2025-01-27 16:06:00 +01:00
parent cf875eb0d7
commit ae160498ca
2 changed files with 21 additions and 3 deletions

View File

@ -1,7 +1,13 @@
const { expect } = require("chai")
const { ethers } = require("hardhat")
const { randomBytes } = ethers.utils
const { currentTime, advanceTimeToForNextBlock, mine } = require("./evm")
const {
currentTime,
advanceTimeToForNextBlock,
mine,
snapshot,
revert,
} = require("./evm")
describe("Vault", function () {
let token
@ -10,6 +16,7 @@ describe("Vault", function () {
let account, account2, account3
beforeEach(async function () {
await snapshot()
const TestToken = await ethers.getContractFactory("TestToken")
token = await TestToken.deploy()
const Vault = await ethers.getContractFactory("Vault")
@ -20,6 +27,10 @@ describe("Vault", function () {
await token.mint(account3.address, 1_000_000)
})
afterEach(async function () {
await revert()
})
describe("depositing", function () {
const context = randomBytes(32)
const amount = 42

View File

@ -5,13 +5,19 @@ let snapshots = []
async function snapshot() {
const id = await ethers.provider.send("evm_snapshot")
const time = await currentTime()
snapshots.push({ id, time })
const automine = await ethers.provider.send("hardhat_getAutomine")
snapshots.push({ id, time, automine })
}
async function revert() {
const { id, time } = snapshots.pop()
const { id, time, automine } = snapshots.pop()
await ethers.provider.send("evm_revert", [id])
await ethers.provider.send("evm_setNextBlockTimestamp", [time + 1])
await ethers.provider.send("evm_setAutomine", [automine])
}
async function setAutomine(enabled) {
await ethers.provider.send("evm_setAutomine", [enabled])
}
/**
@ -65,6 +71,7 @@ async function advanceTimeToForNextBlock(timestamp) {
module.exports = {
snapshot,
revert,
setAutomine,
mine,
ensureMinimumBlockHeight,
currentTime,