import { providers } from "ethers" ; export class Blockchain { public _provider : providers . Web3Provider | providers . JsonRpcProvider ; private _snapshotId : number ; constructor ( _provider ) { this . _provider = _provider ; this . _snapshotId = 0 ; } public async saveSnapshotAsync ( ) { const response = await this . sendJSONRpcRequestAsync ( "evm_snapshot" , [ ] ) ; this . _snapshotId = response ; return response ; } public async revertAsync ( ) { await this . sendJSONRpcRequestAsync ( "evm_revert" , [ this . _snapshotId ] ) ; } public async revertByIdAsync ( id ) { await this . sendJSONRpcRequestAsync ( "evm_revert" , [ id ] ) ; } public async resetAsync ( ) { await this . sendJSONRpcRequestAsync ( "evm_revert" , [ "0x1" ] ) ; } public async increaseTimeAsync ( duration ) { await this . sendJSONRpcRequestAsync ( "evm_increaseTime" , [ duration ] ) ; } public async getCurrentTimestamp ( ) { return await this . _provider . getBlock ( await this . _provider . getBlockNumber ( ) ) ; } public async setNextBlockTimestamp ( timestamp ) { await this . sendJSONRpcRequestAsync ( "evm_setNextBlockTimestamp" , [ timestamp ] ) ; } public async waitBlocksAsync ( count ) { for ( let i = 0 ; i < count ; i ++ ) { await this . sendJSONRpcRequestAsync ( "evm_mine" , [ ] ) ; } } private async sendJSONRpcRequestAsync ( method , params ) { return this . _provider . send ( method , params ) ; } }