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