import { promisify } from "es6-promisify" ; import { BlockWithoutTransactionData , FilterResult } from "web3" ; import { Address , fnVoid , Hash } from "./commonTypes" ; import { Utils , Web3 } from "./utils" ; /**
 * Utils not meant to be exported to the public
 */ export class UtilsInternal { public static sleep ( milliseconds ) { return new Promise ( ( resolve ) : any => setTimeout ( resolve , milliseconds ) ) ; } public static ensureArray < T > ( arr ) { if ( ! Array . isArray ( arr ) ) { arr = [ arr ] ; } return arr ; } /**
     * Returns the last mined block in the chain.
     */ public static async lastBlock ( ) { const web3 = await Utils . getWeb3 ( ) ; return promisify ( ( callback ) : any => web3 . eth . getBlock ( "latest" , callback ) ) ( ) as any ; } /**
     * Returns the date of the last mined block in the chain.
     */ public static async lastBlockDate ( ) { const web3 = await Utils . getWeb3 ( ) ; let block ; do { block = await promisify ( ( callback ) : any => web3 . eth . getBlock ( "latest" , callback ) ) ( ) as BlockWithoutTransactionData ; } while ( ! block ) ; return new Date ( block . timestamp * 1000 ) ; } /**
     * Returns the last mined block in the chain.
     */ public static async lastBlockNumber ( ) { const web3 = await Utils . getWeb3 ( ) ; return promisify ( web3 . eth . getBlockNumber ) ( ) ; } param filter public static stopWatchingAsync ( filter ) { return promisify ( ( callback ) : any => filter . stopWatching ( callback ) ) ( ) ; } public static getRandomNumber ( ) { return Math . floor ( Math . random ( ) * Number . MAX_SAFE_INTEGER ) ; } public static getWeb3Sync ( ) { return ( Utils as any ) . web3 ; } public static isNullAddress ( address ) { return ! address || ! Number . parseInt ( address , 16 ) ; } public static isNullHash ( hash ) { return ! hash || ! Number . parseInt ( hash , 16 ) ; } /**
     * Returns promise of the maximum gasLimit that we dare to ever use, given the
     * current state of the chain.
     */ public static async computeMaxGasLimit ( ) { const web3 = await Utils . getWeb3 ( ) ; return promisify ( ( callback ) => web3 . eth . getBlock ( "latest" , false , callback ) ) ( ) . then ( ( block ) => { return block . gasLimit - 100000 ; } ) ; } }