@@ -16,6 +16,14 @@ type SwapTransactionProgress =
16
16
step : "approve" ;
17
17
status : "idle" ;
18
18
}
19
+ | {
20
+ step : "approve" ;
21
+ status : "simulationPending" ;
22
+ }
23
+ | {
24
+ step : "approve" ;
25
+ status : "simulationSuccess" ;
26
+ }
19
27
| {
20
28
step : "approve" ;
21
29
status : "txPending" ;
@@ -30,6 +38,14 @@ type SwapTransactionProgress =
30
38
step : "swap" ;
31
39
status : "idle" ;
32
40
}
41
+ | {
42
+ step : "swap" ;
43
+ status : "simulationPending" ;
44
+ }
45
+ | {
46
+ step : "swap" ;
47
+ status : "simulationSuccess" ;
48
+ }
33
49
| {
34
50
step : "swap" ;
35
51
status : "txPending" ;
@@ -56,7 +72,7 @@ type SwapTransactionProgress =
56
72
}
57
73
| {
58
74
step : "approve" | "swap" | "fill" ;
59
- status : "txError" | "error" ;
75
+ status : "txError" | "error" | "simulationError" ;
60
76
error : Error ;
61
77
} ;
62
78
@@ -160,7 +176,7 @@ export async function executeSwapQuote(
160
176
161
177
let currentTransactionProgress : SwapTransactionProgress = {
162
178
status : "idle" ,
163
- step : "swap " ,
179
+ step : "approve " ,
164
180
} ;
165
181
166
182
try {
@@ -195,10 +211,25 @@ export async function executeSwapQuote(
195
211
196
212
// Execute approval transactions if present
197
213
if ( swapQuote . approvalTxns && swapQuote . approvalTxns . length > 0 ) {
214
+ onProgressHandler ( currentTransactionProgress ) ;
215
+
198
216
for ( const approvalTxn of swapQuote . approvalTxns ) {
199
217
currentTransactionProgress = {
200
218
step : "approve" ,
201
- status : "idle" ,
219
+ status : "simulationPending" ,
220
+ } ;
221
+ onProgressHandler ( currentTransactionProgress ) ;
222
+
223
+ // simulate approval transaction
224
+ await originClient . call ( {
225
+ account : walletClient . account ,
226
+ data : approvalTxn . data as Hex ,
227
+ to : approvalTxn . to as Address ,
228
+ } ) ;
229
+
230
+ currentTransactionProgress = {
231
+ step : "approve" ,
232
+ status : "simulationSuccess" ,
202
233
} ;
203
234
onProgressHandler ( currentTransactionProgress ) ;
204
235
@@ -228,6 +259,12 @@ export async function executeSwapQuote(
228
259
}
229
260
}
230
261
262
+ currentTransactionProgress = {
263
+ step : "swap" ,
264
+ status : "idle" ,
265
+ } ;
266
+ onProgressHandler ( currentTransactionProgress ) ;
267
+
231
268
const txRequest = {
232
269
to : swapTx . to as Address ,
233
270
data : swapTx . data as Hex ,
@@ -241,6 +278,20 @@ export async function executeSwapQuote(
241
278
: undefined ,
242
279
} ;
243
280
281
+ currentTransactionProgress = {
282
+ step : "swap" ,
283
+ status : "simulationPending" ,
284
+ } ;
285
+ onProgressHandler ( currentTransactionProgress ) ;
286
+
287
+ // simulate swap transaction
288
+ await originClient . call ( txRequest ) ;
289
+
290
+ currentTransactionProgress = {
291
+ step : "swap" ,
292
+ status : "simulationSuccess" ,
293
+ } ;
294
+
244
295
const swapTxHash = await walletClient . sendTransaction ( {
245
296
account,
246
297
...txRequest ,
@@ -315,7 +366,11 @@ export async function executeSwapQuote(
315
366
}
316
367
} catch ( error ) {
317
368
const errorStatus =
318
- currentTransactionProgress . status === "txPending" ? "txError" : "error" ;
369
+ currentTransactionProgress . status === "txPending"
370
+ ? "txError"
371
+ : currentTransactionProgress . status === "simulationPending"
372
+ ? "simulationError"
373
+ : "error" ;
319
374
320
375
onProgressHandler ( {
321
376
...currentTransactionProgress ,
0 commit comments