import { providers } from "ethers" ; import { assertExists } from '../../utils' ; import { RunHandlersOnBlock } from '../../utils/run.handlers.on.block' ; export type RunLive = ( ) => Promise < void > ; export function provideRunLive ( ethersProvider , runHandlersOnBlock , setInterval ) { assertExists ( ethersProvider , 'ethersProvider' ) ; assertExists ( runHandlersOnBlock , 'runHandlersOnBlock' ) ; assertExists ( setInterval , 'setInterval' ) ; return async function runLive ( ) { console . log ( 'listening for blockchain data...' ) ; let latestBlockNumber = await ethersProvider . getBlockNumber ( ) ; await runHandlersOnBlock ( latestBlockNumber ) ; setInterval ( async ( ) => { let currBlockNumber = latestBlockNumber ; latestBlockNumber = await ethersProvider . getBlockNumber ( ) ; const endBlockNumber = latestBlockNumber ; while ( currBlockNumber < endBlockNumber ) { currBlockNumber ++ ; await runHandlersOnBlock ( currBlockNumber ) ; } } , 15000 ) ; } ; }