Skip to content

Commit

Permalink
chore(db): recheck the db and retry the failed data
Browse files Browse the repository at this point in the history
  • Loading branch information
D4mph1r committed Sep 18, 2024
1 parent ca3dc24 commit 2ff1bee
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/handler/evm/utils/nftData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const nftData = (provider: JsonRpcProvider, logger: LogInstance) => {
logger,
5,
).catch(() => {
logger.warn("retry royalty catch");
return undefined;
});

Expand Down
25 changes: 24 additions & 1 deletion src/handler/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -333,6 +355,7 @@ export function eventBuilder(em: EntityManager) {
sourceChain,
transactionHash,
metaDataUri,
listenerChain,
};
},
};
Expand Down
37 changes: 37 additions & 0 deletions src/handler/poller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/handler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface TStakingHandler {
}

export type LockEvent = {
listenerChain: string;
tokenId: string;
destinationChain: string;
destinationUserAddress: string;
Expand Down
1 change: 1 addition & 0 deletions src/handler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function retry<T>(
// Use a Promise-based delay
if (count) {
count = count - 1;
console.log(`Retry count: ${count}`);
if (count <= 0) {
throw new Error(`Failed ${ctx}`);
}
Expand Down
3 changes: 3 additions & 0 deletions src/persistence/entities/locked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export class LockedEvent {
@Property({ default: "" })
metaDataUri!: string;

@Property({ default: false })
status!: boolean;

constructor(
tokenId: string,
destinationChain: string,
Expand Down

0 comments on commit 2ff1bee

Please sign in to comment.