Skip to content

Commit 56f7414

Browse files
authored
Add nonce incrementing on nonce too low (#355)
* Block worker on lock acquisition * Update * Update * Add nonce incrementing on nonce too low * Update * Update * Don't add 1
1 parent 1c7e0c4 commit 56f7414

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

src/worker/tasks/processTx.ts

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ type SentTxStatus =
4141
errorMessage: string;
4242
};
4343

44+
type RpcResponseData = {
45+
queueId: string;
46+
tx: ethers.providers.TransactionRequest;
47+
res: RpcResponse;
48+
sentAt: Date;
49+
};
50+
4451
export const processTx = async () => {
4552
try {
4653
// 0. Initialize queueIds to send webhook
@@ -202,16 +209,14 @@ export const processTx = async () => {
202209
startNonce = dbNonce;
203210
}
204211

205-
// Group all transactions into a single batch rpc request
206-
let sentTxCount = 0;
207-
const rpcResponses: {
208-
queueId: string;
209-
tx: ethers.providers.TransactionRequest;
210-
res: RpcResponse;
211-
sentAt: Date;
212-
}[] = [];
213-
for (const tx of txsToSend) {
214-
const nonce = startNonce.add(sentTxCount);
212+
const rpcResponses: RpcResponseData[] = [];
213+
214+
let txIndex = 0;
215+
let nonceIncrement = 0;
216+
217+
while (txIndex < txsToSend.length) {
218+
const nonce = startNonce.add(nonceIncrement);
219+
const tx = txsToSend[txIndex];
215220

216221
try {
217222
let value: ethers.BigNumberish = tx.value!;
@@ -298,17 +303,43 @@ export const processTx = async () => {
298303
data: rpcResponse,
299304
});
300305

301-
rpcResponses.push({
302-
queueId: tx.queueId!,
303-
tx: txRequest,
304-
res: rpcResponse,
305-
sentAt: new Date(),
306-
});
307-
308306
if (!rpcResponse.error && !!rpcResponse.result) {
309-
sentTxCount++;
307+
// Success (continue to next transaction)
308+
nonceIncrement++;
309+
txIndex++;
310+
311+
rpcResponses.push({
312+
queueId: tx.queueId!,
313+
tx: txRequest,
314+
res: rpcResponse,
315+
sentAt: new Date(),
316+
});
317+
sendWebhookForQueueIds.push(tx.queueId!);
318+
} else if (
319+
typeof rpcResponse.error?.message === "string" &&
320+
(rpcResponse.error.message as string)
321+
.toLowerCase()
322+
.includes("nonce too low")
323+
) {
324+
// Nonce too low (retry same transaction with higher nonce)
325+
nonceIncrement++;
326+
} else {
327+
// Error (continue to next transaction)
328+
txIndex++;
329+
330+
rpcResponses.push({
331+
queueId: tx.queueId!,
332+
tx: txRequest,
333+
res: rpcResponse,
334+
sentAt: new Date(),
335+
});
336+
sendWebhookForQueueIds.push(tx.queueId!);
310337
}
311338
} catch (err: any) {
339+
// Error (continue to next transaction)
340+
txIndex++;
341+
sendWebhookForQueueIds.push(tx.queueId!);
342+
312343
logger({
313344
service: "worker",
314345
level: "warn",
@@ -328,20 +359,14 @@ export const processTx = async () => {
328359
`Failed to handle transaction`,
329360
},
330361
});
331-
sendWebhookForQueueIds.push(tx.queueId!);
332362
}
333363
}
334364

335-
// Check how many transactions succeeded and increment nonce
336-
const incrementNonce = rpcResponses.reduce((acc, curr) => {
337-
return curr.res.result && !curr.res.error ? acc + 1 : acc;
338-
}, 0);
339-
340365
await updateWalletNonce({
341366
pgtx,
342367
address: walletAddress,
343368
chainId,
344-
nonce: startNonce.toNumber() + incrementNonce,
369+
nonce: startNonce.add(nonceIncrement).toNumber(),
345370
});
346371

347372
// Update transaction records with updated data

0 commit comments

Comments
 (0)