@@ -4,16 +4,13 @@ import { prisma } from "../../db/client";
44import { getTxToRetry } from "../../db/transactions/getTxToRetry" ;
55import { updateTx } from "../../db/transactions/updateTx" ;
66import { TransactionStatus } from "../../server/schemas/transaction" ;
7+ import { cancelTransactionAndUpdate } from "../../server/utils/transaction" ;
78import { getConfig } from "../../utils/cache/getConfig" ;
89import { getSdk } from "../../utils/cache/getSdk" ;
910import { parseTxError } from "../../utils/errors" ;
1011import { getGasSettingsForRetry } from "../../utils/gas" ;
1112import { logger } from "../../utils/logger" ;
12- import {
13- ReportUsageParams ,
14- UsageEventTxActionEnum ,
15- reportUsage ,
16- } from "../../utils/usage" ;
13+ import { UsageEventTxActionEnum , reportUsage } from "../../utils/usage" ;
1714
1815export const retryTx = async ( ) => {
1916 try {
@@ -26,13 +23,12 @@ export const retryTx = async () => {
2623 }
2724
2825 const config = await getConfig ( ) ;
29- const reportUsageForQueueIds : ReportUsageParams [ ] = [ ] ;
3026 const sdk = await getSdk ( {
3127 chainId : parseInt ( tx . chainId ! ) ,
3228 walletAddress : tx . fromAddress ! ,
3329 } ) ;
3430 const provider = sdk . getProvider ( ) as StaticJsonRpcBatchProvider ;
35- const blockNumber = await sdk . getProvider ( ) . getBlockNumber ( ) ;
31+ const blockNumber = await provider . getBlockNumber ( ) ;
3632
3733 if (
3834 blockNumber - tx . sentAtBlockNumber ! <=
@@ -58,26 +54,36 @@ export const retryTx = async () => {
5854 } ) ;
5955
6056 const gasOverrides = await getGasSettingsForRetry ( tx , provider ) ;
61- let res : ethers . providers . TransactionResponse ;
62- const txRequest = {
57+ const transactionRequest = {
6358 to : tx . toAddress ! ,
6459 from : tx . fromAddress ! ,
6560 data : tx . data ! ,
6661 nonce : tx . nonce ! ,
6762 value : tx . value ! ,
6863 ...gasOverrides ,
6964 } ;
65+
66+ // Send transaction.
67+ let transactionResponse : ethers . providers . TransactionResponse ;
7068 try {
71- res = await sdk . getSigner ( ) ! . sendTransaction ( txRequest ) ;
69+ transactionResponse = await sdk
70+ . getSigner ( ) !
71+ . sendTransaction ( transactionRequest ) ;
7272 } catch ( err : any ) {
73+ // The RPC rejected this transaction.
7374 logger ( {
7475 service : "worker" ,
7576 level : "error" ,
7677 queueId : tx . id ,
77- message : ` Failed to retry` ,
78+ message : " Failed to retry" ,
7879 error : err ,
7980 } ) ;
8081
82+ // Consume the nonce.
83+ await cancelTransactionAndUpdate ( {
84+ queueId : tx . id ,
85+ pgtx,
86+ } ) ;
8187 await updateTx ( {
8288 pgtx,
8389 queueId : tx . id ,
@@ -87,21 +93,21 @@ export const retryTx = async () => {
8793 } ,
8894 } ) ;
8995
90- reportUsageForQueueIds . push ( {
91- input : {
92- fromAddress : tx . fromAddress || undefined ,
93- toAddress : tx . toAddress || undefined ,
94- value : tx . value || undefined ,
95- chainId : tx . chainId || undefined ,
96- functionName : tx . functionName || undefined ,
97- extension : tx . extension || undefined ,
98- retryCount : tx . retryCount + 1 || 0 ,
99- provider : provider . connection . url || undefined ,
96+ reportUsage ( [
97+ {
98+ input : {
99+ fromAddress : tx . fromAddress || undefined ,
100+ toAddress : tx . toAddress || undefined ,
101+ value : tx . value || undefined ,
102+ chainId : tx . chainId ,
103+ functionName : tx . functionName || undefined ,
104+ extension : tx . extension || undefined ,
105+ retryCount : tx . retryCount + 1 ,
106+ provider : provider . connection . url ,
107+ } ,
108+ action : UsageEventTxActionEnum . ErrorTx ,
100109 } ,
101- action : UsageEventTxActionEnum . ErrorTx ,
102- } ) ;
103-
104- reportUsage ( reportUsageForQueueIds ) ;
110+ ] ) ;
105111
106112 return ;
107113 }
@@ -112,35 +118,35 @@ export const retryTx = async () => {
112118 data : {
113119 sentAt : new Date ( ) ,
114120 status : TransactionStatus . Sent ,
115- res : txRequest ,
116- sentAtBlockNumber : await sdk . getProvider ( ) . getBlockNumber ( ) ,
121+ res : transactionRequest ,
122+ sentAtBlockNumber : await provider . getBlockNumber ( ) ,
117123 retryCount : tx . retryCount + 1 ,
118- transactionHash : res . hash ,
124+ transactionHash : transactionResponse . hash ,
119125 } ,
120126 } ) ;
121127
122- reportUsageForQueueIds . push ( {
123- input : {
124- fromAddress : tx . fromAddress || undefined ,
125- toAddress : tx . toAddress || undefined ,
126- value : tx . value || undefined ,
127- chainId : tx . chainId || undefined ,
128- functionName : tx . functionName || undefined ,
129- extension : tx . extension || undefined ,
130- retryCount : tx . retryCount + 1 ,
131- transactionHash : res . hash || undefined ,
132- provider : provider . connection . url || undefined ,
128+ reportUsage ( [
129+ {
130+ input : {
131+ fromAddress : tx . fromAddress || undefined ,
132+ toAddress : tx . toAddress || undefined ,
133+ value : tx . value || undefined ,
134+ chainId : tx . chainId ,
135+ functionName : tx . functionName || undefined ,
136+ extension : tx . extension || undefined ,
137+ retryCount : tx . retryCount + 1 ,
138+ transactionHash : transactionResponse . hash || undefined ,
139+ provider : provider . connection . url ,
140+ } ,
141+ action : UsageEventTxActionEnum . SendTx ,
133142 } ,
134- action : UsageEventTxActionEnum . SendTx ,
135- } ) ;
136-
137- reportUsage ( reportUsageForQueueIds ) ;
143+ ] ) ;
138144
139145 logger ( {
140146 service : "worker" ,
141147 level : "info" ,
142148 queueId : tx . id ,
143- message : `Retried with hash ${ res . hash } for nonce ${ res . nonce } ` ,
149+ message : `Retried with hash ${ transactionResponse . hash } for nonce ${ transactionResponse . nonce } ` ,
144150 } ) ;
145151 } ,
146152 {
0 commit comments