1
- import { TransactionResponse } from "@ethersproject/abstract-provider" ;
2
1
import { getDefaultGasOverrides } from "@thirdweb-dev/sdk" ;
3
2
import { StatusCodes } from "http-status-codes" ;
4
3
import { getTxById } from "../../db/transactions/getTxById" ;
@@ -25,10 +24,6 @@ export const cancelTransactionAndUpdate = async ({
25
24
} ;
26
25
}
27
26
28
- let message = "" ;
29
- let error = null ;
30
- let transferTransactionResult : TransactionResponse | null = null ;
31
-
32
27
if ( txData . signerAddress && txData . accountAddress ) {
33
28
switch ( txData . status ) {
34
29
case TransactionStatus . Errored :
@@ -62,25 +57,44 @@ export const cancelTransactionAndUpdate = async ({
62
57
status : TransactionStatus . Cancelled ,
63
58
} ,
64
59
} ) ;
65
- message = "Transaction cancelled on-database successfully." ;
66
- break ;
60
+ return {
61
+ message : "Transaction cancelled on-database successfully." ,
62
+ } ;
67
63
}
68
64
} else {
69
65
switch ( txData . status ) {
70
- case TransactionStatus . Errored :
71
- error = createCustomError (
66
+ case TransactionStatus . Errored : {
67
+ if ( txData . chainId && txData . fromAddress && txData . nonce ) {
68
+ const { message, transactionHash } = await sendNullTransaction ( {
69
+ chainId : parseInt ( txData . chainId ) ,
70
+ walletAddress : txData . fromAddress ,
71
+ nonce : txData . nonce ,
72
+ } ) ;
73
+ if ( transactionHash ) {
74
+ await updateTx ( {
75
+ queueId,
76
+ pgtx,
77
+ data : {
78
+ status : TransactionStatus . Cancelled ,
79
+ } ,
80
+ } ) ;
81
+ }
82
+
83
+ return { message, transactionHash } ;
84
+ }
85
+
86
+ throw createCustomError (
72
87
`Transaction has already errored: ${ txData . errorMessage } ` ,
73
88
StatusCodes . BAD_REQUEST ,
74
89
"TransactionErrored" ,
75
90
) ;
76
- break ;
91
+ }
77
92
case TransactionStatus . Cancelled :
78
- error = createCustomError (
93
+ throw createCustomError (
79
94
"Transaction is already cancelled." ,
80
95
StatusCodes . BAD_REQUEST ,
81
96
"TransactionAlreadyCancelled" ,
82
97
) ;
83
- break ;
84
98
case TransactionStatus . Queued :
85
99
await updateTx ( {
86
100
queueId,
@@ -89,61 +103,79 @@ export const cancelTransactionAndUpdate = async ({
89
103
status : TransactionStatus . Cancelled ,
90
104
} ,
91
105
} ) ;
92
- message = "Transaction cancelled successfully." ;
93
- break ;
106
+ return {
107
+ message : "Transaction cancelled successfully." ,
108
+ } ;
94
109
case TransactionStatus . Mined :
95
- error = createCustomError (
110
+ throw createCustomError (
96
111
"Transaction already mined." ,
97
112
StatusCodes . BAD_REQUEST ,
98
113
"TransactionAlreadyMined" ,
99
114
) ;
100
- break ;
101
115
case TransactionStatus . Sent : {
102
- const sdk = await getSdk ( {
103
- chainId : parseInt ( txData . chainId ! ) ,
104
- walletAddress : txData . fromAddress ! ,
105
- } ) ;
116
+ if ( txData . chainId && txData . fromAddress && txData . nonce ) {
117
+ const { message, transactionHash } = await sendNullTransaction ( {
118
+ chainId : parseInt ( txData . chainId ) ,
119
+ walletAddress : txData . fromAddress ,
120
+ nonce : txData . nonce ,
121
+ } ) ;
122
+ if ( transactionHash ) {
123
+ await updateTx ( {
124
+ queueId,
125
+ pgtx,
126
+ data : {
127
+ status : TransactionStatus . Cancelled ,
128
+ } ,
129
+ } ) ;
130
+ }
106
131
107
- const txReceipt = await sdk
108
- . getProvider ( )
109
- . getTransactionReceipt ( txData . transactionHash ! ) ;
110
- if ( txReceipt ) {
111
- message = "Transaction already mined." ;
112
- break ;
132
+ return { message, transactionHash } ;
113
133
}
134
+ }
135
+ }
136
+ }
114
137
115
- const gasOverrides = await getDefaultGasOverrides ( sdk . getProvider ( ) ) ;
116
- transferTransactionResult = await sdk . wallet . sendRawTransaction ( {
117
- to : txData . fromAddress ! ,
118
- from : txData . fromAddress ! ,
119
- data : "0x" ,
120
- value : "0x00" ,
121
- nonce : txData . nonce ! ,
122
- ...multiplyGasOverrides ( gasOverrides , 2 ) ,
123
- } ) ;
138
+ throw new Error ( "Unhandled cancellation state." ) ;
139
+ } ;
124
140
125
- message = "Transaction cancelled successfully." ;
141
+ const sendNullTransaction = async ( args : {
142
+ chainId : number ;
143
+ walletAddress : string ;
144
+ nonce : number ;
145
+ transactionHash ?: string ;
146
+ } ) : Promise < {
147
+ message : string ;
148
+ transactionHash ?: string ;
149
+ } > => {
150
+ const { chainId, walletAddress, nonce, transactionHash } = args ;
126
151
127
- await updateTx ( {
128
- queueId,
129
- pgtx,
130
- data : {
131
- status : TransactionStatus . Cancelled ,
132
- } ,
133
- } ) ;
134
- break ;
135
- }
136
- default :
137
- break ;
152
+ const sdk = await getSdk ( { chainId, walletAddress } ) ;
153
+ const provider = sdk . getProvider ( ) ;
154
+
155
+ // Skip if the transaction is already mined.
156
+ if ( transactionHash ) {
157
+ const receipt = await provider . getTransactionReceipt ( transactionHash ) ;
158
+ if ( receipt ) {
159
+ return { message : "Transaction already mined." } ;
138
160
}
139
161
}
140
162
141
- if ( error ) {
142
- throw error ;
143
- }
163
+ try {
164
+ const gasOverrides = await getDefaultGasOverrides ( provider ) ;
165
+ const { hash } = await sdk . wallet . sendRawTransaction ( {
166
+ to : walletAddress ,
167
+ from : walletAddress ,
168
+ data : "0x" ,
169
+ value : "0" ,
170
+ nonce,
171
+ ...multiplyGasOverrides ( gasOverrides , 2 ) ,
172
+ } ) ;
144
173
145
- return {
146
- message,
147
- transactionHash : transferTransactionResult ?. hash ,
148
- } ;
174
+ return {
175
+ message : "Transaction cancelled successfully." ,
176
+ transactionHash : hash ,
177
+ } ;
178
+ } catch ( e : any ) {
179
+ return { message : e . toString ( ) } ;
180
+ }
149
181
} ;
0 commit comments