import chai from "chai" ; import { solidity } from "ethereum-waffle" ; chai . use ( solidity ) ; import { ethers } from "hardhat" ; import { BigNumber , providers } from "ethers" ; import { Blockchain } from "../common" ; const provider = ethers . provider ; export const getProvider = ( ) : providers . JsonRpcProvider => { return ethers . provider ; } ; export const getWaffleExpect = ( ) : Chai . ExpectStatic => { return chai . expect ; } ; export const addSnapshotBeforeRestoreAfterEach = ( ) => { const blockchain = new Blockchain ( provider ) ; beforeEach ( async ( ) => { await blockchain . saveSnapshotAsync ( ) ; } ) ; afterEach ( async ( ) => { await blockchain . revertAsync ( ) ; } ) ; } ; const SNAPSHOTS = [ ] ; export function cacheBeforeEach ( initializer ) { let initialized = false ; const blockchain = new Blockchain ( provider ) ; beforeEach ( async function ( ) { if ( ! initialized ) { await initializer . call ( this ) ; SNAPSHOTS . push ( await blockchain . saveSnapshotAsync ( ) ) ; initialized = true ; } else { const snapshotId = SNAPSHOTS . pop ( ) ! ; await blockchain . revertByIdAsync ( snapshotId ) ; SNAPSHOTS . push ( await blockchain . saveSnapshotAsync ( ) ) ; } } ) ; after ( async function ( ) { if ( initialized ) { SNAPSHOTS . pop ( ) ; } } ) ; } export async function getTransactionTimestamp ( asyncTxn ) { const txData = await asyncTxn ; return BigNumber . from ( ( await provider . getBlock ( txData . block ) ) . timestamp ) ; } export async function getLastBlockTimestamp ( ) { return BigNumber . from ( ( await provider . getBlock ( "latest" ) ) . timestamp ) ; } export async function mineBlockAsync ( ) { await sendJSONRpcRequestAsync ( "evm_mine" , [ ] ) ; } export async function increaseTimeAsync ( duration ) { await sendJSONRpcRequestAsync ( "evm_increaseTime" , [ duration . toNumber ( ) ] ) ; await mineBlockAsync ( ) ; } async function sendJSONRpcRequestAsync ( method , params ) { return provider . send ( method , params ) ; }