@shogun-sdk/swap 0.0.2-test → 0.0.2-test.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.cjs +153 -45
- package/dist/core.d.cts +2 -2
- package/dist/core.d.ts +2 -2
- package/dist/core.js +145 -38
- package/dist/{execute-FaLLPp1i.d.cts → execute-DEFbp1WZ.d.cts} +13 -5
- package/dist/{execute-HX1fQ7wG.d.ts → execute-St0oo8GR.d.ts} +13 -5
- package/dist/index.cjs +190 -1557
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +216 -1597
- package/dist/react.cjs +197 -1564
- package/dist/react.d.cts +6 -40
- package/dist/react.d.ts +6 -40
- package/dist/react.js +218 -1599
- package/package.json +7 -4
package/dist/core.cjs
CHANGED
|
@@ -20,7 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/core/index.ts
|
|
21
21
|
var core_exports = {};
|
|
22
22
|
__export(core_exports, {
|
|
23
|
-
ChainID: () =>
|
|
23
|
+
ChainID: () => import_intents_sdk11.ChainID,
|
|
24
24
|
NATIVE_TOKEN: () => NATIVE_TOKEN,
|
|
25
25
|
SOLANA_CHAIN_ID: () => SOLANA_CHAIN_ID,
|
|
26
26
|
SupportedChains: () => SupportedChains,
|
|
@@ -29,7 +29,7 @@ __export(core_exports, {
|
|
|
29
29
|
getBalances: () => getBalances,
|
|
30
30
|
getQuote: () => getQuote,
|
|
31
31
|
getTokenList: () => getTokenList,
|
|
32
|
-
isEvmChain: () =>
|
|
32
|
+
isEvmChain: () => import_intents_sdk11.isEvmChain,
|
|
33
33
|
isNativeAddress: () => isNativeAddress,
|
|
34
34
|
isViemWalletClient: () => isViemWalletClient,
|
|
35
35
|
serializeBigIntsToStrings: () => serializeBigIntsToStrings
|
|
@@ -177,22 +177,26 @@ async function getQuote(params) {
|
|
|
177
177
|
tokenOut: params.tokenOut.address,
|
|
178
178
|
amount: params.amount
|
|
179
179
|
});
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
const slippage = Math.min(Math.max(slippageDecimal, 0), 0.5);
|
|
180
|
+
console.log({ data });
|
|
181
|
+
const slippagePercent = Math.min(Math.max(params.slippage ?? 0.5, 0), 50);
|
|
183
182
|
let warning;
|
|
184
|
-
if (
|
|
185
|
-
warning = `\u26A0\uFE0F High slippage tolerance (${
|
|
183
|
+
if (slippagePercent > 10) {
|
|
184
|
+
warning = `\u26A0\uFE0F High slippage tolerance (${slippagePercent.toFixed(2)}%) \u2014 price may vary significantly.`;
|
|
186
185
|
}
|
|
187
|
-
const estimatedAmountOut = BigInt(data.
|
|
188
|
-
const slippageBps = BigInt(Math.round(
|
|
186
|
+
const estimatedAmountOut = BigInt(data.estimatedAmountOut);
|
|
187
|
+
const slippageBps = BigInt(Math.round(slippagePercent * 100));
|
|
189
188
|
const estimatedAmountOutAfterSlippage = estimatedAmountOut * (10000n - slippageBps) / 10000n;
|
|
189
|
+
const pricePerTokenOutInUsd = data.estimatedAmountOutUsd / Number(data.estimatedAmountOut);
|
|
190
|
+
const amountOutUsdAfterSlippage = Number(estimatedAmountOutAfterSlippage) * pricePerTokenOutInUsd;
|
|
191
|
+
const minStablecoinsAmountValue = BigInt(data.estimatedAmountInAsMinStablecoinAmount);
|
|
192
|
+
const minStablecoinsAmountAfterSlippage = minStablecoinsAmountValue * (10000n - slippageBps) / 10000n;
|
|
190
193
|
const pricePerInputToken = estimatedAmountOut * 10n ** BigInt(params.tokenIn.decimals ?? 18) / BigInt(params.amount);
|
|
191
194
|
return {
|
|
192
|
-
amountOut:
|
|
193
|
-
amountOutUsd:
|
|
195
|
+
amountOut: estimatedAmountOutAfterSlippage,
|
|
196
|
+
amountOutUsd: amountOutUsdAfterSlippage,
|
|
194
197
|
amountInUsd: data.amountInUsd,
|
|
195
|
-
|
|
198
|
+
// Input USD stays the same
|
|
199
|
+
minStablecoinsAmount: minStablecoinsAmountAfterSlippage,
|
|
196
200
|
tokenIn: {
|
|
197
201
|
address: params.tokenIn.address,
|
|
198
202
|
decimals: params.tokenIn.decimals ?? 18,
|
|
@@ -205,10 +209,11 @@ async function getQuote(params) {
|
|
|
205
209
|
},
|
|
206
210
|
amountIn: params.amount,
|
|
207
211
|
pricePerInputToken,
|
|
208
|
-
slippage,
|
|
212
|
+
slippage: slippagePercent,
|
|
209
213
|
internal: {
|
|
210
214
|
...data,
|
|
211
|
-
estimatedAmountOutReduced: estimatedAmountOutAfterSlippage
|
|
215
|
+
estimatedAmountOutReduced: estimatedAmountOutAfterSlippage,
|
|
216
|
+
estimatedAmountOutUsdReduced: amountOutUsdAfterSlippage
|
|
212
217
|
},
|
|
213
218
|
warning
|
|
214
219
|
};
|
|
@@ -281,7 +286,7 @@ async function getBalances(params, options) {
|
|
|
281
286
|
}
|
|
282
287
|
|
|
283
288
|
// src/core/executeOrder/execute.ts
|
|
284
|
-
var
|
|
289
|
+
var import_intents_sdk10 = require("@shogun-sdk/intents-sdk");
|
|
285
290
|
var import_viem4 = require("viem");
|
|
286
291
|
|
|
287
292
|
// src/wallet-adapter/evm-wallet-adapter/adapter.ts
|
|
@@ -348,7 +353,7 @@ var adaptViemWallet = (wallet) => {
|
|
|
348
353
|
};
|
|
349
354
|
|
|
350
355
|
// src/core/executeOrder/handleEvmExecution.ts
|
|
351
|
-
var
|
|
356
|
+
var import_intents_sdk8 = require("@shogun-sdk/intents-sdk");
|
|
352
357
|
var import_viem3 = require("viem");
|
|
353
358
|
|
|
354
359
|
// src/core/executeOrder/stageMessages.ts
|
|
@@ -356,10 +361,12 @@ var DEFAULT_STAGE_MESSAGES = {
|
|
|
356
361
|
processing: "Preparing transaction for execution",
|
|
357
362
|
approving: "Approving token allowance",
|
|
358
363
|
approved: "Token approved successfully",
|
|
359
|
-
signing: "Signing
|
|
360
|
-
submitting: "Submitting
|
|
361
|
-
|
|
362
|
-
|
|
364
|
+
signing: "Signing transaction for submission",
|
|
365
|
+
submitting: "Submitting transaction",
|
|
366
|
+
initiated: "Transaction initiated.",
|
|
367
|
+
success: "Transaction Executed successfully",
|
|
368
|
+
shogun_processing: "Shogun is processing your transaction",
|
|
369
|
+
error: "Transaction failed during submission"
|
|
363
370
|
};
|
|
364
371
|
|
|
365
372
|
// src/core/executeOrder/buildOrder.ts
|
|
@@ -398,6 +405,95 @@ async function buildOrder({
|
|
|
398
405
|
});
|
|
399
406
|
}
|
|
400
407
|
|
|
408
|
+
// src/utils/pollOrderStatus.ts
|
|
409
|
+
var import_intents_sdk7 = require("@shogun-sdk/intents-sdk");
|
|
410
|
+
async function pollOrderStatus(jwt, orderId, options = {}) {
|
|
411
|
+
const { intervalMs = 2e3, timeoutMs = 3e5 } = options;
|
|
412
|
+
const startTime = Date.now();
|
|
413
|
+
return new Promise((resolve, reject) => {
|
|
414
|
+
const pollInterval = setInterval(async () => {
|
|
415
|
+
try {
|
|
416
|
+
if (Date.now() - startTime > timeoutMs) {
|
|
417
|
+
clearInterval(pollInterval);
|
|
418
|
+
return resolve("Timeout");
|
|
419
|
+
}
|
|
420
|
+
const res = await fetch(`${import_intents_sdk7.AUCTIONEER_URL}/user_intent`, {
|
|
421
|
+
method: "GET",
|
|
422
|
+
headers: {
|
|
423
|
+
Authorization: `Bearer ${jwt}`,
|
|
424
|
+
"Content-Type": "application/json"
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
if (!res.ok) {
|
|
428
|
+
clearInterval(pollInterval);
|
|
429
|
+
return reject(
|
|
430
|
+
new Error(`Failed to fetch orders: ${res.status} ${res.statusText}`)
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
const json = await res.json();
|
|
434
|
+
const data = json?.data ?? {};
|
|
435
|
+
const allOrders = [
|
|
436
|
+
...data.crossChainDcaOrders ?? [],
|
|
437
|
+
...data.crossChainLimitOrders ?? [],
|
|
438
|
+
...data.singleChainDcaOrders ?? [],
|
|
439
|
+
...data.singleChainLimitOrders ?? []
|
|
440
|
+
];
|
|
441
|
+
const targetOrder = allOrders.find((o) => o.orderId === orderId);
|
|
442
|
+
if (!targetOrder) {
|
|
443
|
+
clearInterval(pollInterval);
|
|
444
|
+
return resolve("NotFound");
|
|
445
|
+
}
|
|
446
|
+
const { orderStatus } = targetOrder;
|
|
447
|
+
if (orderStatus === "Fulfilled" || orderStatus === "Cancelled" || orderStatus === "Outdated") {
|
|
448
|
+
clearInterval(pollInterval);
|
|
449
|
+
return resolve(orderStatus);
|
|
450
|
+
}
|
|
451
|
+
} catch (error) {
|
|
452
|
+
clearInterval(pollInterval);
|
|
453
|
+
return reject(error);
|
|
454
|
+
}
|
|
455
|
+
}, intervalMs);
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/core/executeOrder/handleOrderPollingResult.ts
|
|
460
|
+
async function handleOrderPollingResult({
|
|
461
|
+
status,
|
|
462
|
+
orderId,
|
|
463
|
+
chainId,
|
|
464
|
+
update,
|
|
465
|
+
messageFor
|
|
466
|
+
}) {
|
|
467
|
+
switch (status) {
|
|
468
|
+
case "Fulfilled":
|
|
469
|
+
update("success", messageFor("success"));
|
|
470
|
+
return {
|
|
471
|
+
status: true,
|
|
472
|
+
orderId,
|
|
473
|
+
chainId,
|
|
474
|
+
finalStatus: status,
|
|
475
|
+
stage: "success"
|
|
476
|
+
};
|
|
477
|
+
case "Cancelled":
|
|
478
|
+
update("error", "Order was cancelled before fulfillment");
|
|
479
|
+
break;
|
|
480
|
+
case "Timeout":
|
|
481
|
+
update("error", "Order polling timed out");
|
|
482
|
+
break;
|
|
483
|
+
case "NotFound":
|
|
484
|
+
default:
|
|
485
|
+
update("error", "Order not found");
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
return {
|
|
489
|
+
status: false,
|
|
490
|
+
orderId,
|
|
491
|
+
chainId,
|
|
492
|
+
finalStatus: status,
|
|
493
|
+
stage: "error"
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
|
|
401
497
|
// src/core/executeOrder/handleEvmExecution.ts
|
|
402
498
|
async function handleEvmExecution({
|
|
403
499
|
recipientAddress,
|
|
@@ -426,18 +522,18 @@ async function handleEvmExecution({
|
|
|
426
522
|
from: accountAddress
|
|
427
523
|
});
|
|
428
524
|
}
|
|
429
|
-
update("
|
|
525
|
+
update("processing", messageFor("approving"));
|
|
430
526
|
await wallet.sendTransaction({
|
|
431
527
|
to: tokenIn,
|
|
432
528
|
data: (0, import_viem3.encodeFunctionData)({
|
|
433
529
|
abi: import_viem3.erc20Abi,
|
|
434
530
|
functionName: "approve",
|
|
435
|
-
args: [
|
|
531
|
+
args: [import_intents_sdk8.PERMIT2_ADDRESS[chainId], BigInt(quote.amountIn)]
|
|
436
532
|
}),
|
|
437
533
|
value: 0n,
|
|
438
534
|
from: accountAddress
|
|
439
535
|
});
|
|
440
|
-
update("
|
|
536
|
+
update("processing", messageFor("approved"));
|
|
441
537
|
const destination = recipientAddress ?? accountAddress;
|
|
442
538
|
const order = await buildOrder({
|
|
443
539
|
quote,
|
|
@@ -446,23 +542,32 @@ async function handleEvmExecution({
|
|
|
446
542
|
deadline,
|
|
447
543
|
isSingleChain
|
|
448
544
|
});
|
|
449
|
-
update("
|
|
450
|
-
const { orderTypedData, nonce } = isSingleChain ? await (0,
|
|
545
|
+
update("processing", messageFor("signing"));
|
|
546
|
+
const { orderTypedData, nonce } = isSingleChain ? await (0, import_intents_sdk8.getEVMSingleChainOrderTypedData)(order) : await (0, import_intents_sdk8.getEVMCrossChainOrderTypedData)(order);
|
|
451
547
|
if (!wallet.signTypedData) {
|
|
452
548
|
throw new Error("Wallet does not support EIP-712 signing");
|
|
453
549
|
}
|
|
454
550
|
const signature = await wallet.signTypedData(serializeBigIntsToStrings(orderTypedData));
|
|
455
|
-
update("
|
|
551
|
+
update("processing", messageFor("submitting"));
|
|
456
552
|
const res = await order.sendToAuctioneer({ signature, nonce: nonce.toString() });
|
|
457
553
|
if (!res.success) {
|
|
458
554
|
throw new Error("Auctioneer submission failed");
|
|
459
555
|
}
|
|
460
|
-
update("
|
|
461
|
-
|
|
556
|
+
update("initiated", messageFor("initiated"));
|
|
557
|
+
const { jwt, intentId: orderId } = res.data;
|
|
558
|
+
update("initiated", messageFor("shogun_processing"));
|
|
559
|
+
const status = await pollOrderStatus(jwt, orderId);
|
|
560
|
+
return await handleOrderPollingResult({
|
|
561
|
+
status,
|
|
562
|
+
orderId,
|
|
563
|
+
chainId,
|
|
564
|
+
update,
|
|
565
|
+
messageFor
|
|
566
|
+
});
|
|
462
567
|
}
|
|
463
568
|
|
|
464
569
|
// src/core/executeOrder/handleSolanaExecution.ts
|
|
465
|
-
var
|
|
570
|
+
var import_intents_sdk9 = require("@shogun-sdk/intents-sdk");
|
|
466
571
|
var import_web3 = require("@solana/web3.js");
|
|
467
572
|
async function handleSolanaExecution({
|
|
468
573
|
recipientAddress,
|
|
@@ -492,10 +597,9 @@ async function handleSolanaExecution({
|
|
|
492
597
|
rpcUrl: wallet.rpcUrl
|
|
493
598
|
});
|
|
494
599
|
const transaction = import_web3.VersionedTransaction.deserialize(Uint8Array.from(txData.txBytes));
|
|
495
|
-
update("
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
update("submitting", messageFor("submitting"));
|
|
600
|
+
update("processing", messageFor("signing"));
|
|
601
|
+
await wallet.sendTransaction(transaction);
|
|
602
|
+
update("processing", messageFor("submitting"));
|
|
499
603
|
const response = await submitToAuctioneer({
|
|
500
604
|
order,
|
|
501
605
|
isSingleChain,
|
|
@@ -504,13 +608,17 @@ async function handleSolanaExecution({
|
|
|
504
608
|
if (!response.success) {
|
|
505
609
|
throw new Error("Auctioneer submission failed");
|
|
506
610
|
}
|
|
507
|
-
update("
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
611
|
+
update("initiated", messageFor("initiated"));
|
|
612
|
+
const { jwt, intentId: orderId } = response.data;
|
|
613
|
+
update("initiated", messageFor("shogun_processing"));
|
|
614
|
+
const status = await pollOrderStatus(jwt, orderId);
|
|
615
|
+
return await handleOrderPollingResult({
|
|
616
|
+
status,
|
|
617
|
+
orderId,
|
|
618
|
+
chainId: SOLANA_CHAIN_ID,
|
|
619
|
+
update,
|
|
620
|
+
messageFor
|
|
621
|
+
});
|
|
514
622
|
}
|
|
515
623
|
async function getSolanaOrderInstructions({
|
|
516
624
|
order,
|
|
@@ -518,11 +626,11 @@ async function getSolanaOrderInstructions({
|
|
|
518
626
|
rpcUrl
|
|
519
627
|
}) {
|
|
520
628
|
if (isSingleChain) {
|
|
521
|
-
return await (0,
|
|
629
|
+
return await (0, import_intents_sdk9.getSolanaSingleChainOrderInstructions)(order, {
|
|
522
630
|
rpcUrl
|
|
523
631
|
});
|
|
524
632
|
}
|
|
525
|
-
return await (0,
|
|
633
|
+
return await (0, import_intents_sdk9.getSolanaCrossChainOrderInstructions)(order, {
|
|
526
634
|
rpcUrl
|
|
527
635
|
});
|
|
528
636
|
}
|
|
@@ -562,7 +670,7 @@ async function executeOrder({
|
|
|
562
670
|
const isSingleChain = tokenIn.chainId === tokenOut.chainId;
|
|
563
671
|
const chainId = Number(tokenIn.chainId);
|
|
564
672
|
update("processing", messageFor("processing"));
|
|
565
|
-
if ((0,
|
|
673
|
+
if ((0, import_intents_sdk10.isEvmChain)(chainId)) {
|
|
566
674
|
return await handleEvmExecution({
|
|
567
675
|
recipientAddress,
|
|
568
676
|
quote,
|
|
@@ -574,7 +682,7 @@ async function executeOrder({
|
|
|
574
682
|
update
|
|
575
683
|
});
|
|
576
684
|
}
|
|
577
|
-
if (chainId ===
|
|
685
|
+
if (chainId === import_intents_sdk10.ChainID.Solana) {
|
|
578
686
|
return await handleSolanaExecution({
|
|
579
687
|
recipientAddress,
|
|
580
688
|
quote,
|
|
@@ -600,7 +708,7 @@ function normalizeWallet(wallet) {
|
|
|
600
708
|
}
|
|
601
709
|
|
|
602
710
|
// src/core/index.ts
|
|
603
|
-
var
|
|
711
|
+
var import_intents_sdk11 = require("@shogun-sdk/intents-sdk");
|
|
604
712
|
// Annotate the CommonJS export names for ESM import in node:
|
|
605
713
|
0 && (module.exports = {
|
|
606
714
|
ChainID,
|
package/dist/core.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TokenSearchParams, TokenSearchResponse, ChainID } from '@shogun-sdk/intents-sdk';
|
|
2
2
|
export { ChainID, isEvmChain } from '@shogun-sdk/intents-sdk';
|
|
3
|
-
import { S as SwapQuoteParams, a as SwapQuoteResponse, Q as QuoteTokenInfo, B as BalanceRequestParams, c as BalanceResponse } from './execute-
|
|
4
|
-
export { P as PlaceOrderResult, b as Stage, T as TokenBalance, f as TokenInfo, d as TokenSearchResponse, e as executeOrder } from './execute-
|
|
3
|
+
import { S as SwapQuoteParams, a as SwapQuoteResponse, Q as QuoteTokenInfo, B as BalanceRequestParams, c as BalanceResponse } from './execute-DEFbp1WZ.cjs';
|
|
4
|
+
export { P as PlaceOrderResult, b as Stage, T as TokenBalance, f as TokenInfo, d as TokenSearchResponse, e as executeOrder } from './execute-DEFbp1WZ.cjs';
|
|
5
5
|
import { WalletClient } from 'viem';
|
|
6
6
|
import { A as AdaptedWallet } from './wallet-MmUIz8GE.cjs';
|
|
7
7
|
import '@mysten/sui/transactions';
|
package/dist/core.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TokenSearchParams, TokenSearchResponse, ChainID } from '@shogun-sdk/intents-sdk';
|
|
2
2
|
export { ChainID, isEvmChain } from '@shogun-sdk/intents-sdk';
|
|
3
|
-
import { S as SwapQuoteParams, a as SwapQuoteResponse, Q as QuoteTokenInfo, B as BalanceRequestParams, c as BalanceResponse } from './execute-
|
|
4
|
-
export { P as PlaceOrderResult, b as Stage, T as TokenBalance, f as TokenInfo, d as TokenSearchResponse, e as executeOrder } from './execute-
|
|
3
|
+
import { S as SwapQuoteParams, a as SwapQuoteResponse, Q as QuoteTokenInfo, B as BalanceRequestParams, c as BalanceResponse } from './execute-St0oo8GR.js';
|
|
4
|
+
export { P as PlaceOrderResult, b as Stage, T as TokenBalance, f as TokenInfo, d as TokenSearchResponse, e as executeOrder } from './execute-St0oo8GR.js';
|
|
5
5
|
import { WalletClient } from 'viem';
|
|
6
6
|
import { A as AdaptedWallet } from './wallet-MmUIz8GE.js';
|
|
7
7
|
import '@mysten/sui/transactions';
|
package/dist/core.js
CHANGED
|
@@ -139,22 +139,26 @@ async function getQuote(params) {
|
|
|
139
139
|
tokenOut: params.tokenOut.address,
|
|
140
140
|
amount: params.amount
|
|
141
141
|
});
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
const slippage = Math.min(Math.max(slippageDecimal, 0), 0.5);
|
|
142
|
+
console.log({ data });
|
|
143
|
+
const slippagePercent = Math.min(Math.max(params.slippage ?? 0.5, 0), 50);
|
|
145
144
|
let warning;
|
|
146
|
-
if (
|
|
147
|
-
warning = `\u26A0\uFE0F High slippage tolerance (${
|
|
145
|
+
if (slippagePercent > 10) {
|
|
146
|
+
warning = `\u26A0\uFE0F High slippage tolerance (${slippagePercent.toFixed(2)}%) \u2014 price may vary significantly.`;
|
|
148
147
|
}
|
|
149
|
-
const estimatedAmountOut = BigInt(data.
|
|
150
|
-
const slippageBps = BigInt(Math.round(
|
|
148
|
+
const estimatedAmountOut = BigInt(data.estimatedAmountOut);
|
|
149
|
+
const slippageBps = BigInt(Math.round(slippagePercent * 100));
|
|
151
150
|
const estimatedAmountOutAfterSlippage = estimatedAmountOut * (10000n - slippageBps) / 10000n;
|
|
151
|
+
const pricePerTokenOutInUsd = data.estimatedAmountOutUsd / Number(data.estimatedAmountOut);
|
|
152
|
+
const amountOutUsdAfterSlippage = Number(estimatedAmountOutAfterSlippage) * pricePerTokenOutInUsd;
|
|
153
|
+
const minStablecoinsAmountValue = BigInt(data.estimatedAmountInAsMinStablecoinAmount);
|
|
154
|
+
const minStablecoinsAmountAfterSlippage = minStablecoinsAmountValue * (10000n - slippageBps) / 10000n;
|
|
152
155
|
const pricePerInputToken = estimatedAmountOut * 10n ** BigInt(params.tokenIn.decimals ?? 18) / BigInt(params.amount);
|
|
153
156
|
return {
|
|
154
|
-
amountOut:
|
|
155
|
-
amountOutUsd:
|
|
157
|
+
amountOut: estimatedAmountOutAfterSlippage,
|
|
158
|
+
amountOutUsd: amountOutUsdAfterSlippage,
|
|
156
159
|
amountInUsd: data.amountInUsd,
|
|
157
|
-
|
|
160
|
+
// Input USD stays the same
|
|
161
|
+
minStablecoinsAmount: minStablecoinsAmountAfterSlippage,
|
|
158
162
|
tokenIn: {
|
|
159
163
|
address: params.tokenIn.address,
|
|
160
164
|
decimals: params.tokenIn.decimals ?? 18,
|
|
@@ -167,10 +171,11 @@ async function getQuote(params) {
|
|
|
167
171
|
},
|
|
168
172
|
amountIn: params.amount,
|
|
169
173
|
pricePerInputToken,
|
|
170
|
-
slippage,
|
|
174
|
+
slippage: slippagePercent,
|
|
171
175
|
internal: {
|
|
172
176
|
...data,
|
|
173
|
-
estimatedAmountOutReduced: estimatedAmountOutAfterSlippage
|
|
177
|
+
estimatedAmountOutReduced: estimatedAmountOutAfterSlippage,
|
|
178
|
+
estimatedAmountOutUsdReduced: amountOutUsdAfterSlippage
|
|
174
179
|
},
|
|
175
180
|
warning
|
|
176
181
|
};
|
|
@@ -243,7 +248,7 @@ async function getBalances(params, options) {
|
|
|
243
248
|
}
|
|
244
249
|
|
|
245
250
|
// src/core/executeOrder/execute.ts
|
|
246
|
-
import { ChainID as
|
|
251
|
+
import { ChainID as ChainID3, isEvmChain as isEvmChain2 } from "@shogun-sdk/intents-sdk";
|
|
247
252
|
import { BaseError } from "viem";
|
|
248
253
|
|
|
249
254
|
// src/wallet-adapter/evm-wallet-adapter/adapter.ts
|
|
@@ -324,10 +329,12 @@ var DEFAULT_STAGE_MESSAGES = {
|
|
|
324
329
|
processing: "Preparing transaction for execution",
|
|
325
330
|
approving: "Approving token allowance",
|
|
326
331
|
approved: "Token approved successfully",
|
|
327
|
-
signing: "Signing
|
|
328
|
-
submitting: "Submitting
|
|
329
|
-
|
|
330
|
-
|
|
332
|
+
signing: "Signing transaction for submission",
|
|
333
|
+
submitting: "Submitting transaction",
|
|
334
|
+
initiated: "Transaction initiated.",
|
|
335
|
+
success: "Transaction Executed successfully",
|
|
336
|
+
shogun_processing: "Shogun is processing your transaction",
|
|
337
|
+
error: "Transaction failed during submission"
|
|
331
338
|
};
|
|
332
339
|
|
|
333
340
|
// src/core/executeOrder/buildOrder.ts
|
|
@@ -366,6 +373,95 @@ async function buildOrder({
|
|
|
366
373
|
});
|
|
367
374
|
}
|
|
368
375
|
|
|
376
|
+
// src/utils/pollOrderStatus.ts
|
|
377
|
+
import { AUCTIONEER_URL } from "@shogun-sdk/intents-sdk";
|
|
378
|
+
async function pollOrderStatus(jwt, orderId, options = {}) {
|
|
379
|
+
const { intervalMs = 2e3, timeoutMs = 3e5 } = options;
|
|
380
|
+
const startTime = Date.now();
|
|
381
|
+
return new Promise((resolve, reject) => {
|
|
382
|
+
const pollInterval = setInterval(async () => {
|
|
383
|
+
try {
|
|
384
|
+
if (Date.now() - startTime > timeoutMs) {
|
|
385
|
+
clearInterval(pollInterval);
|
|
386
|
+
return resolve("Timeout");
|
|
387
|
+
}
|
|
388
|
+
const res = await fetch(`${AUCTIONEER_URL}/user_intent`, {
|
|
389
|
+
method: "GET",
|
|
390
|
+
headers: {
|
|
391
|
+
Authorization: `Bearer ${jwt}`,
|
|
392
|
+
"Content-Type": "application/json"
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
if (!res.ok) {
|
|
396
|
+
clearInterval(pollInterval);
|
|
397
|
+
return reject(
|
|
398
|
+
new Error(`Failed to fetch orders: ${res.status} ${res.statusText}`)
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
const json = await res.json();
|
|
402
|
+
const data = json?.data ?? {};
|
|
403
|
+
const allOrders = [
|
|
404
|
+
...data.crossChainDcaOrders ?? [],
|
|
405
|
+
...data.crossChainLimitOrders ?? [],
|
|
406
|
+
...data.singleChainDcaOrders ?? [],
|
|
407
|
+
...data.singleChainLimitOrders ?? []
|
|
408
|
+
];
|
|
409
|
+
const targetOrder = allOrders.find((o) => o.orderId === orderId);
|
|
410
|
+
if (!targetOrder) {
|
|
411
|
+
clearInterval(pollInterval);
|
|
412
|
+
return resolve("NotFound");
|
|
413
|
+
}
|
|
414
|
+
const { orderStatus } = targetOrder;
|
|
415
|
+
if (orderStatus === "Fulfilled" || orderStatus === "Cancelled" || orderStatus === "Outdated") {
|
|
416
|
+
clearInterval(pollInterval);
|
|
417
|
+
return resolve(orderStatus);
|
|
418
|
+
}
|
|
419
|
+
} catch (error) {
|
|
420
|
+
clearInterval(pollInterval);
|
|
421
|
+
return reject(error);
|
|
422
|
+
}
|
|
423
|
+
}, intervalMs);
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// src/core/executeOrder/handleOrderPollingResult.ts
|
|
428
|
+
async function handleOrderPollingResult({
|
|
429
|
+
status,
|
|
430
|
+
orderId,
|
|
431
|
+
chainId,
|
|
432
|
+
update,
|
|
433
|
+
messageFor
|
|
434
|
+
}) {
|
|
435
|
+
switch (status) {
|
|
436
|
+
case "Fulfilled":
|
|
437
|
+
update("success", messageFor("success"));
|
|
438
|
+
return {
|
|
439
|
+
status: true,
|
|
440
|
+
orderId,
|
|
441
|
+
chainId,
|
|
442
|
+
finalStatus: status,
|
|
443
|
+
stage: "success"
|
|
444
|
+
};
|
|
445
|
+
case "Cancelled":
|
|
446
|
+
update("error", "Order was cancelled before fulfillment");
|
|
447
|
+
break;
|
|
448
|
+
case "Timeout":
|
|
449
|
+
update("error", "Order polling timed out");
|
|
450
|
+
break;
|
|
451
|
+
case "NotFound":
|
|
452
|
+
default:
|
|
453
|
+
update("error", "Order not found");
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
return {
|
|
457
|
+
status: false,
|
|
458
|
+
orderId,
|
|
459
|
+
chainId,
|
|
460
|
+
finalStatus: status,
|
|
461
|
+
stage: "error"
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
|
|
369
465
|
// src/core/executeOrder/handleEvmExecution.ts
|
|
370
466
|
async function handleEvmExecution({
|
|
371
467
|
recipientAddress,
|
|
@@ -394,7 +490,7 @@ async function handleEvmExecution({
|
|
|
394
490
|
from: accountAddress
|
|
395
491
|
});
|
|
396
492
|
}
|
|
397
|
-
update("
|
|
493
|
+
update("processing", messageFor("approving"));
|
|
398
494
|
await wallet.sendTransaction({
|
|
399
495
|
to: tokenIn,
|
|
400
496
|
data: encodeFunctionData({
|
|
@@ -405,7 +501,7 @@ async function handleEvmExecution({
|
|
|
405
501
|
value: 0n,
|
|
406
502
|
from: accountAddress
|
|
407
503
|
});
|
|
408
|
-
update("
|
|
504
|
+
update("processing", messageFor("approved"));
|
|
409
505
|
const destination = recipientAddress ?? accountAddress;
|
|
410
506
|
const order = await buildOrder({
|
|
411
507
|
quote,
|
|
@@ -414,24 +510,32 @@ async function handleEvmExecution({
|
|
|
414
510
|
deadline,
|
|
415
511
|
isSingleChain
|
|
416
512
|
});
|
|
417
|
-
update("
|
|
513
|
+
update("processing", messageFor("signing"));
|
|
418
514
|
const { orderTypedData, nonce } = isSingleChain ? await getEVMSingleChainOrderTypedData(order) : await getEVMCrossChainOrderTypedData(order);
|
|
419
515
|
if (!wallet.signTypedData) {
|
|
420
516
|
throw new Error("Wallet does not support EIP-712 signing");
|
|
421
517
|
}
|
|
422
518
|
const signature = await wallet.signTypedData(serializeBigIntsToStrings(orderTypedData));
|
|
423
|
-
update("
|
|
519
|
+
update("processing", messageFor("submitting"));
|
|
424
520
|
const res = await order.sendToAuctioneer({ signature, nonce: nonce.toString() });
|
|
425
521
|
if (!res.success) {
|
|
426
522
|
throw new Error("Auctioneer submission failed");
|
|
427
523
|
}
|
|
428
|
-
update("
|
|
429
|
-
|
|
524
|
+
update("initiated", messageFor("initiated"));
|
|
525
|
+
const { jwt, intentId: orderId } = res.data;
|
|
526
|
+
update("initiated", messageFor("shogun_processing"));
|
|
527
|
+
const status = await pollOrderStatus(jwt, orderId);
|
|
528
|
+
return await handleOrderPollingResult({
|
|
529
|
+
status,
|
|
530
|
+
orderId,
|
|
531
|
+
chainId,
|
|
532
|
+
update,
|
|
533
|
+
messageFor
|
|
534
|
+
});
|
|
430
535
|
}
|
|
431
536
|
|
|
432
537
|
// src/core/executeOrder/handleSolanaExecution.ts
|
|
433
538
|
import {
|
|
434
|
-
ChainID as ChainID3,
|
|
435
539
|
getSolanaSingleChainOrderInstructions,
|
|
436
540
|
getSolanaCrossChainOrderInstructions
|
|
437
541
|
} from "@shogun-sdk/intents-sdk";
|
|
@@ -464,10 +568,9 @@ async function handleSolanaExecution({
|
|
|
464
568
|
rpcUrl: wallet.rpcUrl
|
|
465
569
|
});
|
|
466
570
|
const transaction = VersionedTransaction.deserialize(Uint8Array.from(txData.txBytes));
|
|
467
|
-
update("
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
update("submitting", messageFor("submitting"));
|
|
571
|
+
update("processing", messageFor("signing"));
|
|
572
|
+
await wallet.sendTransaction(transaction);
|
|
573
|
+
update("processing", messageFor("submitting"));
|
|
471
574
|
const response = await submitToAuctioneer({
|
|
472
575
|
order,
|
|
473
576
|
isSingleChain,
|
|
@@ -476,13 +579,17 @@ async function handleSolanaExecution({
|
|
|
476
579
|
if (!response.success) {
|
|
477
580
|
throw new Error("Auctioneer submission failed");
|
|
478
581
|
}
|
|
479
|
-
update("
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
582
|
+
update("initiated", messageFor("initiated"));
|
|
583
|
+
const { jwt, intentId: orderId } = response.data;
|
|
584
|
+
update("initiated", messageFor("shogun_processing"));
|
|
585
|
+
const status = await pollOrderStatus(jwt, orderId);
|
|
586
|
+
return await handleOrderPollingResult({
|
|
587
|
+
status,
|
|
588
|
+
orderId,
|
|
589
|
+
chainId: SOLANA_CHAIN_ID,
|
|
590
|
+
update,
|
|
591
|
+
messageFor
|
|
592
|
+
});
|
|
486
593
|
}
|
|
487
594
|
async function getSolanaOrderInstructions({
|
|
488
595
|
order,
|
|
@@ -546,7 +653,7 @@ async function executeOrder({
|
|
|
546
653
|
update
|
|
547
654
|
});
|
|
548
655
|
}
|
|
549
|
-
if (chainId ===
|
|
656
|
+
if (chainId === ChainID3.Solana) {
|
|
550
657
|
return await handleSolanaExecution({
|
|
551
658
|
recipientAddress,
|
|
552
659
|
quote,
|
|
@@ -572,9 +679,9 @@ function normalizeWallet(wallet) {
|
|
|
572
679
|
}
|
|
573
680
|
|
|
574
681
|
// src/core/index.ts
|
|
575
|
-
import { ChainID as
|
|
682
|
+
import { ChainID as ChainID4, isEvmChain as isEvmChain3 } from "@shogun-sdk/intents-sdk";
|
|
576
683
|
export {
|
|
577
|
-
|
|
684
|
+
ChainID4 as ChainID,
|
|
578
685
|
NATIVE_TOKEN,
|
|
579
686
|
SOLANA_CHAIN_ID,
|
|
580
687
|
SupportedChains,
|