@@ -41,6 +41,13 @@ type SentTxStatus =
41
41
errorMessage : string ;
42
42
} ;
43
43
44
+ type RpcResponseData = {
45
+ queueId : string ;
46
+ tx : ethers . providers . TransactionRequest ;
47
+ res : RpcResponse ;
48
+ sentAt : Date ;
49
+ } ;
50
+
44
51
export const processTx = async ( ) => {
45
52
try {
46
53
// 0. Initialize queueIds to send webhook
@@ -202,16 +209,14 @@ export const processTx = async () => {
202
209
startNonce = dbNonce ;
203
210
}
204
211
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 ] ;
215
220
216
221
try {
217
222
let value : ethers . BigNumberish = tx . value ! ;
@@ -298,17 +303,43 @@ export const processTx = async () => {
298
303
data : rpcResponse ,
299
304
} ) ;
300
305
301
- rpcResponses . push ( {
302
- queueId : tx . queueId ! ,
303
- tx : txRequest ,
304
- res : rpcResponse ,
305
- sentAt : new Date ( ) ,
306
- } ) ;
307
-
308
306
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 ! ) ;
310
337
}
311
338
} catch ( err : any ) {
339
+ // Error (continue to next transaction)
340
+ txIndex ++ ;
341
+ sendWebhookForQueueIds . push ( tx . queueId ! ) ;
342
+
312
343
logger ( {
313
344
service : "worker" ,
314
345
level : "warn" ,
@@ -328,20 +359,14 @@ export const processTx = async () => {
328
359
`Failed to handle transaction` ,
329
360
} ,
330
361
} ) ;
331
- sendWebhookForQueueIds . push ( tx . queueId ! ) ;
332
362
}
333
363
}
334
364
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
-
340
365
await updateWalletNonce ( {
341
366
pgtx,
342
367
address : walletAddress ,
343
368
chainId,
344
- nonce : startNonce . toNumber ( ) + incrementNonce ,
369
+ nonce : startNonce . add ( nonceIncrement ) . toNumber ( ) ,
345
370
} ) ;
346
371
347
372
// Update transaction records with updated data
0 commit comments