@shogun-sdk/swap 0.0.2-test.25 → 0.0.2-test.27
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 +408 -110
- package/dist/core.d.cts +4 -137
- package/dist/core.d.ts +4 -137
- package/dist/core.js +386 -86
- package/dist/index-CmsKzdEu.d.cts +377 -0
- package/dist/index-Qoc6Q9XN.d.ts +377 -0
- package/dist/index.cjs +401 -497
- package/dist/index.d.cts +4 -9
- package/dist/index.d.ts +4 -9
- package/dist/index.js +379 -475
- package/dist/react.cjs +924 -469
- package/dist/react.d.cts +258 -82
- package/dist/react.d.ts +258 -82
- package/dist/react.js +890 -443
- package/dist/{wallet-BhuMJ3K_.d.cts → wallet-B9bKceyN.d.cts} +1 -2
- package/dist/{wallet-BhuMJ3K_.d.ts → wallet-B9bKceyN.d.ts} +1 -2
- package/dist/wallet-adapter.cjs +25607 -11
- package/dist/wallet-adapter.d.cts +1 -2
- package/dist/wallet-adapter.d.ts +1 -2
- package/dist/wallet-adapter.js +25626 -6
- package/package.json +44 -14
- package/dist/execute-D2qcOzkI.d.ts +0 -145
- package/dist/execute-Xvw4wXBo.d.cts +0 -145
package/dist/core.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return intentsGetTokenList(params);
|
|
5
|
-
}
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
4
|
|
|
7
5
|
// src/core/getQuote.ts
|
|
8
6
|
import { QuoteProvider } from "@shogun-sdk/intents-sdk";
|
|
9
7
|
import { parseUnits } from "viem";
|
|
10
8
|
|
|
11
|
-
// src/core/
|
|
12
|
-
import { isEvmChain } from "@shogun-sdk/intents-sdk";
|
|
9
|
+
// src/core/execute/normalizeNative.ts
|
|
10
|
+
import { isEvmChain as isEvmChain2 } from "@shogun-sdk/intents-sdk";
|
|
13
11
|
|
|
14
12
|
// src/utils/address.ts
|
|
15
13
|
import { zeroAddress } from "viem";
|
|
@@ -29,11 +27,25 @@ function normalizeEvmTokenAddress(address) {
|
|
|
29
27
|
}
|
|
30
28
|
|
|
31
29
|
// src/utils/chain.ts
|
|
32
|
-
import { ChainID } from "@shogun-sdk/intents-sdk";
|
|
33
|
-
var SOLANA_CHAIN_ID =
|
|
34
|
-
var
|
|
30
|
+
import { ChainID as BaseChainID, isEvmChain as isEvmChainIntent } from "@shogun-sdk/intents-sdk";
|
|
31
|
+
var SOLANA_CHAIN_ID = BaseChainID.Solana;
|
|
32
|
+
var CURRENT_SUPPORTED = [
|
|
33
|
+
BaseChainID.Solana,
|
|
34
|
+
BaseChainID.BSC,
|
|
35
|
+
BaseChainID.Base
|
|
36
|
+
];
|
|
37
|
+
var ChainId = Object.entries(BaseChainID).reduce(
|
|
38
|
+
(acc, [key, value]) => {
|
|
39
|
+
if (typeof value === "number" && CURRENT_SUPPORTED.includes(value)) {
|
|
40
|
+
acc[key] = value;
|
|
41
|
+
}
|
|
42
|
+
return acc;
|
|
43
|
+
},
|
|
44
|
+
{}
|
|
45
|
+
);
|
|
46
|
+
var SupportedChainsInternal = [
|
|
35
47
|
{
|
|
36
|
-
id:
|
|
48
|
+
id: BaseChainID.Arbitrum,
|
|
37
49
|
name: "Arbitrum",
|
|
38
50
|
isEVM: true,
|
|
39
51
|
wrapped: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
@@ -42,7 +54,7 @@ var SupportedChains = [
|
|
|
42
54
|
tokenAddress: NATIVE_TOKEN.ETH
|
|
43
55
|
},
|
|
44
56
|
{
|
|
45
|
-
id:
|
|
57
|
+
id: BaseChainID.Optimism,
|
|
46
58
|
name: "Optimism",
|
|
47
59
|
isEVM: true,
|
|
48
60
|
wrapped: "0x4200000000000000000000000000000000000006",
|
|
@@ -51,7 +63,7 @@ var SupportedChains = [
|
|
|
51
63
|
tokenAddress: NATIVE_TOKEN.ETH
|
|
52
64
|
},
|
|
53
65
|
{
|
|
54
|
-
id:
|
|
66
|
+
id: BaseChainID.Base,
|
|
55
67
|
name: "Base",
|
|
56
68
|
isEVM: true,
|
|
57
69
|
wrapped: "0x4200000000000000000000000000000000000006",
|
|
@@ -60,7 +72,7 @@ var SupportedChains = [
|
|
|
60
72
|
tokenAddress: NATIVE_TOKEN.ETH
|
|
61
73
|
},
|
|
62
74
|
{
|
|
63
|
-
id:
|
|
75
|
+
id: BaseChainID.Hyperliquid,
|
|
64
76
|
name: "Hyperliquid",
|
|
65
77
|
isEVM: true,
|
|
66
78
|
wrapped: "0x5555555555555555555555555555555555555555",
|
|
@@ -69,7 +81,7 @@ var SupportedChains = [
|
|
|
69
81
|
tokenAddress: NATIVE_TOKEN.ETH
|
|
70
82
|
},
|
|
71
83
|
{
|
|
72
|
-
id:
|
|
84
|
+
id: BaseChainID.BSC,
|
|
73
85
|
name: "BSC",
|
|
74
86
|
isEVM: true,
|
|
75
87
|
wrapped: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
|
|
@@ -87,6 +99,10 @@ var SupportedChains = [
|
|
|
87
99
|
tokenAddress: NATIVE_TOKEN.SOL
|
|
88
100
|
}
|
|
89
101
|
];
|
|
102
|
+
var SupportedChains = SupportedChainsInternal.filter(
|
|
103
|
+
(c) => CURRENT_SUPPORTED.includes(c.id)
|
|
104
|
+
);
|
|
105
|
+
var isEvmChain = isEvmChainIntent;
|
|
90
106
|
|
|
91
107
|
// src/utils/viem.ts
|
|
92
108
|
function isViemWalletClient(wallet) {
|
|
@@ -114,9 +130,9 @@ function serializeBigIntsToStrings(obj) {
|
|
|
114
130
|
return obj;
|
|
115
131
|
}
|
|
116
132
|
|
|
117
|
-
// src/core/
|
|
133
|
+
// src/core/execute/normalizeNative.ts
|
|
118
134
|
function normalizeNative(chainId, address) {
|
|
119
|
-
if (
|
|
135
|
+
if (isEvmChain2(chainId) && isNativeAddress(address)) {
|
|
120
136
|
const chain = SupportedChains.find((c) => c.id === chainId);
|
|
121
137
|
if (!chain?.wrapped)
|
|
122
138
|
throw new Error(`Wrapped token not found for chainId ${chainId}`);
|
|
@@ -127,13 +143,14 @@ function normalizeNative(chainId, address) {
|
|
|
127
143
|
|
|
128
144
|
// src/core/getQuote.ts
|
|
129
145
|
async function getQuote(params) {
|
|
146
|
+
const amount = BigInt(params.amount);
|
|
130
147
|
if (!params.tokenIn?.address || !params.tokenOut?.address) {
|
|
131
148
|
throw new Error("Both tokenIn and tokenOut must include an address.");
|
|
132
149
|
}
|
|
133
150
|
if (!params.sourceChainId || !params.destChainId) {
|
|
134
151
|
throw new Error("Both sourceChainId and destChainId are required.");
|
|
135
152
|
}
|
|
136
|
-
if (
|
|
153
|
+
if (amount <= 0n) {
|
|
137
154
|
throw new Error("Amount must be greater than 0.");
|
|
138
155
|
}
|
|
139
156
|
const normalizedTokenIn = normalizeNative(params.sourceChainId, params.tokenIn.address);
|
|
@@ -142,7 +159,7 @@ async function getQuote(params) {
|
|
|
142
159
|
destChainId: params.destChainId,
|
|
143
160
|
tokenIn: normalizedTokenIn,
|
|
144
161
|
tokenOut: params.tokenOut.address,
|
|
145
|
-
amount
|
|
162
|
+
amount
|
|
146
163
|
});
|
|
147
164
|
const slippagePercent = Math.min(Math.max(params.slippage ?? 0.5, 0), 50);
|
|
148
165
|
let warning;
|
|
@@ -173,7 +190,7 @@ async function getQuote(params) {
|
|
|
173
190
|
decimals: params.tokenOut.decimals ?? 18,
|
|
174
191
|
chainId: params.destChainId
|
|
175
192
|
},
|
|
176
|
-
amountIn: params.amount,
|
|
193
|
+
amountIn: BigInt(params.amount),
|
|
177
194
|
pricePerInputToken,
|
|
178
195
|
slippage: slippagePercent,
|
|
179
196
|
internal: {
|
|
@@ -197,7 +214,7 @@ function buildQuoteParams({
|
|
|
197
214
|
tokenOut,
|
|
198
215
|
sourceChainId,
|
|
199
216
|
destChainId,
|
|
200
|
-
amount: parseUnits(amount.toString(), tokenIn.decimals ?? 18),
|
|
217
|
+
amount: parseUnits(amount.toString(), tokenIn.decimals ?? 18).toString(),
|
|
201
218
|
slippage
|
|
202
219
|
};
|
|
203
220
|
}
|
|
@@ -244,20 +261,66 @@ async function getBalances(params, options) {
|
|
|
244
261
|
const evmItems = data.evm?.items ?? [];
|
|
245
262
|
const svmItems = data.svm?.items ?? [];
|
|
246
263
|
const combined = [...evmItems, ...svmItems];
|
|
264
|
+
const filtered = combined.filter(
|
|
265
|
+
(b) => CURRENT_SUPPORTED.includes(b.chainId)
|
|
266
|
+
);
|
|
247
267
|
return {
|
|
248
|
-
results:
|
|
268
|
+
results: filtered,
|
|
249
269
|
nextCursorEvm: data.evm?.cursor ?? null,
|
|
250
270
|
nextCursorSvm: data.svm?.cursor ?? null
|
|
251
271
|
};
|
|
252
272
|
}
|
|
253
273
|
|
|
254
|
-
// src/core/
|
|
255
|
-
import {
|
|
256
|
-
|
|
274
|
+
// src/core/token-list.ts
|
|
275
|
+
import { TOKEN_SEARCH_API_BASE_URL as TOKEN_SEARCH_API_BASE_URL2 } from "@shogun-sdk/intents-sdk";
|
|
276
|
+
async function getTokenList(params) {
|
|
277
|
+
const url = new URL(`${TOKEN_SEARCH_API_BASE_URL2}/tokens/search`);
|
|
278
|
+
if (params.q) url.searchParams.append("q", params.q);
|
|
279
|
+
if (params.networkId) url.searchParams.append("networkId", String(params.networkId));
|
|
280
|
+
if (params.page) url.searchParams.append("page", String(params.page));
|
|
281
|
+
if (params.limit) url.searchParams.append("limit", String(params.limit));
|
|
282
|
+
const res = await fetch(url.toString(), {
|
|
283
|
+
signal: params.signal
|
|
284
|
+
});
|
|
285
|
+
if (!res.ok) {
|
|
286
|
+
throw new Error(`Failed to fetch tokens: ${res.status} ${res.statusText}`);
|
|
287
|
+
}
|
|
288
|
+
const data = await res.json();
|
|
289
|
+
const filteredResults = data.results.filter(
|
|
290
|
+
(token) => CURRENT_SUPPORTED.includes(token.chainId)
|
|
291
|
+
);
|
|
292
|
+
return {
|
|
293
|
+
...data,
|
|
294
|
+
results: filteredResults,
|
|
295
|
+
count: filteredResults.length
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// src/core/token.ts
|
|
300
|
+
import { TOKEN_SEARCH_API_BASE_URL as TOKEN_SEARCH_API_BASE_URL3 } from "@shogun-sdk/intents-sdk";
|
|
301
|
+
async function getTokensData(addresses) {
|
|
302
|
+
if (!addresses?.length) return [];
|
|
303
|
+
const response = await fetch(`${TOKEN_SEARCH_API_BASE_URL3}/tokens/tokens`, {
|
|
304
|
+
method: "POST",
|
|
305
|
+
headers: {
|
|
306
|
+
"Content-Type": "application/json",
|
|
307
|
+
accept: "*/*"
|
|
308
|
+
},
|
|
309
|
+
body: JSON.stringify({ addresses })
|
|
310
|
+
});
|
|
311
|
+
if (!response.ok) {
|
|
312
|
+
throw new Error(`Failed to fetch token data: ${response.statusText}`);
|
|
313
|
+
}
|
|
314
|
+
const data = await response.json();
|
|
315
|
+
const filtered = data.filter((t) => CURRENT_SUPPORTED.includes(Number(t.chainId)));
|
|
316
|
+
return filtered;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// src/core/execute/execute.ts
|
|
320
|
+
import { ChainID as ChainID2, isEvmChain as isEvmChain3 } from "@shogun-sdk/intents-sdk";
|
|
321
|
+
import "viem";
|
|
257
322
|
|
|
258
323
|
// src/wallet-adapter/evm-wallet-adapter/adapter.ts
|
|
259
|
-
import { utils as ethersUtils } from "ethers/lib/ethers.js";
|
|
260
|
-
import { hexValue } from "ethers/lib/utils.js";
|
|
261
324
|
import {
|
|
262
325
|
custom,
|
|
263
326
|
publicActions
|
|
@@ -347,14 +410,14 @@ var adaptViemWallet = (wallet) => {
|
|
|
347
410
|
};
|
|
348
411
|
};
|
|
349
412
|
|
|
350
|
-
// src/core/
|
|
413
|
+
// src/core/execute/handleEvmExecution.ts
|
|
351
414
|
import {
|
|
352
415
|
getEVMSingleChainOrderTypedData,
|
|
353
416
|
getEVMCrossChainOrderTypedData
|
|
354
417
|
} from "@shogun-sdk/intents-sdk";
|
|
355
418
|
import { encodeFunctionData as encodeFunctionData2 } from "viem";
|
|
356
419
|
|
|
357
|
-
// src/core/
|
|
420
|
+
// src/core/execute/stageMessages.ts
|
|
358
421
|
var DEFAULT_STAGE_MESSAGES = {
|
|
359
422
|
processing: "Preparing transaction for execution",
|
|
360
423
|
approving: "Approving token allowance",
|
|
@@ -363,20 +426,44 @@ var DEFAULT_STAGE_MESSAGES = {
|
|
|
363
426
|
submitting: "Submitting transaction",
|
|
364
427
|
initiated: "Transaction initiated.",
|
|
365
428
|
success: "Transaction Executed successfully",
|
|
429
|
+
success_limit: "Limit order has been submitted successfully.",
|
|
366
430
|
shogun_processing: "Shogun is processing your transaction",
|
|
367
431
|
error: "Transaction failed during submission"
|
|
368
432
|
};
|
|
369
433
|
|
|
370
|
-
// src/core/
|
|
434
|
+
// src/core/execute/buildOrder.ts
|
|
371
435
|
import { CrossChainOrder, SingleChainOrder } from "@shogun-sdk/intents-sdk";
|
|
436
|
+
import { formatUnits, parseUnits as parseUnits2 } from "viem";
|
|
437
|
+
|
|
438
|
+
// src/utils/order.ts
|
|
439
|
+
var OrderExecutionType = /* @__PURE__ */ ((OrderExecutionType2) => {
|
|
440
|
+
OrderExecutionType2["LIMIT"] = "limit";
|
|
441
|
+
OrderExecutionType2["MARKET"] = "market";
|
|
442
|
+
return OrderExecutionType2;
|
|
443
|
+
})(OrderExecutionType || {});
|
|
444
|
+
|
|
445
|
+
// src/core/execute/buildOrder.ts
|
|
372
446
|
async function buildOrder({
|
|
373
447
|
quote,
|
|
374
448
|
accountAddress,
|
|
375
449
|
destination,
|
|
376
450
|
deadline,
|
|
377
|
-
isSingleChain
|
|
451
|
+
isSingleChain,
|
|
452
|
+
orderType,
|
|
453
|
+
options
|
|
378
454
|
}) {
|
|
379
455
|
const { tokenIn, tokenOut } = quote;
|
|
456
|
+
let amountOutMin = BigInt(quote.internal.estimatedAmountOutReduced);
|
|
457
|
+
if (orderType === "limit" /* LIMIT */ && options && "executionPrice" in options) {
|
|
458
|
+
const executionPrice = Number(options.executionPrice);
|
|
459
|
+
if (Number.isFinite(executionPrice) && executionPrice > 0) {
|
|
460
|
+
const decimalsIn = tokenIn.decimals ?? 18;
|
|
461
|
+
const decimalsOut = tokenOut.decimals ?? 18;
|
|
462
|
+
const formattedAmountIn = Number(formatUnits(BigInt(quote.amountIn.toString()), decimalsIn));
|
|
463
|
+
const rawAmountOut = formattedAmountIn * executionPrice;
|
|
464
|
+
amountOutMin = parseUnits2(rawAmountOut.toString(), decimalsOut);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
380
467
|
if (isSingleChain) {
|
|
381
468
|
return await SingleChainOrder.create({
|
|
382
469
|
user: accountAddress,
|
|
@@ -384,7 +471,7 @@ async function buildOrder({
|
|
|
384
471
|
tokenIn: tokenIn.address,
|
|
385
472
|
tokenOut: tokenOut.address,
|
|
386
473
|
amountIn: quote.amountIn,
|
|
387
|
-
amountOutMin
|
|
474
|
+
amountOutMin,
|
|
388
475
|
deadline,
|
|
389
476
|
destinationAddress: destination
|
|
390
477
|
});
|
|
@@ -398,7 +485,7 @@ async function buildOrder({
|
|
|
398
485
|
destinationTokenAddress: tokenOut.address,
|
|
399
486
|
destinationAddress: destination,
|
|
400
487
|
deadline,
|
|
401
|
-
destinationTokenMinAmount:
|
|
488
|
+
destinationTokenMinAmount: amountOutMin,
|
|
402
489
|
minStablecoinAmount: quote.minStablecoinsAmount
|
|
403
490
|
});
|
|
404
491
|
}
|
|
@@ -469,7 +556,7 @@ async function pollOrderStatus(address, orderId, options = {}) {
|
|
|
469
556
|
});
|
|
470
557
|
}
|
|
471
558
|
|
|
472
|
-
// src/core/
|
|
559
|
+
// src/core/execute/handleOrderPollingResult.ts
|
|
473
560
|
async function handleOrderPollingResult({
|
|
474
561
|
status,
|
|
475
562
|
orderId,
|
|
@@ -507,7 +594,7 @@ async function handleOrderPollingResult({
|
|
|
507
594
|
};
|
|
508
595
|
}
|
|
509
596
|
|
|
510
|
-
// src/core/
|
|
597
|
+
// src/core/execute/ensurePermit2Allowance.ts
|
|
511
598
|
import { encodeFunctionData, erc20Abi, maxUint256 } from "viem";
|
|
512
599
|
import { PERMIT2_ADDRESS } from "@shogun-sdk/intents-sdk";
|
|
513
600
|
async function ensurePermit2Allowance({
|
|
@@ -552,7 +639,7 @@ async function ensurePermit2Allowance({
|
|
|
552
639
|
);
|
|
553
640
|
}
|
|
554
641
|
|
|
555
|
-
// src/core/
|
|
642
|
+
// src/core/execute/handleEvmExecution.ts
|
|
556
643
|
async function handleEvmExecution({
|
|
557
644
|
recipientAddress,
|
|
558
645
|
quote,
|
|
@@ -560,16 +647,19 @@ async function handleEvmExecution({
|
|
|
560
647
|
accountAddress,
|
|
561
648
|
wallet,
|
|
562
649
|
isSingleChain,
|
|
563
|
-
|
|
564
|
-
|
|
650
|
+
update,
|
|
651
|
+
orderType,
|
|
652
|
+
options
|
|
565
653
|
}) {
|
|
566
|
-
const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage];
|
|
654
|
+
const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage] ?? "";
|
|
655
|
+
const deadline = options?.deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
|
|
567
656
|
await wallet.switchChain(chainId);
|
|
568
657
|
const tokenIn = normalizeNative(chainId, quote.tokenIn.address);
|
|
569
658
|
quote.tokenOut.address = normalizeEvmTokenAddress(quote.tokenOut.address);
|
|
570
659
|
const shouldWrapNative = isNativeAddress(quote.tokenIn.address);
|
|
571
660
|
update("processing", shouldWrapNative ? `${messageFor("processing")} (wrapping native token)` : messageFor("processing"));
|
|
572
661
|
if (shouldWrapNative) {
|
|
662
|
+
quote.tokenIn.address === tokenIn;
|
|
573
663
|
await wallet.sendTransaction({
|
|
574
664
|
to: tokenIn,
|
|
575
665
|
data: encodeFunctionData2({
|
|
@@ -596,7 +686,9 @@ async function handleEvmExecution({
|
|
|
596
686
|
accountAddress,
|
|
597
687
|
destination,
|
|
598
688
|
deadline,
|
|
599
|
-
isSingleChain
|
|
689
|
+
isSingleChain,
|
|
690
|
+
orderType,
|
|
691
|
+
options
|
|
600
692
|
});
|
|
601
693
|
console.debug(`order`, order);
|
|
602
694
|
update("processing", messageFor("signing"));
|
|
@@ -620,17 +712,28 @@ async function handleEvmExecution({
|
|
|
620
712
|
update("initiated", messageFor("initiated"));
|
|
621
713
|
const { intentId: orderId } = res.data;
|
|
622
714
|
update("initiated", messageFor("shogun_processing"));
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
715
|
+
if (orderType === "limit" /* LIMIT */) {
|
|
716
|
+
update("success", messageFor("success_limit"));
|
|
717
|
+
return {
|
|
718
|
+
status: true,
|
|
719
|
+
orderId,
|
|
720
|
+
chainId,
|
|
721
|
+
finalStatus: "OrderPlaced",
|
|
722
|
+
stage: "success"
|
|
723
|
+
};
|
|
724
|
+
} else {
|
|
725
|
+
const status = await pollOrderStatus(accountAddress, orderId);
|
|
726
|
+
return await handleOrderPollingResult({
|
|
727
|
+
status,
|
|
728
|
+
orderId,
|
|
729
|
+
chainId,
|
|
730
|
+
update,
|
|
731
|
+
messageFor
|
|
732
|
+
});
|
|
733
|
+
}
|
|
631
734
|
}
|
|
632
735
|
|
|
633
|
-
// src/core/
|
|
736
|
+
// src/core/execute/handleSolanaExecution.ts
|
|
634
737
|
import {
|
|
635
738
|
getSolanaSingleChainOrderInstructions,
|
|
636
739
|
getSolanaCrossChainOrderInstructions
|
|
@@ -643,12 +746,14 @@ async function handleSolanaExecution({
|
|
|
643
746
|
isSingleChain,
|
|
644
747
|
update,
|
|
645
748
|
accountAddress,
|
|
646
|
-
|
|
749
|
+
orderType,
|
|
750
|
+
options
|
|
647
751
|
}) {
|
|
648
752
|
if (!wallet.rpcUrl) {
|
|
649
753
|
throw new Error("Solana wallet is missing rpcUrl");
|
|
650
754
|
}
|
|
651
|
-
const
|
|
755
|
+
const deadline = options?.deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
|
|
756
|
+
const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage] ?? "";
|
|
652
757
|
update("processing", messageFor("processing"));
|
|
653
758
|
const destination = recipientAddress ?? accountAddress;
|
|
654
759
|
const order = await buildOrder({
|
|
@@ -656,7 +761,9 @@ async function handleSolanaExecution({
|
|
|
656
761
|
accountAddress,
|
|
657
762
|
destination,
|
|
658
763
|
deadline,
|
|
659
|
-
isSingleChain
|
|
764
|
+
isSingleChain,
|
|
765
|
+
orderType,
|
|
766
|
+
options
|
|
660
767
|
});
|
|
661
768
|
const txData = await getSolanaOrderInstructions({
|
|
662
769
|
order,
|
|
@@ -676,16 +783,27 @@ async function handleSolanaExecution({
|
|
|
676
783
|
throw new Error("Auctioneer submission failed");
|
|
677
784
|
}
|
|
678
785
|
update("initiated", messageFor("initiated"));
|
|
679
|
-
const {
|
|
786
|
+
const { intentId: orderId } = response.data;
|
|
680
787
|
update("initiated", messageFor("shogun_processing"));
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
788
|
+
if (orderType === "limit" /* LIMIT */) {
|
|
789
|
+
update("success", messageFor("success_limit"));
|
|
790
|
+
return {
|
|
791
|
+
status: true,
|
|
792
|
+
orderId,
|
|
793
|
+
chainId: SOLANA_CHAIN_ID,
|
|
794
|
+
finalStatus: "OrderPlaced",
|
|
795
|
+
stage: "success"
|
|
796
|
+
};
|
|
797
|
+
} else {
|
|
798
|
+
const status = await pollOrderStatus(accountAddress, orderId);
|
|
799
|
+
return await handleOrderPollingResult({
|
|
800
|
+
status,
|
|
801
|
+
orderId,
|
|
802
|
+
chainId: SOLANA_CHAIN_ID,
|
|
803
|
+
update,
|
|
804
|
+
messageFor
|
|
805
|
+
});
|
|
806
|
+
}
|
|
689
807
|
}
|
|
690
808
|
async function getSolanaOrderInstructions({
|
|
691
809
|
order,
|
|
@@ -718,13 +836,14 @@ async function submitToAuctioneer({
|
|
|
718
836
|
});
|
|
719
837
|
}
|
|
720
838
|
|
|
721
|
-
// src/core/
|
|
839
|
+
// src/core/execute/execute.ts
|
|
722
840
|
async function executeOrder({
|
|
723
841
|
quote,
|
|
724
842
|
accountAddress,
|
|
725
843
|
recipientAddress,
|
|
726
844
|
wallet,
|
|
727
845
|
onStatus,
|
|
846
|
+
orderType = "market" /* MARKET */,
|
|
728
847
|
options = {}
|
|
729
848
|
}) {
|
|
730
849
|
const isDev = process.env.NODE_ENV !== "production";
|
|
@@ -737,21 +856,31 @@ async function executeOrder({
|
|
|
737
856
|
onStatus?.(stage, message ?? messageFor(stage));
|
|
738
857
|
};
|
|
739
858
|
try {
|
|
740
|
-
const deadline = options.deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
|
|
741
859
|
log("Starting execution:", {
|
|
742
860
|
accountAddress,
|
|
743
861
|
recipientAddress,
|
|
744
|
-
deadline,
|
|
745
862
|
tokenIn: quote?.tokenIn,
|
|
746
863
|
tokenOut: quote?.tokenOut
|
|
747
864
|
});
|
|
748
865
|
const adapter = normalizeWallet(wallet);
|
|
749
866
|
if (!adapter) throw new Error("No wallet provided");
|
|
750
867
|
const { tokenIn, tokenOut } = quote;
|
|
868
|
+
const srcChain = Number(tokenIn.chainId);
|
|
869
|
+
const destChain = Number(tokenOut.chainId);
|
|
870
|
+
if (!CURRENT_SUPPORTED.includes(srcChain) || !CURRENT_SUPPORTED.includes(destChain)) {
|
|
871
|
+
const unsupportedChains = [
|
|
872
|
+
!CURRENT_SUPPORTED.includes(srcChain) ? srcChain : null,
|
|
873
|
+
!CURRENT_SUPPORTED.includes(destChain) ? destChain : null
|
|
874
|
+
].filter(Boolean).join(", ");
|
|
875
|
+
const errorMsg = `Unsupported chain(s): ${unsupportedChains}`;
|
|
876
|
+
update("error", errorMsg);
|
|
877
|
+
log("Error:", errorMsg);
|
|
878
|
+
throw new Error(errorMsg);
|
|
879
|
+
}
|
|
751
880
|
const isSingleChain = tokenIn.chainId === tokenOut.chainId;
|
|
752
881
|
const chainId = Number(tokenIn.chainId);
|
|
753
882
|
update("processing");
|
|
754
|
-
if (
|
|
883
|
+
if (isEvmChain3(chainId)) {
|
|
755
884
|
log("Detected EVM chain:", chainId);
|
|
756
885
|
const result = await handleEvmExecution({
|
|
757
886
|
recipientAddress,
|
|
@@ -760,13 +889,14 @@ async function executeOrder({
|
|
|
760
889
|
accountAddress,
|
|
761
890
|
wallet: adapter,
|
|
762
891
|
isSingleChain,
|
|
763
|
-
|
|
764
|
-
|
|
892
|
+
update,
|
|
893
|
+
orderType,
|
|
894
|
+
options
|
|
765
895
|
});
|
|
766
896
|
log("EVM execution result:", result);
|
|
767
897
|
return result;
|
|
768
898
|
}
|
|
769
|
-
if (chainId ===
|
|
899
|
+
if (chainId === ChainID2.Solana) {
|
|
770
900
|
log("Detected Solana chain");
|
|
771
901
|
const result = await handleSolanaExecution({
|
|
772
902
|
recipientAddress,
|
|
@@ -774,8 +904,9 @@ async function executeOrder({
|
|
|
774
904
|
accountAddress,
|
|
775
905
|
wallet: adapter,
|
|
776
906
|
isSingleChain,
|
|
777
|
-
|
|
778
|
-
|
|
907
|
+
update,
|
|
908
|
+
orderType,
|
|
909
|
+
options
|
|
779
910
|
});
|
|
780
911
|
log("Solana execution result:", result);
|
|
781
912
|
return result;
|
|
@@ -785,8 +916,13 @@ async function executeOrder({
|
|
|
785
916
|
log("Error:", unsupported);
|
|
786
917
|
return { status: false, message: unsupported, stage: "error" };
|
|
787
918
|
} catch (error) {
|
|
788
|
-
|
|
789
|
-
|
|
919
|
+
let message = "An unknown error occurred";
|
|
920
|
+
if (error && typeof error === "object") {
|
|
921
|
+
const err = error;
|
|
922
|
+
message = err.details ?? err.message ?? message;
|
|
923
|
+
} else if (typeof error === "string") {
|
|
924
|
+
message = error;
|
|
925
|
+
}
|
|
790
926
|
update("error", message);
|
|
791
927
|
return { status: false, message, stage: "error" };
|
|
792
928
|
}
|
|
@@ -797,21 +933,185 @@ function normalizeWallet(wallet) {
|
|
|
797
933
|
return wallet;
|
|
798
934
|
}
|
|
799
935
|
|
|
800
|
-
// src/core/
|
|
801
|
-
import {
|
|
936
|
+
// src/core/orders/getOrders.ts
|
|
937
|
+
import { fetchUserOrders } from "@shogun-sdk/intents-sdk";
|
|
938
|
+
async function getOrders({
|
|
939
|
+
evmAddress,
|
|
940
|
+
solAddress
|
|
941
|
+
}) {
|
|
942
|
+
if (!evmAddress && !solAddress) {
|
|
943
|
+
throw new Error("At least one wallet address (EVM, Solana) must be provided.");
|
|
944
|
+
}
|
|
945
|
+
const orders = await fetchUserOrders(evmAddress, solAddress);
|
|
946
|
+
return orders;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
// src/core/client.ts
|
|
950
|
+
var SwapSDK = class {
|
|
951
|
+
constructor(config) {
|
|
952
|
+
__publicField(this, "apiKey");
|
|
953
|
+
/**
|
|
954
|
+
* Fetches metadata for one or more tokens from the Shogun Token Search API.
|
|
955
|
+
*
|
|
956
|
+
* ---
|
|
957
|
+
* ### Overview
|
|
958
|
+
* `getTokensData` retrieves normalized token information — such as symbol, name,
|
|
959
|
+
* decimals, logo URI, and verified status — for a given list of token addresses.
|
|
960
|
+
*
|
|
961
|
+
* It supports both **EVM** and **SVM (Solana)** tokens, returning metadata from
|
|
962
|
+
* Shogun’s unified token registry.
|
|
963
|
+
*
|
|
964
|
+
* ---
|
|
965
|
+
* @example
|
|
966
|
+
* ```ts
|
|
967
|
+
* const tokens = await getTokensData([
|
|
968
|
+
* "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
969
|
+
* "So11111111111111111111111111111111111111112", // SOL
|
|
970
|
+
* ]);
|
|
971
|
+
*
|
|
972
|
+
* console.log(tokens);
|
|
973
|
+
* [
|
|
974
|
+
* { symbol: "USDC", name: "USD Coin", chainId: 1, decimals: 6, ... },
|
|
975
|
+
* { symbol: "SOL", name: "Solana", chainId: 101, decimals: 9, ... }
|
|
976
|
+
* ]
|
|
977
|
+
* ```
|
|
978
|
+
*
|
|
979
|
+
* @param addresses - An array of token addresses (EVM or SVM) to fetch metadata for.
|
|
980
|
+
* @returns A promise resolving to an array of {@link TokenInfo} objects.
|
|
981
|
+
*
|
|
982
|
+
* @throws Will throw an error if the network request fails or the API responds with a non-OK status.
|
|
983
|
+
*/
|
|
984
|
+
__publicField(this, "getTokensData", getTokensData.bind(this));
|
|
985
|
+
if (!config.apiKey) {
|
|
986
|
+
throw new Error("SwapSDK: Missing API key");
|
|
987
|
+
}
|
|
988
|
+
this.apiKey = config.apiKey;
|
|
989
|
+
if (this.apiKey) void this.apiKey;
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Retrieves a swap quote for the given input and output tokens.
|
|
993
|
+
*
|
|
994
|
+
* @param params - Quote parameters including source/destination tokens and amount.
|
|
995
|
+
* @returns A normalized `SwapQuoteResponse` containing output amount, route, and metadata.
|
|
996
|
+
*/
|
|
997
|
+
async getQuote(params) {
|
|
998
|
+
return getQuote(params);
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* Fetches token balances for the specified user wallet(s).
|
|
1002
|
+
*
|
|
1003
|
+
* Supports both EVM and SVM (Solana) wallet addresses.
|
|
1004
|
+
*
|
|
1005
|
+
* @param params - Wallet address and optional chain filters.
|
|
1006
|
+
* @param options - Optional abort signal for cancellation.
|
|
1007
|
+
* @returns A unified balance response with per-chain token details.
|
|
1008
|
+
*/
|
|
1009
|
+
async getBalances(params, options) {
|
|
1010
|
+
return getBalances(params, options);
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Retrieves a list of verified tokens based on search query or chain filter.
|
|
1014
|
+
*
|
|
1015
|
+
* @param params - Search parameters (query, chain ID, pagination options).
|
|
1016
|
+
* @returns Paginated `TokenSearchResponse` containing token metadata.
|
|
1017
|
+
*/
|
|
1018
|
+
async getTokenList(params) {
|
|
1019
|
+
return getTokenList(params);
|
|
1020
|
+
}
|
|
1021
|
+
/**
|
|
1022
|
+
* Executes a prepared swap quote using the provided wallet and configuration.
|
|
1023
|
+
*
|
|
1024
|
+
* Handles:
|
|
1025
|
+
* - Token approval (if required)
|
|
1026
|
+
* - Transaction signing and broadcasting
|
|
1027
|
+
* - Confirmation polling and stage-based status updates
|
|
1028
|
+
*
|
|
1029
|
+
* Supports both:
|
|
1030
|
+
* - Market orders (with optional deadline)
|
|
1031
|
+
* - Limit orders (requires executionPrice and deadline)
|
|
1032
|
+
*
|
|
1033
|
+
* @param quote - The swap quote to execute, containing route and metadata.
|
|
1034
|
+
* @param accountAddress - The user's wallet address executing the swap.
|
|
1035
|
+
* @param recipientAddress - Optional recipient address for the output tokens (defaults to sender).
|
|
1036
|
+
* @param wallet - Adapted wallet instance (EVM/Solana) or a standard Viem `WalletClient`.
|
|
1037
|
+
* @param onStatus - Optional callback for receiving execution stage updates and messages.
|
|
1038
|
+
* @param orderType - Defines whether this is a market or limit order.
|
|
1039
|
+
* @param options - Execution parameters (different per order type).
|
|
1040
|
+
*
|
|
1041
|
+
* @returns A finalized execution result containing transaction hash, status, and any returned data.
|
|
1042
|
+
*
|
|
1043
|
+
* @example
|
|
1044
|
+
* ```ts
|
|
1045
|
+
* * Market order
|
|
1046
|
+
* const result = await sdk.executeTransaction({
|
|
1047
|
+
* quote,
|
|
1048
|
+
* accountAddress: "0x123...",
|
|
1049
|
+
* wallet,
|
|
1050
|
+
* orderType: OrderExecutionType.MARKET,
|
|
1051
|
+
* options: { deadline: 1800 },
|
|
1052
|
+
* onStatus: (stage, msg) => console.log(stage, msg),
|
|
1053
|
+
* });
|
|
1054
|
+
*
|
|
1055
|
+
* * Limit order
|
|
1056
|
+
* const result = await sdk.executeTransaction({
|
|
1057
|
+
* quote,
|
|
1058
|
+
* accountAddress: "0x123...",
|
|
1059
|
+
* wallet,
|
|
1060
|
+
* orderType: OrderExecutionType.LIMIT,
|
|
1061
|
+
* options: { executionPrice: "0.0021", deadline: 3600 },
|
|
1062
|
+
* });
|
|
1063
|
+
* ```
|
|
1064
|
+
*/
|
|
1065
|
+
async executeTransaction({
|
|
1066
|
+
quote,
|
|
1067
|
+
accountAddress,
|
|
1068
|
+
recipientAddress,
|
|
1069
|
+
wallet,
|
|
1070
|
+
onStatus,
|
|
1071
|
+
orderType,
|
|
1072
|
+
options
|
|
1073
|
+
}) {
|
|
1074
|
+
return executeOrder({
|
|
1075
|
+
quote,
|
|
1076
|
+
wallet,
|
|
1077
|
+
accountAddress,
|
|
1078
|
+
recipientAddress,
|
|
1079
|
+
onStatus,
|
|
1080
|
+
orderType,
|
|
1081
|
+
options
|
|
1082
|
+
});
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* Fetches all user orders (Market, Limit, Cross-chain) for connected wallets.
|
|
1086
|
+
*
|
|
1087
|
+
* ---
|
|
1088
|
+
* ### Overview
|
|
1089
|
+
* Retrieves both **single-chain** and **cross-chain** orders from the Shogun Intents API.
|
|
1090
|
+
* Works across EVM, Solana
|
|
1091
|
+
*
|
|
1092
|
+
* ---
|
|
1093
|
+
* @example
|
|
1094
|
+
* ```ts
|
|
1095
|
+
* const orders = await sdk.getOrders({
|
|
1096
|
+
* evmAddress: "0x123...",
|
|
1097
|
+
* solAddress: "9d12hF...abc",
|
|
1098
|
+
* });
|
|
1099
|
+
*
|
|
1100
|
+
* console.log(orders.singleChainLimitOrders);
|
|
1101
|
+
* ```
|
|
1102
|
+
*
|
|
1103
|
+
* @param params - Wallet addresses to fetch orders for (EVM, Solana).
|
|
1104
|
+
* @returns A structured {@link ApiUserOrders} object containing all user orders.
|
|
1105
|
+
*/
|
|
1106
|
+
async getOrders(params) {
|
|
1107
|
+
return getOrders(params);
|
|
1108
|
+
}
|
|
1109
|
+
};
|
|
802
1110
|
export {
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
SOLANA_CHAIN_ID,
|
|
1111
|
+
ChainId,
|
|
1112
|
+
OrderExecutionType,
|
|
806
1113
|
SupportedChains,
|
|
1114
|
+
SwapSDK,
|
|
807
1115
|
buildQuoteParams,
|
|
808
|
-
|
|
809
|
-
getBalances,
|
|
810
|
-
getQuote,
|
|
811
|
-
getTokenList,
|
|
812
|
-
isEvmChain3 as isEvmChain,
|
|
813
|
-
isNativeAddress,
|
|
814
|
-
isViemWalletClient,
|
|
815
|
-
normalizeEvmTokenAddress,
|
|
816
|
-
serializeBigIntsToStrings
|
|
1116
|
+
isEvmChain
|
|
817
1117
|
};
|