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

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