|
| 1 | +import { Static, Type } from "@sinclair/typebox"; |
| 2 | +import { FastifyInstance } from "fastify"; |
| 3 | +import { StatusCodes } from "http-status-codes"; |
| 4 | +import { eth_getTransactionReceipt, getRpcClient } from "thirdweb"; |
| 5 | +import { TransactionDB } from "../../../db/transactions/db"; |
| 6 | +import { getChain } from "../../../utils/chain"; |
| 7 | +import { thirdwebClient } from "../../../utils/sdk"; |
| 8 | +import { SendTransactionQueue } from "../../../worker/queues/sendTransactionQueue"; |
| 9 | +import { createCustomError } from "../../middleware/error"; |
| 10 | +import { standardResponseSchema } from "../../schemas/sharedApiSchemas"; |
| 11 | + |
| 12 | +const requestBodySchema = Type.Object({ |
| 13 | + queueId: Type.String({ |
| 14 | + description: "Transaction queue ID", |
| 15 | + examples: ["9eb88b00-f04f-409b-9df7-7dcc9003bc35"], |
| 16 | + }), |
| 17 | +}); |
| 18 | + |
| 19 | +export const responseBodySchema = Type.Object({ |
| 20 | + result: Type.Object({ |
| 21 | + message: Type.String(), |
| 22 | + status: Type.String(), |
| 23 | + }), |
| 24 | +}); |
| 25 | + |
| 26 | +responseBodySchema.example = { |
| 27 | + result: { |
| 28 | + message: |
| 29 | + "Transaction queued for retry with queueId: a20ed4ce-301d-4251-a7af-86bd88f6c015", |
| 30 | + status: "success", |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +export async function retryFailedTransaction(fastify: FastifyInstance) { |
| 35 | + fastify.route<{ |
| 36 | + Body: Static<typeof requestBodySchema>; |
| 37 | + Reply: Static<typeof responseBodySchema>; |
| 38 | + }>({ |
| 39 | + method: "POST", |
| 40 | + url: "/transaction/retry-failed", |
| 41 | + schema: { |
| 42 | + summary: "Retry failed transaction", |
| 43 | + description: "Retry a failed transaction", |
| 44 | + tags: ["Transaction"], |
| 45 | + operationId: "retryFailed", |
| 46 | + body: requestBodySchema, |
| 47 | + response: { |
| 48 | + ...standardResponseSchema, |
| 49 | + [StatusCodes.OK]: responseBodySchema, |
| 50 | + }, |
| 51 | + }, |
| 52 | + handler: async (request, reply) => { |
| 53 | + const { queueId } = request.body; |
| 54 | + |
| 55 | + const transaction = await TransactionDB.get(queueId); |
| 56 | + if (!transaction) { |
| 57 | + throw createCustomError( |
| 58 | + "Transaction not found.", |
| 59 | + StatusCodes.BAD_REQUEST, |
| 60 | + "TRANSACTION_NOT_FOUND", |
| 61 | + ); |
| 62 | + } |
| 63 | + if (transaction.status !== "errored") { |
| 64 | + throw createCustomError( |
| 65 | + `Transaction cannot be retried because status: ${transaction.status}`, |
| 66 | + StatusCodes.BAD_REQUEST, |
| 67 | + "TRANSACTION_CANNOT_BE_RETRIED", |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + // temp do not handle userop |
| 72 | + if (transaction.isUserOp) { |
| 73 | + throw createCustomError( |
| 74 | + `Transaction cannot be retried because it is a userop`, |
| 75 | + StatusCodes.BAD_REQUEST, |
| 76 | + "TRANSACTION_CANNOT_BE_RETRIED", |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + const rpcRequest = getRpcClient({ |
| 81 | + client: thirdwebClient, |
| 82 | + chain: await getChain(transaction.chainId), |
| 83 | + }); |
| 84 | + |
| 85 | + // if transaction has sentTransactionHashes, we need to check if any of them are mined |
| 86 | + if ("sentTransactionHashes" in transaction) { |
| 87 | + const receiptPromises = transaction.sentTransactionHashes.map( |
| 88 | + (hash) => { |
| 89 | + // if receipt is not found, it will throw an error |
| 90 | + // so we catch it and return null |
| 91 | + return eth_getTransactionReceipt(rpcRequest, { |
| 92 | + hash, |
| 93 | + }).catch(() => null); |
| 94 | + }, |
| 95 | + ); |
| 96 | + |
| 97 | + const receipts = await Promise.all(receiptPromises); |
| 98 | + |
| 99 | + // If any of the transactions are mined, we should not retry. |
| 100 | + const minedReceipt = receipts.find((receipt) => !!receipt); |
| 101 | + |
| 102 | + if (minedReceipt) { |
| 103 | + throw createCustomError( |
| 104 | + `Transaction cannot be retried because it has already been mined with hash: ${minedReceipt.transactionHash}`, |
| 105 | + StatusCodes.BAD_REQUEST, |
| 106 | + "TRANSACTION_CANNOT_BE_RETRIED", |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + const job = await SendTransactionQueue.q.getJob( |
| 112 | + SendTransactionQueue.jobId({ |
| 113 | + queueId: transaction.queueId, |
| 114 | + resendCount: 0, |
| 115 | + }), |
| 116 | + ); |
| 117 | + |
| 118 | + if (job) { |
| 119 | + await job.remove(); |
| 120 | + } |
| 121 | + |
| 122 | + await SendTransactionQueue.add({ |
| 123 | + queueId: transaction.queueId, |
| 124 | + resendCount: 0, |
| 125 | + }); |
| 126 | + |
| 127 | + reply.status(StatusCodes.OK).send({ |
| 128 | + result: { |
| 129 | + message: `Transaction queued for retry with queueId: ${queueId}`, |
| 130 | + status: "success", |
| 131 | + }, |
| 132 | + }); |
| 133 | + }, |
| 134 | + }); |
| 135 | +} |
0 commit comments