@sodax/dapp-kit 0.0.1-rc.8 → 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 (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +157 -60
  3. package/dist/index.d.mts +1558 -0
  4. package/dist/index.d.ts +1558 -4
  5. package/dist/index.js +1030 -135
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +988 -134
  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 -1
  35. package/src/hooks/mm/useAToken.ts +47 -0
  36. package/src/hooks/mm/useBorrow.ts +2 -2
  37. package/src/hooks/mm/useMMAllowance.ts +2 -1
  38. package/src/hooks/mm/useMMApprove.ts +2 -1
  39. package/src/hooks/mm/useRepay.ts +2 -2
  40. package/src/hooks/mm/useReservesData.ts +1 -8
  41. package/src/hooks/mm/useReservesHumanized.ts +30 -0
  42. package/src/hooks/mm/useReservesList.ts +29 -0
  43. package/src/hooks/mm/useReservesUsdFormat.ts +38 -0
  44. package/src/hooks/mm/useSupply.ts +2 -2
  45. package/src/hooks/mm/useUserFormattedSummary.ts +54 -0
  46. package/src/hooks/mm/useUserReservesData.ts +30 -37
  47. package/src/hooks/mm/useWithdraw.ts +2 -2
  48. package/src/hooks/provider/useHubProvider.ts +3 -3
  49. package/src/hooks/provider/useSpokeProvider.ts +50 -18
  50. package/src/hooks/shared/index.ts +4 -0
  51. package/src/hooks/shared/useDeriveUserWalletAddress.ts +44 -0
  52. package/src/hooks/shared/useEstimateGas.ts +18 -0
  53. package/src/hooks/shared/useRequestTrustline.ts +103 -0
  54. package/src/hooks/shared/useStellarTrustlineCheck.ts +71 -0
  55. package/src/hooks/staking/index.ts +19 -0
  56. package/src/hooks/staking/useCancelUnstake.ts +52 -0
  57. package/src/hooks/staking/useClaim.ts +46 -0
  58. package/src/hooks/staking/useConvertedAssets.ts +47 -0
  59. package/src/hooks/staking/useInstantUnstake.ts +50 -0
  60. package/src/hooks/staking/useInstantUnstakeAllowance.ts +59 -0
  61. package/src/hooks/staking/useInstantUnstakeApprove.ts +52 -0
  62. package/src/hooks/staking/useInstantUnstakeRatio.ts +54 -0
  63. package/src/hooks/staking/useStake.ts +47 -0
  64. package/src/hooks/staking/useStakeAllowance.ts +57 -0
  65. package/src/hooks/staking/useStakeApprove.ts +50 -0
  66. package/src/hooks/staking/useStakeRatio.ts +53 -0
  67. package/src/hooks/staking/useStakingConfig.ts +40 -0
  68. package/src/hooks/staking/useStakingInfo.ts +50 -0
  69. package/src/hooks/staking/useUnstake.ts +54 -0
  70. package/src/hooks/staking/useUnstakeAllowance.ts +58 -0
  71. package/src/hooks/staking/useUnstakeApprove.ts +52 -0
  72. package/src/hooks/staking/useUnstakingInfo.ts +53 -0
  73. package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +59 -0
  74. package/src/hooks/swap/index.ts +2 -1
  75. package/src/hooks/swap/useCancelSwap.ts +44 -0
  76. package/src/hooks/swap/useQuote.ts +21 -7
  77. package/src/hooks/swap/useStatus.ts +4 -4
  78. package/src/hooks/swap/{useCreateIntentOrder.ts → useSwap.ts} +19 -14
  79. package/src/hooks/swap/useSwapAllowance.ts +5 -1
  80. package/src/hooks/swap/useSwapApprove.ts +14 -14
  81. package/src/index.ts +0 -1
  82. package/src/providers/SodaxProvider.tsx +8 -20
  83. package/dist/contexts/index.d.ts +0 -8
  84. package/dist/contexts/index.d.ts.map +0 -1
  85. package/dist/core/index.d.ts +0 -4
  86. package/dist/core/index.d.ts.map +0 -1
  87. package/dist/hooks/index.d.ts +0 -5
  88. package/dist/hooks/index.d.ts.map +0 -1
  89. package/dist/hooks/mm/index.d.ts +0 -10
  90. package/dist/hooks/mm/index.d.ts.map +0 -1
  91. package/dist/hooks/mm/useBorrow.d.ts +0 -35
  92. package/dist/hooks/mm/useBorrow.d.ts.map +0 -1
  93. package/dist/hooks/mm/useHubWalletAddress.d.ts +0 -24
  94. package/dist/hooks/mm/useHubWalletAddress.d.ts.map +0 -1
  95. package/dist/hooks/mm/useMMAllowance.d.ts +0 -26
  96. package/dist/hooks/mm/useMMAllowance.d.ts.map +0 -1
  97. package/dist/hooks/mm/useMMApprove.d.ts +0 -27
  98. package/dist/hooks/mm/useMMApprove.d.ts.map +0 -1
  99. package/dist/hooks/mm/useRepay.d.ts +0 -35
  100. package/dist/hooks/mm/useRepay.d.ts.map +0 -1
  101. package/dist/hooks/mm/useReservesData.d.ts +0 -19
  102. package/dist/hooks/mm/useReservesData.d.ts.map +0 -1
  103. package/dist/hooks/mm/useSupply.d.ts +0 -34
  104. package/dist/hooks/mm/useSupply.d.ts.map +0 -1
  105. package/dist/hooks/mm/useUserReservesData.d.ts +0 -9
  106. package/dist/hooks/mm/useUserReservesData.d.ts.map +0 -1
  107. package/dist/hooks/mm/useWithdraw.d.ts +0 -33
  108. package/dist/hooks/mm/useWithdraw.d.ts.map +0 -1
  109. package/dist/hooks/provider/index.d.ts +0 -3
  110. package/dist/hooks/provider/index.d.ts.map +0 -1
  111. package/dist/hooks/provider/useHubProvider.d.ts +0 -3
  112. package/dist/hooks/provider/useHubProvider.d.ts.map +0 -1
  113. package/dist/hooks/provider/useSpokeProvider.d.ts +0 -18
  114. package/dist/hooks/provider/useSpokeProvider.d.ts.map +0 -1
  115. package/dist/hooks/shared/index.d.ts +0 -2
  116. package/dist/hooks/shared/index.d.ts.map +0 -1
  117. package/dist/hooks/shared/useSodaxContext.d.ts +0 -8
  118. package/dist/hooks/shared/useSodaxContext.d.ts.map +0 -1
  119. package/dist/hooks/swap/index.d.ts +0 -6
  120. package/dist/hooks/swap/index.d.ts.map +0 -1
  121. package/dist/hooks/swap/useCreateIntentOrder.d.ts +0 -33
  122. package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +0 -1
  123. package/dist/hooks/swap/useQuote.d.ts +0 -39
  124. package/dist/hooks/swap/useQuote.d.ts.map +0 -1
  125. package/dist/hooks/swap/useStatus.d.ts +0 -31
  126. package/dist/hooks/swap/useStatus.d.ts.map +0 -1
  127. package/dist/hooks/swap/useSwapAllowance.d.ts +0 -23
  128. package/dist/hooks/swap/useSwapAllowance.d.ts.map +0 -1
  129. package/dist/hooks/swap/useSwapApprove.d.ts +0 -26
  130. package/dist/hooks/swap/useSwapApprove.d.ts.map +0 -1
  131. package/dist/index.d.ts.map +0 -1
  132. package/dist/providers/SodaxProvider.d.ts +0 -10
  133. package/dist/providers/SodaxProvider.d.ts.map +0 -1
  134. package/dist/providers/index.d.ts +0 -2
  135. package/dist/providers/index.d.ts.map +0 -1
  136. package/src/core/index.ts +0 -35
  137. package/src/hooks/mm/useHubWalletAddress.ts +0 -49
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,54 +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
161
  return new sdk.StellarSpokeProvider(
62
- _walletProvider,
63
- stellarConfig.addresses.assetManager,
162
+ walletProvider,
64
163
  stellarConfig,
65
- stellarConfig.rpc_url
164
+ rpcConfig.stellar ? rpcConfig.stellar : {
165
+ horizonRpcUrl: stellarConfig.horizonRpcUrl,
166
+ sorobanRpcUrl: stellarConfig.sorobanRpcUrl
167
+ }
168
+ );
169
+ }
170
+ if (xChainType === "SOLANA") {
171
+ return new sdk.SolanaSpokeProvider(
172
+ walletProvider,
173
+ rpcConfig.solana ? {
174
+ ...sdk.spokeChainConfig[spokeChainId],
175
+ rpcUrl: rpcConfig.solana
176
+ } : sdk.spokeChainConfig[spokeChainId]
66
177
  );
67
178
  }
68
179
  return void 0;
69
- }, [spokeChainId, xChainType, _walletProvider]);
180
+ }, [spokeChainId, xChainType, walletProvider, rpcConfig]);
70
181
  return spokeProvider;
71
182
  }
72
183
  function useBorrow(spokeToken, spokeProvider) {
@@ -76,7 +187,7 @@ function useBorrow(spokeToken, spokeProvider) {
76
187
  if (!spokeProvider) {
77
188
  throw new Error("spokeProvider is not found");
78
189
  }
79
- const response = await sodax.moneyMarket.borrowAndSubmit(
190
+ const response = await sodax.moneyMarket.borrow(
80
191
  {
81
192
  token: spokeToken.address,
82
193
  amount: viem.parseUnits(amount, 18),
@@ -99,7 +210,7 @@ function useRepay(spokeToken, spokeProvider) {
99
210
  if (!spokeProvider) {
100
211
  throw new Error("spokeProvider is not found");
101
212
  }
102
- const response = await sodax.moneyMarket.repayAndSubmit(
213
+ const response = await sodax.moneyMarket.repay(
103
214
  {
104
215
  token: spokeToken.address,
105
216
  amount: viem.parseUnits(amount, spokeToken.decimals),
@@ -122,7 +233,7 @@ function useSupply(spokeToken, spokeProvider) {
122
233
  if (!spokeProvider) {
123
234
  throw new Error("spokeProvider is not found");
124
235
  }
125
- const response = await sodax.moneyMarket.supplyAndSubmit(
236
+ const response = await sodax.moneyMarket.supply(
126
237
  {
127
238
  token: spokeToken.address,
128
239
  amount: viem.parseUnits(amount, spokeToken.decimals),
@@ -145,7 +256,7 @@ function useWithdraw(spokeToken, spokeProvider) {
145
256
  if (!spokeProvider) {
146
257
  throw new Error("spokeProvider is not found");
147
258
  }
148
- const response = await sodax.moneyMarket.withdrawAndSubmit(
259
+ const response = await sodax.moneyMarket.withdraw(
149
260
  {
150
261
  token: spokeToken.address,
151
262
  // vault token on hub chain decimals is 18
@@ -162,97 +273,26 @@ function useWithdraw(spokeToken, spokeProvider) {
162
273
  }
163
274
  });
164
275
  }
165
- var allXTokens = [];
166
- Object.keys(sdk.hubAssets).forEach((xChainId) => {
167
- const tokens = sdk.hubAssets[xChainId];
168
- Object.keys(tokens).forEach((tokenAddress) => {
169
- const token = tokens[tokenAddress];
170
- allXTokens.push({
171
- xChainId,
172
- symbol: token.symbol,
173
- name: token.name,
174
- decimals: token.decimal,
175
- address: tokenAddress
176
- });
177
- allXTokens.push({
178
- xChainId: "sonic",
179
- symbol: token.symbol,
180
- name: token.name,
181
- decimals: token.decimal,
182
- address: token.vault
183
- });
184
- });
185
- });
186
- var getSpokeTokenAddressByVault = (spokeChainId, vault) => {
187
- const tokens = sdk.hubAssets[spokeChainId];
188
- const address = Object.keys(tokens).find((tokenAddress) => tokens[tokenAddress].vault === vault);
189
- return address;
190
- };
191
- function useUserReservesData(spokeChainId, address) {
276
+ function useUserReservesData(spokeProvider, address, refetchInterval = 5e3) {
192
277
  const { sodax } = useSodaxContext();
193
- const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? "sonic";
194
- const hubProvider = useHubProvider();
195
- const { data: userReserves } = reactQuery.useQuery({
196
- queryKey: ["userReserves", spokeChainId, address],
197
- queryFn: async () => {
198
- if (!hubProvider || !address) {
199
- return;
200
- }
201
- const addressBytes = sdk.encodeAddress(spokeChainId, address);
202
- const hubWalletAddress = await sdk.EvmWalletAbstraction.getUserHubWalletAddress(
203
- spokeChainId,
204
- addressBytes,
205
- hubProvider
206
- );
207
- const moneyMarketConfig = sdk.getMoneyMarketConfig(hubChainId);
208
- const [res] = await sodax.moneyMarket.getUserReservesData(
209
- hubWalletAddress,
210
- moneyMarketConfig.uiPoolDataProvider,
211
- moneyMarketConfig.poolAddressesProvider
212
- );
213
- return res?.map((r) => {
214
- return {
215
- ...r,
216
- token: allXTokens.find((t) => t.address === r.underlyingAsset)
217
- };
218
- });
219
- },
220
- enabled: !!spokeChainId && !!hubProvider && !!address,
221
- refetchInterval: 5e3
222
- });
223
- return userReserves;
224
- }
225
- function useHubWalletAddress(spokeChainId, address, hubProvider) {
226
278
  return reactQuery.useQuery({
227
- queryKey: ["hubWallet", spokeChainId, address],
279
+ queryKey: ["userReserves", spokeProvider?.chainConfig.chain.id, address],
228
280
  queryFn: async () => {
229
- if (!address) return null;
230
- try {
231
- const hubWalletAddress = await sdk.EvmWalletAbstraction.getUserHubWalletAddress(
232
- spokeChainId,
233
- address,
234
- hubProvider
235
- );
236
- return hubWalletAddress;
237
- } catch (error) {
238
- console.log("error", error);
239
- return null;
281
+ if (!spokeProvider) {
282
+ throw new Error("Spoke provider or address is not defined");
240
283
  }
284
+ return await sodax.moneyMarket.data.getUserReservesData(spokeProvider);
241
285
  },
242
- enabled: !!address && !!hubProvider
286
+ enabled: !!spokeProvider && !!address,
287
+ refetchInterval
243
288
  });
244
289
  }
245
290
  function useReservesData() {
246
291
  const { sodax } = useSodaxContext();
247
- const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? "sonic";
248
292
  return reactQuery.useQuery({
249
293
  queryKey: ["reservesData"],
250
294
  queryFn: async () => {
251
- const moneyMarketConfig = sdk.getMoneyMarketConfig(hubChainId);
252
- return await sodax.moneyMarket.getReservesData(
253
- moneyMarketConfig.uiPoolDataProvider,
254
- moneyMarketConfig.poolAddressesProvider
255
- );
295
+ return await sodax.moneyMarket.data.getReservesData();
256
296
  }
257
297
  });
258
298
  }
@@ -262,10 +302,11 @@ function useMMAllowance(token, amount, action, spokeProvider) {
262
302
  queryKey: ["allowance", token.address, amount, action],
263
303
  queryFn: async () => {
264
304
  if (!spokeProvider) throw new Error("Spoke provider is required");
305
+ const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
265
306
  const allowance = await sodax.moneyMarket.isAllowanceValid(
266
307
  {
267
308
  token: token.address,
268
- amount: viem.parseUnits(amount, token.decimals),
309
+ amount: viem.parseUnits(amount, actionBasedDecimals),
269
310
  action
270
311
  },
271
312
  spokeProvider
@@ -291,10 +332,11 @@ function useMMApprove(token, spokeProvider) {
291
332
  if (!spokeProvider) {
292
333
  throw new Error("Spoke provider not found");
293
334
  }
335
+ const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
294
336
  const allowance = await sodax.moneyMarket.approve(
295
337
  {
296
338
  token: token.address,
297
- amount: viem.parseUnits(amount, token.decimals),
339
+ amount: viem.parseUnits(amount, actionBasedDecimals),
298
340
  action
299
341
  },
300
342
  spokeProvider
@@ -315,28 +357,72 @@ function useMMApprove(token, spokeProvider) {
315
357
  resetError
316
358
  };
317
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
+ }
318
387
  var useQuote = (payload) => {
319
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]);
320
399
  return reactQuery.useQuery({
321
- queryKey: [payload],
400
+ queryKey,
322
401
  queryFn: async () => {
323
402
  if (!payload) {
324
403
  return void 0;
325
404
  }
326
- return sodax.solver.getQuote(payload);
405
+ return sodax.swaps.getQuote(payload);
327
406
  },
328
407
  enabled: !!payload,
329
408
  refetchInterval: 3e3
330
409
  });
331
410
  };
332
- function useCreateIntentOrder(spokeProvider) {
411
+ function useSwap(spokeProvider) {
333
412
  const { sodax } = useSodaxContext();
413
+ const queryClient = reactQuery.useQueryClient();
334
414
  return reactQuery.useMutation({
335
415
  mutationFn: async (params) => {
336
416
  if (!spokeProvider) {
337
417
  throw new Error("Spoke provider not found");
338
418
  }
339
- 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"] });
340
426
  }
341
427
  });
342
428
  }
@@ -345,7 +431,7 @@ var useStatus = (intent_tx_hash) => {
345
431
  return reactQuery.useQuery({
346
432
  queryKey: [intent_tx_hash],
347
433
  queryFn: async () => {
348
- return sodax.solver.getStatus({ intent_tx_hash });
434
+ return sodax.swaps.getStatus({ intent_tx_hash });
349
435
  },
350
436
  refetchInterval: 3e3
351
437
  // 3s
@@ -359,16 +445,20 @@ function useSwapAllowance(params, spokeProvider) {
359
445
  if (!spokeProvider || !params) {
360
446
  return false;
361
447
  }
362
- const allowance = await sodax.solver.isAllowanceValid(params, spokeProvider);
448
+ const allowance = await sodax.swaps.isAllowanceValid({
449
+ intentParams: params,
450
+ spokeProvider
451
+ });
363
452
  if (allowance.ok) {
364
453
  return allowance.value;
365
454
  }
366
455
  return false;
367
456
  },
368
- enabled: !!spokeProvider && !!params
457
+ enabled: !!spokeProvider && !!params,
458
+ refetchInterval: 2e3
369
459
  });
370
460
  }
371
- function useSwapApprove(token, spokeProvider) {
461
+ function useSwapApprove(params, spokeProvider) {
372
462
  const { sodax } = useSodaxContext();
373
463
  const queryClient = reactQuery.useQueryClient();
374
464
  const {
@@ -377,25 +467,24 @@ function useSwapApprove(token, spokeProvider) {
377
467
  error,
378
468
  reset: resetError
379
469
  } = reactQuery.useMutation({
380
- mutationFn: async ({ amount }) => {
470
+ mutationFn: async ({ params: params2 }) => {
381
471
  if (!spokeProvider) {
382
472
  throw new Error("Spoke provider not found");
383
473
  }
384
- if (!token) {
385
- throw new Error("Token not found");
474
+ if (!params2) {
475
+ throw new Error("Swap Params not found");
386
476
  }
387
- const allowance = await sodax.solver.approve(
388
- token.address,
389
- viem.parseUnits(amount, token.decimals),
477
+ const allowance = await sodax.swaps.approve({
478
+ intentParams: params2,
390
479
  spokeProvider
391
- );
480
+ });
392
481
  if (!allowance.ok) {
393
- throw new Error("Failed to approve tokens");
482
+ throw new Error("Failed to approve input token");
394
483
  }
395
484
  return allowance.ok;
396
485
  },
397
486
  onSuccess: () => {
398
- queryClient.invalidateQueries({ queryKey: ["allowance", token?.address] });
487
+ queryClient.invalidateQueries({ queryKey: ["allowance", params] });
399
488
  }
400
489
  });
401
490
  return {
@@ -405,41 +494,847 @@ function useSwapApprove(token, spokeProvider) {
405
494
  resetError
406
495
  };
407
496
  }
408
- var SodaxProvider = ({ children, testnet = false, config }) => {
409
- const sodax = new sdk.Sodax(config);
410
- const hubChainId = config?.hubProviderConfig?.chainConfig.chain.id;
411
- const hubRpcUrl = config?.hubProviderConfig?.hubRpcUrl;
412
- const hubProvider = React.useMemo(() => {
413
- if (hubChainId && hubRpcUrl) {
414
- const hubChainCfg = sdk.getHubChainConfig(hubChainId);
415
- return new sdk.EvmHubProvider({
416
- hubRpcUrl,
417
- chainConfig: hubChainCfg
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);
505
+ }
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
+ });
566
+ };
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
418
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] });
419
683
  }
420
- return void 0;
421
- }, [hubChainId, hubRpcUrl]);
422
- return /* @__PURE__ */ React__default.default.createElement(SodaxContext.Provider, { value: { sodax, testnet, hubProvider } }, children);
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]
1262
+ );
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);
423
1277
  };
424
1278
 
1279
+ exports.MIGRATION_MODE_BNUSD = MIGRATION_MODE_BNUSD;
1280
+ exports.MIGRATION_MODE_ICX_SODA = MIGRATION_MODE_ICX_SODA;
425
1281
  exports.SodaxProvider = SodaxProvider;
426
- exports.allXTokens = allXTokens;
427
- 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;
428
1292
  exports.useBorrow = useBorrow;
429
- 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;
430
1304
  exports.useHubProvider = useHubProvider;
431
- exports.useHubWalletAddress = useHubWalletAddress;
1305
+ exports.useInstantUnstake = useInstantUnstake;
1306
+ exports.useInstantUnstakeAllowance = useInstantUnstakeAllowance;
1307
+ exports.useInstantUnstakeApprove = useInstantUnstakeApprove;
1308
+ exports.useInstantUnstakeRatio = useInstantUnstakeRatio;
432
1309
  exports.useMMAllowance = useMMAllowance;
433
1310
  exports.useMMApprove = useMMApprove;
1311
+ exports.useMigrate = useMigrate;
1312
+ exports.useMigrationAllowance = useMigrationAllowance;
1313
+ exports.useMigrationApprove = useMigrationApprove;
434
1314
  exports.useQuote = useQuote;
435
1315
  exports.useRepay = useRepay;
1316
+ exports.useRequestTrustline = useRequestTrustline;
436
1317
  exports.useReservesData = useReservesData;
1318
+ exports.useReservesUsdFormat = useReservesUsdFormat;
437
1319
  exports.useSodaxContext = useSodaxContext;
438
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;
439
1327
  exports.useStatus = useStatus;
1328
+ exports.useStellarTrustlineCheck = useStellarTrustlineCheck;
440
1329
  exports.useSupply = useSupply;
1330
+ exports.useSwap = useSwap;
441
1331
  exports.useSwapAllowance = useSwapAllowance;
442
1332
  exports.useSwapApprove = useSwapApprove;
1333
+ exports.useUnstake = useUnstake;
1334
+ exports.useUnstakeAllowance = useUnstakeAllowance;
1335
+ exports.useUnstakeApprove = useUnstakeApprove;
1336
+ exports.useUnstakingInfo = useUnstakingInfo;
1337
+ exports.useUnstakingInfoWithPenalty = useUnstakingInfoWithPenalty;
443
1338
  exports.useUserReservesData = useUserReservesData;
444
1339
  exports.useWithdraw = useWithdraw;
445
1340
  //# sourceMappingURL=index.js.map