-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathstatus.ts
More file actions
234 lines (220 loc) · 6.83 KB
/
status.ts
File metadata and controls
234 lines (220 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { TransactionDB } from "../../../shared/db/transactions/db";
import { env } from "../../../shared/utils/env";
import { createCustomError } from "../../middleware/error";
import { standardResponseSchema } from "../../schemas/shared-api-schemas";
import {
TransactionSchema,
toTransactionSchema,
} from "../../schemas/transaction";
/**
* Creates a minimal transaction response from backfill data.
* Used when the transaction is not found in Redis but exists in the backfill table.
*/
const createBackfillResponse = (
queueId: string,
backfill: { status: "mined" | "errored"; transactionHash?: string },
): Static<typeof TransactionSchema> => {
const baseResponse: Static<typeof TransactionSchema> = {
queueId,
status: backfill.status,
chainId: null,
fromAddress: null,
toAddress: null,
data: null,
extension: null,
value: null,
nonce: null,
gasLimit: null,
gasPrice: null,
maxFeePerGas: null,
maxPriorityFeePerGas: null,
transactionType: null,
transactionHash: null,
queuedAt: null,
sentAt: null,
minedAt: null,
cancelledAt: null,
deployedContractAddress: null,
deployedContractType: null,
errorMessage: null,
sentAtBlockNumber: null,
blockNumber: null,
retryCount: 0,
retryGasValues: null,
retryMaxFeePerGas: null,
retryMaxPriorityFeePerGas: null,
signerAddress: null,
accountAddress: null,
accountSalt: null,
accountFactoryAddress: null,
target: null,
sender: null,
initCode: null,
callData: null,
callGasLimit: null,
verificationGasLimit: null,
preVerificationGas: null,
paymasterAndData: null,
userOpHash: null,
functionName: null,
functionArgs: null,
onChainTxStatus: null,
onchainStatus: null,
effectiveGasPrice: null,
cumulativeGasUsed: null,
batchOperations: null,
};
if (backfill.status === "mined" && backfill.transactionHash) {
return {
...baseResponse,
transactionHash: backfill.transactionHash,
onchainStatus: "success",
onChainTxStatus: 1,
};
}
return baseResponse;
};
// INPUT
const requestSchema = Type.Object({
queueId: Type.String({
description: "Transaction queue ID",
examples: ["9eb88b00-f04f-409b-9df7-7dcc9003bc35"],
}),
});
// OUTPUT
export const responseBodySchema = Type.Object({
result: TransactionSchema,
});
responseBodySchema.example = {
result: {
queueId: "a20ed4ce-301d-4251-a7af-86bd88f6c015",
walletAddress: "0x3ecdbf3b911d0e9052b64850693888b008e18373",
contractAddress: "0x365b83d67d5539c6583b9c0266a548926bf216f4",
chainId: "80001",
extension: "non-extension",
status: "mined",
encodedInputData:
"0xa9059cbb0000000000000000000000001946267d81fb8adeeea28e6b98bcd446c824847300000000000000000000000000000000000000000000000000000000000186a0",
txType: 2,
gasPrice: "1500000017",
gasLimit: "46512",
maxPriorityFeePerGas: "1500000000",
maxFeePerGas: "1500000034",
txHash:
"0x6de86da898fa4beb13d965c42bf331ad46cfa061cadf75f69791f31c9d8a4f66",
submittedTxNonce: 698,
createdTimestamp: "2023-08-25T22:42:26.910Z",
txProcessedTimestamp: "2023-08-25T22:42:27.302Z",
txSubmittedTimestamp: "2023-08-25T22:42:28.743Z",
deployedContractAddress: "",
contractType: "",
errorMessage: "",
txMinedTimestamp: "2023-08-25T22:42:33.000Z",
blockNumber: 39398545,
onChainTxStatus: 1,
},
};
export async function getTransactionStatusRoute(fastify: FastifyInstance) {
fastify.route<{
Params: Static<typeof requestSchema>;
Reply: Static<typeof responseBodySchema>;
}>({
method: "GET",
url: "/transaction/status/:queueId",
schema: {
summary: "Get transaction status",
description: "Get the status for a transaction request.",
tags: ["Transaction"],
operationId: "status",
params: requestSchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: responseBodySchema,
},
},
handler: async (request, reply) => {
const { queueId } = request.params;
// SPECIAL LOGIC FOR AMEX
// Backfill table takes priority — entries are intentional overrides for
// queue IDs that are stuck in Redis (e.g. orphaned "queued" transactions).
// See https://github.com/thirdweb-dev/solutions-customer-scripts/blob/main/amex/scripts/load-backfill-via-api.ts
if (env.ENABLE_TX_BACKFILL_FALLBACK) {
const backfill = await TransactionDB.getBackfill(queueId);
if (backfill) {
return reply.status(StatusCodes.OK).send({
result: createBackfillResponse(queueId, backfill),
});
}
}
const transaction = await TransactionDB.get(queueId);
if (!transaction) {
throw createCustomError(
"Transaction not found.",
StatusCodes.BAD_REQUEST,
"TRANSACTION_NOT_FOUND",
);
}
reply.status(StatusCodes.OK).send({
result: toTransactionSchema(transaction),
});
},
});
}
// An alterate route that accepts the queueId as a query param.
export async function getTransactionStatusQueryParamRoute(
fastify: FastifyInstance,
) {
fastify.route<{
Querystring: Static<typeof requestSchema>;
Reply: Static<typeof responseBodySchema>;
}>({
method: "GET",
url: "/transaction/status",
schema: {
summary: "Get transaction status",
description: "Get the status for a transaction request.",
tags: ["Transaction"],
operationId: "status",
querystring: requestSchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: responseBodySchema,
},
},
handler: async (request, reply) => {
const { queueId } = request.query;
if (!queueId) {
throw createCustomError(
"Queue ID is required.",
StatusCodes.BAD_REQUEST,
"QUEUE_ID_REQUIRED",
);
}
// SPECIAL LOGIC FOR AMEX
// Backfill table takes priority — entries are intentional overrides for
// queue IDs that are stuck in Redis (e.g. orphaned "queued" transactions).
if (env.ENABLE_TX_BACKFILL_FALLBACK) {
const backfill = await TransactionDB.getBackfill(queueId);
if (backfill) {
return reply.status(StatusCodes.OK).send({
result: createBackfillResponse(queueId, backfill),
});
}
}
const transaction = await TransactionDB.get(queueId);
if (!transaction) {
throw createCustomError(
"Transaction not found.",
StatusCodes.BAD_REQUEST,
"TRANSACTION_NOT_FOUND",
);
}
reply.status(StatusCodes.OK).send({
result: toTransactionSchema(transaction),
});
},
});
}