From 8b1d1852001f9a8ebfc64d966dbc51b657aad15d Mon Sep 17 00:00:00 2001 From: lorcan-codes <126797224+lorcan-codes@users.noreply.github.com> Date: Mon, 12 Feb 2024 19:00:27 +0500 Subject: [PATCH] Handle reverted transactions and delete old transactions --- indexer/src/indexer.ts | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/indexer/src/indexer.ts b/indexer/src/indexer.ts index 4822c41..45fed16 100644 --- a/indexer/src/indexer.ts +++ b/indexer/src/indexer.ts @@ -209,23 +209,39 @@ async function refreshMaterializedViews() { } const updateTransactions = async () => { - const transactionsToUpdate = await AppDataSource.manager.find(Transaction, { - where: [ - { status: "NOT_RECEIVED" }, - { status: "RECEIVED" }, - { status: "PENDING" }, - // { status: "ACCEPTED_ON_L2" }, - ], - }); + const deletedTransactions = await AppDataSource.manager.query(` + DELETE + FROM TRANSACTION t + WHERE + (SELECT 'txHash' + FROM event e + WHERE e."txHash" = t.hash + limit 1 + ) IS NOT NULL + OR + (t.status = 'REJECTED' AND t."createdAt" < (now() - interval '15 minutes')) + returning hash; + `) + + if(deletedTransactions?.[0]?.length > 0){ + logger.info( + deletedTransactions[0].map((t: any) => t.hash), + "Deleting transaction." + ); + } + + const transactionsToUpdate = await AppDataSource.manager.find(Transaction); + for (let transaction in transactionsToUpdate) { const tx = (await provider.getTransactionStatus( transactionsToUpdate[transaction].hash - )) as any as ReturnedTransactionStatus; - transactionsToUpdate[transaction].blockHash = tx.block_hash; - // @ts-ignore + )); + transactionsToUpdate[transaction].status = tx.finality_status; + if(tx.execution_status === 'REVERTED'){ + transactionsToUpdate[transaction].status = 'REJECTED'; + } transactionsToUpdate[transaction].updatedAt = new Date(); - transactionsToUpdate[transaction].errorContent = tx.tx_failure_reason; logger.info( { transactionHash: Number(transactionsToUpdate[transaction].hash),