@shogun-sdk/swap 0.0.2-test.25 → 0.0.2-test.26
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 +369 -109
- package/dist/core.d.cts +4 -137
- package/dist/core.d.ts +4 -137
- package/dist/core.js +347 -85
- package/dist/index-Czt8I0Vg.d.ts +351 -0
- package/dist/index-D62OxLwp.d.cts +351 -0
- package/dist/index.cjs +363 -497
- package/dist/index.d.cts +4 -9
- package/dist/index.d.ts +4 -9
- package/dist/index.js +341 -475
- package/dist/react.cjs +823 -476
- package/dist/react.d.cts +192 -82
- package/dist/react.d.ts +192 -82
- package/dist/react.js +793 -454
- 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.cjs
CHANGED
|
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
7
|
var __export = (target, all) => {
|
|
7
8
|
for (var name in all)
|
|
8
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -16,39 +17,26 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
17
|
return to;
|
|
17
18
|
};
|
|
18
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
19
21
|
|
|
20
22
|
// src/core/index.ts
|
|
21
23
|
var core_exports = {};
|
|
22
24
|
__export(core_exports, {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
SOLANA_CHAIN_ID: () => SOLANA_CHAIN_ID,
|
|
25
|
+
ChainId: () => ChainId,
|
|
26
|
+
OrderExecutionType: () => OrderExecutionType,
|
|
26
27
|
SupportedChains: () => SupportedChains,
|
|
28
|
+
SwapSDK: () => SwapSDK,
|
|
27
29
|
buildQuoteParams: () => buildQuoteParams,
|
|
28
|
-
|
|
29
|
-
getBalances: () => getBalances,
|
|
30
|
-
getQuote: () => getQuote,
|
|
31
|
-
getTokenList: () => getTokenList,
|
|
32
|
-
isEvmChain: () => import_intents_sdk12.isEvmChain,
|
|
33
|
-
isNativeAddress: () => isNativeAddress,
|
|
34
|
-
isViemWalletClient: () => isViemWalletClient,
|
|
35
|
-
normalizeEvmTokenAddress: () => normalizeEvmTokenAddress,
|
|
36
|
-
serializeBigIntsToStrings: () => serializeBigIntsToStrings
|
|
30
|
+
isEvmChain: () => isEvmChain
|
|
37
31
|
});
|
|
38
32
|
module.exports = __toCommonJS(core_exports);
|
|
39
33
|
|
|
40
|
-
// src/core/token-list.ts
|
|
41
|
-
var import_intents_sdk = require("@shogun-sdk/intents-sdk");
|
|
42
|
-
async function getTokenList(params) {
|
|
43
|
-
return (0, import_intents_sdk.getTokenList)(params);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
34
|
// src/core/getQuote.ts
|
|
47
|
-
var
|
|
35
|
+
var import_intents_sdk3 = require("@shogun-sdk/intents-sdk");
|
|
48
36
|
var import_viem2 = require("viem");
|
|
49
37
|
|
|
50
|
-
// src/core/
|
|
51
|
-
var
|
|
38
|
+
// src/core/execute/normalizeNative.ts
|
|
39
|
+
var import_intents_sdk2 = require("@shogun-sdk/intents-sdk");
|
|
52
40
|
|
|
53
41
|
// src/utils/address.ts
|
|
54
42
|
var import_viem = require("viem");
|
|
@@ -68,11 +56,25 @@ function normalizeEvmTokenAddress(address) {
|
|
|
68
56
|
}
|
|
69
57
|
|
|
70
58
|
// src/utils/chain.ts
|
|
71
|
-
var
|
|
72
|
-
var SOLANA_CHAIN_ID =
|
|
73
|
-
var
|
|
59
|
+
var import_intents_sdk = require("@shogun-sdk/intents-sdk");
|
|
60
|
+
var SOLANA_CHAIN_ID = import_intents_sdk.ChainID.Solana;
|
|
61
|
+
var CURRENT_SUPPORTED = [
|
|
62
|
+
import_intents_sdk.ChainID.Solana,
|
|
63
|
+
import_intents_sdk.ChainID.BSC,
|
|
64
|
+
import_intents_sdk.ChainID.Base
|
|
65
|
+
];
|
|
66
|
+
var ChainId = Object.entries(import_intents_sdk.ChainID).reduce(
|
|
67
|
+
(acc, [key, value]) => {
|
|
68
|
+
if (typeof value === "number" && CURRENT_SUPPORTED.includes(value)) {
|
|
69
|
+
acc[key] = value;
|
|
70
|
+
}
|
|
71
|
+
return acc;
|
|
72
|
+
},
|
|
73
|
+
{}
|
|
74
|
+
);
|
|
75
|
+
var SupportedChainsInternal = [
|
|
74
76
|
{
|
|
75
|
-
id:
|
|
77
|
+
id: import_intents_sdk.ChainID.Arbitrum,
|
|
76
78
|
name: "Arbitrum",
|
|
77
79
|
isEVM: true,
|
|
78
80
|
wrapped: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
@@ -81,7 +83,7 @@ var SupportedChains = [
|
|
|
81
83
|
tokenAddress: NATIVE_TOKEN.ETH
|
|
82
84
|
},
|
|
83
85
|
{
|
|
84
|
-
id:
|
|
86
|
+
id: import_intents_sdk.ChainID.Optimism,
|
|
85
87
|
name: "Optimism",
|
|
86
88
|
isEVM: true,
|
|
87
89
|
wrapped: "0x4200000000000000000000000000000000000006",
|
|
@@ -90,7 +92,7 @@ var SupportedChains = [
|
|
|
90
92
|
tokenAddress: NATIVE_TOKEN.ETH
|
|
91
93
|
},
|
|
92
94
|
{
|
|
93
|
-
id:
|
|
95
|
+
id: import_intents_sdk.ChainID.Base,
|
|
94
96
|
name: "Base",
|
|
95
97
|
isEVM: true,
|
|
96
98
|
wrapped: "0x4200000000000000000000000000000000000006",
|
|
@@ -99,7 +101,7 @@ var SupportedChains = [
|
|
|
99
101
|
tokenAddress: NATIVE_TOKEN.ETH
|
|
100
102
|
},
|
|
101
103
|
{
|
|
102
|
-
id:
|
|
104
|
+
id: import_intents_sdk.ChainID.Hyperliquid,
|
|
103
105
|
name: "Hyperliquid",
|
|
104
106
|
isEVM: true,
|
|
105
107
|
wrapped: "0x5555555555555555555555555555555555555555",
|
|
@@ -108,7 +110,7 @@ var SupportedChains = [
|
|
|
108
110
|
tokenAddress: NATIVE_TOKEN.ETH
|
|
109
111
|
},
|
|
110
112
|
{
|
|
111
|
-
id:
|
|
113
|
+
id: import_intents_sdk.ChainID.BSC,
|
|
112
114
|
name: "BSC",
|
|
113
115
|
isEVM: true,
|
|
114
116
|
wrapped: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
|
|
@@ -126,6 +128,10 @@ var SupportedChains = [
|
|
|
126
128
|
tokenAddress: NATIVE_TOKEN.SOL
|
|
127
129
|
}
|
|
128
130
|
];
|
|
131
|
+
var SupportedChains = SupportedChainsInternal.filter(
|
|
132
|
+
(c) => CURRENT_SUPPORTED.includes(c.id)
|
|
133
|
+
);
|
|
134
|
+
var isEvmChain = import_intents_sdk.isEvmChain;
|
|
129
135
|
|
|
130
136
|
// src/utils/viem.ts
|
|
131
137
|
function isViemWalletClient(wallet) {
|
|
@@ -153,9 +159,9 @@ function serializeBigIntsToStrings(obj) {
|
|
|
153
159
|
return obj;
|
|
154
160
|
}
|
|
155
161
|
|
|
156
|
-
// src/core/
|
|
162
|
+
// src/core/execute/normalizeNative.ts
|
|
157
163
|
function normalizeNative(chainId, address) {
|
|
158
|
-
if ((0,
|
|
164
|
+
if ((0, import_intents_sdk2.isEvmChain)(chainId) && isNativeAddress(address)) {
|
|
159
165
|
const chain = SupportedChains.find((c) => c.id === chainId);
|
|
160
166
|
if (!chain?.wrapped)
|
|
161
167
|
throw new Error(`Wrapped token not found for chainId ${chainId}`);
|
|
@@ -166,22 +172,23 @@ function normalizeNative(chainId, address) {
|
|
|
166
172
|
|
|
167
173
|
// src/core/getQuote.ts
|
|
168
174
|
async function getQuote(params) {
|
|
175
|
+
const amount = BigInt(params.amount);
|
|
169
176
|
if (!params.tokenIn?.address || !params.tokenOut?.address) {
|
|
170
177
|
throw new Error("Both tokenIn and tokenOut must include an address.");
|
|
171
178
|
}
|
|
172
179
|
if (!params.sourceChainId || !params.destChainId) {
|
|
173
180
|
throw new Error("Both sourceChainId and destChainId are required.");
|
|
174
181
|
}
|
|
175
|
-
if (
|
|
182
|
+
if (amount <= 0n) {
|
|
176
183
|
throw new Error("Amount must be greater than 0.");
|
|
177
184
|
}
|
|
178
185
|
const normalizedTokenIn = normalizeNative(params.sourceChainId, params.tokenIn.address);
|
|
179
|
-
const data = await
|
|
186
|
+
const data = await import_intents_sdk3.QuoteProvider.getQuote({
|
|
180
187
|
sourceChainId: params.sourceChainId,
|
|
181
188
|
destChainId: params.destChainId,
|
|
182
189
|
tokenIn: normalizedTokenIn,
|
|
183
190
|
tokenOut: params.tokenOut.address,
|
|
184
|
-
amount
|
|
191
|
+
amount
|
|
185
192
|
});
|
|
186
193
|
const slippagePercent = Math.min(Math.max(params.slippage ?? 0.5, 0), 50);
|
|
187
194
|
let warning;
|
|
@@ -212,7 +219,7 @@ async function getQuote(params) {
|
|
|
212
219
|
decimals: params.tokenOut.decimals ?? 18,
|
|
213
220
|
chainId: params.destChainId
|
|
214
221
|
},
|
|
215
|
-
amountIn: params.amount,
|
|
222
|
+
amountIn: BigInt(params.amount),
|
|
216
223
|
pricePerInputToken,
|
|
217
224
|
slippage: slippagePercent,
|
|
218
225
|
internal: {
|
|
@@ -236,13 +243,13 @@ function buildQuoteParams({
|
|
|
236
243
|
tokenOut,
|
|
237
244
|
sourceChainId,
|
|
238
245
|
destChainId,
|
|
239
|
-
amount: (0, import_viem2.parseUnits)(amount.toString(), tokenIn.decimals ?? 18),
|
|
246
|
+
amount: (0, import_viem2.parseUnits)(amount.toString(), tokenIn.decimals ?? 18).toString(),
|
|
240
247
|
slippage
|
|
241
248
|
};
|
|
242
249
|
}
|
|
243
250
|
|
|
244
251
|
// src/core/getBalances.ts
|
|
245
|
-
var
|
|
252
|
+
var import_intents_sdk4 = require("@shogun-sdk/intents-sdk");
|
|
246
253
|
async function getBalances(params, options) {
|
|
247
254
|
const { addresses, cursorEvm, cursorSvm } = params;
|
|
248
255
|
const { signal } = options ?? {};
|
|
@@ -255,7 +262,7 @@ async function getBalances(params, options) {
|
|
|
255
262
|
cursorSvm
|
|
256
263
|
});
|
|
257
264
|
const start = performance.now();
|
|
258
|
-
const response = await fetch(`${
|
|
265
|
+
const response = await fetch(`${import_intents_sdk4.TOKEN_SEARCH_API_BASE_URL}/tokens/balances`, {
|
|
259
266
|
method: "POST",
|
|
260
267
|
headers: {
|
|
261
268
|
accept: "application/json",
|
|
@@ -283,20 +290,66 @@ async function getBalances(params, options) {
|
|
|
283
290
|
const evmItems = data.evm?.items ?? [];
|
|
284
291
|
const svmItems = data.svm?.items ?? [];
|
|
285
292
|
const combined = [...evmItems, ...svmItems];
|
|
293
|
+
const filtered = combined.filter(
|
|
294
|
+
(b) => CURRENT_SUPPORTED.includes(b.chainId)
|
|
295
|
+
);
|
|
286
296
|
return {
|
|
287
|
-
results:
|
|
297
|
+
results: filtered,
|
|
288
298
|
nextCursorEvm: data.evm?.cursor ?? null,
|
|
289
299
|
nextCursorSvm: data.svm?.cursor ?? null
|
|
290
300
|
};
|
|
291
301
|
}
|
|
292
302
|
|
|
293
|
-
// src/core/
|
|
294
|
-
var
|
|
295
|
-
|
|
303
|
+
// src/core/token-list.ts
|
|
304
|
+
var import_intents_sdk5 = require("@shogun-sdk/intents-sdk");
|
|
305
|
+
async function getTokenList(params) {
|
|
306
|
+
const url = new URL(`${import_intents_sdk5.TOKEN_SEARCH_API_BASE_URL}/tokens/search`);
|
|
307
|
+
if (params.q) url.searchParams.append("q", params.q);
|
|
308
|
+
if (params.networkId) url.searchParams.append("networkId", String(params.networkId));
|
|
309
|
+
if (params.page) url.searchParams.append("page", String(params.page));
|
|
310
|
+
if (params.limit) url.searchParams.append("limit", String(params.limit));
|
|
311
|
+
const res = await fetch(url.toString(), {
|
|
312
|
+
signal: params.signal
|
|
313
|
+
});
|
|
314
|
+
if (!res.ok) {
|
|
315
|
+
throw new Error(`Failed to fetch tokens: ${res.status} ${res.statusText}`);
|
|
316
|
+
}
|
|
317
|
+
const data = await res.json();
|
|
318
|
+
const filteredResults = data.results.filter(
|
|
319
|
+
(token) => CURRENT_SUPPORTED.includes(token.chainId)
|
|
320
|
+
);
|
|
321
|
+
return {
|
|
322
|
+
...data,
|
|
323
|
+
results: filteredResults,
|
|
324
|
+
count: filteredResults.length
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// src/core/token.ts
|
|
329
|
+
var import_intents_sdk6 = require("@shogun-sdk/intents-sdk");
|
|
330
|
+
async function getTokensData(addresses) {
|
|
331
|
+
if (!addresses?.length) return [];
|
|
332
|
+
const response = await fetch(`${import_intents_sdk6.TOKEN_SEARCH_API_BASE_URL}/tokens/tokens`, {
|
|
333
|
+
method: "POST",
|
|
334
|
+
headers: {
|
|
335
|
+
"Content-Type": "application/json",
|
|
336
|
+
accept: "*/*"
|
|
337
|
+
},
|
|
338
|
+
body: JSON.stringify({ addresses })
|
|
339
|
+
});
|
|
340
|
+
if (!response.ok) {
|
|
341
|
+
throw new Error(`Failed to fetch token data: ${response.statusText}`);
|
|
342
|
+
}
|
|
343
|
+
const data = await response.json();
|
|
344
|
+
const filtered = data.filter((t) => CURRENT_SUPPORTED.includes(Number(t.chainId)));
|
|
345
|
+
return filtered;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// src/core/execute/execute.ts
|
|
349
|
+
var import_intents_sdk12 = require("@shogun-sdk/intents-sdk");
|
|
350
|
+
var import_viem7 = require("viem");
|
|
296
351
|
|
|
297
352
|
// src/wallet-adapter/evm-wallet-adapter/adapter.ts
|
|
298
|
-
var import_ethers = require("ethers/lib/ethers.js");
|
|
299
|
-
var import_utils2 = require("ethers/lib/utils.js");
|
|
300
353
|
var import_viem3 = require("viem");
|
|
301
354
|
function isEVMTransaction(tx) {
|
|
302
355
|
return typeof tx.from === "string";
|
|
@@ -383,11 +436,11 @@ var adaptViemWallet = (wallet) => {
|
|
|
383
436
|
};
|
|
384
437
|
};
|
|
385
438
|
|
|
386
|
-
// src/core/
|
|
387
|
-
var
|
|
388
|
-
var
|
|
439
|
+
// src/core/execute/handleEvmExecution.ts
|
|
440
|
+
var import_intents_sdk10 = require("@shogun-sdk/intents-sdk");
|
|
441
|
+
var import_viem6 = require("viem");
|
|
389
442
|
|
|
390
|
-
// src/core/
|
|
443
|
+
// src/core/execute/stageMessages.ts
|
|
391
444
|
var DEFAULT_STAGE_MESSAGES = {
|
|
392
445
|
processing: "Preparing transaction for execution",
|
|
393
446
|
approving: "Approving token allowance",
|
|
@@ -396,33 +449,57 @@ var DEFAULT_STAGE_MESSAGES = {
|
|
|
396
449
|
submitting: "Submitting transaction",
|
|
397
450
|
initiated: "Transaction initiated.",
|
|
398
451
|
success: "Transaction Executed successfully",
|
|
452
|
+
success_limit: "Limit order has been submitted successfully.",
|
|
399
453
|
shogun_processing: "Shogun is processing your transaction",
|
|
400
454
|
error: "Transaction failed during submission"
|
|
401
455
|
};
|
|
402
456
|
|
|
403
|
-
// src/core/
|
|
404
|
-
var
|
|
457
|
+
// src/core/execute/buildOrder.ts
|
|
458
|
+
var import_intents_sdk7 = require("@shogun-sdk/intents-sdk");
|
|
459
|
+
var import_viem4 = require("viem");
|
|
460
|
+
|
|
461
|
+
// src/utils/order.ts
|
|
462
|
+
var OrderExecutionType = /* @__PURE__ */ ((OrderExecutionType2) => {
|
|
463
|
+
OrderExecutionType2["LIMIT"] = "limit";
|
|
464
|
+
OrderExecutionType2["MARKET"] = "market";
|
|
465
|
+
return OrderExecutionType2;
|
|
466
|
+
})(OrderExecutionType || {});
|
|
467
|
+
|
|
468
|
+
// src/core/execute/buildOrder.ts
|
|
405
469
|
async function buildOrder({
|
|
406
470
|
quote,
|
|
407
471
|
accountAddress,
|
|
408
472
|
destination,
|
|
409
473
|
deadline,
|
|
410
|
-
isSingleChain
|
|
474
|
+
isSingleChain,
|
|
475
|
+
orderType,
|
|
476
|
+
options
|
|
411
477
|
}) {
|
|
412
478
|
const { tokenIn, tokenOut } = quote;
|
|
479
|
+
let amountOutMin = BigInt(quote.internal.estimatedAmountOutReduced);
|
|
480
|
+
if (orderType === "limit" /* LIMIT */ && options && "executionPrice" in options) {
|
|
481
|
+
const executionPrice = Number(options.executionPrice);
|
|
482
|
+
if (Number.isFinite(executionPrice) && executionPrice > 0) {
|
|
483
|
+
const decimalsIn = tokenIn.decimals ?? 18;
|
|
484
|
+
const decimalsOut = tokenOut.decimals ?? 18;
|
|
485
|
+
const formattedAmountIn = Number((0, import_viem4.formatUnits)(BigInt(quote.amountIn.toString()), decimalsIn));
|
|
486
|
+
const rawAmountOut = formattedAmountIn * executionPrice;
|
|
487
|
+
amountOutMin = (0, import_viem4.parseUnits)(rawAmountOut.toString(), decimalsOut);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
413
490
|
if (isSingleChain) {
|
|
414
|
-
return await
|
|
491
|
+
return await import_intents_sdk7.SingleChainOrder.create({
|
|
415
492
|
user: accountAddress,
|
|
416
493
|
chainId: tokenIn.chainId,
|
|
417
494
|
tokenIn: tokenIn.address,
|
|
418
495
|
tokenOut: tokenOut.address,
|
|
419
496
|
amountIn: quote.amountIn,
|
|
420
|
-
amountOutMin
|
|
497
|
+
amountOutMin,
|
|
421
498
|
deadline,
|
|
422
499
|
destinationAddress: destination
|
|
423
500
|
});
|
|
424
501
|
}
|
|
425
|
-
return await
|
|
502
|
+
return await import_intents_sdk7.CrossChainOrder.create({
|
|
426
503
|
user: accountAddress,
|
|
427
504
|
sourceChainId: tokenIn.chainId,
|
|
428
505
|
sourceTokenAddress: tokenIn.address,
|
|
@@ -431,13 +508,13 @@ async function buildOrder({
|
|
|
431
508
|
destinationTokenAddress: tokenOut.address,
|
|
432
509
|
destinationAddress: destination,
|
|
433
510
|
deadline,
|
|
434
|
-
destinationTokenMinAmount:
|
|
511
|
+
destinationTokenMinAmount: amountOutMin,
|
|
435
512
|
minStablecoinAmount: quote.minStablecoinsAmount
|
|
436
513
|
});
|
|
437
514
|
}
|
|
438
515
|
|
|
439
516
|
// src/utils/pollOrderStatus.ts
|
|
440
|
-
var
|
|
517
|
+
var import_intents_sdk8 = require("@shogun-sdk/intents-sdk");
|
|
441
518
|
async function pollOrderStatus(address, orderId, options = {}) {
|
|
442
519
|
const { intervalMs = 2e3, timeoutMs = 3e5 } = options;
|
|
443
520
|
const startTime = Date.now();
|
|
@@ -450,7 +527,7 @@ async function pollOrderStatus(address, orderId, options = {}) {
|
|
|
450
527
|
else if (isSuiAddress) queryParam = `suiWallets=${address}`;
|
|
451
528
|
else if (isSolanaAddress) queryParam = `solanaWallets=${address}`;
|
|
452
529
|
else throw new Error(`Unrecognized wallet address format: ${address}`);
|
|
453
|
-
const queryUrl = `${
|
|
530
|
+
const queryUrl = `${import_intents_sdk8.AUCTIONEER_URL}/user_intent?${queryParam}`;
|
|
454
531
|
return new Promise((resolve, reject) => {
|
|
455
532
|
const pollInterval = setInterval(async () => {
|
|
456
533
|
try {
|
|
@@ -502,7 +579,7 @@ async function pollOrderStatus(address, orderId, options = {}) {
|
|
|
502
579
|
});
|
|
503
580
|
}
|
|
504
581
|
|
|
505
|
-
// src/core/
|
|
582
|
+
// src/core/execute/handleOrderPollingResult.ts
|
|
506
583
|
async function handleOrderPollingResult({
|
|
507
584
|
status,
|
|
508
585
|
orderId,
|
|
@@ -540,9 +617,9 @@ async function handleOrderPollingResult({
|
|
|
540
617
|
};
|
|
541
618
|
}
|
|
542
619
|
|
|
543
|
-
// src/core/
|
|
544
|
-
var
|
|
545
|
-
var
|
|
620
|
+
// src/core/execute/ensurePermit2Allowance.ts
|
|
621
|
+
var import_viem5 = require("viem");
|
|
622
|
+
var import_intents_sdk9 = require("@shogun-sdk/intents-sdk");
|
|
546
623
|
async function ensurePermit2Allowance({
|
|
547
624
|
chainId,
|
|
548
625
|
tokenIn,
|
|
@@ -551,7 +628,7 @@ async function ensurePermit2Allowance({
|
|
|
551
628
|
requiredAmount,
|
|
552
629
|
increaseByDelta = false
|
|
553
630
|
}) {
|
|
554
|
-
const spender =
|
|
631
|
+
const spender = import_intents_sdk9.PERMIT2_ADDRESS[chainId];
|
|
555
632
|
let currentAllowance = 0n;
|
|
556
633
|
try {
|
|
557
634
|
if (!wallet.readContract) {
|
|
@@ -559,22 +636,22 @@ async function ensurePermit2Allowance({
|
|
|
559
636
|
}
|
|
560
637
|
currentAllowance = await wallet.readContract({
|
|
561
638
|
address: tokenIn,
|
|
562
|
-
abi:
|
|
639
|
+
abi: import_viem5.erc20Abi,
|
|
563
640
|
functionName: "allowance",
|
|
564
641
|
args: [accountAddress, spender]
|
|
565
642
|
});
|
|
566
643
|
} catch (error) {
|
|
567
644
|
console.warn(`[Permit2] Failed to read allowance for ${tokenIn}`, error);
|
|
568
645
|
}
|
|
569
|
-
const approvalAmount = increaseByDelta ? currentAllowance + requiredAmount :
|
|
646
|
+
const approvalAmount = increaseByDelta ? currentAllowance + requiredAmount : import_viem5.maxUint256;
|
|
570
647
|
console.debug(
|
|
571
648
|
`[Permit2] Approving ${approvalAmount} for ${tokenIn} (current: ${currentAllowance}, required: ${requiredAmount})`
|
|
572
649
|
);
|
|
573
650
|
await wallet.sendTransaction({
|
|
574
651
|
to: tokenIn,
|
|
575
652
|
from: accountAddress,
|
|
576
|
-
data: (0,
|
|
577
|
-
abi:
|
|
653
|
+
data: (0, import_viem5.encodeFunctionData)({
|
|
654
|
+
abi: import_viem5.erc20Abi,
|
|
578
655
|
functionName: "approve",
|
|
579
656
|
args: [spender, approvalAmount]
|
|
580
657
|
}),
|
|
@@ -585,7 +662,7 @@ async function ensurePermit2Allowance({
|
|
|
585
662
|
);
|
|
586
663
|
}
|
|
587
664
|
|
|
588
|
-
// src/core/
|
|
665
|
+
// src/core/execute/handleEvmExecution.ts
|
|
589
666
|
async function handleEvmExecution({
|
|
590
667
|
recipientAddress,
|
|
591
668
|
quote,
|
|
@@ -593,19 +670,22 @@ async function handleEvmExecution({
|
|
|
593
670
|
accountAddress,
|
|
594
671
|
wallet,
|
|
595
672
|
isSingleChain,
|
|
596
|
-
|
|
597
|
-
|
|
673
|
+
update,
|
|
674
|
+
orderType,
|
|
675
|
+
options
|
|
598
676
|
}) {
|
|
599
|
-
const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage];
|
|
677
|
+
const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage] ?? "";
|
|
678
|
+
const deadline = options?.deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
|
|
600
679
|
await wallet.switchChain(chainId);
|
|
601
680
|
const tokenIn = normalizeNative(chainId, quote.tokenIn.address);
|
|
602
681
|
quote.tokenOut.address = normalizeEvmTokenAddress(quote.tokenOut.address);
|
|
603
682
|
const shouldWrapNative = isNativeAddress(quote.tokenIn.address);
|
|
604
683
|
update("processing", shouldWrapNative ? `${messageFor("processing")} (wrapping native token)` : messageFor("processing"));
|
|
605
684
|
if (shouldWrapNative) {
|
|
685
|
+
quote.tokenIn.address === tokenIn;
|
|
606
686
|
await wallet.sendTransaction({
|
|
607
687
|
to: tokenIn,
|
|
608
|
-
data: (0,
|
|
688
|
+
data: (0, import_viem6.encodeFunctionData)({
|
|
609
689
|
abi: [{ type: "function", name: "deposit", stateMutability: "payable", inputs: [], outputs: [] }],
|
|
610
690
|
functionName: "deposit",
|
|
611
691
|
args: []
|
|
@@ -629,11 +709,13 @@ async function handleEvmExecution({
|
|
|
629
709
|
accountAddress,
|
|
630
710
|
destination,
|
|
631
711
|
deadline,
|
|
632
|
-
isSingleChain
|
|
712
|
+
isSingleChain,
|
|
713
|
+
orderType,
|
|
714
|
+
options
|
|
633
715
|
});
|
|
634
716
|
console.debug(`order`, order);
|
|
635
717
|
update("processing", messageFor("signing"));
|
|
636
|
-
const { orderTypedData, nonce } = isSingleChain ? await (0,
|
|
718
|
+
const { orderTypedData, nonce } = isSingleChain ? await (0, import_intents_sdk10.getEVMSingleChainOrderTypedData)(order) : await (0, import_intents_sdk10.getEVMCrossChainOrderTypedData)(order);
|
|
637
719
|
const typedData = serializeBigIntsToStrings(orderTypedData);
|
|
638
720
|
if (!wallet.signTypedData) {
|
|
639
721
|
throw new Error("Wallet does not support EIP-712 signing");
|
|
@@ -653,18 +735,29 @@ async function handleEvmExecution({
|
|
|
653
735
|
update("initiated", messageFor("initiated"));
|
|
654
736
|
const { intentId: orderId } = res.data;
|
|
655
737
|
update("initiated", messageFor("shogun_processing"));
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
738
|
+
if (orderType === "limit" /* LIMIT */) {
|
|
739
|
+
update("success", messageFor("success_limit"));
|
|
740
|
+
return {
|
|
741
|
+
status: true,
|
|
742
|
+
orderId,
|
|
743
|
+
chainId,
|
|
744
|
+
finalStatus: "OrderPlaced",
|
|
745
|
+
stage: "success"
|
|
746
|
+
};
|
|
747
|
+
} else {
|
|
748
|
+
const status = await pollOrderStatus(accountAddress, orderId);
|
|
749
|
+
return await handleOrderPollingResult({
|
|
750
|
+
status,
|
|
751
|
+
orderId,
|
|
752
|
+
chainId,
|
|
753
|
+
update,
|
|
754
|
+
messageFor
|
|
755
|
+
});
|
|
756
|
+
}
|
|
664
757
|
}
|
|
665
758
|
|
|
666
|
-
// src/core/
|
|
667
|
-
var
|
|
759
|
+
// src/core/execute/handleSolanaExecution.ts
|
|
760
|
+
var import_intents_sdk11 = require("@shogun-sdk/intents-sdk");
|
|
668
761
|
var import_web3 = require("@solana/web3.js");
|
|
669
762
|
async function handleSolanaExecution({
|
|
670
763
|
recipientAddress,
|
|
@@ -673,12 +766,14 @@ async function handleSolanaExecution({
|
|
|
673
766
|
isSingleChain,
|
|
674
767
|
update,
|
|
675
768
|
accountAddress,
|
|
676
|
-
|
|
769
|
+
orderType,
|
|
770
|
+
options
|
|
677
771
|
}) {
|
|
678
772
|
if (!wallet.rpcUrl) {
|
|
679
773
|
throw new Error("Solana wallet is missing rpcUrl");
|
|
680
774
|
}
|
|
681
|
-
const
|
|
775
|
+
const deadline = options?.deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
|
|
776
|
+
const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage] ?? "";
|
|
682
777
|
update("processing", messageFor("processing"));
|
|
683
778
|
const destination = recipientAddress ?? accountAddress;
|
|
684
779
|
const order = await buildOrder({
|
|
@@ -686,7 +781,9 @@ async function handleSolanaExecution({
|
|
|
686
781
|
accountAddress,
|
|
687
782
|
destination,
|
|
688
783
|
deadline,
|
|
689
|
-
isSingleChain
|
|
784
|
+
isSingleChain,
|
|
785
|
+
orderType,
|
|
786
|
+
options
|
|
690
787
|
});
|
|
691
788
|
const txData = await getSolanaOrderInstructions({
|
|
692
789
|
order,
|
|
@@ -708,14 +805,25 @@ async function handleSolanaExecution({
|
|
|
708
805
|
update("initiated", messageFor("initiated"));
|
|
709
806
|
const { jwt, intentId: orderId } = response.data;
|
|
710
807
|
update("initiated", messageFor("shogun_processing"));
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
808
|
+
if (orderType === "limit" /* LIMIT */) {
|
|
809
|
+
update("success", messageFor("success_limit"));
|
|
810
|
+
return {
|
|
811
|
+
status: true,
|
|
812
|
+
orderId,
|
|
813
|
+
chainId: SOLANA_CHAIN_ID,
|
|
814
|
+
finalStatus: "OrderPlaced",
|
|
815
|
+
stage: "success"
|
|
816
|
+
};
|
|
817
|
+
} else {
|
|
818
|
+
const status = await pollOrderStatus(jwt, orderId);
|
|
819
|
+
return await handleOrderPollingResult({
|
|
820
|
+
status,
|
|
821
|
+
orderId,
|
|
822
|
+
chainId: SOLANA_CHAIN_ID,
|
|
823
|
+
update,
|
|
824
|
+
messageFor
|
|
825
|
+
});
|
|
826
|
+
}
|
|
719
827
|
}
|
|
720
828
|
async function getSolanaOrderInstructions({
|
|
721
829
|
order,
|
|
@@ -723,11 +831,11 @@ async function getSolanaOrderInstructions({
|
|
|
723
831
|
rpcUrl
|
|
724
832
|
}) {
|
|
725
833
|
if (isSingleChain) {
|
|
726
|
-
return await (0,
|
|
834
|
+
return await (0, import_intents_sdk11.getSolanaSingleChainOrderInstructions)(order, {
|
|
727
835
|
rpcUrl
|
|
728
836
|
});
|
|
729
837
|
}
|
|
730
|
-
return await (0,
|
|
838
|
+
return await (0, import_intents_sdk11.getSolanaCrossChainOrderInstructions)(order, {
|
|
731
839
|
rpcUrl
|
|
732
840
|
});
|
|
733
841
|
}
|
|
@@ -748,13 +856,14 @@ async function submitToAuctioneer({
|
|
|
748
856
|
});
|
|
749
857
|
}
|
|
750
858
|
|
|
751
|
-
// src/core/
|
|
859
|
+
// src/core/execute/execute.ts
|
|
752
860
|
async function executeOrder({
|
|
753
861
|
quote,
|
|
754
862
|
accountAddress,
|
|
755
863
|
recipientAddress,
|
|
756
864
|
wallet,
|
|
757
865
|
onStatus,
|
|
866
|
+
orderType = "market" /* MARKET */,
|
|
758
867
|
options = {}
|
|
759
868
|
}) {
|
|
760
869
|
const isDev = process.env.NODE_ENV !== "production";
|
|
@@ -767,21 +876,31 @@ async function executeOrder({
|
|
|
767
876
|
onStatus?.(stage, message ?? messageFor(stage));
|
|
768
877
|
};
|
|
769
878
|
try {
|
|
770
|
-
const deadline = options.deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
|
|
771
879
|
log("Starting execution:", {
|
|
772
880
|
accountAddress,
|
|
773
881
|
recipientAddress,
|
|
774
|
-
deadline,
|
|
775
882
|
tokenIn: quote?.tokenIn,
|
|
776
883
|
tokenOut: quote?.tokenOut
|
|
777
884
|
});
|
|
778
885
|
const adapter = normalizeWallet(wallet);
|
|
779
886
|
if (!adapter) throw new Error("No wallet provided");
|
|
780
887
|
const { tokenIn, tokenOut } = quote;
|
|
888
|
+
const srcChain = Number(tokenIn.chainId);
|
|
889
|
+
const destChain = Number(tokenOut.chainId);
|
|
890
|
+
if (!CURRENT_SUPPORTED.includes(srcChain) || !CURRENT_SUPPORTED.includes(destChain)) {
|
|
891
|
+
const unsupportedChains = [
|
|
892
|
+
!CURRENT_SUPPORTED.includes(srcChain) ? srcChain : null,
|
|
893
|
+
!CURRENT_SUPPORTED.includes(destChain) ? destChain : null
|
|
894
|
+
].filter(Boolean).join(", ");
|
|
895
|
+
const errorMsg = `Unsupported chain(s): ${unsupportedChains}`;
|
|
896
|
+
update("error", errorMsg);
|
|
897
|
+
log("Error:", errorMsg);
|
|
898
|
+
throw new Error(errorMsg);
|
|
899
|
+
}
|
|
781
900
|
const isSingleChain = tokenIn.chainId === tokenOut.chainId;
|
|
782
901
|
const chainId = Number(tokenIn.chainId);
|
|
783
902
|
update("processing");
|
|
784
|
-
if ((0,
|
|
903
|
+
if ((0, import_intents_sdk12.isEvmChain)(chainId)) {
|
|
785
904
|
log("Detected EVM chain:", chainId);
|
|
786
905
|
const result = await handleEvmExecution({
|
|
787
906
|
recipientAddress,
|
|
@@ -790,13 +909,14 @@ async function executeOrder({
|
|
|
790
909
|
accountAddress,
|
|
791
910
|
wallet: adapter,
|
|
792
911
|
isSingleChain,
|
|
793
|
-
|
|
794
|
-
|
|
912
|
+
update,
|
|
913
|
+
orderType,
|
|
914
|
+
options
|
|
795
915
|
});
|
|
796
916
|
log("EVM execution result:", result);
|
|
797
917
|
return result;
|
|
798
918
|
}
|
|
799
|
-
if (chainId ===
|
|
919
|
+
if (chainId === import_intents_sdk12.ChainID.Solana) {
|
|
800
920
|
log("Detected Solana chain");
|
|
801
921
|
const result = await handleSolanaExecution({
|
|
802
922
|
recipientAddress,
|
|
@@ -804,8 +924,9 @@ async function executeOrder({
|
|
|
804
924
|
accountAddress,
|
|
805
925
|
wallet: adapter,
|
|
806
926
|
isSingleChain,
|
|
807
|
-
|
|
808
|
-
|
|
927
|
+
update,
|
|
928
|
+
orderType,
|
|
929
|
+
options
|
|
809
930
|
});
|
|
810
931
|
log("Solana execution result:", result);
|
|
811
932
|
return result;
|
|
@@ -815,8 +936,13 @@ async function executeOrder({
|
|
|
815
936
|
log("Error:", unsupported);
|
|
816
937
|
return { status: false, message: unsupported, stage: "error" };
|
|
817
938
|
} catch (error) {
|
|
818
|
-
|
|
819
|
-
|
|
939
|
+
let message = "An unknown error occurred";
|
|
940
|
+
if (error && typeof error === "object") {
|
|
941
|
+
const err = error;
|
|
942
|
+
message = err.details ?? err.message ?? message;
|
|
943
|
+
} else if (typeof error === "string") {
|
|
944
|
+
message = error;
|
|
945
|
+
}
|
|
820
946
|
update("error", message);
|
|
821
947
|
return { status: false, message, stage: "error" };
|
|
822
948
|
}
|
|
@@ -827,5 +953,139 @@ function normalizeWallet(wallet) {
|
|
|
827
953
|
return wallet;
|
|
828
954
|
}
|
|
829
955
|
|
|
830
|
-
// src/core/
|
|
831
|
-
var
|
|
956
|
+
// src/core/client.ts
|
|
957
|
+
var SwapSDK = class {
|
|
958
|
+
constructor(config) {
|
|
959
|
+
__publicField(this, "apiKey");
|
|
960
|
+
/**
|
|
961
|
+
* Fetches metadata for one or more tokens from the Shogun Token Search API.
|
|
962
|
+
*
|
|
963
|
+
* ---
|
|
964
|
+
* ### Overview
|
|
965
|
+
* `getTokensData` retrieves normalized token information — such as symbol, name,
|
|
966
|
+
* decimals, logo URI, and verified status — for a given list of token addresses.
|
|
967
|
+
*
|
|
968
|
+
* It supports both **EVM** and **SVM (Solana)** tokens, returning metadata from
|
|
969
|
+
* Shogun’s unified token registry.
|
|
970
|
+
*
|
|
971
|
+
* ---
|
|
972
|
+
* @example
|
|
973
|
+
* ```ts
|
|
974
|
+
* const tokens = await getTokensData([
|
|
975
|
+
* "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
976
|
+
* "So11111111111111111111111111111111111111112", // SOL
|
|
977
|
+
* ]);
|
|
978
|
+
*
|
|
979
|
+
* console.log(tokens);
|
|
980
|
+
* [
|
|
981
|
+
* { symbol: "USDC", name: "USD Coin", chainId: 1, decimals: 6, ... },
|
|
982
|
+
* { symbol: "SOL", name: "Solana", chainId: 101, decimals: 9, ... }
|
|
983
|
+
* ]
|
|
984
|
+
* ```
|
|
985
|
+
*
|
|
986
|
+
* @param addresses - An array of token addresses (EVM or SVM) to fetch metadata for.
|
|
987
|
+
* @returns A promise resolving to an array of {@link TokenInfo} objects.
|
|
988
|
+
*
|
|
989
|
+
* @throws Will throw an error if the network request fails or the API responds with a non-OK status.
|
|
990
|
+
*/
|
|
991
|
+
__publicField(this, "getTokensData", getTokensData.bind(this));
|
|
992
|
+
if (!config.apiKey) {
|
|
993
|
+
throw new Error("SwapSDK: Missing API key");
|
|
994
|
+
}
|
|
995
|
+
this.apiKey = config.apiKey;
|
|
996
|
+
if (this.apiKey) void this.apiKey;
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* Retrieves a swap quote for the given input and output tokens.
|
|
1000
|
+
*
|
|
1001
|
+
* @param params - Quote parameters including source/destination tokens and amount.
|
|
1002
|
+
* @returns A normalized `SwapQuoteResponse` containing output amount, route, and metadata.
|
|
1003
|
+
*/
|
|
1004
|
+
async getQuote(params) {
|
|
1005
|
+
return getQuote(params);
|
|
1006
|
+
}
|
|
1007
|
+
/**
|
|
1008
|
+
* Fetches token balances for the specified user wallet(s).
|
|
1009
|
+
*
|
|
1010
|
+
* Supports both EVM and SVM (Solana) wallet addresses.
|
|
1011
|
+
*
|
|
1012
|
+
* @param params - Wallet address and optional chain filters.
|
|
1013
|
+
* @param options - Optional abort signal for cancellation.
|
|
1014
|
+
* @returns A unified balance response with per-chain token details.
|
|
1015
|
+
*/
|
|
1016
|
+
async getBalances(params, options) {
|
|
1017
|
+
return getBalances(params, options);
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* Retrieves a list of verified tokens based on search query or chain filter.
|
|
1021
|
+
*
|
|
1022
|
+
* @param params - Search parameters (query, chain ID, pagination options).
|
|
1023
|
+
* @returns Paginated `TokenSearchResponse` containing token metadata.
|
|
1024
|
+
*/
|
|
1025
|
+
async getTokenList(params) {
|
|
1026
|
+
return getTokenList(params);
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Executes a prepared swap quote using the provided wallet and configuration.
|
|
1030
|
+
*
|
|
1031
|
+
* Handles:
|
|
1032
|
+
* - Token approval (if required)
|
|
1033
|
+
* - Transaction signing and broadcasting
|
|
1034
|
+
* - Confirmation polling and stage-based status updates
|
|
1035
|
+
*
|
|
1036
|
+
* Supports both:
|
|
1037
|
+
* - Market orders (with optional deadline)
|
|
1038
|
+
* - Limit orders (requires executionPrice and deadline)
|
|
1039
|
+
*
|
|
1040
|
+
* @param quote - The swap quote to execute, containing route and metadata.
|
|
1041
|
+
* @param accountAddress - The user's wallet address executing the swap.
|
|
1042
|
+
* @param recipientAddress - Optional recipient address for the output tokens (defaults to sender).
|
|
1043
|
+
* @param wallet - Adapted wallet instance (EVM/Solana) or a standard Viem `WalletClient`.
|
|
1044
|
+
* @param onStatus - Optional callback for receiving execution stage updates and messages.
|
|
1045
|
+
* @param orderType - Defines whether this is a market or limit order.
|
|
1046
|
+
* @param options - Execution parameters (different per order type).
|
|
1047
|
+
*
|
|
1048
|
+
* @returns A finalized execution result containing transaction hash, status, and any returned data.
|
|
1049
|
+
*
|
|
1050
|
+
* @example
|
|
1051
|
+
* ```ts
|
|
1052
|
+
* * Market order
|
|
1053
|
+
* const result = await sdk.executeTransaction({
|
|
1054
|
+
* quote,
|
|
1055
|
+
* accountAddress: "0x123...",
|
|
1056
|
+
* wallet,
|
|
1057
|
+
* orderType: OrderExecutionType.MARKET,
|
|
1058
|
+
* options: { deadline: 1800 },
|
|
1059
|
+
* onStatus: (stage, msg) => console.log(stage, msg),
|
|
1060
|
+
* });
|
|
1061
|
+
*
|
|
1062
|
+
* * Limit order
|
|
1063
|
+
* const result = await sdk.executeTransaction({
|
|
1064
|
+
* quote,
|
|
1065
|
+
* accountAddress: "0x123...",
|
|
1066
|
+
* wallet,
|
|
1067
|
+
* orderType: OrderExecutionType.LIMIT,
|
|
1068
|
+
* options: { executionPrice: "0.0021", deadline: 3600 },
|
|
1069
|
+
* });
|
|
1070
|
+
* ```
|
|
1071
|
+
*/
|
|
1072
|
+
async executeTransaction({
|
|
1073
|
+
quote,
|
|
1074
|
+
accountAddress,
|
|
1075
|
+
recipientAddress,
|
|
1076
|
+
wallet,
|
|
1077
|
+
onStatus,
|
|
1078
|
+
orderType,
|
|
1079
|
+
options
|
|
1080
|
+
}) {
|
|
1081
|
+
return executeOrder({
|
|
1082
|
+
quote,
|
|
1083
|
+
wallet,
|
|
1084
|
+
accountAddress,
|
|
1085
|
+
recipientAddress,
|
|
1086
|
+
onStatus,
|
|
1087
|
+
orderType,
|
|
1088
|
+
options
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
};
|