@sodax/dapp-kit 0.0.1-rc.9 → 1.0.0-rc.1

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.
Files changed (126) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +142 -44
  3. package/dist/index.d.mts +1514 -0
  4. package/dist/index.d.ts +1514 -4
  5. package/dist/index.js +997 -82
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +957 -84
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +7 -8
  10. package/src/contexts/index.ts +3 -2
  11. package/src/hooks/backend/README.md +135 -0
  12. package/src/hooks/backend/index.ts +23 -0
  13. package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +49 -0
  14. package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +61 -0
  15. package/src/hooks/backend/useBackendIntentByHash.ts +53 -0
  16. package/src/hooks/backend/useBackendIntentByTxHash.ts +52 -0
  17. package/src/hooks/backend/useBackendMoneyMarketAsset.ts +57 -0
  18. package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +67 -0
  19. package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +67 -0
  20. package/src/hooks/backend/useBackendMoneyMarketPosition.ts +56 -0
  21. package/src/hooks/backend/useBackendOrderbook.ts +63 -0
  22. package/src/hooks/bridge/index.ts +5 -0
  23. package/src/hooks/bridge/useBridge.ts +57 -0
  24. package/src/hooks/bridge/useBridgeAllowance.ts +49 -0
  25. package/src/hooks/bridge/useBridgeApprove.ts +68 -0
  26. package/src/hooks/bridge/useGetBridgeableAmount.ts +50 -0
  27. package/src/hooks/bridge/useGetBridgeableTokens.ts +62 -0
  28. package/src/hooks/index.ts +4 -0
  29. package/src/hooks/migrate/index.ts +4 -0
  30. package/src/hooks/migrate/types.ts +15 -0
  31. package/src/hooks/migrate/useMigrate.tsx +110 -0
  32. package/src/hooks/migrate/useMigrationAllowance.tsx +79 -0
  33. package/src/hooks/migrate/useMigrationApprove.tsx +129 -0
  34. package/src/hooks/mm/useMMAllowance.ts +2 -1
  35. package/src/hooks/mm/useMMApprove.ts +2 -1
  36. package/src/hooks/mm/useReservesData.ts +1 -8
  37. package/src/hooks/mm/useReservesHumanized.ts +30 -0
  38. package/src/hooks/mm/useReservesList.ts +29 -0
  39. package/src/hooks/mm/useReservesUsdFormat.ts +38 -0
  40. package/src/hooks/mm/useUserFormattedSummary.ts +54 -0
  41. package/src/hooks/mm/useUserReservesData.ts +30 -31
  42. package/src/hooks/provider/useHubProvider.ts +3 -3
  43. package/src/hooks/provider/useSpokeProvider.ts +45 -21
  44. package/src/hooks/shared/index.ts +4 -0
  45. package/src/hooks/shared/useDeriveUserWalletAddress.ts +44 -0
  46. package/src/hooks/shared/useEstimateGas.ts +18 -0
  47. package/src/hooks/shared/useRequestTrustline.ts +103 -0
  48. package/src/hooks/shared/useStellarTrustlineCheck.ts +71 -0
  49. package/src/hooks/staking/index.ts +19 -0
  50. package/src/hooks/staking/useCancelUnstake.ts +52 -0
  51. package/src/hooks/staking/useClaim.ts +46 -0
  52. package/src/hooks/staking/useConvertedAssets.ts +47 -0
  53. package/src/hooks/staking/useInstantUnstake.ts +50 -0
  54. package/src/hooks/staking/useInstantUnstakeAllowance.ts +59 -0
  55. package/src/hooks/staking/useInstantUnstakeApprove.ts +52 -0
  56. package/src/hooks/staking/useInstantUnstakeRatio.ts +54 -0
  57. package/src/hooks/staking/useStake.ts +47 -0
  58. package/src/hooks/staking/useStakeAllowance.ts +57 -0
  59. package/src/hooks/staking/useStakeApprove.ts +50 -0
  60. package/src/hooks/staking/useStakeRatio.ts +53 -0
  61. package/src/hooks/staking/useStakingConfig.ts +40 -0
  62. package/src/hooks/staking/useStakingInfo.ts +50 -0
  63. package/src/hooks/staking/useUnstake.ts +54 -0
  64. package/src/hooks/staking/useUnstakeAllowance.ts +58 -0
  65. package/src/hooks/staking/useUnstakeApprove.ts +52 -0
  66. package/src/hooks/staking/useUnstakingInfo.ts +53 -0
  67. package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +59 -0
  68. package/src/hooks/swap/index.ts +2 -1
  69. package/src/hooks/swap/useCancelSwap.ts +44 -0
  70. package/src/hooks/swap/useQuote.ts +16 -2
  71. package/src/hooks/swap/useStatus.ts +1 -1
  72. package/src/hooks/swap/{useCreateIntentOrder.ts → useSwap.ts} +16 -11
  73. package/src/hooks/swap/useSwapAllowance.ts +5 -1
  74. package/src/hooks/swap/useSwapApprove.ts +14 -14
  75. package/src/providers/SodaxProvider.tsx +8 -20
  76. package/dist/contexts/index.d.ts +0 -8
  77. package/dist/contexts/index.d.ts.map +0 -1
  78. package/dist/core/index.d.ts +0 -3
  79. package/dist/core/index.d.ts.map +0 -1
  80. package/dist/hooks/index.d.ts +0 -5
  81. package/dist/hooks/index.d.ts.map +0 -1
  82. package/dist/hooks/mm/index.d.ts +0 -9
  83. package/dist/hooks/mm/index.d.ts.map +0 -1
  84. package/dist/hooks/mm/useBorrow.d.ts +0 -35
  85. package/dist/hooks/mm/useBorrow.d.ts.map +0 -1
  86. package/dist/hooks/mm/useMMAllowance.d.ts +0 -26
  87. package/dist/hooks/mm/useMMAllowance.d.ts.map +0 -1
  88. package/dist/hooks/mm/useMMApprove.d.ts +0 -27
  89. package/dist/hooks/mm/useMMApprove.d.ts.map +0 -1
  90. package/dist/hooks/mm/useRepay.d.ts +0 -35
  91. package/dist/hooks/mm/useRepay.d.ts.map +0 -1
  92. package/dist/hooks/mm/useReservesData.d.ts +0 -19
  93. package/dist/hooks/mm/useReservesData.d.ts.map +0 -1
  94. package/dist/hooks/mm/useSupply.d.ts +0 -34
  95. package/dist/hooks/mm/useSupply.d.ts.map +0 -1
  96. package/dist/hooks/mm/useUserReservesData.d.ts +0 -3
  97. package/dist/hooks/mm/useUserReservesData.d.ts.map +0 -1
  98. package/dist/hooks/mm/useWithdraw.d.ts +0 -33
  99. package/dist/hooks/mm/useWithdraw.d.ts.map +0 -1
  100. package/dist/hooks/provider/index.d.ts +0 -3
  101. package/dist/hooks/provider/index.d.ts.map +0 -1
  102. package/dist/hooks/provider/useHubProvider.d.ts +0 -3
  103. package/dist/hooks/provider/useHubProvider.d.ts.map +0 -1
  104. package/dist/hooks/provider/useSpokeProvider.d.ts +0 -18
  105. package/dist/hooks/provider/useSpokeProvider.d.ts.map +0 -1
  106. package/dist/hooks/shared/index.d.ts +0 -2
  107. package/dist/hooks/shared/index.d.ts.map +0 -1
  108. package/dist/hooks/shared/useSodaxContext.d.ts +0 -8
  109. package/dist/hooks/shared/useSodaxContext.d.ts.map +0 -1
  110. package/dist/hooks/swap/index.d.ts +0 -6
  111. package/dist/hooks/swap/index.d.ts.map +0 -1
  112. package/dist/hooks/swap/useCreateIntentOrder.d.ts +0 -33
  113. package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +0 -1
  114. package/dist/hooks/swap/useQuote.d.ts +0 -39
  115. package/dist/hooks/swap/useQuote.d.ts.map +0 -1
  116. package/dist/hooks/swap/useStatus.d.ts +0 -31
  117. package/dist/hooks/swap/useStatus.d.ts.map +0 -1
  118. package/dist/hooks/swap/useSwapAllowance.d.ts +0 -23
  119. package/dist/hooks/swap/useSwapAllowance.d.ts.map +0 -1
  120. package/dist/hooks/swap/useSwapApprove.d.ts +0 -26
  121. package/dist/hooks/swap/useSwapApprove.d.ts.map +0 -1
  122. package/dist/index.d.ts.map +0 -1
  123. package/dist/providers/SodaxProvider.d.ts +0 -10
  124. package/dist/providers/SodaxProvider.d.ts.map +0 -1
  125. package/dist/providers/index.d.ts +0 -2
  126. package/dist/providers/index.d.ts.map +0 -1
package/dist/index.js CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  var React = require('react');
4
4
  var sdk = require('@sodax/sdk');
5
- var walletSdk = require('@sodax/wallet-sdk');
6
5
  var reactQuery = require('@tanstack/react-query');
7
6
  var viem = require('viem');
7
+ var types = require('@sodax/types');
8
8
 
9
9
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
10
 
@@ -19,58 +19,165 @@ var useSodaxContext = () => {
19
19
  }
20
20
  return context;
21
21
  };
22
+ function useEstimateGas(spokeProvider) {
23
+ return reactQuery.useMutation({
24
+ mutationFn: async (rawTx) => {
25
+ if (!spokeProvider) {
26
+ throw new Error("spokeProvider is not found");
27
+ }
28
+ const response = await sdk.SpokeService.estimateGas(rawTx, spokeProvider);
29
+ return response;
30
+ }
31
+ });
32
+ }
33
+ function useDeriveUserWalletAddress(spokeProvider, walletAddress) {
34
+ const { sodax } = useSodaxContext();
35
+ return reactQuery.useQuery({
36
+ queryKey: ["deriveUserWalletAddress", spokeProvider?.chainConfig.chain.id, walletAddress],
37
+ queryFn: async () => {
38
+ if (!spokeProvider) {
39
+ throw new Error("Spoke provider is required");
40
+ }
41
+ return await sdk.deriveUserWalletAddress(spokeProvider, sodax.hubProvider, walletAddress);
42
+ },
43
+ enabled: !!spokeProvider,
44
+ refetchInterval: false
45
+ // This is a deterministic operation, no need to refetch
46
+ });
47
+ }
48
+ function useStellarTrustlineCheck(token, amount, spokeProvider, chainId) {
49
+ return reactQuery.useQuery({
50
+ queryKey: ["stellar-trustline-check", token],
51
+ queryFn: async () => {
52
+ if (chainId !== sdk.STELLAR_MAINNET_CHAIN_ID) {
53
+ return true;
54
+ }
55
+ if (!spokeProvider || !token || !amount || !(spokeProvider instanceof sdk.StellarSpokeProvider)) {
56
+ console.error(
57
+ "Spoke provider, token or amount not found. Details: spokeProvider:",
58
+ spokeProvider,
59
+ "token:",
60
+ token,
61
+ "amount:",
62
+ amount
63
+ );
64
+ return false;
65
+ }
66
+ const response = await sdk.StellarSpokeService.hasSufficientTrustline(token, amount, spokeProvider);
67
+ return response;
68
+ },
69
+ enabled: !!spokeProvider && !!token && !!amount
70
+ });
71
+ }
72
+ function useRequestTrustline(token) {
73
+ const queryClient = reactQuery.useQueryClient();
74
+ const [isLoading, setIsLoading] = React.useState(false);
75
+ const [isRequested, setIsRequested] = React.useState(false);
76
+ const [error, setError] = React.useState(null);
77
+ const [data, setData] = React.useState(null);
78
+ const requestTrustline = React.useCallback(
79
+ async ({
80
+ token: token2,
81
+ amount,
82
+ spokeProvider
83
+ }) => {
84
+ if (!spokeProvider || !token2 || !amount || !(spokeProvider instanceof sdk.StellarSpokeProvider)) {
85
+ const error2 = new Error("Spoke provider, token or amount not found");
86
+ setError(error2);
87
+ throw error2;
88
+ }
89
+ setIsLoading(true);
90
+ setError(null);
91
+ try {
92
+ const result = await sdk.StellarSpokeService.requestTrustline(token2, amount, spokeProvider);
93
+ setData(result);
94
+ setIsRequested(true);
95
+ queryClient.invalidateQueries({ queryKey: ["stellar-trustline-check", token2] });
96
+ return result;
97
+ } catch (err) {
98
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
99
+ setError(error2);
100
+ throw error2;
101
+ } finally {
102
+ setIsLoading(false);
103
+ }
104
+ },
105
+ [queryClient]
106
+ );
107
+ return {
108
+ requestTrustline,
109
+ isLoading,
110
+ isRequested,
111
+ error,
112
+ data
113
+ };
114
+ }
22
115
 
23
116
  // src/hooks/provider/useHubProvider.ts
24
117
  function useHubProvider() {
25
- const { hubProvider } = useSodaxContext();
26
- return hubProvider;
118
+ const { sodax } = useSodaxContext();
119
+ return sodax.hubProvider;
27
120
  }
28
121
  function useSpokeProvider(spokeChainId, walletProvider) {
29
- const xChainType = walletSdk.getXChainType(spokeChainId);
30
- const walletProvider_ = walletSdk.useWalletProvider(spokeChainId);
31
- const _walletProvider = walletProvider ?? walletProvider_;
122
+ const { rpcConfig } = useSodaxContext();
123
+ const xChainType = spokeChainId ? sdk.spokeChainConfig[spokeChainId]?.chain.type : void 0;
32
124
  const spokeProvider = React.useMemo(() => {
33
- if (!_walletProvider) return void 0;
125
+ if (!walletProvider) return void 0;
34
126
  if (!spokeChainId) return void 0;
127
+ if (!xChainType) return void 0;
128
+ if (!rpcConfig) return void 0;
35
129
  if (xChainType === "EVM") {
130
+ if (spokeChainId === sdk.SONIC_MAINNET_CHAIN_ID) {
131
+ return new sdk.SonicSpokeProvider(
132
+ walletProvider,
133
+ sdk.spokeChainConfig[spokeChainId]
134
+ );
135
+ }
36
136
  return new sdk.EvmSpokeProvider(
37
- _walletProvider,
137
+ walletProvider,
38
138
  sdk.spokeChainConfig[spokeChainId]
39
139
  );
40
140
  }
41
141
  if (xChainType === "SUI") {
42
142
  return new sdk.SuiSpokeProvider(
43
143
  sdk.spokeChainConfig[spokeChainId],
44
- _walletProvider
144
+ walletProvider
45
145
  );
46
146
  }
47
147
  if (xChainType === "ICON") {
48
148
  return new sdk.IconSpokeProvider(
49
- _walletProvider,
149
+ walletProvider,
50
150
  sdk.spokeChainConfig[spokeChainId]
51
151
  );
52
152
  }
53
153
  if (xChainType === "INJECTIVE") {
54
- return new sdk.CWSpokeProvider(
154
+ return new sdk.InjectiveSpokeProvider(
55
155
  sdk.spokeChainConfig[spokeChainId],
56
- _walletProvider
156
+ walletProvider
57
157
  );
58
158
  }
59
159
  if (xChainType === "STELLAR") {
60
160
  const stellarConfig = sdk.spokeChainConfig[spokeChainId];
61
- return new sdk.StellarSpokeProvider(_walletProvider, stellarConfig, {
62
- horizonRpcUrl: stellarConfig.horizonRpcUrl,
63
- sorobanRpcUrl: stellarConfig.sorobanRpcUrl
64
- });
161
+ return new sdk.StellarSpokeProvider(
162
+ walletProvider,
163
+ stellarConfig,
164
+ rpcConfig.stellar ? rpcConfig.stellar : {
165
+ horizonRpcUrl: stellarConfig.horizonRpcUrl,
166
+ sorobanRpcUrl: stellarConfig.sorobanRpcUrl
167
+ }
168
+ );
65
169
  }
66
170
  if (xChainType === "SOLANA") {
67
171
  return new sdk.SolanaSpokeProvider(
68
- _walletProvider,
69
- sdk.spokeChainConfig[spokeChainId]
172
+ walletProvider,
173
+ rpcConfig.solana ? {
174
+ ...sdk.spokeChainConfig[spokeChainId],
175
+ rpcUrl: rpcConfig.solana
176
+ } : sdk.spokeChainConfig[spokeChainId]
70
177
  );
71
178
  }
72
179
  return void 0;
73
- }, [spokeChainId, xChainType, _walletProvider]);
180
+ }, [spokeChainId, xChainType, walletProvider, rpcConfig]);
74
181
  return spokeProvider;
75
182
  }
76
183
  function useBorrow(spokeToken, spokeProvider) {
@@ -166,46 +273,26 @@ function useWithdraw(spokeToken, spokeProvider) {
166
273
  }
167
274
  });
168
275
  }
169
- function useUserReservesData(spokeChainId, address) {
276
+ function useUserReservesData(spokeProvider, address, refetchInterval = 5e3) {
170
277
  const { sodax } = useSodaxContext();
171
- const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? "sonic";
172
- const hubProvider = useHubProvider();
173
- const { data: userReserves } = reactQuery.useQuery({
174
- queryKey: ["userReserves", spokeChainId, address],
278
+ return reactQuery.useQuery({
279
+ queryKey: ["userReserves", spokeProvider?.chainConfig.chain.id, address],
175
280
  queryFn: async () => {
176
- if (!hubProvider || !address) {
177
- return;
178
- }
179
- const addressBytes = sdk.encodeAddress(spokeChainId, address);
180
- const hubWalletAddress = await sdk.EvmWalletAbstraction.getUserHubWalletAddress(
181
- spokeChainId,
182
- addressBytes,
183
- hubProvider
184
- );
185
- const moneyMarketConfig = sdk.getMoneyMarketConfig(hubChainId);
186
- const [res] = await sodax.moneyMarket.getUserReservesData(
187
- hubWalletAddress,
188
- moneyMarketConfig.uiPoolDataProvider,
189
- moneyMarketConfig.poolAddressesProvider
190
- );
191
- return res;
281
+ if (!spokeProvider) {
282
+ throw new Error("Spoke provider or address is not defined");
283
+ }
284
+ return await sodax.moneyMarket.data.getUserReservesData(spokeProvider);
192
285
  },
193
- enabled: !!spokeChainId && !!hubProvider && !!address,
194
- refetchInterval: 5e3
286
+ enabled: !!spokeProvider && !!address,
287
+ refetchInterval
195
288
  });
196
- return userReserves;
197
289
  }
198
290
  function useReservesData() {
199
291
  const { sodax } = useSodaxContext();
200
- const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? "sonic";
201
292
  return reactQuery.useQuery({
202
293
  queryKey: ["reservesData"],
203
294
  queryFn: async () => {
204
- const moneyMarketConfig = sdk.getMoneyMarketConfig(hubChainId);
205
- return await sodax.moneyMarket.getReservesData(
206
- moneyMarketConfig.uiPoolDataProvider,
207
- moneyMarketConfig.poolAddressesProvider
208
- );
295
+ return await sodax.moneyMarket.data.getReservesData();
209
296
  }
210
297
  });
211
298
  }
@@ -215,10 +302,11 @@ function useMMAllowance(token, amount, action, spokeProvider) {
215
302
  queryKey: ["allowance", token.address, amount, action],
216
303
  queryFn: async () => {
217
304
  if (!spokeProvider) throw new Error("Spoke provider is required");
305
+ const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
218
306
  const allowance = await sodax.moneyMarket.isAllowanceValid(
219
307
  {
220
308
  token: token.address,
221
- amount: viem.parseUnits(amount, token.decimals),
309
+ amount: viem.parseUnits(amount, actionBasedDecimals),
222
310
  action
223
311
  },
224
312
  spokeProvider
@@ -244,10 +332,11 @@ function useMMApprove(token, spokeProvider) {
244
332
  if (!spokeProvider) {
245
333
  throw new Error("Spoke provider not found");
246
334
  }
335
+ const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
247
336
  const allowance = await sodax.moneyMarket.approve(
248
337
  {
249
338
  token: token.address,
250
- amount: viem.parseUnits(amount, token.decimals),
339
+ amount: viem.parseUnits(amount, actionBasedDecimals),
251
340
  action
252
341
  },
253
342
  spokeProvider
@@ -270,26 +359,43 @@ function useMMApprove(token, spokeProvider) {
270
359
  }
271
360
  var useQuote = (payload) => {
272
361
  const { sodax } = useSodaxContext();
362
+ const queryKey = React.useMemo(() => {
363
+ if (!payload) return ["quote", void 0];
364
+ return [
365
+ "quote",
366
+ {
367
+ ...payload,
368
+ amount: payload.amount.toString()
369
+ }
370
+ ];
371
+ }, [payload]);
273
372
  return reactQuery.useQuery({
274
- queryKey: [payload],
373
+ queryKey,
275
374
  queryFn: async () => {
276
375
  if (!payload) {
277
376
  return void 0;
278
377
  }
279
- return sodax.solver.getQuote(payload);
378
+ return sodax.swap.getQuote(payload);
280
379
  },
281
380
  enabled: !!payload,
282
381
  refetchInterval: 3e3
283
382
  });
284
383
  };
285
- function useCreateIntentOrder(spokeProvider) {
384
+ function useSwap(spokeProvider) {
286
385
  const { sodax } = useSodaxContext();
386
+ const queryClient = reactQuery.useQueryClient();
287
387
  return reactQuery.useMutation({
288
388
  mutationFn: async (params) => {
289
389
  if (!spokeProvider) {
290
390
  throw new Error("Spoke provider not found");
291
391
  }
292
- return sodax.solver.createAndSubmitIntent(params, spokeProvider);
392
+ return sodax.swap.swap({
393
+ intentParams: params,
394
+ spokeProvider
395
+ });
396
+ },
397
+ onSuccess: () => {
398
+ queryClient.invalidateQueries({ queryKey: ["xBalances"] });
293
399
  }
294
400
  });
295
401
  }
@@ -298,7 +404,7 @@ var useStatus = (intent_tx_hash) => {
298
404
  return reactQuery.useQuery({
299
405
  queryKey: [intent_tx_hash],
300
406
  queryFn: async () => {
301
- return sodax.solver.getStatus({ intent_tx_hash });
407
+ return sodax.swap.getStatus({ intent_tx_hash });
302
408
  },
303
409
  refetchInterval: 3e3
304
410
  // 3s
@@ -312,16 +418,20 @@ function useSwapAllowance(params, spokeProvider) {
312
418
  if (!spokeProvider || !params) {
313
419
  return false;
314
420
  }
315
- const allowance = await sodax.solver.isAllowanceValid(params, spokeProvider);
421
+ const allowance = await sodax.swap.isAllowanceValid({
422
+ intentParams: params,
423
+ spokeProvider
424
+ });
316
425
  if (allowance.ok) {
317
426
  return allowance.value;
318
427
  }
319
428
  return false;
320
429
  },
321
- enabled: !!spokeProvider && !!params
430
+ enabled: !!spokeProvider && !!params,
431
+ refetchInterval: 2e3
322
432
  });
323
433
  }
324
- function useSwapApprove(token, spokeProvider) {
434
+ function useSwapApprove(params, spokeProvider) {
325
435
  const { sodax } = useSodaxContext();
326
436
  const queryClient = reactQuery.useQueryClient();
327
437
  const {
@@ -330,25 +440,24 @@ function useSwapApprove(token, spokeProvider) {
330
440
  error,
331
441
  reset: resetError
332
442
  } = reactQuery.useMutation({
333
- mutationFn: async ({ amount }) => {
443
+ mutationFn: async ({ params: params2 }) => {
334
444
  if (!spokeProvider) {
335
445
  throw new Error("Spoke provider not found");
336
446
  }
337
- if (!token) {
338
- throw new Error("Token not found");
447
+ if (!params2) {
448
+ throw new Error("Swap Params not found");
339
449
  }
340
- const allowance = await sodax.solver.approve(
341
- token.address,
342
- viem.parseUnits(amount, token.decimals),
450
+ const allowance = await sodax.swap.approve({
451
+ intentParams: params2,
343
452
  spokeProvider
344
- );
453
+ });
345
454
  if (!allowance.ok) {
346
- throw new Error("Failed to approve tokens");
455
+ throw new Error("Failed to approve input token");
347
456
  }
348
457
  return allowance.ok;
349
458
  },
350
459
  onSuccess: () => {
351
- queryClient.invalidateQueries({ queryKey: ["allowance", token?.address] });
460
+ queryClient.invalidateQueries({ queryKey: ["allowance", params] });
352
461
  }
353
462
  });
354
463
  return {
@@ -358,21 +467,785 @@ function useSwapApprove(token, spokeProvider) {
358
467
  resetError
359
468
  };
360
469
  }
361
- var SodaxProvider = ({ children, testnet = false, config }) => {
362
- const sodax = new sdk.Sodax(config);
363
- const hubChainId = config?.hubProviderConfig?.chainConfig.chain.id;
364
- const hubRpcUrl = config?.hubProviderConfig?.hubRpcUrl;
365
- const hubProvider = React.useMemo(() => {
366
- if (hubChainId && hubRpcUrl) {
367
- const hubChainCfg = sdk.getHubChainConfig(hubChainId);
368
- return new sdk.EvmHubProvider({
369
- hubRpcUrl,
370
- chainConfig: hubChainCfg
470
+ function useCancelSwap(spokeProvider) {
471
+ const { sodax } = useSodaxContext();
472
+ return reactQuery.useMutation({
473
+ mutationFn: async ({ intent, raw = false }) => {
474
+ if (!spokeProvider) {
475
+ throw new Error("Spoke provider not found");
476
+ }
477
+ return sodax.swap.cancelIntent(intent, spokeProvider, raw);
478
+ }
479
+ });
480
+ }
481
+ var useBackendIntentByTxHash = (txHash) => {
482
+ const { sodax } = useSodaxContext();
483
+ return reactQuery.useQuery({
484
+ queryKey: ["backend", "intent", "txHash", txHash],
485
+ queryFn: async () => {
486
+ if (!txHash) {
487
+ return void 0;
488
+ }
489
+ return sodax.backendApi.getIntentByTxHash(txHash);
490
+ },
491
+ enabled: !!txHash && txHash.length > 0,
492
+ retry: 3
493
+ });
494
+ };
495
+ var useBackendIntentByHash = (intentHash) => {
496
+ const { sodax } = useSodaxContext();
497
+ return reactQuery.useQuery({
498
+ queryKey: ["backend", "intent", "hash", intentHash],
499
+ queryFn: async () => {
500
+ if (!intentHash) {
501
+ return void 0;
502
+ }
503
+ return sodax.backendApi.getIntentByHash(intentHash);
504
+ },
505
+ enabled: !!intentHash && intentHash.length > 0,
506
+ retry: 3
507
+ });
508
+ };
509
+ var useBackendOrderbook = (params) => {
510
+ const { sodax } = useSodaxContext();
511
+ return reactQuery.useQuery({
512
+ queryKey: ["backend", "solver", "orderbook", params],
513
+ queryFn: async () => {
514
+ if (!params || !params.offset || !params.limit) {
515
+ return void 0;
516
+ }
517
+ return sodax.backendApi.getOrderbook(params);
518
+ },
519
+ enabled: !!params && !!params.offset && !!params.limit,
520
+ staleTime: 30 * 1e3,
521
+ // 30 seconds for real-time data
522
+ retry: 3
523
+ });
524
+ };
525
+ var useBackendMoneyMarketPosition = (userAddress) => {
526
+ const { sodax } = useSodaxContext();
527
+ return reactQuery.useQuery({
528
+ queryKey: ["backend", "moneymarket", "position", userAddress],
529
+ queryFn: async () => {
530
+ if (!userAddress) {
531
+ return void 0;
532
+ }
533
+ return sodax.backendApi.getMoneyMarketPosition(userAddress);
534
+ },
535
+ enabled: !!userAddress && userAddress.length > 0,
536
+ retry: 3
537
+ });
538
+ };
539
+ var useBackendAllMoneyMarketAssets = () => {
540
+ const { sodax } = useSodaxContext();
541
+ return reactQuery.useQuery({
542
+ queryKey: ["backend", "moneymarket", "assets", "all"],
543
+ queryFn: async () => {
544
+ return sodax.backendApi.getAllMoneyMarketAssets();
545
+ },
546
+ retry: 3
547
+ });
548
+ };
549
+ var useBackendMoneyMarketAsset = (reserveAddress) => {
550
+ const { sodax } = useSodaxContext();
551
+ return reactQuery.useQuery({
552
+ queryKey: ["backend", "moneymarket", "asset", reserveAddress],
553
+ queryFn: async () => {
554
+ if (!reserveAddress) {
555
+ return void 0;
556
+ }
557
+ return sodax.backendApi.getMoneyMarketAsset(reserveAddress);
558
+ },
559
+ enabled: !!reserveAddress && reserveAddress.length > 0,
560
+ retry: 3
561
+ });
562
+ };
563
+ var useBackendMoneyMarketAssetBorrowers = (params) => {
564
+ const { sodax } = useSodaxContext();
565
+ return reactQuery.useQuery({
566
+ queryKey: ["backend", "moneymarket", "asset", "borrowers", params],
567
+ queryFn: async () => {
568
+ if (!params.reserveAddress || !params.offset || !params.limit) {
569
+ return void 0;
570
+ }
571
+ return sodax.backendApi.getMoneyMarketAssetBorrowers(params.reserveAddress, {
572
+ offset: params.offset,
573
+ limit: params.limit
574
+ });
575
+ },
576
+ enabled: !!params.reserveAddress && !!params.offset && !!params.limit,
577
+ retry: 3
578
+ });
579
+ };
580
+ var useBackendMoneyMarketAssetSuppliers = (params) => {
581
+ const { sodax } = useSodaxContext();
582
+ return reactQuery.useQuery({
583
+ queryKey: ["backend", "moneymarket", "asset", "suppliers", params],
584
+ queryFn: async () => {
585
+ if (!params.reserveAddress || !params.offset || !params.limit) {
586
+ return void 0;
587
+ }
588
+ return sodax.backendApi.getMoneyMarketAssetSuppliers(params.reserveAddress, {
589
+ offset: params.offset,
590
+ limit: params.limit
371
591
  });
592
+ },
593
+ enabled: !!params.reserveAddress && !!params.offset && !!params.limit,
594
+ retry: 3
595
+ });
596
+ };
597
+ var useBackendAllMoneyMarketBorrowers = (params) => {
598
+ const { sodax } = useSodaxContext();
599
+ return reactQuery.useQuery({
600
+ queryKey: ["backend", "moneymarket", "borrowers", "all", params],
601
+ queryFn: async () => {
602
+ if (!params || !params.offset || !params.limit) {
603
+ return void 0;
604
+ }
605
+ return sodax.backendApi.getAllMoneyMarketBorrowers(params);
606
+ },
607
+ enabled: !!params && !!params.offset && !!params.limit,
608
+ retry: 3
609
+ });
610
+ };
611
+ function useBridgeAllowance(params, spokeProvider) {
612
+ const { sodax } = useSodaxContext();
613
+ return reactQuery.useQuery({
614
+ queryKey: ["bridge-allowance", params],
615
+ queryFn: async () => {
616
+ if (!spokeProvider || !params) {
617
+ return false;
618
+ }
619
+ const allowance = await sodax.bridge.isAllowanceValid({
620
+ params,
621
+ spokeProvider
622
+ });
623
+ if (allowance.ok) {
624
+ return allowance.value;
625
+ }
626
+ return false;
627
+ },
628
+ enabled: !!spokeProvider && !!params
629
+ });
630
+ }
631
+ function useBridgeApprove(spokeProvider) {
632
+ const { sodax } = useSodaxContext();
633
+ const queryClient = reactQuery.useQueryClient();
634
+ const {
635
+ mutateAsync: approve,
636
+ isPending,
637
+ error,
638
+ reset: resetError
639
+ } = reactQuery.useMutation({
640
+ mutationFn: async (params) => {
641
+ if (!spokeProvider) {
642
+ throw new Error("Spoke provider not found");
643
+ }
644
+ const allowance = await sodax.bridge.approve({
645
+ params,
646
+ spokeProvider
647
+ });
648
+ if (!allowance.ok) {
649
+ throw new Error("Failed to approve tokens for bridge");
650
+ }
651
+ return true;
652
+ },
653
+ onSuccess: (_, params) => {
654
+ queryClient.invalidateQueries({ queryKey: ["bridge-allowance", params] });
372
655
  }
373
- return void 0;
374
- }, [hubChainId, hubRpcUrl]);
375
- return /* @__PURE__ */ React__default.default.createElement(SodaxContext.Provider, { value: { sodax, testnet, hubProvider } }, children);
656
+ });
657
+ return {
658
+ approve,
659
+ isLoading: isPending,
660
+ error,
661
+ resetError
662
+ };
663
+ }
664
+ function useBridge(spokeProvider) {
665
+ const { sodax } = useSodaxContext();
666
+ return reactQuery.useMutation({
667
+ mutationFn: async (params) => {
668
+ if (!spokeProvider) {
669
+ throw new Error("Spoke provider not found");
670
+ }
671
+ const result = await sodax.bridge.bridge({
672
+ params,
673
+ spokeProvider
674
+ });
675
+ if (!result.ok) {
676
+ throw new Error(`Bridge failed: ${result.error.code}`);
677
+ }
678
+ return result;
679
+ }
680
+ });
681
+ }
682
+ function useGetBridgeableAmount(from, to) {
683
+ const { sodax } = useSodaxContext();
684
+ return reactQuery.useQuery({
685
+ queryKey: ["spoke-asset-manager-token-balance", from, to],
686
+ queryFn: async () => {
687
+ if (!from || !to) {
688
+ return 0n;
689
+ }
690
+ const result = await sodax.bridge.getBridgeableAmount(from, to);
691
+ if (result.ok) {
692
+ return result.value;
693
+ }
694
+ console.error("Error getting bridgeable amount:", result.error);
695
+ return 0n;
696
+ },
697
+ enabled: !!from && !!to
698
+ });
699
+ }
700
+ function useGetBridgeableTokens(from, to, token) {
701
+ const { sodax } = useSodaxContext();
702
+ return reactQuery.useQuery({
703
+ queryKey: ["bridgeable-tokens", from, to, token],
704
+ queryFn: async () => {
705
+ if (!from || !to || !token) {
706
+ return [];
707
+ }
708
+ const result = sodax.bridge.getBridgeableTokens(from, to, token);
709
+ if (result.ok) {
710
+ return result.value;
711
+ }
712
+ console.error("Error getting bridgeable tokens:", result.error);
713
+ return [];
714
+ },
715
+ enabled: !!from && !!to && !!token
716
+ });
717
+ }
718
+ function useStake(spokeProvider) {
719
+ const { sodax } = useSodaxContext();
720
+ return reactQuery.useMutation({
721
+ mutationFn: async (params) => {
722
+ if (!spokeProvider) {
723
+ throw new Error("Spoke provider not found");
724
+ }
725
+ const result = await sodax.staking.stake(params, spokeProvider);
726
+ if (!result.ok) {
727
+ throw new Error(`Stake failed: ${result.error.code}`);
728
+ }
729
+ return result.value;
730
+ }
731
+ });
732
+ }
733
+ function useStakeApprove(spokeProvider) {
734
+ const { sodax } = useSodaxContext();
735
+ return reactQuery.useMutation({
736
+ mutationFn: async (params) => {
737
+ if (!spokeProvider) {
738
+ throw new Error("Spoke provider not found");
739
+ }
740
+ const result = await sodax.staking.approve({
741
+ params: { ...params, action: "stake" },
742
+ spokeProvider
743
+ });
744
+ if (!result.ok) {
745
+ throw new Error(`Stake approval failed: ${result.error.code}`);
746
+ }
747
+ return result.value;
748
+ }
749
+ });
750
+ }
751
+ function useStakeAllowance(params, spokeProvider) {
752
+ const { sodax } = useSodaxContext();
753
+ return reactQuery.useQuery({
754
+ queryKey: ["soda", "stakeAllowance", params, spokeProvider?.chainConfig.chain.id],
755
+ queryFn: async () => {
756
+ if (!params || !spokeProvider) {
757
+ return false;
758
+ }
759
+ const result = await sodax.staking.isAllowanceValid({
760
+ params: { ...params, action: "stake" },
761
+ spokeProvider
762
+ });
763
+ if (!result.ok) {
764
+ throw new Error(`Allowance check failed: ${result.error.code}`);
765
+ }
766
+ return result.value;
767
+ },
768
+ enabled: !!params && !!spokeProvider,
769
+ refetchInterval: 5e3
770
+ // Refetch every 5 seconds
771
+ });
772
+ }
773
+ function useUnstake(spokeProvider) {
774
+ const { sodax } = useSodaxContext();
775
+ const queryClient = reactQuery.useQueryClient();
776
+ return reactQuery.useMutation({
777
+ mutationFn: async (params) => {
778
+ if (!spokeProvider) {
779
+ throw new Error("Spoke provider not found");
780
+ }
781
+ const result = await sodax.staking.unstake({ ...params, action: "unstake" }, spokeProvider);
782
+ if (!result.ok) {
783
+ throw new Error(`Unstake failed: ${result.error.code}`);
784
+ }
785
+ return result.value;
786
+ },
787
+ onSuccess: () => {
788
+ queryClient.invalidateQueries({ queryKey: ["stakingInfo"] });
789
+ queryClient.invalidateQueries({ queryKey: ["unstakingInfo"] });
790
+ queryClient.invalidateQueries({ queryKey: ["unstakingInfoWithPenalty"] });
791
+ }
792
+ });
793
+ }
794
+ function useClaim(spokeProvider) {
795
+ const { sodax } = useSodaxContext();
796
+ return reactQuery.useMutation({
797
+ mutationFn: async (params) => {
798
+ if (!spokeProvider) {
799
+ throw new Error("Spoke provider not found");
800
+ }
801
+ const result = await sodax.staking.claim({ ...params, action: "claim" }, spokeProvider);
802
+ if (!result.ok) {
803
+ throw new Error(`Claim failed: ${result.error.code}`);
804
+ }
805
+ return result.value;
806
+ }
807
+ });
808
+ }
809
+ function useCancelUnstake(spokeProvider) {
810
+ const { sodax } = useSodaxContext();
811
+ const queryClient = reactQuery.useQueryClient();
812
+ return reactQuery.useMutation({
813
+ mutationFn: async (params) => {
814
+ if (!spokeProvider) {
815
+ throw new Error("Spoke provider not available");
816
+ }
817
+ const result = await sodax.staking.cancelUnstake({ ...params, action: "cancelUnstake" }, spokeProvider);
818
+ if (!result.ok) {
819
+ throw new Error(`Cancel unstake failed: ${result.error.code}`);
820
+ }
821
+ return result.value;
822
+ },
823
+ onSuccess: () => {
824
+ queryClient.invalidateQueries({ queryKey: ["stakingInfo"] });
825
+ queryClient.invalidateQueries({ queryKey: ["unstakingInfo"] });
826
+ queryClient.invalidateQueries({ queryKey: ["unstakingInfoWithPenalty"] });
827
+ }
828
+ });
829
+ }
830
+ function useStakingInfo(spokeProvider, refetchInterval = 5e3) {
831
+ const { sodax } = useSodaxContext();
832
+ return reactQuery.useQuery({
833
+ queryKey: ["soda", "stakingInfo", spokeProvider?.chainConfig.chain.id],
834
+ queryFn: async () => {
835
+ if (!spokeProvider) {
836
+ throw new Error("Spoke provider not found");
837
+ }
838
+ const result = await sodax.staking.getStakingInfoFromSpoke(spokeProvider);
839
+ if (!result.ok) {
840
+ throw new Error(`Failed to fetch staking info: ${result.error.code}`);
841
+ }
842
+ return result.value;
843
+ },
844
+ enabled: !!spokeProvider,
845
+ refetchInterval
846
+ });
847
+ }
848
+ function useUnstakingInfoWithPenalty(userAddress, spokeProvider, refetchInterval = 5e3) {
849
+ const { sodax } = useSodaxContext();
850
+ return reactQuery.useQuery({
851
+ queryKey: ["soda", "unstakingInfoWithPenalty", spokeProvider?.chainConfig.chain.id, userAddress],
852
+ queryFn: async () => {
853
+ if (!spokeProvider) {
854
+ throw new Error("Spoke provider not found");
855
+ }
856
+ const penaltyResult = await sodax.staking.getUnstakingInfoWithPenalty(spokeProvider);
857
+ if (!penaltyResult.ok) {
858
+ throw new Error(`Failed to fetch unstaking info with penalty: ${penaltyResult.error.code}`);
859
+ }
860
+ return penaltyResult.value;
861
+ },
862
+ enabled: !!spokeProvider && !!userAddress,
863
+ refetchInterval
864
+ });
865
+ }
866
+ function useStakingConfig(refetchInterval = 3e4) {
867
+ const { sodax } = useSodaxContext();
868
+ return reactQuery.useQuery({
869
+ queryKey: ["soda", "stakingConfig"],
870
+ queryFn: async () => {
871
+ const result = await sodax.staking.getStakingConfig();
872
+ if (!result.ok) {
873
+ throw new Error(`Failed to fetch staking config: ${result.error.code}`);
874
+ }
875
+ return result.value;
876
+ },
877
+ refetchInterval
878
+ });
879
+ }
880
+ function useStakeRatio(amount, refetchInterval = 1e4) {
881
+ const { sodax } = useSodaxContext();
882
+ return reactQuery.useQuery({
883
+ queryKey: ["soda", "stakeRatio", amount?.toString()],
884
+ queryFn: async () => {
885
+ if (!amount || amount <= 0n) {
886
+ throw new Error("Amount must be greater than 0");
887
+ }
888
+ if (!sodax?.staking) {
889
+ throw new Error("Staking service not available");
890
+ }
891
+ const result = await sodax.staking.getStakeRatio(amount);
892
+ if (!result.ok) {
893
+ throw new Error(`Failed to fetch stake ratio: ${result.error.code}`);
894
+ }
895
+ return result.value;
896
+ },
897
+ enabled: !!amount && amount > 0n && !!sodax?.staking,
898
+ refetchInterval
899
+ });
900
+ }
901
+ function useInstantUnstakeRatio(amount, refetchInterval = 1e4) {
902
+ const { sodax } = useSodaxContext();
903
+ console.log("useInstantUnstakeRatio hook called with:", { amount: amount?.toString(), sodax: !!sodax });
904
+ return reactQuery.useQuery({
905
+ queryKey: ["soda", "instantUnstakeRatio", amount?.toString()],
906
+ queryFn: async () => {
907
+ console.log("useInstantUnstakeRatio queryFn called with amount:", amount?.toString());
908
+ if (!amount || amount <= 0n) {
909
+ throw new Error("Amount must be greater than 0");
910
+ }
911
+ if (!sodax?.staking) {
912
+ throw new Error("Staking service not available");
913
+ }
914
+ const result = await sodax.staking.getInstantUnstakeRatio(amount);
915
+ if (!result.ok) {
916
+ throw new Error(`Failed to fetch instant unstake ratio: ${result.error.code}`);
917
+ }
918
+ return result.value;
919
+ },
920
+ enabled: !!amount && amount > 0n && !!sodax?.staking,
921
+ refetchInterval
922
+ });
923
+ }
924
+ function useConvertedAssets(amount, refetchInterval = 1e4) {
925
+ const { sodax } = useSodaxContext();
926
+ console.log("useConvertedAssets hook called with:", { amount: amount?.toString(), sodax: !!sodax });
927
+ return reactQuery.useQuery({
928
+ queryKey: ["soda", "convertedAssets", amount?.toString()],
929
+ queryFn: async () => {
930
+ console.log("useConvertedAssets queryFn called with amount:", amount?.toString());
931
+ if (!amount || amount <= 0n) {
932
+ throw new Error("Amount must be greater than 0");
933
+ }
934
+ const result = await sodax.staking.getConvertedAssets(amount);
935
+ if (!result.ok) {
936
+ throw new Error(`Failed to fetch converted assets: ${result.error.code}`);
937
+ }
938
+ return result.value;
939
+ },
940
+ enabled: !!amount && amount > 0n && !!sodax?.staking,
941
+ refetchInterval
942
+ });
943
+ }
944
+ function useInstantUnstake(spokeProvider) {
945
+ const { sodax } = useSodaxContext();
946
+ return reactQuery.useMutation({
947
+ mutationFn: async (params) => {
948
+ if (!spokeProvider) {
949
+ throw new Error("spokeProvider is not found");
950
+ }
951
+ const result = await sodax.staking.instantUnstake({ ...params, action: "instantUnstake" }, spokeProvider);
952
+ if (!result.ok) {
953
+ throw new Error(`Instant unstake failed: ${result.error.code}`);
954
+ }
955
+ return result.value;
956
+ },
957
+ onError: (error) => {
958
+ console.error("Instant unstake error:", error);
959
+ }
960
+ });
961
+ }
962
+ function useUnstakeAllowance(params, spokeProvider) {
963
+ const { sodax } = useSodaxContext();
964
+ return reactQuery.useQuery({
965
+ queryKey: ["soda", "unstakeAllowance", params, spokeProvider?.chainConfig.chain.id],
966
+ queryFn: async () => {
967
+ if (!params || !spokeProvider) {
968
+ return false;
969
+ }
970
+ const result = await sodax.staking.isAllowanceValid({
971
+ params: { ...params, action: "unstake" },
972
+ spokeProvider
973
+ });
974
+ if (!result.ok) {
975
+ console.error(`Unstake allowance check failed: ${result.error.code}, error: ${result.error.error}`);
976
+ throw new Error(`Unstake allowance check failed: ${result.error.code}`);
977
+ }
978
+ return result.value;
979
+ },
980
+ enabled: !!params && !!spokeProvider,
981
+ refetchInterval: 5e3
982
+ // Refetch every 5 seconds
983
+ });
984
+ }
985
+ function useUnstakeApprove(spokeProvider) {
986
+ const { sodax } = useSodaxContext();
987
+ return reactQuery.useMutation({
988
+ mutationFn: async (params) => {
989
+ console.log("useUnstakeApprove called with params:", params);
990
+ if (!spokeProvider) {
991
+ throw new Error("Spoke provider not found");
992
+ }
993
+ const result = await sodax.staking.approve({
994
+ params: { ...params, action: "unstake" },
995
+ spokeProvider
996
+ });
997
+ if (!result.ok) {
998
+ throw new Error(`Unstake approval failed: ${result.error.code}`);
999
+ }
1000
+ return result.value;
1001
+ }
1002
+ });
1003
+ }
1004
+ function useUnstakingInfo(userAddress, spokeProvider, refetchInterval = 5e3) {
1005
+ const { sodax } = useSodaxContext();
1006
+ return reactQuery.useQuery({
1007
+ queryKey: ["soda", "unstakingInfoWithPenalty", spokeProvider?.chainConfig.chain.id, userAddress],
1008
+ queryFn: async () => {
1009
+ if (!spokeProvider || !userAddress) {
1010
+ throw new Error("Spoke provider or user address not found");
1011
+ }
1012
+ const result = await sodax.staking.getUnstakingInfo(spokeProvider);
1013
+ if (!result.ok) {
1014
+ throw new Error(`Failed to fetch unstaking info: ${result.error.code}`);
1015
+ }
1016
+ return result.value;
1017
+ },
1018
+ enabled: !!spokeProvider && !!userAddress,
1019
+ refetchInterval
1020
+ });
1021
+ }
1022
+ function useInstantUnstakeApprove(spokeProvider) {
1023
+ const { sodax } = useSodaxContext();
1024
+ return reactQuery.useMutation({
1025
+ mutationFn: async (params) => {
1026
+ console.log("useInstantUnstakeApprove called with params:", params);
1027
+ if (!spokeProvider) {
1028
+ throw new Error("Spoke provider not found");
1029
+ }
1030
+ const result = await sodax.staking.approve({
1031
+ params: { ...params, action: "instantUnstake" },
1032
+ spokeProvider
1033
+ });
1034
+ if (!result.ok) {
1035
+ throw new Error(`Instant unstake approval failed: ${result.error.code}`);
1036
+ }
1037
+ return result.value;
1038
+ }
1039
+ });
1040
+ }
1041
+ function useInstantUnstakeAllowance(params, spokeProvider) {
1042
+ const { sodax } = useSodaxContext();
1043
+ return reactQuery.useQuery({
1044
+ queryKey: ["soda", "instantUnstakeAllowance", params, spokeProvider?.chainConfig.chain.id],
1045
+ queryFn: async () => {
1046
+ if (!params || !spokeProvider) {
1047
+ return false;
1048
+ }
1049
+ const result = await sodax.staking.isAllowanceValid({
1050
+ params: { ...params, action: "instantUnstake" },
1051
+ spokeProvider
1052
+ });
1053
+ if (!result.ok) {
1054
+ console.error(`Unstake allowance check failed: ${result.error.code}, error: ${result.error.error}`);
1055
+ throw new Error(`Unstake allowance check failed: ${result.error.code}`);
1056
+ }
1057
+ return result.value;
1058
+ },
1059
+ enabled: !!params && !!spokeProvider,
1060
+ refetchInterval: 5e3
1061
+ // Refetch every 5 seconds
1062
+ });
1063
+ }
1064
+
1065
+ // src/hooks/migrate/types.ts
1066
+ var MIGRATION_MODE_ICX_SODA = "icxsoda";
1067
+ var MIGRATION_MODE_BNUSD = "bnusd";
1068
+
1069
+ // src/hooks/migrate/useMigrate.tsx
1070
+ function useMigrate(spokeProvider) {
1071
+ const { sodax } = useSodaxContext();
1072
+ return reactQuery.useMutation({
1073
+ mutationFn: async (params) => {
1074
+ const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
1075
+ const amountToMigrate = viem.parseUnits(amount ?? "0", token?.decimals ?? 0);
1076
+ if (!spokeProvider) {
1077
+ throw new Error("Spoke provider not found");
1078
+ }
1079
+ if (migrationMode === MIGRATION_MODE_ICX_SODA) {
1080
+ if (token?.xChainId === types.ICON_MAINNET_CHAIN_ID) {
1081
+ const params2 = {
1082
+ address: sdk.spokeChainConfig[types.ICON_MAINNET_CHAIN_ID].nativeToken,
1083
+ amount: amountToMigrate,
1084
+ to: destinationAddress
1085
+ };
1086
+ const result2 = await sodax.migration.migrateIcxToSoda(params2, spokeProvider, 3e4);
1087
+ if (result2.ok) {
1088
+ const [spokeTxHash, hubTxHash] = result2.value;
1089
+ return { spokeTxHash, hubTxHash };
1090
+ }
1091
+ throw new Error("ICX to SODA migration failed. Please try again.");
1092
+ }
1093
+ const revertParams = {
1094
+ amount: amountToMigrate,
1095
+ to: destinationAddress
1096
+ };
1097
+ const result = await sodax.migration.revertMigrateSodaToIcx(
1098
+ revertParams,
1099
+ spokeProvider,
1100
+ 3e4
1101
+ );
1102
+ if (result.ok) {
1103
+ const [hubTxHash, spokeTxHash] = result.value;
1104
+ return { spokeTxHash, hubTxHash };
1105
+ }
1106
+ throw new Error("SODA to ICX migration failed. Please try again.");
1107
+ }
1108
+ if (migrationMode === MIGRATION_MODE_BNUSD) {
1109
+ const params2 = {
1110
+ srcChainId: token?.xChainId,
1111
+ dstChainId: toToken?.xChainId,
1112
+ srcbnUSD: token?.address,
1113
+ dstbnUSD: toToken?.address,
1114
+ amount: amountToMigrate,
1115
+ to: destinationAddress
1116
+ };
1117
+ const result = await sodax.migration.migratebnUSD(params2, spokeProvider, 3e4);
1118
+ if (result.ok) {
1119
+ const [spokeTxHash, hubTxHash] = result.value;
1120
+ return { spokeTxHash, hubTxHash };
1121
+ }
1122
+ const errorMessage = sdk.isLegacybnUSDToken(token?.address) ? "bnUSD migration failed. Please try again." : "bnUSD reverse migration failed. Please try again.";
1123
+ throw new Error(errorMessage);
1124
+ }
1125
+ throw new Error("Invalid migration mode");
1126
+ }
1127
+ });
1128
+ }
1129
+ function useMigrationAllowance(params, spokeProvider) {
1130
+ const { sodax } = useSodaxContext();
1131
+ return reactQuery.useQuery({
1132
+ queryKey: ["migration-allowance", params],
1133
+ queryFn: async () => {
1134
+ if (!spokeProvider || !params) {
1135
+ return false;
1136
+ }
1137
+ const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
1138
+ if (token?.xChainId === types.ICON_MAINNET_CHAIN_ID) {
1139
+ return true;
1140
+ }
1141
+ if (!spokeProvider) throw new Error("Spoke provider is required");
1142
+ const amountToMigrate = viem.parseUnits(amount ?? "0", token?.decimals ?? 0);
1143
+ let migrationParams;
1144
+ if (migrationMode === MIGRATION_MODE_ICX_SODA) {
1145
+ migrationParams = {
1146
+ amount: amountToMigrate,
1147
+ to: destinationAddress
1148
+ };
1149
+ } else {
1150
+ if (!toToken) throw new Error("Destination token is required for bnUSD migration");
1151
+ migrationParams = {
1152
+ srcChainId: token?.xChainId,
1153
+ dstChainId: toToken?.xChainId,
1154
+ srcbnUSD: token?.address,
1155
+ dstbnUSD: toToken?.address,
1156
+ amount: amountToMigrate,
1157
+ to: destinationAddress
1158
+ };
1159
+ }
1160
+ const allowance = await sodax.migration.isAllowanceValid(migrationParams, "revert", spokeProvider);
1161
+ if (allowance.ok) {
1162
+ return allowance.value;
1163
+ }
1164
+ return false;
1165
+ },
1166
+ enabled: !!spokeProvider && !!params,
1167
+ refetchInterval: 2e3
1168
+ });
1169
+ }
1170
+ function useMigrationApprove(params, spokeProvider) {
1171
+ const { sodax } = useSodaxContext();
1172
+ const [isLoading, setIsLoading] = React.useState(false);
1173
+ const [error, setError] = React.useState(null);
1174
+ const [isApproved, setIsApproved] = React.useState(false);
1175
+ const queryClient = reactQuery.useQueryClient();
1176
+ const prevTokenAddress = React.useRef(void 0);
1177
+ const prevAmount = React.useRef(void 0);
1178
+ React.useEffect(() => {
1179
+ if (prevTokenAddress.current !== params?.token?.address || prevAmount.current !== params?.amount) {
1180
+ setIsApproved(false);
1181
+ prevTokenAddress.current = params?.token?.address;
1182
+ prevAmount.current = params?.amount;
1183
+ }
1184
+ }, [params?.token?.address, params?.amount]);
1185
+ const approve = React.useCallback(
1186
+ async ({ params: approveParams }) => {
1187
+ try {
1188
+ setIsLoading(true);
1189
+ setError(null);
1190
+ if (!spokeProvider) {
1191
+ throw new Error("Spoke provider not found");
1192
+ }
1193
+ if (!approveParams) {
1194
+ throw new Error("Migration intent not found");
1195
+ }
1196
+ const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = approveParams;
1197
+ const amountToMigrate = viem.parseUnits(amount ?? "0", token?.decimals ?? 0);
1198
+ let result;
1199
+ if (migrationMode === MIGRATION_MODE_ICX_SODA) {
1200
+ const revertParams = {
1201
+ amount: amountToMigrate,
1202
+ to: destinationAddress
1203
+ };
1204
+ result = await sodax.migration.approve(revertParams, "revert", spokeProvider, false);
1205
+ } else if (migrationMode === MIGRATION_MODE_BNUSD) {
1206
+ if (!toToken) throw new Error("Destination token is required for bnUSD migration");
1207
+ const migrationParams = {
1208
+ srcChainId: token?.xChainId,
1209
+ dstChainId: toToken?.xChainId,
1210
+ srcbnUSD: token?.address,
1211
+ dstbnUSD: toToken?.address,
1212
+ amount: amountToMigrate,
1213
+ to: destinationAddress
1214
+ };
1215
+ result = await sodax.migration.approve(migrationParams, "revert", spokeProvider, false);
1216
+ } else {
1217
+ throw new Error("Invalid migration mode");
1218
+ }
1219
+ if (!result.ok) {
1220
+ throw new Error("Failed to approve tokens");
1221
+ }
1222
+ setIsApproved(true);
1223
+ queryClient.invalidateQueries({ queryKey: ["migration-allowance", params] });
1224
+ return result.ok;
1225
+ } catch (err) {
1226
+ const error2 = err instanceof Error ? err : new Error("An unknown error occurred");
1227
+ setError(error2);
1228
+ throw error2;
1229
+ } finally {
1230
+ setIsLoading(false);
1231
+ }
1232
+ },
1233
+ [spokeProvider, sodax, queryClient, params]
1234
+ );
1235
+ const resetError = React.useCallback(() => {
1236
+ setError(null);
1237
+ }, []);
1238
+ return {
1239
+ approve,
1240
+ isLoading,
1241
+ error,
1242
+ resetError,
1243
+ isApproved
1244
+ };
1245
+ }
1246
+ var SodaxProvider = ({ children, testnet = false, config, rpcConfig }) => {
1247
+ const sodax = new sdk.Sodax(config);
1248
+ return /* @__PURE__ */ React__default.default.createElement(SodaxContext.Provider, { value: { sodax, testnet, rpcConfig } }, children);
376
1249
  };
377
1250
  var getSpokeTokenAddressByVault = (spokeChainId, vault) => {
378
1251
  const tokens = sdk.hubAssets[spokeChainId];
@@ -382,22 +1255,64 @@ var getSpokeTokenAddressByVault = (spokeChainId, vault) => {
382
1255
  return address;
383
1256
  };
384
1257
 
1258
+ exports.MIGRATION_MODE_BNUSD = MIGRATION_MODE_BNUSD;
1259
+ exports.MIGRATION_MODE_ICX_SODA = MIGRATION_MODE_ICX_SODA;
385
1260
  exports.SodaxProvider = SodaxProvider;
386
1261
  exports.getSpokeTokenAddressByVault = getSpokeTokenAddressByVault;
1262
+ exports.useBackendAllMoneyMarketAssets = useBackendAllMoneyMarketAssets;
1263
+ exports.useBackendAllMoneyMarketBorrowers = useBackendAllMoneyMarketBorrowers;
1264
+ exports.useBackendIntentByHash = useBackendIntentByHash;
1265
+ exports.useBackendIntentByTxHash = useBackendIntentByTxHash;
1266
+ exports.useBackendMoneyMarketAsset = useBackendMoneyMarketAsset;
1267
+ exports.useBackendMoneyMarketAssetBorrowers = useBackendMoneyMarketAssetBorrowers;
1268
+ exports.useBackendMoneyMarketAssetSuppliers = useBackendMoneyMarketAssetSuppliers;
1269
+ exports.useBackendMoneyMarketPosition = useBackendMoneyMarketPosition;
1270
+ exports.useBackendOrderbook = useBackendOrderbook;
387
1271
  exports.useBorrow = useBorrow;
388
- exports.useCreateIntentOrder = useCreateIntentOrder;
1272
+ exports.useBridge = useBridge;
1273
+ exports.useBridgeAllowance = useBridgeAllowance;
1274
+ exports.useBridgeApprove = useBridgeApprove;
1275
+ exports.useCancelSwap = useCancelSwap;
1276
+ exports.useCancelUnstake = useCancelUnstake;
1277
+ exports.useClaim = useClaim;
1278
+ exports.useConvertedAssets = useConvertedAssets;
1279
+ exports.useDeriveUserWalletAddress = useDeriveUserWalletAddress;
1280
+ exports.useEstimateGas = useEstimateGas;
1281
+ exports.useGetBridgeableAmount = useGetBridgeableAmount;
1282
+ exports.useGetBridgeableTokens = useGetBridgeableTokens;
389
1283
  exports.useHubProvider = useHubProvider;
1284
+ exports.useInstantUnstake = useInstantUnstake;
1285
+ exports.useInstantUnstakeAllowance = useInstantUnstakeAllowance;
1286
+ exports.useInstantUnstakeApprove = useInstantUnstakeApprove;
1287
+ exports.useInstantUnstakeRatio = useInstantUnstakeRatio;
390
1288
  exports.useMMAllowance = useMMAllowance;
391
1289
  exports.useMMApprove = useMMApprove;
1290
+ exports.useMigrate = useMigrate;
1291
+ exports.useMigrationAllowance = useMigrationAllowance;
1292
+ exports.useMigrationApprove = useMigrationApprove;
392
1293
  exports.useQuote = useQuote;
393
1294
  exports.useRepay = useRepay;
1295
+ exports.useRequestTrustline = useRequestTrustline;
394
1296
  exports.useReservesData = useReservesData;
395
1297
  exports.useSodaxContext = useSodaxContext;
396
1298
  exports.useSpokeProvider = useSpokeProvider;
1299
+ exports.useStake = useStake;
1300
+ exports.useStakeAllowance = useStakeAllowance;
1301
+ exports.useStakeApprove = useStakeApprove;
1302
+ exports.useStakeRatio = useStakeRatio;
1303
+ exports.useStakingConfig = useStakingConfig;
1304
+ exports.useStakingInfo = useStakingInfo;
397
1305
  exports.useStatus = useStatus;
1306
+ exports.useStellarTrustlineCheck = useStellarTrustlineCheck;
398
1307
  exports.useSupply = useSupply;
1308
+ exports.useSwap = useSwap;
399
1309
  exports.useSwapAllowance = useSwapAllowance;
400
1310
  exports.useSwapApprove = useSwapApprove;
1311
+ exports.useUnstake = useUnstake;
1312
+ exports.useUnstakeAllowance = useUnstakeAllowance;
1313
+ exports.useUnstakeApprove = useUnstakeApprove;
1314
+ exports.useUnstakingInfo = useUnstakingInfo;
1315
+ exports.useUnstakingInfoWithPenalty = useUnstakingInfoWithPenalty;
401
1316
  exports.useUserReservesData = useUserReservesData;
402
1317
  exports.useWithdraw = useWithdraw;
403
1318
  //# sourceMappingURL=index.js.map