UNPKG

@dydxfoundation/governance

Version:
81 lines (80 loc) 3.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.incrementTimeToTimestamp = exports.increaseTimeAndMine = exports.increaseTime = exports.advanceBlockTo = exports.advanceBlock = exports.latestBlock = exports.latestBlockTimestamp = exports.loadSnapshot = exports.saveSnapshot = exports.evmReset = exports.evmSnapshot = void 0; const ethers_1 = require("ethers"); const hre_1 = __importDefault(require("../hre")); async function evmSnapshot() { return hre_1.default.ethers.provider.send('evm_snapshot', []); } exports.evmSnapshot = evmSnapshot; async function evmReset(id) { await hre_1.default.ethers.provider.send('evm_revert', [id]); } exports.evmReset = evmReset; async function saveSnapshot(snapshots, label, contract) { snapshots.set(label, await evmSnapshot()); contract === null || contract === void 0 ? void 0 : contract.saveSnapshot(label); } exports.saveSnapshot = saveSnapshot; async function loadSnapshot(snapshots, label, contract) { const snapshot = snapshots.get(label); if (!snapshot) { throw new Error(`Cannot load since snapshot has not been saved: ${label}`); } await evmReset(snapshot); snapshots.set(label, await evmSnapshot()); contract === null || contract === void 0 ? void 0 : contract.loadSnapshot(label); } exports.loadSnapshot = loadSnapshot; async function latestBlockTimestamp() { const block = await hre_1.default.ethers.provider.getBlock('latest'); return block.timestamp; } exports.latestBlockTimestamp = latestBlockTimestamp; async function latestBlock() { const block = await hre_1.default.ethers.provider.getBlock('latest'); return block.number; } exports.latestBlock = latestBlock; async function advanceBlock(timestamp) { return hre_1.default.ethers.provider.send('evm_mine', timestamp ? [timestamp] : []); } exports.advanceBlock = advanceBlock; async function advanceBlockTo(target) { const currentBlock = await latestBlock(); const start = Date.now(); if (target < currentBlock) { throw Error(`Target block #(${target}) is lower than current block #(${currentBlock})`); } let notified = false; while ((await latestBlock()) < target) { if (!notified && Date.now() - start >= 5000) { notified = true; console.log('advanceBlockTo: Advancing too many blocks is causing this test to be slow.'); } await advanceBlock(); } } exports.advanceBlockTo = advanceBlockTo; async function increaseTime(secondsToIncrease) { await hre_1.default.ethers.provider.send('evm_increaseTime', [secondsToIncrease]); } exports.increaseTime = increaseTime; async function increaseTimeAndMine(secondsToIncrease) { await hre_1.default.ethers.provider.send('evm_increaseTime', [secondsToIncrease]); await hre_1.default.ethers.provider.send('evm_mine', []); } exports.increaseTimeAndMine = increaseTimeAndMine; async function incrementTimeToTimestamp(timestamp) { const latestTimestamp = await latestBlockTimestamp(); const timestampBN = ethers_1.BigNumber.from(timestamp); if (latestTimestamp > timestampBN.toNumber()) { throw new Error('incrementTimeToTimestamp: Cannot move backwards in time'); } const timestampDiff = timestampBN.sub(latestTimestamp).toNumber(); await increaseTimeAndMine(timestampDiff); } exports.incrementTimeToTimestamp = incrementTimeToTimestamp;