import * as promisify from "tiny-promisify" ; import * as Web3 from "web3" ; export class Web3Utils { private web3 : Web3 ; constructor ( web3 ) { this . web3 = web3 ; } public async doesContractExistAtAddressAsync ( address ) { const code = await promisify ( this . web3 . eth . getCode ) ( address ) ; const codeIsEmpty = /^0x0{0,40}$/i . test ( code ) ; return ! codeIsEmpty ; } returns { Promise < number > } public async getLatestBlockTime ( ) { await this . mineBlock ( ) ; const latestBlock = await promisify ( this . web3 . eth . getBlock ) ( "latest" ) ; return latestBlock . timestamp ; } param { number } seconds returns { Promise < boolean > } public async increaseTime ( seconds ) { const increaseTimeResponse = await this . sendJsonRpcRequestAsync ( "evm_increaseTime" , [ seconds , ] ) ; const blockMineResponse = await this . mineBlock ( ) ; return ! increaseTimeResponse [ "error" ] && ! blockMineResponse [ "error" ] ; } returns { Promise < "web3" public async mineBlock ( ) { return this . sendJsonRpcRequestAsync ( "evm_mine" , [ ] ) ; } private async sendJsonRpcRequestAsync ( method , params ) { return promisify ( this . web3 . currentProvider . sendAsync , { context : this . web3 . currentProvider , } ) ( { jsonrpc : "2.0" , method , params , id : new Date ( ) . getTime ( ) , } ) ; } }