@@ -4,16 +4,13 @@ import { prisma } from "../../db/client";
4
4
import { getTxToRetry } from "../../db/transactions/getTxToRetry" ;
5
5
import { updateTx } from "../../db/transactions/updateTx" ;
6
6
import { TransactionStatus } from "../../server/schemas/transaction" ;
7
+ import { cancelTransactionAndUpdate } from "../../server/utils/transaction" ;
7
8
import { getConfig } from "../../utils/cache/getConfig" ;
8
9
import { getSdk } from "../../utils/cache/getSdk" ;
9
10
import { parseTxError } from "../../utils/errors" ;
10
11
import { getGasSettingsForRetry } from "../../utils/gas" ;
11
12
import { logger } from "../../utils/logger" ;
12
- import {
13
- ReportUsageParams ,
14
- UsageEventTxActionEnum ,
15
- reportUsage ,
16
- } from "../../utils/usage" ;
13
+ import { UsageEventTxActionEnum , reportUsage } from "../../utils/usage" ;
17
14
18
15
export const retryTx = async ( ) => {
19
16
try {
@@ -26,13 +23,12 @@ export const retryTx = async () => {
26
23
}
27
24
28
25
const config = await getConfig ( ) ;
29
- const reportUsageForQueueIds : ReportUsageParams [ ] = [ ] ;
30
26
const sdk = await getSdk ( {
31
27
chainId : parseInt ( tx . chainId ! ) ,
32
28
walletAddress : tx . fromAddress ! ,
33
29
} ) ;
34
30
const provider = sdk . getProvider ( ) as StaticJsonRpcBatchProvider ;
35
- const blockNumber = await sdk . getProvider ( ) . getBlockNumber ( ) ;
31
+ const blockNumber = await provider . getBlockNumber ( ) ;
36
32
37
33
if (
38
34
blockNumber - tx . sentAtBlockNumber ! <=
@@ -58,26 +54,36 @@ export const retryTx = async () => {
58
54
} ) ;
59
55
60
56
const gasOverrides = await getGasSettingsForRetry ( tx , provider ) ;
61
- let res : ethers . providers . TransactionResponse ;
62
- const txRequest = {
57
+ const transactionRequest = {
63
58
to : tx . toAddress ! ,
64
59
from : tx . fromAddress ! ,
65
60
data : tx . data ! ,
66
61
nonce : tx . nonce ! ,
67
62
value : tx . value ! ,
68
63
...gasOverrides ,
69
64
} ;
65
+
66
+ // Send transaction.
67
+ let transactionResponse : ethers . providers . TransactionResponse ;
70
68
try {
71
- res = await sdk . getSigner ( ) ! . sendTransaction ( txRequest ) ;
69
+ transactionResponse = await sdk
70
+ . getSigner ( ) !
71
+ . sendTransaction ( transactionRequest ) ;
72
72
} catch ( err : any ) {
73
+ // The RPC rejected this transaction.
73
74
logger ( {
74
75
service : "worker" ,
75
76
level : "error" ,
76
77
queueId : tx . id ,
77
- message : ` Failed to retry` ,
78
+ message : " Failed to retry" ,
78
79
error : err ,
79
80
} ) ;
80
81
82
+ // Consume the nonce.
83
+ await cancelTransactionAndUpdate ( {
84
+ queueId : tx . id ,
85
+ pgtx,
86
+ } ) ;
81
87
await updateTx ( {
82
88
pgtx,
83
89
queueId : tx . id ,
@@ -87,21 +93,21 @@ export const retryTx = async () => {
87
93
} ,
88
94
} ) ;
89
95
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 ,
100
109
} ,
101
- action : UsageEventTxActionEnum . ErrorTx ,
102
- } ) ;
103
-
104
- reportUsage ( reportUsageForQueueIds ) ;
110
+ ] ) ;
105
111
106
112
return ;
107
113
}
@@ -112,35 +118,35 @@ export const retryTx = async () => {
112
118
data : {
113
119
sentAt : new Date ( ) ,
114
120
status : TransactionStatus . Sent ,
115
- res : txRequest ,
116
- sentAtBlockNumber : await sdk . getProvider ( ) . getBlockNumber ( ) ,
121
+ res : transactionRequest ,
122
+ sentAtBlockNumber : await provider . getBlockNumber ( ) ,
117
123
retryCount : tx . retryCount + 1 ,
118
- transactionHash : res . hash ,
124
+ transactionHash : transactionResponse . hash ,
119
125
} ,
120
126
} ) ;
121
127
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 ,
133
142
} ,
134
- action : UsageEventTxActionEnum . SendTx ,
135
- } ) ;
136
-
137
- reportUsage ( reportUsageForQueueIds ) ;
143
+ ] ) ;
138
144
139
145
logger ( {
140
146
service : "worker" ,
141
147
level : "info" ,
142
148
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 } ` ,
144
150
} ) ;
145
151
} ,
146
152
{
0 commit comments