|
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { Cron } from '@nestjs/schedule'; |
| 3 | +import { createPublicClient, http, type Hex } from 'viem'; |
| 4 | +import { PrismaService } from '@/database/prisma.service'; |
| 5 | +import { TransactionExecutorService } from './transaction-executor.service'; |
| 6 | +import { TxStatus, getChainById } from '@polypay/shared'; |
| 7 | + |
| 8 | +@Injectable() |
| 9 | +export class TransactionReconcilerScheduler { |
| 10 | + private readonly logger = new Logger(TransactionReconcilerScheduler.name); |
| 11 | + private readonly publicClients = new Map<number, any>(); |
| 12 | + |
| 13 | + constructor( |
| 14 | + private readonly prisma: PrismaService, |
| 15 | + private readonly transactionExecutor: TransactionExecutorService, |
| 16 | + ) {} |
| 17 | + |
| 18 | + private getPublicClient(chainId: number) { |
| 19 | + let client = this.publicClients.get(chainId); |
| 20 | + if (!client) { |
| 21 | + const chain = getChainById(chainId); |
| 22 | + client = createPublicClient({ chain, transport: http() }); |
| 23 | + this.publicClients.set(chainId, client); |
| 24 | + } |
| 25 | + return client; |
| 26 | + } |
| 27 | + |
| 28 | + // 13:00 Vietnam time = 06:00 UTC |
| 29 | + @Cron('0 6 * * *', { timeZone: 'UTC' }) |
| 30 | + async reconcileStuckTransactions() { |
| 31 | + this.logger.log('Running daily transaction reconciliation'); |
| 32 | + |
| 33 | + const stuckTxs = await this.prisma.transaction.findMany({ |
| 34 | + where: { |
| 35 | + txHash: { not: null }, |
| 36 | + status: { in: [TxStatus.EXECUTING, TxStatus.PENDING] }, |
| 37 | + }, |
| 38 | + include: { account: true }, |
| 39 | + }); |
| 40 | + |
| 41 | + if (stuckTxs.length === 0) { |
| 42 | + this.logger.log('No stuck transactions found'); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + this.logger.log(`Found ${stuckTxs.length} stuck transactions with txHash`); |
| 47 | + |
| 48 | + let reconciled = 0; |
| 49 | + let reverted = 0; |
| 50 | + let skipped = 0; |
| 51 | + |
| 52 | + for (const tx of stuckTxs) { |
| 53 | + try { |
| 54 | + const publicClient = this.getPublicClient(tx.account.chainId); |
| 55 | + const receipt = await publicClient.getTransactionReceipt({ |
| 56 | + hash: tx.txHash as Hex, |
| 57 | + }); |
| 58 | + |
| 59 | + if (receipt.status === 'success') { |
| 60 | + await this.transactionExecutor.markExecuted(tx.txId, tx.txHash); |
| 61 | + this.logger.log( |
| 62 | + `txId ${tx.txId} reconciled to EXECUTED (txHash: ${tx.txHash})`, |
| 63 | + ); |
| 64 | + reconciled++; |
| 65 | + } else { |
| 66 | + // receipt.status === 'reverted' |
| 67 | + await this.prisma.transaction.update({ |
| 68 | + where: { txId: tx.txId }, |
| 69 | + data: { status: TxStatus.PENDING, txHash: null }, |
| 70 | + }); |
| 71 | + this.logger.warn( |
| 72 | + `txId ${tx.txId} reverted on-chain, reset to PENDING`, |
| 73 | + ); |
| 74 | + reverted++; |
| 75 | + } |
| 76 | + } catch (error) { |
| 77 | + // No receipt found (tx not mined or invalid hash) — skip |
| 78 | + this.logger.warn( |
| 79 | + `txId ${tx.txId} receipt not found, skipping: ${error.message}`, |
| 80 | + ); |
| 81 | + skipped++; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + this.logger.log( |
| 86 | + `Reconciliation complete: ${reconciled} executed, ${reverted} reverted, ${skipped} skipped`, |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments