Skip to content

Commit 5fb886c

Browse files
authored
feat: time out sent txns after an hour (#347)
1 parent 3cd52b4 commit 5fb886c

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/worker/tasks/retryTx.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export const retryTx = async () => {
1919
}
2020

2121
const config = await getConfiguration();
22-
2322
const sdk = await getSdk({
2423
chainId: parseInt(tx.chainId!),
2524
walletAddress: tx.fromAddress!,
2625
});
26+
2727
const blockNumber = await sdk.getProvider().getBlockNumber();
28-
// Only retry if more than the ellapsed blocks before retry has passed
28+
// Only retry if more than the elapsed blocks before retry has passed.
2929
if (
3030
blockNumber - tx.sentAtBlockNumber! <=
3131
config.minEllapsedBlocksBeforeRetry
@@ -37,8 +37,8 @@ export const retryTx = async () => {
3737
.getProvider()
3838
.getTransactionReceipt(tx.transactionHash!);
3939

40-
// If the transaction has been mined but just not updated yet in database, don't retry
41-
if (!!receipt) {
40+
// If the transaction is mined, update the DB.
41+
if (receipt) {
4242
return;
4343
}
4444

src/worker/tasks/updateMinedTx.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export const updateMinedTx = async () => {
2020
return;
2121
}
2222

23+
const droppedTxs: Transactions[] = [];
24+
2325
const txsWithReceipts = (
2426
await Promise.all(
2527
txs.map(async (tx) => {
@@ -30,8 +32,16 @@ export const updateMinedTx = async () => {
3032
.getTransactionReceipt(tx.transactionHash!);
3133

3234
if (!receipt) {
33-
// If no receipt was received, return undefined to filter out tx
34-
return undefined;
35+
// This tx is not yet mined or was dropped.
36+
37+
// If the tx was submitted over 1 hour ago, assume it is dropped.
38+
// @TODO: move duration to config
39+
const sentAt = new Date(tx.sentAt!);
40+
const ageInMilliseconds = Date.now() - sentAt.getTime();
41+
if (ageInMilliseconds > 1000 * 60 * 60 * 1) {
42+
droppedTxs.push(tx);
43+
}
44+
return;
3545
}
3646

3747
const response = (await sdk
@@ -68,7 +78,7 @@ export const updateMinedTx = async () => {
6878
minedAt: Date;
6979
}[];
7080

71-
// Update all transactions with a receipt in parallel
81+
// Update mined transactions.
7282
await Promise.all(
7383
txsWithReceipts.map(async (txWithReceipt) => {
7484
await updateTx({
@@ -94,12 +104,35 @@ export const updateMinedTx = async () => {
94104
service: "worker",
95105
level: "info",
96106
queueId: txWithReceipt.tx.id,
97-
message: `Updated with receipt`,
107+
message: "Updated mined tx.",
98108
});
99109

100110
sendWebhookForQueueIds.push(txWithReceipt.tx.id);
101111
}),
102112
);
113+
114+
// Update dropped txs.
115+
await Promise.all(
116+
droppedTxs.map(async (tx) => {
117+
await updateTx({
118+
pgtx,
119+
queueId: tx.id,
120+
data: {
121+
status: TransactionStatusEnum.Errored,
122+
errorMessage: "Transaction timed out.",
123+
},
124+
});
125+
126+
logger({
127+
service: "worker",
128+
level: "info",
129+
queueId: tx.id,
130+
message: "Update dropped tx.",
131+
});
132+
133+
sendWebhookForQueueIds.push(tx.id);
134+
}),
135+
);
103136
},
104137
{
105138
timeout: 5 * 60000,

0 commit comments

Comments
 (0)