From dba16bcb9ebb557be4d4097b04858829155b79ed Mon Sep 17 00:00:00 2001 From: imsk17 Date: Fri, 30 Aug 2024 14:08:29 +0530 Subject: [PATCH] feat(mx::listener): implement variable wait time depending on gap blocks --- .../multiversx/utils/listenForLockEvents.ts | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/handler/multiversx/utils/listenForLockEvents.ts b/src/handler/multiversx/utils/listenForLockEvents.ts index f05603c..7ed632e 100644 --- a/src/handler/multiversx/utils/listenForLockEvents.ts +++ b/src/handler/multiversx/utils/listenForLockEvents.ts @@ -19,6 +19,22 @@ import type { Root } from "../types/gateway"; const CHAIN_IDENT = "MULTIVERSX"; const WAIT_TIME = 10000; +function generateWaitTime(num1: number, num2: number) { + // Calculate the absolute difference between the two numbers + const difference = Math.abs(num1 - num2); + + // Define the maximum difference we want to consider + const maxDifference = 100; + + // Calculate the score (inverse relationship with the difference) + // Scale from 1 to 10 instead of 0 to 10 + const score = + 7.9 * (1 - Math.min(difference, maxDifference) / maxDifference) + 2; + + // Round the score to one decimal place + return Math.round(score * 10) / 10; +} + export default async function listenForLockEvents( builder: EventBuilder, cb: LockEventIter, @@ -65,13 +81,15 @@ export default async function listenForLockEvents( ); if (!txsForBridge.length) { - logger.trace(`No TX Since: ${lastBlock_}. Awaiting 10s`); const lastestStatus = await provider.getNetworkStatus(); const lastNonce = lastestStatus.HighestFinalNonce; + const wt = generateWaitTime(lastBlock_, lastNonce); + logger.trace( + `No TX Since: ${lastBlock_}. Awaiting ${Math.round(wt)}s`, + ); lastBlock_ = lastBlock_ + 1; - if (lastBlock >= lastNonce) { - // Sleep for 2 minutes - await setTimeout(2 * 60 * 1000); + if (lastBlock_ >= lastNonce) { + await setTimeout(30 * 1000); // 30 seconds continue; } await em.upsert(Block, { @@ -80,7 +98,7 @@ export default async function listenForLockEvents( lastBlock: Number(lastBlock_), }); await em.flush(); - await setTimeout(WAIT_TIME); + await setTimeout(wt * 1000); continue; } logger.trace(`Found ${txsForBridge.length} TXs in ${lastBlock_ - 1}`);