From b44796c93cb21af2992a2b68fd61db5b8b6cb54f Mon Sep 17 00:00:00 2001 From: imsk17 Date: Wed, 4 Sep 2024 13:11:55 +0530 Subject: [PATCH] feat(evm::listener): if synced, wait for some time before checking again --- src/handler/evm/utils/listenForLockEvents.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/handler/evm/utils/listenForLockEvents.ts b/src/handler/evm/utils/listenForLockEvents.ts index 93f4d29..8337210 100644 --- a/src/handler/evm/utils/listenForLockEvents.ts +++ b/src/handler/evm/utils/listenForLockEvents.ts @@ -1,3 +1,4 @@ +import { setTimeout } from "node:timers/promises"; import type { EntityManager } from "@mikro-orm/sqlite"; import type { JsonRpcProvider } from "ethers"; import type { EventBuilder } from "../.."; @@ -27,6 +28,10 @@ const listenForLockEvents = ( lastBlock + blockChunks < latestBlockNumber ? lastBlock + blockChunks : latestBlockNumber; + if (lastBlock >= latestBlock) { + await setTimeout(2000); // Sleep for 2 seconds + continue; + } const logs = await provider.getLogs({ fromBlock: lastBlock, @@ -48,7 +53,7 @@ const listenForLockEvents = ( lastBlock: lastBlock, }); await em.flush(); - await new Promise((e) => setTimeout(e, 10000)); + await setTimeout(10000); continue; } for (const log of logs.filter( @@ -92,7 +97,7 @@ const listenForLockEvents = ( `${e} while listening for lock events. Sleeping for 10 seconds`, chainIdent, ); - await new Promise((e) => setTimeout(e, 10000)); + await setTimeout(10000); } } };