Skip to content

Commit 309e427

Browse files
authored
feat: add simulation steps to executeSwapQuote (#245)
* feat: add simulation steps to `executeSwapQuote` * add changeset
1 parent 6d8613b commit 309e427

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

.changeset/dirty-turtles-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@across-protocol/app-sdk": patch
3+
---
4+
5+
feat: add simulation steps to `executeSwapQuote`

packages/sdk/src/actions/executeSwapQuote.ts

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ type SwapTransactionProgress =
1616
step: "approve";
1717
status: "idle";
1818
}
19+
| {
20+
step: "approve";
21+
status: "simulationPending";
22+
}
23+
| {
24+
step: "approve";
25+
status: "simulationSuccess";
26+
}
1927
| {
2028
step: "approve";
2129
status: "txPending";
@@ -30,6 +38,14 @@ type SwapTransactionProgress =
3038
step: "swap";
3139
status: "idle";
3240
}
41+
| {
42+
step: "swap";
43+
status: "simulationPending";
44+
}
45+
| {
46+
step: "swap";
47+
status: "simulationSuccess";
48+
}
3349
| {
3450
step: "swap";
3551
status: "txPending";
@@ -56,7 +72,7 @@ type SwapTransactionProgress =
5672
}
5773
| {
5874
step: "approve" | "swap" | "fill";
59-
status: "txError" | "error";
75+
status: "txError" | "error" | "simulationError";
6076
error: Error;
6177
};
6278

@@ -160,7 +176,7 @@ export async function executeSwapQuote(
160176

161177
let currentTransactionProgress: SwapTransactionProgress = {
162178
status: "idle",
163-
step: "swap",
179+
step: "approve",
164180
};
165181

166182
try {
@@ -195,10 +211,25 @@ export async function executeSwapQuote(
195211

196212
// Execute approval transactions if present
197213
if (swapQuote.approvalTxns && swapQuote.approvalTxns.length > 0) {
214+
onProgressHandler(currentTransactionProgress);
215+
198216
for (const approvalTxn of swapQuote.approvalTxns) {
199217
currentTransactionProgress = {
200218
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",
202233
};
203234
onProgressHandler(currentTransactionProgress);
204235

@@ -228,6 +259,12 @@ export async function executeSwapQuote(
228259
}
229260
}
230261

262+
currentTransactionProgress = {
263+
step: "swap",
264+
status: "idle",
265+
};
266+
onProgressHandler(currentTransactionProgress);
267+
231268
const txRequest = {
232269
to: swapTx.to as Address,
233270
data: swapTx.data as Hex,
@@ -241,6 +278,20 @@ export async function executeSwapQuote(
241278
: undefined,
242279
};
243280

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+
244295
const swapTxHash = await walletClient.sendTransaction({
245296
account,
246297
...txRequest,
@@ -315,7 +366,11 @@ export async function executeSwapQuote(
315366
}
316367
} catch (error) {
317368
const errorStatus =
318-
currentTransactionProgress.status === "txPending" ? "txError" : "error";
369+
currentTransactionProgress.status === "txPending"
370+
? "txError"
371+
: currentTransactionProgress.status === "simulationPending"
372+
? "simulationError"
373+
: "error";
319374

320375
onProgressHandler({
321376
...currentTransactionProgress,

0 commit comments

Comments
 (0)