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

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