import { Block } from '@celo/connect' ; import Web3 from 'web3' ; import { failWith } from './cli' ; export function enumEntriesDupWithLowercase < T > ( entries ) { const enumMap = { } ; entries . forEach ( ( value ) => { enumMap [ value [ 0 ] ] = value [ 1 ] ; enumMap [ value [ 0 ] . toLowerCase ( ) ] = value [ 1 ] ; } ) ; return enumMap ; } export async function nodeIsSynced ( web3 ) { if ( process . env . NO_SYNCCHECK ) { return true ; } try { const syncProgress = await web3 . eth . isSyncing ( ) ; if ( typeof syncProgress === 'boolean' && ! syncProgress ) { const latestBlock = await web3 . eth . getBlock ( 'latest' ) ; if ( latestBlock && latestBlock . number > 0 ) { const ageOfBlock = Date . now ( ) / 1000 - Number ( latestBlock . timestamp ) ; if ( ageOfBlock > 120 ) { console . log ( `Latest block is ${ ageOfBlock } seconds old, and syncing is not currently in progress` ) ; console . log ( 'To disable this check, set the NO_SYNCCHECK environment variable' ) ; return false ; } else { return true ; } } } return false ; } catch ( error ) { console . log ( "An error occurred while trying to reach the node. Perhaps your node isn't running?" ) ; return false ; } } export async function requireNodeIsSynced ( web3 ) { if ( ! ( await nodeIsSynced ( web3 ) ) ) { failWith ( 'Node is not currently synced. Run node:synced to check its status.' ) ; } }