@shogun-sdk/swap 0.0.2-test.3 → 0.0.2-test.31

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/react.cjs CHANGED
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
9
  var __export = (target, all) => {
7
10
  for (var name in all)
8
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -15,107 +18,49 @@ var __copyProps = (to, from, except, desc) => {
15
18
  }
16
19
  return to;
17
20
  };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
18
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
19
31
 
20
32
  // src/react/index.ts
21
33
  var react_exports = {};
22
34
  __export(react_exports, {
23
- ChainID: () => import_intents_sdk10.ChainID,
24
- isEvmChain: () => import_intents_sdk10.isEvmChain,
35
+ ChainId: () => ChainId,
36
+ OrderExecutionType: () => OrderExecutionType,
37
+ SupportedChains: () => SupportedChains,
38
+ SwapProvider: () => SwapProvider,
39
+ buildQuoteParams: () => buildQuoteParams,
40
+ isEvmChain: () => isEvmChain,
25
41
  useBalances: () => useBalances,
26
- useExecuteOrder: () => useExecuteOrder,
42
+ useCancelOrder: () => useCancelOrder,
43
+ useExecuteTransaction: () => useExecuteTransaction,
44
+ useOrders: () => useOrders,
27
45
  useQuote: () => useQuote,
28
- useTokenList: () => useTokenList
46
+ useSwap: () => useSwap,
47
+ useTokenList: () => useTokenList,
48
+ useTokensData: () => useTokensData
29
49
  });
30
50
  module.exports = __toCommonJS(react_exports);
31
51
 
32
- // src/react/useTokenList.ts
52
+ // src/react/SwapProvider.tsx
33
53
  var import_react = require("react");
34
54
 
35
- // src/core/token-list.ts
36
- var import_intents_sdk = require("@shogun-sdk/intents-sdk");
37
- async function getTokenList(params) {
38
- return (0, import_intents_sdk.getTokenList)(params);
39
- }
40
-
41
- // src/react/useTokenList.ts
42
- var tokenCache = /* @__PURE__ */ new Map();
43
- function useTokenList(params) {
44
- const [data, setData] = (0, import_react.useState)(null);
45
- const [loading, setLoading] = (0, import_react.useState)(false);
46
- const [error, setError] = (0, import_react.useState)(null);
47
- const controllerRef = (0, import_react.useRef)(null);
48
- const debounceRef = (0, import_react.useRef)(null);
49
- const debounceMs = params.debounceMs ?? 250;
50
- const cacheKey = (0, import_react.useMemo)(() => {
51
- return JSON.stringify({
52
- q: params.q?.trim().toLowerCase() ?? "",
53
- networkId: params.networkId ?? "all",
54
- page: params.page ?? 1,
55
- limit: params.limit ?? 50
56
- });
57
- }, [params.q, params.networkId, params.page, params.limit]);
58
- async function fetchTokens(signal) {
59
- if (tokenCache.has(cacheKey)) {
60
- setData(tokenCache.get(cacheKey));
61
- setLoading(false);
62
- setError(null);
63
- return;
64
- }
65
- try {
66
- setLoading(true);
67
- const result = await getTokenList({ ...params, signal });
68
- tokenCache.set(cacheKey, result);
69
- setData(result);
70
- setError(null);
71
- } catch (err) {
72
- if (err.name !== "AbortError") {
73
- const e = err instanceof Error ? err : new Error("Unknown error while fetching tokens");
74
- setError(e);
75
- }
76
- } finally {
77
- setLoading(false);
78
- }
79
- }
80
- (0, import_react.useEffect)(() => {
81
- if (!params.q && !params.networkId) return;
82
- if (debounceRef.current) clearTimeout(debounceRef.current);
83
- if (controllerRef.current) controllerRef.current.abort();
84
- const controller = new AbortController();
85
- controllerRef.current = controller;
86
- debounceRef.current = setTimeout(() => {
87
- fetchTokens(controller.signal);
88
- }, debounceMs);
89
- return () => {
90
- controller.abort();
91
- if (debounceRef.current) clearTimeout(debounceRef.current);
92
- };
93
- }, [cacheKey, debounceMs]);
94
- return (0, import_react.useMemo)(
95
- () => ({
96
- /** Current fetched data (cached when possible) */
97
- data,
98
- /** Whether a request is in progress */
99
- loading,
100
- /** Error object if a request failed */
101
- error,
102
- /** Manually refetch the token list */
103
- refetch: () => fetchTokens(),
104
- /** Clear all cached token results (shared across hook instances) */
105
- clearCache: () => tokenCache.clear()
106
- }),
107
- [data, loading, error]
108
- );
109
- }
110
-
111
- // src/react/useExecuteOrder.ts
112
- var import_react2 = require("react");
55
+ // src/core/getQuote.ts
56
+ var import_intents_sdk3 = require("@shogun-sdk/intents-sdk");
57
+ var import_viem2 = require("viem");
113
58
 
114
- // src/core/executeOrder/execute.ts
115
- var import_intents_sdk7 = require("@shogun-sdk/intents-sdk");
116
- var import_viem3 = require("viem");
59
+ // src/core/execute/normalizeNative.ts
60
+ var import_intents_sdk2 = require("@shogun-sdk/intents-sdk");
117
61
 
118
62
  // src/utils/address.ts
63
+ var import_viem = require("viem");
119
64
  var NATIVE_TOKEN = {
120
65
  ETH: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
121
66
  SOL: "So11111111111111111111111111111111111111111",
@@ -126,13 +71,32 @@ var isNativeAddress = (tokenAddress) => {
126
71
  const normalizedTokenAddress = tokenAddress.toLowerCase();
127
72
  return !!tokenAddress && NATIVE_ADDRESSES.includes(normalizedTokenAddress);
128
73
  };
74
+ function normalizeEvmTokenAddress(address) {
75
+ const lower = address.toLowerCase();
76
+ return lower === NATIVE_TOKEN.ETH.toLowerCase() ? import_viem.zeroAddress : address;
77
+ }
129
78
 
130
79
  // src/utils/chain.ts
131
- var import_intents_sdk2 = require("@shogun-sdk/intents-sdk");
132
- var SOLANA_CHAIN_ID = import_intents_sdk2.ChainID.Solana;
133
- var SupportedChains = [
80
+ var import_intents_sdk = require("@shogun-sdk/intents-sdk");
81
+ var SOLANA_CHAIN_ID = import_intents_sdk.ChainID.Solana;
82
+ var CURRENT_SUPPORTED = [
83
+ import_intents_sdk.ChainID.Solana,
84
+ import_intents_sdk.ChainID.BSC,
85
+ import_intents_sdk.ChainID.Base,
86
+ import_intents_sdk.ChainID.MONAD
87
+ ];
88
+ var ChainId = Object.entries(import_intents_sdk.ChainID).reduce(
89
+ (acc, [key, value]) => {
90
+ if (typeof value === "number" && CURRENT_SUPPORTED.includes(value)) {
91
+ acc[key] = value;
92
+ }
93
+ return acc;
94
+ },
95
+ {}
96
+ );
97
+ var SupportedChainsInternal = [
134
98
  {
135
- id: import_intents_sdk2.ChainID.Arbitrum,
99
+ id: import_intents_sdk.ChainID.Arbitrum,
136
100
  name: "Arbitrum",
137
101
  isEVM: true,
138
102
  wrapped: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
@@ -141,7 +105,7 @@ var SupportedChains = [
141
105
  tokenAddress: NATIVE_TOKEN.ETH
142
106
  },
143
107
  {
144
- id: import_intents_sdk2.ChainID.Optimism,
108
+ id: import_intents_sdk.ChainID.Optimism,
145
109
  name: "Optimism",
146
110
  isEVM: true,
147
111
  wrapped: "0x4200000000000000000000000000000000000006",
@@ -150,7 +114,7 @@ var SupportedChains = [
150
114
  tokenAddress: NATIVE_TOKEN.ETH
151
115
  },
152
116
  {
153
- id: import_intents_sdk2.ChainID.Base,
117
+ id: import_intents_sdk.ChainID.Base,
154
118
  name: "Base",
155
119
  isEVM: true,
156
120
  wrapped: "0x4200000000000000000000000000000000000006",
@@ -159,7 +123,7 @@ var SupportedChains = [
159
123
  tokenAddress: NATIVE_TOKEN.ETH
160
124
  },
161
125
  {
162
- id: import_intents_sdk2.ChainID.Hyperliquid,
126
+ id: import_intents_sdk.ChainID.Hyperliquid,
163
127
  name: "Hyperliquid",
164
128
  isEVM: true,
165
129
  wrapped: "0x5555555555555555555555555555555555555555",
@@ -168,7 +132,7 @@ var SupportedChains = [
168
132
  tokenAddress: NATIVE_TOKEN.ETH
169
133
  },
170
134
  {
171
- id: import_intents_sdk2.ChainID.BSC,
135
+ id: import_intents_sdk.ChainID.BSC,
172
136
  name: "BSC",
173
137
  isEVM: true,
174
138
  wrapped: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
@@ -184,8 +148,21 @@ var SupportedChains = [
184
148
  symbol: "SOL",
185
149
  decimals: 9,
186
150
  tokenAddress: NATIVE_TOKEN.SOL
151
+ },
152
+ {
153
+ id: import_intents_sdk.ChainID.MONAD,
154
+ name: "Monad",
155
+ isEVM: true,
156
+ wrapped: "0x3bd359C1119dA7Da1D913D1C4D2B7c461115433A",
157
+ symbol: "MON",
158
+ decimals: 18,
159
+ tokenAddress: NATIVE_TOKEN.ETH
187
160
  }
188
161
  ];
162
+ var SupportedChains = SupportedChainsInternal.filter(
163
+ (c) => CURRENT_SUPPORTED.includes(c.id)
164
+ );
165
+ var isEvmChain = import_intents_sdk.isEvmChain;
189
166
 
190
167
  // src/utils/viem.ts
191
168
  function isViemWalletClient(wallet) {
@@ -213,10 +190,198 @@ function serializeBigIntsToStrings(obj) {
213
190
  return obj;
214
191
  }
215
192
 
193
+ // src/core/execute/normalizeNative.ts
194
+ function normalizeNative(chainId, address) {
195
+ if ((0, import_intents_sdk2.isEvmChain)(chainId) && isNativeAddress(address)) {
196
+ const chain = SupportedChains.find((c) => c.id === chainId);
197
+ if (!chain?.wrapped)
198
+ throw new Error(`Wrapped token not found for chainId ${chainId}`);
199
+ return chain.wrapped;
200
+ }
201
+ return address;
202
+ }
203
+
204
+ // src/core/getQuote.ts
205
+ async function getQuote(params) {
206
+ const amount = BigInt(params.amount);
207
+ if (!params.tokenIn?.address || !params.tokenOut?.address) {
208
+ throw new Error("Both tokenIn and tokenOut must include an address.");
209
+ }
210
+ if (!params.sourceChainId || !params.destChainId) {
211
+ throw new Error("Both sourceChainId and destChainId are required.");
212
+ }
213
+ if (amount <= 0n) {
214
+ throw new Error("Amount must be greater than 0.");
215
+ }
216
+ const normalizedTokenIn = normalizeNative(params.sourceChainId, params.tokenIn.address);
217
+ const data = await import_intents_sdk3.QuoteProvider.getQuote({
218
+ sourceChainId: params.sourceChainId,
219
+ destChainId: params.destChainId,
220
+ tokenIn: normalizedTokenIn,
221
+ tokenOut: params.tokenOut.address,
222
+ amount
223
+ });
224
+ const slippagePercent = Math.min(Math.max(params.slippage ?? 0.5, 0), 50);
225
+ let warning;
226
+ if (slippagePercent > 10) {
227
+ warning = `\u26A0\uFE0F High slippage tolerance (${slippagePercent.toFixed(2)}%) \u2014 price may vary significantly.`;
228
+ }
229
+ const estimatedAmountOut = BigInt(data.estimatedAmountOut);
230
+ const slippageBps = BigInt(Math.round(slippagePercent * 100));
231
+ const estimatedAmountOutAfterSlippage = estimatedAmountOut * (10000n - slippageBps) / 10000n;
232
+ const pricePerTokenOutInUsd = data.estimatedAmountOutUsd / Number(data.estimatedAmountOut);
233
+ const amountOutUsdAfterSlippage = Number(estimatedAmountOutAfterSlippage) * pricePerTokenOutInUsd;
234
+ const minStablecoinsAmountValue = BigInt(data.estimatedAmountInAsMinStablecoinAmount);
235
+ const minStablecoinsAmountAfterSlippage = minStablecoinsAmountValue * (10000n - slippageBps) / 10000n;
236
+ const pricePerInputToken = estimatedAmountOut * 10n ** BigInt(params.tokenIn.decimals ?? 18) / BigInt(params.amount);
237
+ return {
238
+ amountOut: estimatedAmountOutAfterSlippage,
239
+ amountOutUsd: amountOutUsdAfterSlippage,
240
+ amountInUsd: data.amountInUsd,
241
+ // Input USD stays the same
242
+ minStablecoinsAmount: minStablecoinsAmountAfterSlippage,
243
+ tokenIn: {
244
+ address: params.tokenIn.address,
245
+ decimals: params.tokenIn.decimals ?? 18,
246
+ chainId: params.sourceChainId
247
+ },
248
+ tokenOut: {
249
+ address: params.tokenOut.address,
250
+ decimals: params.tokenOut.decimals ?? 18,
251
+ chainId: params.destChainId
252
+ },
253
+ amountIn: BigInt(params.amount),
254
+ pricePerInputToken,
255
+ slippage: slippagePercent,
256
+ internal: {
257
+ ...data,
258
+ estimatedAmountOutReduced: estimatedAmountOutAfterSlippage,
259
+ estimatedAmountOutUsdReduced: amountOutUsdAfterSlippage
260
+ },
261
+ warning
262
+ };
263
+ }
264
+ function buildQuoteParams({
265
+ tokenIn,
266
+ tokenOut,
267
+ sourceChainId,
268
+ destChainId,
269
+ amount,
270
+ slippage
271
+ }) {
272
+ return {
273
+ tokenIn,
274
+ tokenOut,
275
+ sourceChainId,
276
+ destChainId,
277
+ amount: (0, import_viem2.parseUnits)(amount.toString(), tokenIn.decimals ?? 18).toString(),
278
+ slippage
279
+ };
280
+ }
281
+
282
+ // src/core/getBalances.ts
283
+ var import_intents_sdk4 = require("@shogun-sdk/intents-sdk");
284
+ async function getBalances(params, options) {
285
+ const { addresses, cursorEvm, cursorSvm } = params;
286
+ const { signal } = options ?? {};
287
+ if (!addresses?.evm && !addresses?.svm) {
288
+ throw new Error("At least one address (EVM or SVM) must be provided.");
289
+ }
290
+ const payload = JSON.stringify({
291
+ addresses,
292
+ cursorEvm,
293
+ cursorSvm
294
+ });
295
+ const start = performance.now();
296
+ const response = await fetch(`${import_intents_sdk4.TOKEN_SEARCH_API_BASE_URL}/tokens/balances`, {
297
+ method: "POST",
298
+ headers: {
299
+ accept: "application/json",
300
+ "Content-Type": "application/json"
301
+ },
302
+ body: payload,
303
+ signal
304
+ }).catch((err) => {
305
+ if (err.name === "AbortError") {
306
+ throw new Error("Balance request was cancelled.");
307
+ }
308
+ throw err;
309
+ });
310
+ if (!response.ok) {
311
+ const text = await response.text().catch(() => "");
312
+ throw new Error(`Failed to fetch balances: ${response.status} ${text}`);
313
+ }
314
+ const data = await response.json().catch(() => {
315
+ throw new Error("Invalid JSON response from balances API.");
316
+ });
317
+ const duration = (performance.now() - start).toFixed(1);
318
+ if (process.env.NODE_ENV !== "production") {
319
+ console.debug(`[Shogun SDK] Fetched balances in ${duration}ms`);
320
+ }
321
+ const evmItems = data.evm?.items ?? [];
322
+ const svmItems = data.svm?.items ?? [];
323
+ const combined = [...evmItems, ...svmItems];
324
+ const filtered = combined.filter(
325
+ (b) => CURRENT_SUPPORTED.includes(b.chainId)
326
+ );
327
+ return {
328
+ results: filtered,
329
+ nextCursorEvm: data.evm?.cursor ?? null,
330
+ nextCursorSvm: data.svm?.cursor ?? null
331
+ };
332
+ }
333
+
334
+ // src/core/token-list.ts
335
+ var import_intents_sdk5 = require("@shogun-sdk/intents-sdk");
336
+ async function getTokenList(params) {
337
+ const url = new URL(`${import_intents_sdk5.TOKEN_SEARCH_API_BASE_URL}/tokens/search`);
338
+ if (params.q) url.searchParams.append("q", params.q);
339
+ if (params.networkId) url.searchParams.append("networkId", String(params.networkId));
340
+ if (params.page) url.searchParams.append("page", String(params.page));
341
+ if (params.limit) url.searchParams.append("limit", String(params.limit));
342
+ const res = await fetch(url.toString(), {
343
+ signal: params.signal
344
+ });
345
+ if (!res.ok) {
346
+ throw new Error(`Failed to fetch tokens: ${res.status} ${res.statusText}`);
347
+ }
348
+ const data = await res.json();
349
+ const filteredResults = data.results.filter(
350
+ (token) => CURRENT_SUPPORTED.includes(token.chainId)
351
+ );
352
+ return {
353
+ ...data,
354
+ results: filteredResults,
355
+ count: filteredResults.length
356
+ };
357
+ }
358
+
359
+ // src/core/token.ts
360
+ var import_intents_sdk6 = require("@shogun-sdk/intents-sdk");
361
+ async function getTokensData(addresses) {
362
+ if (!addresses?.length) return [];
363
+ const response = await fetch(`${import_intents_sdk6.TOKEN_SEARCH_API_BASE_URL}/tokens/tokens`, {
364
+ method: "POST",
365
+ headers: {
366
+ "Content-Type": "application/json",
367
+ accept: "*/*"
368
+ },
369
+ body: JSON.stringify({ addresses })
370
+ });
371
+ if (!response.ok) {
372
+ throw new Error(`Failed to fetch token data: ${response.statusText}`);
373
+ }
374
+ const data = await response.json();
375
+ const filtered = data.filter((t) => CURRENT_SUPPORTED.includes(Number(t.chainId)));
376
+ return filtered;
377
+ }
378
+
379
+ // src/core/execute/execute.ts
380
+ var import_intents_sdk12 = require("@shogun-sdk/intents-sdk");
381
+ var import_viem7 = require("viem");
382
+
216
383
  // src/wallet-adapter/evm-wallet-adapter/adapter.ts
217
- var import_ethers = require("ethers/lib/ethers.js");
218
- var import_utils = require("ethers/lib/utils.js");
219
- var import_viem = require("viem");
384
+ var import_viem3 = require("viem");
220
385
  function isEVMTransaction(tx) {
221
386
  return typeof tx.from === "string";
222
387
  }
@@ -234,15 +399,26 @@ var adaptViemWallet = (wallet) => {
234
399
  if (!isEVMTransaction(transaction)) {
235
400
  throw new Error("Expected EVMTransaction but got SolanaTransaction");
236
401
  }
237
- const tx = await wallet.sendTransaction({
238
- from: transaction.from,
239
- to: transaction.to,
240
- data: transaction.data,
241
- value: transaction.value,
242
- account: wallet.account?.address,
243
- chain: wallet.chain
244
- });
245
- return tx;
402
+ if (wallet.transport.type === "http") {
403
+ const request = await wallet.prepareTransactionRequest({
404
+ to: transaction.to,
405
+ data: transaction.data,
406
+ value: transaction.value,
407
+ chain: wallet.chain
408
+ });
409
+ const serializedTransaction = await wallet.signTransaction(request);
410
+ const tx = await wallet.sendRawTransaction({ serializedTransaction });
411
+ return tx;
412
+ } else {
413
+ const hash = await wallet.sendTransaction({
414
+ to: transaction.to,
415
+ data: transaction.data,
416
+ value: transaction.value,
417
+ chain: wallet.chain,
418
+ account: wallet.account
419
+ });
420
+ return hash;
421
+ }
246
422
  };
247
423
  const switchChain = async (chainId) => {
248
424
  try {
@@ -265,67 +441,96 @@ var adaptViemWallet = (wallet) => {
265
441
  if (!addr) throw new Error("No address found");
266
442
  return addr;
267
443
  };
444
+ const readContract = async ({
445
+ address: address2,
446
+ abi,
447
+ functionName,
448
+ args = []
449
+ }) => {
450
+ const publicClient = wallet.extend(import_viem3.publicActions);
451
+ return await publicClient.readContract({
452
+ address: address2,
453
+ abi,
454
+ functionName,
455
+ args
456
+ });
457
+ };
268
458
  return {
269
459
  vmType: "EVM" /* EVM */,
270
- transport: (0, import_viem.custom)(wallet.transport),
460
+ transport: (0, import_viem3.custom)(wallet.transport),
271
461
  getChainId: async () => wallet.getChainId(),
272
462
  address,
273
463
  sendTransaction,
274
464
  signTypedData,
275
- switchChain
465
+ switchChain,
466
+ readContract
276
467
  };
277
468
  };
278
469
 
279
- // src/core/executeOrder/handleEvmExecution.ts
280
- var import_intents_sdk5 = require("@shogun-sdk/intents-sdk");
281
- var import_viem2 = require("viem");
282
-
283
- // src/core/executeOrder/normalizeNative.ts
284
- var import_intents_sdk3 = require("@shogun-sdk/intents-sdk");
285
- function normalizeNative(chainId, address) {
286
- if ((0, import_intents_sdk3.isEvmChain)(chainId) && isNativeAddress(address)) {
287
- const chain = SupportedChains.find((c) => c.id === chainId);
288
- if (!chain?.wrapped)
289
- throw new Error(`Wrapped token not found for chainId ${chainId}`);
290
- return chain.wrapped;
291
- }
292
- return address;
293
- }
470
+ // src/core/execute/handleEvmExecution.ts
471
+ var import_intents_sdk10 = require("@shogun-sdk/intents-sdk");
472
+ var import_viem6 = require("viem");
294
473
 
295
- // src/core/executeOrder/stageMessages.ts
474
+ // src/core/execute/stageMessages.ts
296
475
  var DEFAULT_STAGE_MESSAGES = {
297
476
  processing: "Preparing transaction for execution",
298
477
  approving: "Approving token allowance",
299
478
  approved: "Token approved successfully",
300
- signing: "Signing order for submission",
301
- submitting: "Submitting order to Auctioneer",
302
- success: "Order executed successfully",
303
- error: "Order execution failed"
479
+ signing: "Signing transaction for submission",
480
+ submitting: "Submitting transaction",
481
+ initiated: "Transaction initiated.",
482
+ success: "Transaction Executed successfully",
483
+ success_limit: "Limit order has been submitted successfully.",
484
+ shogun_processing: "Shogun is processing your transaction",
485
+ error: "Transaction failed during submission"
304
486
  };
305
487
 
306
- // src/core/executeOrder/buildOrder.ts
307
- var import_intents_sdk4 = require("@shogun-sdk/intents-sdk");
488
+ // src/core/execute/buildOrder.ts
489
+ var import_intents_sdk7 = require("@shogun-sdk/intents-sdk");
490
+ var import_viem4 = require("viem");
491
+
492
+ // src/utils/order.ts
493
+ var OrderExecutionType = /* @__PURE__ */ ((OrderExecutionType2) => {
494
+ OrderExecutionType2["LIMIT"] = "limit";
495
+ OrderExecutionType2["MARKET"] = "market";
496
+ return OrderExecutionType2;
497
+ })(OrderExecutionType || {});
498
+
499
+ // src/core/execute/buildOrder.ts
308
500
  async function buildOrder({
309
501
  quote,
310
502
  accountAddress,
311
503
  destination,
312
504
  deadline,
313
- isSingleChain
505
+ isSingleChain,
506
+ orderType,
507
+ options
314
508
  }) {
315
509
  const { tokenIn, tokenOut } = quote;
510
+ let amountOutMin = BigInt(quote.internal.estimatedAmountOutReduced);
511
+ if (orderType === "limit" /* LIMIT */ && options && "executionPrice" in options) {
512
+ const executionPrice = Number(options.executionPrice);
513
+ if (Number.isFinite(executionPrice) && executionPrice > 0) {
514
+ const decimalsIn = tokenIn.decimals ?? 18;
515
+ const decimalsOut = tokenOut.decimals ?? 18;
516
+ const formattedAmountIn = Number((0, import_viem4.formatUnits)(BigInt(quote.amountIn.toString()), decimalsIn));
517
+ const rawAmountOut = formattedAmountIn * executionPrice;
518
+ amountOutMin = (0, import_viem4.parseUnits)(rawAmountOut.toString(), decimalsOut);
519
+ }
520
+ }
316
521
  if (isSingleChain) {
317
- return await import_intents_sdk4.SingleChainOrder.create({
522
+ return await import_intents_sdk7.SingleChainOrder.create({
318
523
  user: accountAddress,
319
524
  chainId: tokenIn.chainId,
320
525
  tokenIn: tokenIn.address,
321
526
  tokenOut: tokenOut.address,
322
527
  amountIn: quote.amountIn,
323
- amountOutMin: quote.internal.estimatedAmountOutReduced,
528
+ amountOutMin,
324
529
  deadline,
325
530
  destinationAddress: destination
326
531
  });
327
532
  }
328
- return await import_intents_sdk4.CrossChainOrder.create({
533
+ return await import_intents_sdk7.CrossChainOrder.create({
329
534
  user: accountAddress,
330
535
  sourceChainId: tokenIn.chainId,
331
536
  sourceTokenAddress: tokenIn.address,
@@ -334,12 +539,161 @@ async function buildOrder({
334
539
  destinationTokenAddress: tokenOut.address,
335
540
  destinationAddress: destination,
336
541
  deadline,
337
- destinationTokenMinAmount: quote.internal.estimatedAmountOutReduced,
542
+ destinationTokenMinAmount: amountOutMin,
338
543
  minStablecoinAmount: quote.minStablecoinsAmount
339
544
  });
340
545
  }
341
546
 
342
- // src/core/executeOrder/handleEvmExecution.ts
547
+ // src/utils/pollOrderStatus.ts
548
+ var import_intents_sdk8 = require("@shogun-sdk/intents-sdk");
549
+ async function pollOrderStatus(address, orderId, options = {}) {
550
+ const { intervalMs = 2e3, timeoutMs = 3e5 } = options;
551
+ const startTime = Date.now();
552
+ const isDebug = process.env.NODE_ENV !== "production";
553
+ const isEvmAddress = /^0x[a-fA-F0-9]{40}$/.test(address);
554
+ const isSuiAddress = /^0x[a-fA-F0-9]{64}$/.test(address);
555
+ const isSolanaAddress = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(address);
556
+ let queryParam;
557
+ if (isEvmAddress) queryParam = `evmWallets=${address}`;
558
+ else if (isSuiAddress) queryParam = `suiWallets=${address}`;
559
+ else if (isSolanaAddress) queryParam = `solanaWallets=${address}`;
560
+ else throw new Error(`Unrecognized wallet address format: ${address}`);
561
+ const queryUrl = `${import_intents_sdk8.AUCTIONEER_URL}/user_intent?${queryParam}`;
562
+ return new Promise((resolve, reject) => {
563
+ const pollInterval = setInterval(async () => {
564
+ try {
565
+ if (Date.now() - startTime > timeoutMs) {
566
+ clearInterval(pollInterval);
567
+ return resolve("Timeout");
568
+ }
569
+ const res = await fetch(queryUrl, {
570
+ method: "GET",
571
+ headers: { "Content-Type": "application/json" }
572
+ });
573
+ if (!res.ok) {
574
+ clearInterval(pollInterval);
575
+ return reject(
576
+ new Error(`Failed to fetch orders: ${res.status} ${res.statusText}`)
577
+ );
578
+ }
579
+ const json = await res.json();
580
+ const data = json?.data ?? {};
581
+ const allOrders = [
582
+ ...data.crossChainDcaOrders ?? [],
583
+ ...data.crossChainLimitOrders ?? [],
584
+ ...data.singleChainDcaOrders ?? [],
585
+ ...data.singleChainLimitOrders ?? []
586
+ ];
587
+ const targetOrder = allOrders.find((o) => o.orderId === orderId);
588
+ if (!targetOrder) {
589
+ if (isDebug)
590
+ console.debug(`[pollOrderStatus] [${orderId}] Not found yet`);
591
+ return;
592
+ }
593
+ const { orderStatus } = targetOrder;
594
+ if (isDebug) {
595
+ const elapsed = ((Date.now() - startTime) / 1e3).toFixed(1);
596
+ console.debug(`targetOrder`, targetOrder);
597
+ console.debug(
598
+ `[pollOrderStatus] [${orderId}] status=${orderStatus} (elapsed ${elapsed}s)`
599
+ );
600
+ }
601
+ if (["Fulfilled", "Cancelled", "Outdated"].includes(orderStatus)) {
602
+ clearInterval(pollInterval);
603
+ return resolve(orderStatus);
604
+ }
605
+ } catch (error) {
606
+ clearInterval(pollInterval);
607
+ return reject(error);
608
+ }
609
+ }, intervalMs);
610
+ });
611
+ }
612
+
613
+ // src/core/execute/handleOrderPollingResult.ts
614
+ async function handleOrderPollingResult({
615
+ status,
616
+ orderId,
617
+ chainId,
618
+ update,
619
+ messageFor
620
+ }) {
621
+ switch (status) {
622
+ case "Fulfilled":
623
+ update("success", messageFor("success"));
624
+ return {
625
+ status: true,
626
+ orderId,
627
+ chainId,
628
+ finalStatus: status,
629
+ stage: "success"
630
+ };
631
+ case "Cancelled":
632
+ update("error", "Order was cancelled before fulfillment");
633
+ break;
634
+ case "Timeout":
635
+ update("error", "Order polling timed out");
636
+ break;
637
+ case "NotFound":
638
+ default:
639
+ update("error", "Order not found");
640
+ break;
641
+ }
642
+ return {
643
+ status: false,
644
+ orderId,
645
+ chainId,
646
+ finalStatus: status,
647
+ stage: "error"
648
+ };
649
+ }
650
+
651
+ // src/core/execute/ensurePermit2Allowance.ts
652
+ var import_viem5 = require("viem");
653
+ var import_intents_sdk9 = require("@shogun-sdk/intents-sdk");
654
+ async function ensurePermit2Allowance({
655
+ chainId,
656
+ tokenIn,
657
+ wallet,
658
+ accountAddress,
659
+ requiredAmount,
660
+ increaseByDelta = false
661
+ }) {
662
+ const spender = import_intents_sdk9.PERMIT2_ADDRESS[chainId];
663
+ let currentAllowance = 0n;
664
+ try {
665
+ if (!wallet.readContract) {
666
+ throw new Error("Wallet does not implement readContract()");
667
+ }
668
+ currentAllowance = await wallet.readContract({
669
+ address: tokenIn,
670
+ abi: import_viem5.erc20Abi,
671
+ functionName: "allowance",
672
+ args: [accountAddress, spender]
673
+ });
674
+ } catch (error) {
675
+ console.warn(`[Permit2] Failed to read allowance for ${tokenIn}`, error);
676
+ }
677
+ const approvalAmount = increaseByDelta ? currentAllowance + requiredAmount : import_viem5.maxUint256;
678
+ console.debug(
679
+ `[Permit2] Approving ${approvalAmount} for ${tokenIn} (current: ${currentAllowance}, required: ${requiredAmount})`
680
+ );
681
+ await wallet.sendTransaction({
682
+ to: tokenIn,
683
+ from: accountAddress,
684
+ data: (0, import_viem5.encodeFunctionData)({
685
+ abi: import_viem5.erc20Abi,
686
+ functionName: "approve",
687
+ args: [spender, approvalAmount]
688
+ }),
689
+ value: 0n
690
+ });
691
+ console.info(
692
+ `[Permit2] Approval transaction sent for ${tokenIn} on chain ${chainId}`
693
+ );
694
+ }
695
+
696
+ // src/core/execute/handleEvmExecution.ts
343
697
  async function handleEvmExecution({
344
698
  recipientAddress,
345
699
  quote,
@@ -347,18 +701,22 @@ async function handleEvmExecution({
347
701
  accountAddress,
348
702
  wallet,
349
703
  isSingleChain,
350
- deadline,
351
- update
704
+ update,
705
+ orderType,
706
+ options
352
707
  }) {
353
- const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage];
708
+ const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage] ?? "";
709
+ const deadline = options?.deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
354
710
  await wallet.switchChain(chainId);
355
711
  const tokenIn = normalizeNative(chainId, quote.tokenIn.address);
712
+ quote.tokenOut.address = normalizeEvmTokenAddress(quote.tokenOut.address);
356
713
  const shouldWrapNative = isNativeAddress(quote.tokenIn.address);
357
714
  update("processing", shouldWrapNative ? `${messageFor("processing")} (wrapping native token)` : messageFor("processing"));
358
715
  if (shouldWrapNative) {
716
+ quote.tokenIn.address === tokenIn;
359
717
  await wallet.sendTransaction({
360
718
  to: tokenIn,
361
- data: (0, import_viem2.encodeFunctionData)({
719
+ data: (0, import_viem6.encodeFunctionData)({
362
720
  abi: [{ type: "function", name: "deposit", stateMutability: "payable", inputs: [], outputs: [] }],
363
721
  functionName: "deposit",
364
722
  args: []
@@ -367,43 +725,70 @@ async function handleEvmExecution({
367
725
  from: accountAddress
368
726
  });
369
727
  }
370
- update("approving", messageFor("approving"));
371
- await wallet.sendTransaction({
372
- to: tokenIn,
373
- data: (0, import_viem2.encodeFunctionData)({
374
- abi: import_viem2.erc20Abi,
375
- functionName: "approve",
376
- args: [import_intents_sdk5.PERMIT2_ADDRESS[chainId], BigInt(quote.amountIn)]
377
- }),
378
- value: 0n,
379
- from: accountAddress
728
+ update("processing", messageFor("approving"));
729
+ await ensurePermit2Allowance({
730
+ chainId,
731
+ tokenIn,
732
+ wallet,
733
+ accountAddress,
734
+ requiredAmount: BigInt(quote.amountIn)
380
735
  });
381
- update("approved", messageFor("approved"));
736
+ update("processing", messageFor("approved"));
382
737
  const destination = recipientAddress ?? accountAddress;
383
738
  const order = await buildOrder({
384
739
  quote,
385
740
  accountAddress,
386
741
  destination,
387
742
  deadline,
388
- isSingleChain
743
+ isSingleChain,
744
+ orderType,
745
+ options
389
746
  });
390
- update("signing", messageFor("signing"));
391
- const { orderTypedData, nonce } = isSingleChain ? await (0, import_intents_sdk5.getEVMSingleChainOrderTypedData)(order) : await (0, import_intents_sdk5.getEVMCrossChainOrderTypedData)(order);
747
+ console.debug(`order`, order);
748
+ update("processing", messageFor("signing"));
749
+ const { orderTypedData, nonce } = isSingleChain ? await (0, import_intents_sdk10.getEVMSingleChainOrderTypedData)(order) : await (0, import_intents_sdk10.getEVMCrossChainOrderTypedData)(order);
750
+ const typedData = serializeBigIntsToStrings(orderTypedData);
392
751
  if (!wallet.signTypedData) {
393
752
  throw new Error("Wallet does not support EIP-712 signing");
394
753
  }
395
- const signature = await wallet.signTypedData(serializeBigIntsToStrings(orderTypedData));
396
- update("submitting", messageFor("submitting"));
754
+ const signature = await wallet.signTypedData({
755
+ domain: typedData.domain,
756
+ types: typedData.types,
757
+ primaryType: typedData.primaryType,
758
+ value: typedData.message,
759
+ message: typedData.message
760
+ });
761
+ update("processing", messageFor("submitting"));
397
762
  const res = await order.sendToAuctioneer({ signature, nonce: nonce.toString() });
398
763
  if (!res.success) {
399
764
  throw new Error("Auctioneer submission failed");
400
765
  }
401
- update("success", messageFor("success"));
402
- return { status: true, txHash: res.data, chainId, stage: "success" };
766
+ update("initiated", messageFor("initiated"));
767
+ const { intentId: orderId } = res.data;
768
+ update("initiated", messageFor("shogun_processing"));
769
+ if (orderType === "limit" /* LIMIT */) {
770
+ update("success", messageFor("success_limit"));
771
+ return {
772
+ status: true,
773
+ orderId,
774
+ chainId,
775
+ finalStatus: "OrderPlaced",
776
+ stage: "success"
777
+ };
778
+ } else {
779
+ const status = await pollOrderStatus(accountAddress, orderId);
780
+ return await handleOrderPollingResult({
781
+ status,
782
+ orderId,
783
+ chainId,
784
+ update,
785
+ messageFor
786
+ });
787
+ }
403
788
  }
404
789
 
405
- // src/core/executeOrder/handleSolanaExecution.ts
406
- var import_intents_sdk6 = require("@shogun-sdk/intents-sdk");
790
+ // src/core/execute/handleSolanaExecution.ts
791
+ var import_intents_sdk11 = require("@shogun-sdk/intents-sdk");
407
792
  var import_web3 = require("@solana/web3.js");
408
793
  async function handleSolanaExecution({
409
794
  recipientAddress,
@@ -412,12 +797,14 @@ async function handleSolanaExecution({
412
797
  isSingleChain,
413
798
  update,
414
799
  accountAddress,
415
- deadline
800
+ orderType,
801
+ options
416
802
  }) {
417
803
  if (!wallet.rpcUrl) {
418
804
  throw new Error("Solana wallet is missing rpcUrl");
419
805
  }
420
- const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage];
806
+ const deadline = options?.deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
807
+ const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage] ?? "";
421
808
  update("processing", messageFor("processing"));
422
809
  const destination = recipientAddress ?? accountAddress;
423
810
  const order = await buildOrder({
@@ -425,7 +812,9 @@ async function handleSolanaExecution({
425
812
  accountAddress,
426
813
  destination,
427
814
  deadline,
428
- isSingleChain
815
+ isSingleChain,
816
+ orderType,
817
+ options
429
818
  });
430
819
  const txData = await getSolanaOrderInstructions({
431
820
  order,
@@ -433,10 +822,9 @@ async function handleSolanaExecution({
433
822
  rpcUrl: wallet.rpcUrl
434
823
  });
435
824
  const transaction = import_web3.VersionedTransaction.deserialize(Uint8Array.from(txData.txBytes));
436
- update("signing", messageFor("signing"));
437
- console.log({ order });
438
- const txSignature = await wallet.sendTransaction(transaction);
439
- update("submitting", messageFor("submitting"));
825
+ update("processing", messageFor("signing"));
826
+ await wallet.sendTransaction(transaction);
827
+ update("processing", messageFor("submitting"));
440
828
  const response = await submitToAuctioneer({
441
829
  order,
442
830
  isSingleChain,
@@ -445,13 +833,28 @@ async function handleSolanaExecution({
445
833
  if (!response.success) {
446
834
  throw new Error("Auctioneer submission failed");
447
835
  }
448
- update("success", messageFor("success"));
449
- return {
450
- status: true,
451
- txHash: txSignature,
452
- chainId: import_intents_sdk6.ChainID.Solana,
453
- stage: "success"
454
- };
836
+ update("initiated", messageFor("initiated"));
837
+ const { intentId: orderId } = response.data;
838
+ update("initiated", messageFor("shogun_processing"));
839
+ if (orderType === "limit" /* LIMIT */) {
840
+ update("success", messageFor("success_limit"));
841
+ return {
842
+ status: true,
843
+ orderId,
844
+ chainId: SOLANA_CHAIN_ID,
845
+ finalStatus: "OrderPlaced",
846
+ stage: "success"
847
+ };
848
+ } else {
849
+ const status = await pollOrderStatus(accountAddress, orderId);
850
+ return await handleOrderPollingResult({
851
+ status,
852
+ orderId,
853
+ chainId: SOLANA_CHAIN_ID,
854
+ update,
855
+ messageFor
856
+ });
857
+ }
455
858
  }
456
859
  async function getSolanaOrderInstructions({
457
860
  order,
@@ -459,11 +862,11 @@ async function getSolanaOrderInstructions({
459
862
  rpcUrl
460
863
  }) {
461
864
  if (isSingleChain) {
462
- return await (0, import_intents_sdk6.getSolanaSingleChainOrderInstructions)(order, {
865
+ return await (0, import_intents_sdk11.getSolanaSingleChainOrderInstructions)(order, {
463
866
  rpcUrl
464
867
  });
465
868
  }
466
- return await (0, import_intents_sdk6.getSolanaCrossChainOrderInstructions)(order, {
869
+ return await (0, import_intents_sdk11.getSolanaCrossChainOrderInstructions)(order, {
467
870
  rpcUrl
468
871
  });
469
872
  }
@@ -484,52 +887,93 @@ async function submitToAuctioneer({
484
887
  });
485
888
  }
486
889
 
487
- // src/core/executeOrder/execute.ts
890
+ // src/core/execute/execute.ts
488
891
  async function executeOrder({
489
892
  quote,
490
893
  accountAddress,
491
894
  recipientAddress,
492
895
  wallet,
493
896
  onStatus,
897
+ orderType = "market" /* MARKET */,
494
898
  options = {}
495
899
  }) {
496
- const deadline = options.deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
900
+ const isDev = process.env.NODE_ENV !== "production";
901
+ const log = (...args) => {
902
+ if (isDev) console.debug("[OneShot::executeOrder]", ...args);
903
+ };
497
904
  const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage];
498
- const update = (stage, message) => onStatus?.(stage, message ?? messageFor(stage));
905
+ const update = (stage, message) => {
906
+ log("Stage:", stage, "| Message:", message ?? messageFor(stage));
907
+ onStatus?.(stage, message ?? messageFor(stage));
908
+ };
499
909
  try {
910
+ log("Starting execution:", {
911
+ accountAddress,
912
+ recipientAddress,
913
+ tokenIn: quote?.tokenIn,
914
+ tokenOut: quote?.tokenOut
915
+ });
500
916
  const adapter = normalizeWallet(wallet);
501
917
  if (!adapter) throw new Error("No wallet provided");
502
918
  const { tokenIn, tokenOut } = quote;
919
+ const srcChain = Number(tokenIn.chainId);
920
+ const destChain = Number(tokenOut.chainId);
921
+ if (!CURRENT_SUPPORTED.includes(srcChain) || !CURRENT_SUPPORTED.includes(destChain)) {
922
+ const unsupportedChains = [
923
+ !CURRENT_SUPPORTED.includes(srcChain) ? srcChain : null,
924
+ !CURRENT_SUPPORTED.includes(destChain) ? destChain : null
925
+ ].filter(Boolean).join(", ");
926
+ const errorMsg = `Unsupported chain(s): ${unsupportedChains}`;
927
+ update("error", errorMsg);
928
+ log("Error:", errorMsg);
929
+ throw new Error(errorMsg);
930
+ }
503
931
  const isSingleChain = tokenIn.chainId === tokenOut.chainId;
504
932
  const chainId = Number(tokenIn.chainId);
505
- update("processing", messageFor("processing"));
506
- if ((0, import_intents_sdk7.isEvmChain)(chainId)) {
507
- return await handleEvmExecution({
933
+ update("processing");
934
+ if ((0, import_intents_sdk12.isEvmChain)(chainId)) {
935
+ log("Detected EVM chain:", chainId);
936
+ const result = await handleEvmExecution({
508
937
  recipientAddress,
509
938
  quote,
510
939
  chainId,
511
940
  accountAddress,
512
941
  wallet: adapter,
513
942
  isSingleChain,
514
- deadline,
515
- update
943
+ update,
944
+ orderType,
945
+ options
516
946
  });
947
+ log("EVM execution result:", result);
948
+ return result;
517
949
  }
518
- if (chainId === import_intents_sdk7.ChainID.Solana) {
519
- return await handleSolanaExecution({
950
+ if (chainId === import_intents_sdk12.ChainID.Solana) {
951
+ log("Detected Solana chain");
952
+ const result = await handleSolanaExecution({
520
953
  recipientAddress,
521
954
  quote,
522
955
  accountAddress,
523
956
  wallet: adapter,
524
957
  isSingleChain,
525
- deadline,
526
- update
958
+ update,
959
+ orderType,
960
+ options
527
961
  });
962
+ log("Solana execution result:", result);
963
+ return result;
528
964
  }
529
- update("error", "Unsupported chain");
530
- return { status: false, message: "Unsupported chain", stage: "error" };
965
+ const unsupported = `Unsupported chain: ${chainId}`;
966
+ update("error", unsupported);
967
+ log("Error:", unsupported);
968
+ return { status: false, message: unsupported, stage: "error" };
531
969
  } catch (error) {
532
- const message = error instanceof import_viem3.BaseError ? error.shortMessage : error instanceof Error ? error.message : String(error);
970
+ let message = "An unknown error occurred";
971
+ if (error && typeof error === "object") {
972
+ const err = error;
973
+ message = err.details ?? err.message ?? message;
974
+ } else if (typeof error === "string") {
975
+ message = error;
976
+ }
533
977
  update("error", message);
534
978
  return { status: false, message, stage: "error" };
535
979
  }
@@ -540,348 +984,937 @@ function normalizeWallet(wallet) {
540
984
  return wallet;
541
985
  }
542
986
 
543
- // src/react/useExecuteOrder.ts
544
- function useExecuteOrder() {
545
- const [status, setStatus] = (0, import_react2.useState)("processing");
546
- const [message, setMessage] = (0, import_react2.useState)(null);
547
- const [loading, setLoading] = (0, import_react2.useState)(false);
548
- const [data, setData] = (0, import_react2.useState)(null);
549
- const [error, setError] = (0, import_react2.useState)(null);
550
- const isMounted = (0, import_react2.useRef)(true);
987
+ // src/core/orders/getOrders.ts
988
+ var import_intents_sdk13 = require("@shogun-sdk/intents-sdk");
989
+ async function getOrders({
990
+ evmAddress,
991
+ solAddress
992
+ }) {
993
+ if (!evmAddress && !solAddress) {
994
+ throw new Error("At least one wallet address (EVM, Solana) must be provided.");
995
+ }
996
+ const orders = await (0, import_intents_sdk13.fetchUserOrders)(evmAddress, solAddress);
997
+ return orders;
998
+ }
999
+
1000
+ // src/core/orders/cancelOrder.ts
1001
+ var import_intents_sdk14 = require("@shogun-sdk/intents-sdk");
1002
+ var import_web32 = require("@solana/web3.js");
1003
+ var import_viem8 = require("viem");
1004
+ async function cancelIntentsOrder({
1005
+ order,
1006
+ wallet,
1007
+ sol_rpc
1008
+ }) {
1009
+ const isCrossChain = "srcChainId" in order && "destChainId" in order && order.srcChainId !== order.destChainId;
1010
+ const srcChain = "srcChainId" in order ? order.srcChainId : order.chainId;
1011
+ const isSolanaOrder = srcChain === import_intents_sdk14.ChainID.Solana;
1012
+ if (isSolanaOrder) {
1013
+ if (!isCrossChain) {
1014
+ const { versionedMessageBytes: versionedMessageBytes2 } = await (0, import_intents_sdk14.cancelSingleChainOrderInstructionsAsBytes)(
1015
+ order.orderId,
1016
+ order.user,
1017
+ { rpcUrl: sol_rpc }
1018
+ );
1019
+ const message2 = import_web32.VersionedMessage.deserialize(
1020
+ versionedMessageBytes2
1021
+ );
1022
+ const tx2 = new import_web32.VersionedTransaction(message2);
1023
+ return await wallet.sendTransaction(tx2);
1024
+ }
1025
+ const { versionedMessageBytes } = await (0, import_intents_sdk14.cancelCrossChainOrderInstructionsAsBytes)(
1026
+ order.orderId,
1027
+ order.user,
1028
+ { rpcUrl: sol_rpc }
1029
+ );
1030
+ const message = import_web32.VersionedMessage.deserialize(
1031
+ versionedMessageBytes
1032
+ );
1033
+ const tx = new import_web32.VersionedTransaction(message);
1034
+ return await wallet.sendTransaction(tx);
1035
+ }
1036
+ const chainId = srcChain;
1037
+ const { readContract } = wallet;
1038
+ if (!readContract) {
1039
+ throw new Error("Wallet does not support readContract");
1040
+ }
1041
+ const nonce = BigInt(order.nonce ?? "0");
1042
+ const nonceWordPos = nonce >> 8n;
1043
+ const nonceBitPos = nonce - nonceWordPos * 256n;
1044
+ if (isCrossChain) {
1045
+ const [orderData = [false, false], currentNonceBitmap = 0n] = await Promise.all([
1046
+ readContract({
1047
+ address: import_intents_sdk14.CROSS_CHAIN_GUARD_ADDRESSES[chainId],
1048
+ abi: [
1049
+ {
1050
+ inputs: [{ name: "orderId", type: "bytes32" }],
1051
+ name: "orderData",
1052
+ outputs: [
1053
+ { name: "initialized", type: "bool" },
1054
+ { name: "deactivated", type: "bool" }
1055
+ ],
1056
+ stateMutability: "view",
1057
+ type: "function"
1058
+ }
1059
+ ],
1060
+ functionName: "orderData",
1061
+ args: [order.orderId]
1062
+ }),
1063
+ readContract({
1064
+ address: import_intents_sdk14.PERMIT2_ADDRESS[chainId],
1065
+ abi: [
1066
+ {
1067
+ inputs: [
1068
+ { name: "owner", type: "address" },
1069
+ { name: "wordPos", type: "uint256" }
1070
+ ],
1071
+ name: "nonceBitmap",
1072
+ outputs: [{ name: "", type: "uint256" }],
1073
+ stateMutability: "view",
1074
+ type: "function"
1075
+ }
1076
+ ],
1077
+ functionName: "nonceBitmap",
1078
+ args: [order.user, nonceWordPos]
1079
+ })
1080
+ ]);
1081
+ const [initialized, deactivated] = orderData;
1082
+ if (initialized) {
1083
+ if (deactivated) {
1084
+ throw new Error("Order is already deactivated");
1085
+ }
1086
+ return await wallet.sendTransaction({
1087
+ to: import_intents_sdk14.CROSS_CHAIN_GUARD_ADDRESSES[order.srcChainId],
1088
+ data: (0, import_viem8.encodeFunctionData)({
1089
+ abi: [
1090
+ {
1091
+ inputs: [
1092
+ {
1093
+ components: [
1094
+ { name: "user", type: "address" },
1095
+ { name: "tokenIn", type: "address" },
1096
+ { name: "srcChainId", type: "uint256" },
1097
+ { name: "deadline", type: "uint256" },
1098
+ { name: "amountIn", type: "uint256" },
1099
+ { name: "minStablecoinsAmount", type: "uint256" },
1100
+ { name: "executionDetailsHash", type: "bytes32" },
1101
+ { name: "nonce", type: "uint256" }
1102
+ ],
1103
+ name: "orderInfo",
1104
+ type: "tuple"
1105
+ }
1106
+ ],
1107
+ name: "cancelOrder",
1108
+ outputs: [],
1109
+ stateMutability: "nonpayable",
1110
+ type: "function"
1111
+ }
1112
+ ],
1113
+ functionName: "cancelOrder",
1114
+ args: [
1115
+ {
1116
+ user: order.user,
1117
+ tokenIn: order.tokenIn,
1118
+ srcChainId: BigInt(order.srcChainId),
1119
+ deadline: BigInt(order.deadline),
1120
+ amountIn: BigInt(order.amountIn),
1121
+ minStablecoinsAmount: BigInt(order.minStablecoinsAmount),
1122
+ executionDetailsHash: order.executionDetailsHash,
1123
+ nonce
1124
+ }
1125
+ ]
1126
+ }),
1127
+ value: BigInt(0),
1128
+ from: order.user
1129
+ });
1130
+ } else {
1131
+ if ((currentNonceBitmap & 1n << nonceBitPos) !== 0n) {
1132
+ throw new Error("Nonce is already invalidated");
1133
+ }
1134
+ const mask2 = 1n << nonceBitPos;
1135
+ return await wallet.sendTransaction({
1136
+ to: import_intents_sdk14.PERMIT2_ADDRESS[order.srcChainId],
1137
+ data: (0, import_viem8.encodeFunctionData)({
1138
+ abi: [
1139
+ {
1140
+ inputs: [
1141
+ { name: "wordPos", type: "uint256" },
1142
+ { name: "mask", type: "uint256" }
1143
+ ],
1144
+ name: "invalidateUnorderedNonces",
1145
+ outputs: [],
1146
+ stateMutability: "nonpayable",
1147
+ type: "function"
1148
+ }
1149
+ ],
1150
+ functionName: "invalidateUnorderedNonces",
1151
+ args: [nonceWordPos, mask2]
1152
+ }),
1153
+ value: BigInt(0),
1154
+ from: order.user
1155
+ });
1156
+ }
1157
+ }
1158
+ const [wasManuallyInitialized, currentBitmap = 0n] = await Promise.all([
1159
+ readContract({
1160
+ address: import_intents_sdk14.SINGLE_CHAIN_GUARD_ADDRESSES[chainId],
1161
+ abi: [
1162
+ {
1163
+ inputs: [{ name: "orderHash", type: "bytes32" }],
1164
+ name: "orderManuallyInitialized",
1165
+ outputs: [{ name: "", type: "bool" }],
1166
+ stateMutability: "view",
1167
+ type: "function"
1168
+ }
1169
+ ],
1170
+ functionName: "orderManuallyInitialized",
1171
+ args: [order.orderId]
1172
+ }),
1173
+ readContract({
1174
+ address: import_intents_sdk14.PERMIT2_ADDRESS[chainId],
1175
+ abi: [
1176
+ {
1177
+ inputs: [
1178
+ { name: "owner", type: "address" },
1179
+ { name: "wordPos", type: "uint256" }
1180
+ ],
1181
+ name: "nonceBitmap",
1182
+ outputs: [{ name: "", type: "uint256" }],
1183
+ stateMutability: "view",
1184
+ type: "function"
1185
+ }
1186
+ ],
1187
+ functionName: "nonceBitmap",
1188
+ args: [order.user, nonceWordPos]
1189
+ })
1190
+ ]);
1191
+ if (wasManuallyInitialized) {
1192
+ return await wallet.sendTransaction({
1193
+ to: import_intents_sdk14.SINGLE_CHAIN_GUARD_ADDRESSES[chainId],
1194
+ data: (0, import_viem8.encodeFunctionData)({
1195
+ abi: [
1196
+ {
1197
+ inputs: [
1198
+ {
1199
+ name: "order",
1200
+ type: "tuple",
1201
+ components: [
1202
+ { name: "amountIn", type: "uint256" },
1203
+ { name: "tokenIn", type: "address" },
1204
+ { name: "deadline", type: "uint256" },
1205
+ { name: "nonce", type: "uint256" },
1206
+ { name: "encodedExternalCallData", type: "bytes" },
1207
+ {
1208
+ name: "extraTransfers",
1209
+ type: "tuple[]",
1210
+ components: [
1211
+ { name: "amount", type: "uint256" },
1212
+ { name: "receiver", type: "address" },
1213
+ { name: "token", type: "address" }
1214
+ ]
1215
+ },
1216
+ {
1217
+ name: "requestedOutput",
1218
+ type: "tuple",
1219
+ components: [
1220
+ { name: "amount", type: "uint256" },
1221
+ { name: "receiver", type: "address" },
1222
+ { name: "token", type: "address" }
1223
+ ]
1224
+ },
1225
+ { name: "user", type: "address" }
1226
+ ]
1227
+ }
1228
+ ],
1229
+ name: "cancelManuallyCreatedOrder",
1230
+ outputs: [],
1231
+ stateMutability: "nonpayable",
1232
+ type: "function"
1233
+ }
1234
+ ],
1235
+ functionName: "cancelManuallyCreatedOrder",
1236
+ args: [
1237
+ {
1238
+ amountIn: BigInt(order.amountIn),
1239
+ tokenIn: order.tokenIn,
1240
+ deadline: BigInt(order.deadline),
1241
+ nonce,
1242
+ encodedExternalCallData: "0x",
1243
+ extraTransfers: (order.extraTransfers || []).map((t) => ({
1244
+ amount: BigInt(t.amount),
1245
+ receiver: t.receiver,
1246
+ token: t.token
1247
+ })),
1248
+ requestedOutput: {
1249
+ amount: BigInt(order.amountOutMin),
1250
+ receiver: order.destinationAddress,
1251
+ token: order.tokenOut
1252
+ },
1253
+ user: order.user
1254
+ }
1255
+ ]
1256
+ }),
1257
+ value: 0n,
1258
+ from: order.user
1259
+ });
1260
+ }
1261
+ const mask = 1n << nonceBitPos;
1262
+ if ((currentBitmap & mask) !== 0n) {
1263
+ throw new Error("Nonce is already invalidated");
1264
+ }
1265
+ return await wallet.sendTransaction({
1266
+ to: import_intents_sdk14.PERMIT2_ADDRESS[chainId],
1267
+ data: (0, import_viem8.encodeFunctionData)({
1268
+ abi: [
1269
+ {
1270
+ inputs: [
1271
+ { name: "wordPos", type: "uint256" },
1272
+ { name: "mask", type: "uint256" }
1273
+ ],
1274
+ name: "invalidateUnorderedNonces",
1275
+ outputs: [],
1276
+ stateMutability: "nonpayable",
1277
+ type: "function"
1278
+ }
1279
+ ],
1280
+ functionName: "invalidateUnorderedNonces",
1281
+ args: [nonceWordPos, mask]
1282
+ }),
1283
+ value: 0n,
1284
+ from: order.user
1285
+ });
1286
+ }
1287
+
1288
+ // src/core/client.ts
1289
+ var SwapSDK = class {
1290
+ constructor(config) {
1291
+ __publicField(this, "apiKey");
1292
+ /**
1293
+ * Fetches metadata for one or more tokens from the Shogun Token Search API.
1294
+ *
1295
+ * ---
1296
+ * ### Overview
1297
+ * `getTokensData` retrieves normalized token information — such as symbol, name,
1298
+ * decimals, logo URI, and verified status — for a given list of token addresses.
1299
+ *
1300
+ * It supports both **EVM** and **SVM (Solana)** tokens, returning metadata from
1301
+ * Shogun’s unified token registry.
1302
+ *
1303
+ * ---
1304
+ * @example
1305
+ * ```ts
1306
+ * const tokens = await getTokensData([
1307
+ * "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
1308
+ * "So11111111111111111111111111111111111111112", // SOL
1309
+ * ]);
1310
+ *
1311
+ * console.log(tokens);
1312
+ * [
1313
+ * { symbol: "USDC", name: "USD Coin", chainId: 1, decimals: 6, ... },
1314
+ * { symbol: "SOL", name: "Solana", chainId: 101, decimals: 9, ... }
1315
+ * ]
1316
+ * ```
1317
+ *
1318
+ * @param addresses - An array of token addresses (EVM or SVM) to fetch metadata for.
1319
+ * @returns A promise resolving to an array of {@link TokenInfo} objects.
1320
+ *
1321
+ * @throws Will throw an error if the network request fails or the API responds with a non-OK status.
1322
+ */
1323
+ __publicField(this, "getTokensData", getTokensData.bind(this));
1324
+ if (!config.apiKey) {
1325
+ throw new Error("SwapSDK: Missing API key");
1326
+ }
1327
+ this.apiKey = config.apiKey;
1328
+ if (this.apiKey) void this.apiKey;
1329
+ }
1330
+ /**
1331
+ * Retrieves a swap quote for the given input and output tokens.
1332
+ *
1333
+ * @param params - Quote parameters including source/destination tokens and amount.
1334
+ * @returns A normalized `SwapQuoteResponse` containing output amount, route, and metadata.
1335
+ */
1336
+ async getQuote(params) {
1337
+ return getQuote(params);
1338
+ }
1339
+ /**
1340
+ * Fetches token balances for the specified user wallet(s).
1341
+ *
1342
+ * Supports both EVM and SVM (Solana) wallet addresses.
1343
+ *
1344
+ * @param params - Wallet address and optional chain filters.
1345
+ * @param options - Optional abort signal for cancellation.
1346
+ * @returns A unified balance response with per-chain token details.
1347
+ */
1348
+ async getBalances(params, options) {
1349
+ return getBalances(params, options);
1350
+ }
1351
+ /**
1352
+ * Retrieves a list of verified tokens based on search query or chain filter.
1353
+ *
1354
+ * @param params - Search parameters (query, chain ID, pagination options).
1355
+ * @returns Paginated `TokenSearchResponse` containing token metadata.
1356
+ */
1357
+ async getTokenList(params) {
1358
+ return getTokenList(params);
1359
+ }
1360
+ /**
1361
+ * Executes a prepared swap quote using the provided wallet and configuration.
1362
+ *
1363
+ * Handles:
1364
+ * - Token approval (if required)
1365
+ * - Transaction signing and broadcasting
1366
+ * - Confirmation polling and stage-based status updates
1367
+ *
1368
+ * Supports both:
1369
+ * - Market orders (with optional deadline)
1370
+ * - Limit orders (requires executionPrice and deadline)
1371
+ *
1372
+ * @param quote - The swap quote to execute, containing route and metadata.
1373
+ * @param accountAddress - The user's wallet address executing the swap.
1374
+ * @param recipientAddress - Optional recipient address for the output tokens (defaults to sender).
1375
+ * @param wallet - Adapted wallet instance (EVM/Solana) or a standard Viem `WalletClient`.
1376
+ * @param onStatus - Optional callback for receiving execution stage updates and messages.
1377
+ * @param orderType - Defines whether this is a market or limit order.
1378
+ * @param options - Execution parameters (different per order type).
1379
+ *
1380
+ * @returns A finalized execution result containing transaction hash, status, and any returned data.
1381
+ *
1382
+ * @example
1383
+ * ```ts
1384
+ * * Market order
1385
+ * const result = await sdk.executeTransaction({
1386
+ * quote,
1387
+ * accountAddress: "0x123...",
1388
+ * wallet,
1389
+ * orderType: OrderExecutionType.MARKET,
1390
+ * options: { deadline: 1800 },
1391
+ * onStatus: (stage, msg) => console.log(stage, msg),
1392
+ * });
1393
+ *
1394
+ * * Limit order
1395
+ * const result = await sdk.executeTransaction({
1396
+ * quote,
1397
+ * accountAddress: "0x123...",
1398
+ * wallet,
1399
+ * orderType: OrderExecutionType.LIMIT,
1400
+ * options: { executionPrice: "0.0021", deadline: 3600 },
1401
+ * });
1402
+ * ```
1403
+ */
1404
+ async executeTransaction({
1405
+ quote,
1406
+ accountAddress,
1407
+ recipientAddress,
1408
+ wallet,
1409
+ onStatus,
1410
+ orderType,
1411
+ options
1412
+ }) {
1413
+ return executeOrder({
1414
+ quote,
1415
+ wallet,
1416
+ accountAddress,
1417
+ recipientAddress,
1418
+ onStatus,
1419
+ orderType,
1420
+ options
1421
+ });
1422
+ }
1423
+ /**
1424
+ * Fetches all user orders (Market, Limit, Cross-chain) for connected wallets.
1425
+ *
1426
+ * ---
1427
+ * ### Overview
1428
+ * Retrieves both **single-chain** and **cross-chain** orders from the Shogun Intents API.
1429
+ * Works across EVM, Solana
1430
+ *
1431
+ * ---
1432
+ * @example
1433
+ * ```ts
1434
+ * const orders = await sdk.getOrders({
1435
+ * evmAddress: "0x123...",
1436
+ * solAddress: "9d12hF...abc",
1437
+ * });
1438
+ *
1439
+ * console.log(orders.singleChainLimitOrders);
1440
+ * ```
1441
+ *
1442
+ * @param params - Wallet addresses to fetch orders for (EVM, Solana).
1443
+ * @returns A structured {@link ApiUserOrders} object containing all user orders.
1444
+ */
1445
+ async getOrders(params) {
1446
+ return getOrders(params);
1447
+ }
1448
+ /**
1449
+ * Cancels an order (single-chain or cross-chain) on EVM or Solana.
1450
+ *
1451
+ * Internally routes to the correct guard contract or Solana program to
1452
+ * deactivate the order or invalidate its nonce.
1453
+ *
1454
+ * @param params.order - Order payload returned from the Intents API.
1455
+ * @param params.wallet - Adapted wallet used to submit the cancellation transaction.
1456
+ * @param params.solRpc - Solana RPC URL used for SVM cancellations.
1457
+ *
1458
+ * @returns A transaction signature or hash from the underlying wallet.
1459
+ */
1460
+ async cancelOrder(params) {
1461
+ return cancelIntentsOrder({
1462
+ order: params.order,
1463
+ wallet: params.wallet,
1464
+ sol_rpc: params.solRpc
1465
+ });
1466
+ }
1467
+ };
1468
+
1469
+ // src/react/SwapProvider.tsx
1470
+ var import_jsx_runtime = require("react/jsx-runtime");
1471
+ var SwapContext = (0, import_react.createContext)(null);
1472
+ var SwapProvider = ({ config, children }) => {
1473
+ const sdk = (0, import_react.useMemo)(() => new SwapSDK(config), [config.apiKey]);
1474
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SwapContext.Provider, { value: sdk, children });
1475
+ };
1476
+ var useSwap = () => {
1477
+ const ctx = (0, import_react.useContext)(SwapContext);
1478
+ if (!ctx) {
1479
+ throw new Error("useSwap must be used within <SwapProvider>");
1480
+ }
1481
+ return ctx;
1482
+ };
1483
+
1484
+ // src/react/useQuote.ts
1485
+ var import_react2 = require("react");
1486
+ var import_swr = __toESM(require("swr"), 1);
1487
+ var QUOTE_KEY = "quote-global";
1488
+ function isValidQuoteParams(p) {
1489
+ if (!p) return false;
1490
+ const { tokenIn, tokenOut, amount } = p;
1491
+ if (!tokenIn || !tokenOut || !amount) return false;
1492
+ const n = Number(amount);
1493
+ return Number.isFinite(n) && n > 0;
1494
+ }
1495
+ function useQuote(initialParams) {
1496
+ const sdk = useSwap();
1497
+ const abortRef = (0, import_react2.useRef)(null);
1498
+ const paramsRef = globalThis.__QUOTE_PARAMS_REF__ ?? (globalThis.__QUOTE_PARAMS_REF__ = { current: initialParams ?? null });
551
1499
  (0, import_react2.useEffect)(() => {
552
- return () => {
553
- isMounted.current = false;
554
- };
555
- }, []);
556
- const execute = (0, import_react2.useCallback)(
1500
+ if (initialParams && JSON.stringify(paramsRef.current) !== JSON.stringify(initialParams)) {
1501
+ paramsRef.current = initialParams;
1502
+ void (0, import_swr.mutate)(QUOTE_KEY);
1503
+ }
1504
+ }, [initialParams]);
1505
+ const fetcher = (0, import_react2.useCallback)(async () => {
1506
+ const activeParams = paramsRef.current;
1507
+ if (!isValidQuoteParams(activeParams)) return null;
1508
+ if (abortRef.current) abortRef.current.abort();
1509
+ const controller = new AbortController();
1510
+ abortRef.current = controller;
1511
+ try {
1512
+ return await sdk.getQuote(activeParams);
1513
+ } catch (err) {
1514
+ if (err instanceof DOMException && err.name === "AbortError") return null;
1515
+ if (err instanceof Error) throw err;
1516
+ throw new Error(String(err));
1517
+ }
1518
+ }, [sdk]);
1519
+ const {
1520
+ data,
1521
+ error,
1522
+ isValidating: loading,
1523
+ mutate: mutateQuote
1524
+ } = (0, import_swr.default)(QUOTE_KEY, fetcher, {
1525
+ revalidateOnFocus: false,
1526
+ shouldRetryOnError: false,
1527
+ keepPreviousData: true
1528
+ });
1529
+ const refetch = (0, import_react2.useCallback)(async () => {
1530
+ await mutateQuote();
1531
+ }, [mutateQuote]);
1532
+ const setParams = (0, import_react2.useCallback)(
1533
+ (next) => {
1534
+ paramsRef.current = next;
1535
+ void (0, import_swr.mutate)(QUOTE_KEY);
1536
+ },
1537
+ []
1538
+ );
1539
+ return (0, import_react2.useMemo)(
1540
+ () => ({
1541
+ data,
1542
+ loading,
1543
+ error: error instanceof Error ? error.message : null,
1544
+ refetch,
1545
+ setParams,
1546
+ get activeParams() {
1547
+ return paramsRef.current;
1548
+ }
1549
+ }),
1550
+ [data, loading, error, refetch, setParams]
1551
+ );
1552
+ }
1553
+
1554
+ // src/react/useExecuteTransaction.ts
1555
+ var import_react4 = require("react");
1556
+
1557
+ // src/react/useBalances.ts
1558
+ var import_react3 = require("react");
1559
+ var import_swr2 = __toESM(require("swr"), 1);
1560
+ var BALANCE_KEY = "balances-global";
1561
+ function useBalances(initialParams) {
1562
+ const sdk = useSwap();
1563
+ const abortRef = (0, import_react3.useRef)(null);
1564
+ const paramsRef = globalThis.__BALANCES_PARAMS_REF__ ?? (globalThis.__BALANCES_PARAMS_REF__ = { current: initialParams ?? null });
1565
+ const fetcher = (0, import_react3.useCallback)(async () => {
1566
+ const activeParams = paramsRef.current;
1567
+ if (!activeParams) return null;
1568
+ if (abortRef.current) abortRef.current.abort();
1569
+ const controller = new AbortController();
1570
+ abortRef.current = controller;
1571
+ try {
1572
+ return await sdk.getBalances(activeParams, { signal: controller.signal });
1573
+ } catch (err) {
1574
+ if (err instanceof DOMException && err.name === "AbortError") return null;
1575
+ if (err instanceof Error) throw err;
1576
+ throw new Error(String(err));
1577
+ }
1578
+ }, [sdk, paramsRef]);
1579
+ const {
1580
+ data,
1581
+ error,
1582
+ isValidating: loading,
1583
+ mutate: mutateBalances
1584
+ } = (0, import_swr2.default)(BALANCE_KEY, fetcher, {
1585
+ revalidateOnFocus: false,
1586
+ shouldRetryOnError: false,
1587
+ keepPreviousData: true
1588
+ });
1589
+ const refetch = (0, import_react3.useCallback)(async () => {
1590
+ await mutateBalances();
1591
+ }, [mutateBalances]);
1592
+ const setParams = (0, import_react3.useCallback)((next) => {
1593
+ paramsRef.current = next;
1594
+ void (0, import_swr2.mutate)(BALANCE_KEY);
1595
+ }, [paramsRef]);
1596
+ (0, import_react3.useEffect)(() => {
1597
+ if (initialParams) {
1598
+ const prevParams = paramsRef.current;
1599
+ const paramsChanged = !prevParams || prevParams.addresses.svm !== initialParams.addresses.svm || prevParams.addresses.evm !== initialParams.addresses.evm;
1600
+ if (paramsChanged) {
1601
+ paramsRef.current = initialParams;
1602
+ void (0, import_swr2.mutate)(BALANCE_KEY);
1603
+ }
1604
+ }
1605
+ }, [initialParams, paramsRef]);
1606
+ return (0, import_react3.useMemo)(
1607
+ () => ({
1608
+ data,
1609
+ loading,
1610
+ error: error instanceof Error ? error : null,
1611
+ refetch,
1612
+ setParams,
1613
+ get activeParams() {
1614
+ return paramsRef.current;
1615
+ }
1616
+ }),
1617
+ [data, loading, error, refetch, setParams, paramsRef]
1618
+ );
1619
+ }
1620
+
1621
+ // src/react/useExecuteTransaction.ts
1622
+ function useExecuteTransaction() {
1623
+ const sdk = useSwap();
1624
+ const [isLoading, setLoading] = (0, import_react4.useState)(false);
1625
+ const [stage, setStage] = (0, import_react4.useState)(null);
1626
+ const [message, setMessage] = (0, import_react4.useState)(null);
1627
+ const [error, setError] = (0, import_react4.useState)(null);
1628
+ const [result, setResult] = (0, import_react4.useState)(null);
1629
+ const { refetch: refetchQuote } = useQuote();
1630
+ const { refetch: refetchBalances } = useBalances();
1631
+ const execute = (0, import_react4.useCallback)(
557
1632
  async ({
558
1633
  quote,
559
1634
  accountAddress,
560
1635
  recipientAddress,
561
1636
  wallet,
562
- deadline
1637
+ orderType,
1638
+ options
563
1639
  }) => {
564
- if (!quote || !wallet) {
565
- throw new Error("Quote and wallet are required for order execution.");
566
- }
567
1640
  setLoading(true);
568
1641
  setError(null);
569
- setData(null);
570
- setMessage(null);
1642
+ setResult(null);
1643
+ const onStatus = (s, msg) => {
1644
+ setStage(s);
1645
+ setMessage(msg ?? "");
1646
+ };
571
1647
  try {
572
- const effectiveDeadline = deadline ?? Math.floor(Date.now() / 1e3) + 20 * 60;
573
- const onStatus = (stage, msg) => {
574
- if (!isMounted.current) return;
575
- setStatus(stage);
576
- if (msg) setMessage(msg);
577
- };
578
- const result = await executeOrder({
1648
+ const res = await sdk.executeTransaction({
579
1649
  quote,
1650
+ wallet,
580
1651
  accountAddress,
581
1652
  recipientAddress,
582
- wallet,
583
1653
  onStatus,
584
- options: { deadline: effectiveDeadline }
1654
+ orderType,
1655
+ options
585
1656
  });
586
- if (!isMounted.current) return result;
587
- setData(result);
588
- setStatus(result.stage);
589
- setMessage("Order executed successfully");
590
- return result;
591
- } catch (err) {
592
- const errorObj = err instanceof Error ? err : new Error(String(err));
593
- if (isMounted.current) {
594
- setError(errorObj);
595
- setStatus("error");
596
- setMessage(errorObj.message);
597
- setData({
598
- status: false,
599
- stage: "error",
600
- message: errorObj.message
1657
+ if (res && typeof res === "object" && "status" in res && "stage" in res && typeof res.stage === "string") {
1658
+ setResult({
1659
+ ...res,
1660
+ stage: res.stage
601
1661
  });
1662
+ } else {
1663
+ setResult(res);
602
1664
  }
603
- return {
604
- status: false,
605
- stage: "error",
606
- message: errorObj.message
607
- };
1665
+ return res;
1666
+ } catch (err) {
1667
+ const msg = err instanceof Error ? err.message : String(err);
1668
+ setError(msg);
1669
+ throw err;
608
1670
  } finally {
609
- if (isMounted.current) setLoading(false);
1671
+ await Promise.allSettled([refetchQuote(), refetchBalances()]);
1672
+ setLoading(false);
610
1673
  }
611
1674
  },
612
- []
1675
+ [sdk, refetchQuote, refetchBalances]
613
1676
  );
614
1677
  return {
615
- /** Executes the swap order. */
616
1678
  execute,
617
- /** Current execution stage. */
618
- status,
619
- /** Human-readable status message. */
1679
+ isLoading,
1680
+ stage,
620
1681
  message,
621
- /** Whether execution is ongoing. */
622
- loading,
623
- /** Raw SDK response data. */
624
- data,
625
- /** Captured error (if execution failed). */
626
- error
1682
+ error,
1683
+ result
627
1684
  };
628
1685
  }
629
1686
 
630
- // src/react/useQuote.ts
631
- var import_react3 = require("react");
632
-
633
- // src/core/getQuote.ts
634
- var import_intents_sdk8 = require("@shogun-sdk/intents-sdk");
635
- var import_viem4 = require("viem");
636
- async function getQuote(params) {
637
- if (!params.tokenIn?.address || !params.tokenOut?.address) {
638
- throw new Error("Both tokenIn and tokenOut must include an address.");
639
- }
640
- if (!params.sourceChainId || !params.destChainId) {
641
- throw new Error("Both sourceChainId and destChainId are required.");
642
- }
643
- if (params.amount <= 0n) {
644
- throw new Error("Amount must be greater than 0.");
645
- }
646
- const normalizedTokenIn = normalizeNative(params.sourceChainId, params.tokenIn.address);
647
- const data = await import_intents_sdk8.QuoteProvider.getQuote({
648
- sourceChainId: params.sourceChainId,
649
- destChainId: params.destChainId,
650
- tokenIn: normalizedTokenIn,
651
- tokenOut: params.tokenOut.address,
652
- amount: params.amount
653
- });
654
- const inputSlippage = params.slippage ?? 5;
655
- const slippageDecimal = inputSlippage / 100;
656
- const slippage = Math.min(Math.max(slippageDecimal, 0), 0.5);
657
- let warning;
658
- if (slippage > 0.1) {
659
- warning = `\u26A0\uFE0F High slippage tolerance (${(slippage * 100).toFixed(2)}%) \u2014 price may vary significantly.`;
660
- }
661
- const estimatedAmountOut = BigInt(data.estimatedAmountOutReduced);
662
- const slippageBps = BigInt(Math.round(slippage * 1e4));
663
- const estimatedAmountOutAfterSlippage = estimatedAmountOut * (10000n - slippageBps) / 10000n;
664
- const pricePerInputToken = estimatedAmountOut * 10n ** BigInt(params.tokenIn.decimals ?? 18) / BigInt(params.amount);
665
- return {
666
- amountOut: estimatedAmountOut,
667
- amountOutUsd: data.estimatedAmountOutUsd,
668
- amountInUsd: data.amountInUsd,
669
- minStablecoinsAmount: data.estimatedAmountInAsMinStablecoinAmount,
670
- tokenIn: {
671
- address: params.tokenIn.address,
672
- decimals: params.tokenIn.decimals ?? 18,
673
- chainId: params.sourceChainId
674
- },
675
- tokenOut: {
676
- address: params.tokenOut.address,
677
- decimals: params.tokenOut.decimals ?? 18,
678
- chainId: params.destChainId
679
- },
680
- amountIn: params.amount,
681
- pricePerInputToken,
682
- slippage,
683
- internal: {
684
- ...data,
685
- estimatedAmountOutReduced: estimatedAmountOutAfterSlippage
686
- },
687
- warning
688
- };
689
- }
690
-
691
- // src/react/useQuote.ts
692
- function useQuote(params, options) {
693
- const [data, setData] = (0, import_react3.useState)(null);
694
- const [loading, setLoading] = (0, import_react3.useState)(false);
695
- const [error, setError] = (0, import_react3.useState)(null);
696
- const [warning, setWarning] = (0, import_react3.useState)(null);
697
- const debounceMs = options?.debounceMs ?? 250;
698
- const autoRefreshMs = options?.autoRefreshMs;
699
- const abortRef = (0, import_react3.useRef)(null);
700
- const debounceRef = (0, import_react3.useRef)(null);
701
- const mounted = (0, import_react3.useRef)(false);
702
- (0, import_react3.useEffect)(() => {
703
- mounted.current = true;
704
- return () => {
705
- mounted.current = false;
706
- abortRef.current?.abort();
707
- if (debounceRef.current) clearTimeout(debounceRef.current);
708
- };
709
- }, []);
710
- const fetchQuote = (0, import_react3.useCallback)(
711
- async () => {
712
- if (!params) return;
1687
+ // src/react/useTokenList.ts
1688
+ var import_react5 = require("react");
1689
+ function useTokenList() {
1690
+ const [tokens, setTokens] = (0, import_react5.useState)([]);
1691
+ const [loading, setLoading] = (0, import_react5.useState)(false);
1692
+ const [error, setError] = (0, import_react5.useState)(null);
1693
+ const [hasMore, setHasMore] = (0, import_react5.useState)(true);
1694
+ const [page, setPage] = (0, import_react5.useState)(1);
1695
+ const sdk = useSwap();
1696
+ const pageRef = (0, import_react5.useRef)(1);
1697
+ const hasMoreRef = (0, import_react5.useRef)(true);
1698
+ const lastQuery = (0, import_react5.useRef)({});
1699
+ const cache = (0, import_react5.useRef)(/* @__PURE__ */ new Map());
1700
+ const isLoadingRef = (0, import_react5.useRef)(false);
1701
+ pageRef.current = page;
1702
+ hasMoreRef.current = hasMore;
1703
+ const loadTokens = (0, import_react5.useCallback)(
1704
+ async (params) => {
1705
+ const { q, networkId, reset } = params;
1706
+ if (isLoadingRef.current && !reset) return;
713
1707
  try {
1708
+ let currentPage = pageRef.current;
1709
+ if (reset) {
1710
+ currentPage = 1;
1711
+ setTokens([]);
1712
+ setPage(1);
1713
+ setHasMore(true);
1714
+ pageRef.current = 1;
1715
+ hasMoreRef.current = true;
1716
+ }
1717
+ if (!reset && !hasMoreRef.current) return;
1718
+ isLoadingRef.current = true;
714
1719
  setLoading(true);
715
- setWarning(null);
716
- const result = await getQuote(params);
717
- if (!mounted.current) return;
718
- setData((prev) => {
719
- if (JSON.stringify(prev) === JSON.stringify(result)) return prev;
720
- return result;
721
- });
722
- setWarning(result.warning ?? null);
723
1720
  setError(null);
724
- } catch (err) {
725
- if (err.name === "AbortError") return;
726
- console.error("[useQuote] fetch error:", err);
727
- if (mounted.current) setError(err instanceof Error ? err : new Error(String(err)));
1721
+ const cacheKey = JSON.stringify({
1722
+ q: q?.toLowerCase() ?? "",
1723
+ networkId: networkId ?? void 0,
1724
+ page: currentPage
1725
+ });
1726
+ if (cache.current.has(cacheKey)) {
1727
+ const cached = cache.current.get(cacheKey);
1728
+ setTokens((prev) => reset ? cached.results : [...prev, ...cached.results]);
1729
+ if (!reset && cached.results.length > 0) {
1730
+ const nextPage = currentPage + 1;
1731
+ setPage(nextPage);
1732
+ pageRef.current = nextPage;
1733
+ }
1734
+ isLoadingRef.current = false;
1735
+ setLoading(false);
1736
+ return;
1737
+ }
1738
+ const apiParams = {
1739
+ q,
1740
+ page: currentPage,
1741
+ limit: 20,
1742
+ ...networkId !== void 0 ? { networkId } : {}
1743
+ };
1744
+ const res = await sdk.getTokenList(apiParams);
1745
+ cache.current.set(cacheKey, res);
1746
+ setTokens((prev) => reset ? res.results : [...prev, ...res.results]);
1747
+ const isLastPage = res.results.length === 0 || res.results.length < 20 || res.count && currentPage * 20 >= res.count;
1748
+ setHasMore(!isLastPage);
1749
+ hasMoreRef.current = !isLastPage;
1750
+ if (!reset && !isLastPage) {
1751
+ const nextPage = currentPage + 1;
1752
+ setPage(nextPage);
1753
+ pageRef.current = nextPage;
1754
+ }
1755
+ lastQuery.current = { q, networkId };
1756
+ } catch (e) {
1757
+ console.error("useTokenList error:", e);
1758
+ setError("Failed to load tokens");
728
1759
  } finally {
729
- if (mounted.current) setLoading(false);
1760
+ setLoading(false);
1761
+ isLoadingRef.current = false;
730
1762
  }
731
1763
  },
732
- [params]
733
- );
734
- (0, import_react3.useEffect)(() => {
735
- if (!params) return;
736
- if (debounceRef.current) clearTimeout(debounceRef.current);
737
- debounceRef.current = setTimeout(() => {
738
- fetchQuote();
739
- }, debounceMs);
740
- return () => {
741
- if (debounceRef.current) clearTimeout(debounceRef.current);
742
- abortRef.current?.abort();
743
- };
744
- }, [params, debounceMs, fetchQuote]);
745
- (0, import_react3.useEffect)(() => {
746
- if (!autoRefreshMs || !params) return;
747
- const interval = setInterval(() => fetchQuote(), autoRefreshMs);
748
- return () => clearInterval(interval);
749
- }, [autoRefreshMs, params, fetchQuote]);
750
- return (0, import_react3.useMemo)(
751
- () => ({
752
- data,
753
- loading,
754
- error,
755
- warning,
756
- refetch: () => fetchQuote()
757
- }),
758
- [data, loading, error, warning, fetchQuote]
1764
+ [sdk]
759
1765
  );
760
- }
761
-
762
- // src/react/useBalances.ts
763
- var import_react4 = require("react");
764
-
765
- // src/core/getBalances.ts
766
- var import_intents_sdk9 = require("@shogun-sdk/intents-sdk");
767
- async function getBalances(params, options) {
768
- const { addresses, cursorEvm, cursorSvm } = params;
769
- const { signal } = options ?? {};
770
- if (!addresses?.evm && !addresses?.svm) {
771
- throw new Error("At least one address (EVM or SVM) must be provided.");
772
- }
773
- const payload = JSON.stringify({
774
- addresses,
775
- cursorEvm,
776
- cursorSvm
777
- });
778
- const start = performance.now();
779
- const response = await fetch(`${import_intents_sdk9.TOKEN_SEARCH_API_BASE_URL}/tokens/balances`, {
780
- method: "POST",
781
- headers: {
782
- accept: "application/json",
783
- "Content-Type": "application/json"
784
- },
785
- body: payload,
786
- signal
787
- }).catch((err) => {
788
- if (err.name === "AbortError") {
789
- throw new Error("Balance request was cancelled.");
790
- }
791
- throw err;
792
- });
793
- if (!response.ok) {
794
- const text = await response.text().catch(() => "");
795
- throw new Error(`Failed to fetch balances: ${response.status} ${text}`);
796
- }
797
- const data = await response.json().catch(() => {
798
- throw new Error("Invalid JSON response from balances API.");
799
- });
800
- const duration = (performance.now() - start).toFixed(1);
801
- if (process.env.NODE_ENV !== "production") {
802
- console.debug(`[Shogun SDK] Fetched balances in ${duration}ms`);
803
- }
804
- const evmItems = data.evm?.items ?? [];
805
- const svmItems = data.svm?.items ?? [];
806
- const combined = [...evmItems, ...svmItems];
1766
+ const resetTokens = (0, import_react5.useCallback)(() => {
1767
+ setTokens([]);
1768
+ setPage(1);
1769
+ setHasMore(true);
1770
+ pageRef.current = 1;
1771
+ hasMoreRef.current = true;
1772
+ cache.current.clear();
1773
+ lastQuery.current = {};
1774
+ }, []);
807
1775
  return {
808
- results: combined,
809
- nextCursorEvm: data.evm?.cursor ?? null,
810
- nextCursorSvm: data.svm?.cursor ?? null
1776
+ tokens,
1777
+ loading,
1778
+ error,
1779
+ hasMore,
1780
+ page,
1781
+ lastQuery: lastQuery.current,
1782
+ loadTokens,
1783
+ resetTokens
811
1784
  };
812
1785
  }
813
1786
 
814
- // src/react/useBalances.ts
815
- function useBalances(params) {
816
- const [data, setData] = (0, import_react4.useState)(null);
817
- const [loading, setLoading] = (0, import_react4.useState)(false);
818
- const [error, setError] = (0, import_react4.useState)(null);
819
- const abortRef = (0, import_react4.useRef)(null);
820
- const stableParams = (0, import_react4.useMemo)(() => {
821
- if (!params) return null;
822
- const { addresses, cursorEvm, cursorSvm } = params;
823
- return {
824
- addresses: {
825
- evm: addresses?.evm ?? void 0,
826
- svm: addresses?.svm ?? void 0
827
- },
828
- cursorEvm,
829
- cursorSvm
1787
+ // src/react/useTokensData.ts
1788
+ var import_react6 = require("react");
1789
+ function useTokensData(addresses) {
1790
+ const sdk = useSwap();
1791
+ const [data, setData] = (0, import_react6.useState)([]);
1792
+ const [loading, setLoading] = (0, import_react6.useState)(false);
1793
+ const [error, setError] = (0, import_react6.useState)(null);
1794
+ (0, import_react6.useEffect)(() => {
1795
+ if (!addresses?.length) return;
1796
+ let isMounted = true;
1797
+ setLoading(true);
1798
+ setError(null);
1799
+ sdk.getTokensData(addresses).then((res) => {
1800
+ if (isMounted) setData(res);
1801
+ }).catch((e) => {
1802
+ if (isMounted) setError(e instanceof Error ? e : new Error(String(e)));
1803
+ }).finally(() => {
1804
+ if (isMounted) setLoading(false);
1805
+ });
1806
+ return () => {
1807
+ isMounted = false;
830
1808
  };
831
- }, [params?.addresses?.evm, params?.addresses?.svm, params?.cursorEvm, params?.cursorSvm]);
832
- const fetchBalances = (0, import_react4.useCallback)(async () => {
833
- if (!stableParams) return;
1809
+ }, [sdk, JSON.stringify(addresses)]);
1810
+ return { data, loading, error };
1811
+ }
1812
+
1813
+ // src/react/useOrders.ts
1814
+ var import_react7 = require("react");
1815
+ var import_swr3 = __toESM(require("swr"), 1);
1816
+ var ORDERS_KEY = "orders-global";
1817
+ function hasValidAddress(addrs) {
1818
+ if (!addrs) return false;
1819
+ return Boolean(addrs.evmAddress || addrs.solAddress || addrs.suiAddress);
1820
+ }
1821
+ function useOrders(initialAddresses) {
1822
+ const sdk = useSwap();
1823
+ const abortRef = (0, import_react7.useRef)(null);
1824
+ const addrRef = globalThis.__ORDERS_ADDR_REF__ ?? (globalThis.__ORDERS_ADDR_REF__ = { current: initialAddresses ?? null });
1825
+ (0, import_react7.useEffect)(() => {
1826
+ if (initialAddresses && JSON.stringify(addrRef.current) !== JSON.stringify(initialAddresses)) {
1827
+ addrRef.current = initialAddresses;
1828
+ void (0, import_swr3.mutate)(ORDERS_KEY);
1829
+ }
1830
+ }, [initialAddresses]);
1831
+ const fetcher = (0, import_react7.useCallback)(async () => {
1832
+ const active = addrRef.current;
1833
+ if (!hasValidAddress(active)) return null;
834
1834
  if (abortRef.current) abortRef.current.abort();
835
1835
  const controller = new AbortController();
836
1836
  abortRef.current = controller;
837
- setLoading(true);
838
- setError(null);
839
1837
  try {
840
- const result = await getBalances(stableParams, { signal: controller.signal });
841
- setData((prev) => {
842
- if (JSON.stringify(prev) === JSON.stringify(result)) return prev;
843
- return result;
844
- });
845
- return result;
1838
+ return await sdk.getOrders(active ?? {});
846
1839
  } catch (err) {
847
- if (err.name === "AbortError") return;
848
- const e = err instanceof Error ? err : new Error(String(err));
849
- setError(e);
850
- throw e;
851
- } finally {
852
- setLoading(false);
1840
+ if (err instanceof DOMException && err.name === "AbortError") return null;
1841
+ if (err instanceof Error) throw err;
1842
+ throw new Error(String(err));
853
1843
  }
854
- }, [stableParams]);
855
- (0, import_react4.useEffect)(() => {
856
- if (stableParams) fetchBalances().catch(() => {
857
- });
858
- return () => {
859
- if (abortRef.current) abortRef.current.abort();
860
- };
861
- }, [fetchBalances]);
862
- return (0, import_react4.useMemo)(
1844
+ }, [sdk]);
1845
+ const {
1846
+ data,
1847
+ error,
1848
+ isValidating: loading,
1849
+ mutate: mutateOrders
1850
+ } = (0, import_swr3.default)(ORDERS_KEY, fetcher, {
1851
+ revalidateOnFocus: false,
1852
+ revalidateOnReconnect: false,
1853
+ shouldRetryOnError: false,
1854
+ keepPreviousData: true
1855
+ });
1856
+ const refetch = (0, import_react7.useCallback)(async () => {
1857
+ await mutateOrders();
1858
+ }, [mutateOrders]);
1859
+ const setAddresses = (0, import_react7.useCallback)(
1860
+ (next) => {
1861
+ if (!next) return;
1862
+ addrRef.current = next;
1863
+ void (0, import_swr3.mutate)(ORDERS_KEY);
1864
+ },
1865
+ []
1866
+ );
1867
+ return (0, import_react7.useMemo)(
863
1868
  () => ({
864
- /** Latest fetched balance data */
865
1869
  data,
866
- /** Whether the hook is currently fetching */
867
1870
  loading,
868
- /** Error object if fetching failed */
869
- error,
870
- /** Manually trigger a refresh */
871
- refetch: fetchBalances
1871
+ error: error instanceof Error ? error.message : null,
1872
+ refetch,
1873
+ setAddresses,
1874
+ get activeAddresses() {
1875
+ return addrRef.current;
1876
+ }
872
1877
  }),
873
- [data, loading, error, fetchBalances]
1878
+ [data, loading, error, refetch, setAddresses]
874
1879
  );
875
1880
  }
876
1881
 
877
- // src/react/index.ts
878
- var import_intents_sdk10 = require("@shogun-sdk/intents-sdk");
879
- // Annotate the CommonJS export names for ESM import in node:
880
- 0 && (module.exports = {
881
- ChainID,
882
- isEvmChain,
883
- useBalances,
884
- useExecuteOrder,
885
- useQuote,
886
- useTokenList
887
- });
1882
+ // src/react/useCancelOrder.ts
1883
+ var import_react8 = require("react");
1884
+ var import_swr4 = require("swr");
1885
+ function useCancelOrder() {
1886
+ const sdk = useSwap();
1887
+ const [isLoading, setLoading] = (0, import_react8.useState)(false);
1888
+ const [error, setError] = (0, import_react8.useState)(null);
1889
+ const [result, setResult] = (0, import_react8.useState)(null);
1890
+ const cancel = (0, import_react8.useCallback)(
1891
+ async (params) => {
1892
+ setLoading(true);
1893
+ setError(null);
1894
+ setResult(null);
1895
+ try {
1896
+ const tx = await sdk.cancelOrder({
1897
+ order: params.order,
1898
+ wallet: params.wallet,
1899
+ solRpc: params.solRpc
1900
+ });
1901
+ setResult(tx);
1902
+ void (0, import_swr4.mutate)("orders-global");
1903
+ return tx;
1904
+ } catch (err) {
1905
+ const msg = err instanceof Error ? err.message : String(err);
1906
+ setError(msg);
1907
+ throw err;
1908
+ } finally {
1909
+ setLoading(false);
1910
+ }
1911
+ },
1912
+ [sdk]
1913
+ );
1914
+ return {
1915
+ cancel,
1916
+ isLoading,
1917
+ error,
1918
+ result
1919
+ };
1920
+ }