import ora from 'ora' ; import Web3 from 'web3' ; import { TransactionReceipt } from 'web3/types' ; import { sleep } from './utils' ; export const awaitTransactionMined = async ( networkUrl , txHash , confirmationBlocks ) : Promise < TransactionReceipt > => { const web3 = new Web3 ( networkUrl ) ; const spinner = ora ( 'Waiting for transaction to be mined' ) . start ( ) ; let receipt = null ; while ( true ) { receipt = await web3 . eth . getTransactionReceipt ( txHash ) ; if ( receipt ) { break ; } await sleep ( 1000 ) ; } while ( true ) { const blockNumber = await web3 . eth . getBlockNumber ( ) ; const blocksSinceMined = blockNumber - receipt . blockNumber ; if ( blocksSinceMined >= 0 ) { spinner . text = `${ blocksSinceMined }/${ confirmationBlocks } confirmations` ; if ( blocksSinceMined >= confirmationBlocks ) { break ; } } await sleep ( 1000 ) ; } spinner . stop ( ) ; return receipt ; } ;