import { BigNumber } from '@0x/asset-swapper' ; import { Web3Wrapper } from '@0x/web3-wrapper' ; import { BlockParamLiteral } from 'ethereum-types' ; import { RFQM_TX_GAS_ESTIMATE } from '../constants' ; import { logger } from '../logger' ; const MIN_NUM_TRADES_FOR_HEALTHCHECK = 3 ; param wrapper param accountAddress param accountBalance param gasPriceBaseUnits returns export async function isWorkerReadyAndAbleAsync ( wrapper , accountAddress , accountBalance , gasPriceBaseUnits ) { const minimumCostToTrade = gasPriceBaseUnits . times ( RFQM_TX_GAS_ESTIMATE ) . times ( MIN_NUM_TRADES_FOR_HEALTHCHECK ) ; const hasEnoughBalance = accountBalance . gte ( minimumCostToTrade ) ; if ( ! hasEnoughBalance ) { logger . error ( { accountAddress , accountBalance : accountBalance . toString ( ) , minimumCostToTrade : minimumCostToTrade . toString ( ) , gasPriceBaseUnits : gasPriceBaseUnits . toString ( ) , } , 'Worker does not have enough balance to trade.' ) ; } if ( ! hasEnoughBalance ) { return false ; } const lastNonceOnChain = await wrapper . getAccountNonceAsync ( accountAddress ) ; const lastNoncePending = await wrapper . getAccountNonceAsync ( accountAddress , BlockParamLiteral . Pending ) ; const hasNoPendingTransactions = lastNonceOnChain . toString ( ) === lastNoncePending . toString ( ) ; if ( ! hasNoPendingTransactions ) { logger . error ( { accountAddress , lastNonceOnChain : lastNonceOnChain . toString ( ) , lastNoncePending : lastNoncePending . toString ( ) , } , 'Worker has pending transactions and cannot trade.' ) ; } return hasNoPendingTransactions ; }