From 2ff1bee31cb25a973b5fe0a6bb82106aa2884d5d Mon Sep 17 00:00:00 2001 From: D Date: Wed, 18 Sep 2024 12:04:07 +0500 Subject: [PATCH] chore(db): recheck the db and retry the failed data --- src/config.ts | 2 +- src/handler/evm/utils/nftData.ts | 1 + src/handler/index.ts | 25 +++++++++++++++++++- src/handler/poller/index.ts | 37 ++++++++++++++++++++++++++++++ src/handler/types.ts | 1 + src/handler/utils.ts | 1 + src/persistence/entities/locked.ts | 3 +++ 7 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index 0c768d0..e4e0164 100644 --- a/src/config.ts +++ b/src/config.ts @@ -145,7 +145,7 @@ export const prodBridgeConfig: IBridgeConfig = { }, { chain: "MATIC", - rpcURL: "https://polygon.llamarpc.com", + rpcURL: "https://polygon-bor-rpc.publicnode.com", nativeCoinSymbol: "MATIC", intialFund: "50000000000000000", contractAddress: "0x2011DAD5caE280243d075D12a38CcCc0Fb4412dE", diff --git a/src/handler/evm/utils/nftData.ts b/src/handler/evm/utils/nftData.ts index b63a6e1..9cc8b0d 100644 --- a/src/handler/evm/utils/nftData.ts +++ b/src/handler/evm/utils/nftData.ts @@ -30,6 +30,7 @@ const nftData = (provider: JsonRpcProvider, logger: LogInstance) => { logger, 5, ).catch(() => { + logger.warn("retry royalty catch"); return undefined; }); diff --git a/src/handler/index.ts b/src/handler/index.ts index 1e0b054..f55dae1 100644 --- a/src/handler/index.ts +++ b/src/handler/index.ts @@ -1,5 +1,5 @@ import { setTimeout } from "node:timers/promises"; -import type { EntityManager } from "@mikro-orm/sqlite"; +import { type EntityManager, wrap } from "@mikro-orm/sqlite"; import { Mutex, type MutexInterface } from "async-mutex"; import type { AxiosInstance } from "axios"; import axios from "axios"; @@ -124,6 +124,17 @@ export async function listenEvents( log.warn( `Signature already processed for ${inft.transactionHash} on ${sourceChain.chainIdent}`, ); + const found = await em.findOne(LockedEvent, { + transactionHash: ev.transactionHash, + listenerChain: ev.listenerChain, + }); + if (found) { + const to_save = wrap(found).assign({ + status: true, + }); + + await em.persistAndFlush(to_save); + } return; } const approvalFn = async () => { @@ -178,6 +189,17 @@ export async function listenEvents( log.info( `Approved and Signed Data for ${inft.transactionHash} on ${sourceChain.chainIdent} at TX: ${approved?.hash}`, ); + const found = await em.findOne(LockedEvent, { + transactionHash: ev.transactionHash, + listenerChain: ev.listenerChain, + }); + if (found) { + const to_save = wrap(found).assign({ + status: true, + }); + + await em.persistAndFlush(to_save); + } } async function poolEvents(chain: THandler) { @@ -333,6 +355,7 @@ export function eventBuilder(em: EntityManager) { sourceChain, transactionHash, metaDataUri, + listenerChain, }; }, }; diff --git a/src/handler/poller/index.ts b/src/handler/poller/index.ts index cda5f4e..cd88eae 100644 --- a/src/handler/poller/index.ts +++ b/src/handler/poller/index.ts @@ -27,6 +27,43 @@ export default async function pollForLockEvents( }) .getSingleResult(); + const failedData = await em + .createQueryBuilder(LockedEvent) + .select("*") + .where({ + listenerChain: identifier, + status: false, + }) + .orderBy({ + id: "desc", + }); + + for (const tx of failedData) { + try { + await cb( + await builder.nftLocked( + tx.tokenId.toString(), + tx.destinationChain, + tx.destinationUserAddress, + tx.sourceNftContractAddress, + tx.tokenAmount.toString(), + tx.nftType, + tx.sourceChain, + tx.transactionHash, + tx.listenerChain, + tx.metaDataUri, + tx.id, + ), + ); + } catch (e) { + logger.error( + identifier, + `${e} while polling for events. Sleeping for 10 seconds`, + ); + await setTimeout(10000); + } + } + let lastId = lastEv?.id ?? 0; if (lastId) { lastId += 1; diff --git a/src/handler/types.ts b/src/handler/types.ts index 2a48c5c..d88e466 100644 --- a/src/handler/types.ts +++ b/src/handler/types.ts @@ -63,6 +63,7 @@ export interface TStakingHandler { } export type LockEvent = { + listenerChain: string; tokenId: string; destinationChain: string; destinationUserAddress: string; diff --git a/src/handler/utils.ts b/src/handler/utils.ts index dd9186a..6602d41 100644 --- a/src/handler/utils.ts +++ b/src/handler/utils.ts @@ -57,6 +57,7 @@ export async function retry( // Use a Promise-based delay if (count) { count = count - 1; + console.log(`Retry count: ${count}`); if (count <= 0) { throw new Error(`Failed ${ctx}`); } diff --git a/src/persistence/entities/locked.ts b/src/persistence/entities/locked.ts index 146988a..6afadd4 100644 --- a/src/persistence/entities/locked.ts +++ b/src/persistence/entities/locked.ts @@ -40,6 +40,9 @@ export class LockedEvent { @Property({ default: "" }) metaDataUri!: string; + @Property({ default: false }) + status!: boolean; + constructor( tokenId: string, destinationChain: string,