@sodax/dapp-kit 1.5.7-beta → 2.0.0-rc.2

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 (202) hide show
  1. package/README.md +300 -422
  2. package/ai-exported/AGENTS.md +134 -0
  3. package/ai-exported/integration/README.md +49 -0
  4. package/ai-exported/integration/ai-rules.md +79 -0
  5. package/ai-exported/integration/architecture.md +274 -0
  6. package/ai-exported/integration/features/README.md +29 -0
  7. package/ai-exported/integration/features/auxiliary-services.md +169 -0
  8. package/ai-exported/integration/features/bitcoin.md +87 -0
  9. package/ai-exported/integration/features/bridge.md +91 -0
  10. package/ai-exported/integration/features/dex.md +152 -0
  11. package/ai-exported/integration/features/migration.md +118 -0
  12. package/ai-exported/integration/features/money-market.md +116 -0
  13. package/ai-exported/integration/features/staking.md +123 -0
  14. package/ai-exported/integration/features/swap.md +101 -0
  15. package/ai-exported/integration/quickstart.md +187 -0
  16. package/ai-exported/integration/recipes/README.md +136 -0
  17. package/ai-exported/integration/recipes/backend-queries.md +157 -0
  18. package/ai-exported/integration/recipes/bitcoin.md +193 -0
  19. package/ai-exported/integration/recipes/bridge.md +174 -0
  20. package/ai-exported/integration/recipes/dex.md +204 -0
  21. package/ai-exported/integration/recipes/invalidations.md +115 -0
  22. package/ai-exported/integration/recipes/migration.md +212 -0
  23. package/ai-exported/integration/recipes/money-market.md +206 -0
  24. package/ai-exported/integration/recipes/mutation-error-handling.md +118 -0
  25. package/ai-exported/integration/recipes/observability.md +93 -0
  26. package/ai-exported/integration/recipes/setup.md +144 -0
  27. package/ai-exported/integration/recipes/staking.md +202 -0
  28. package/ai-exported/integration/recipes/swap.md +272 -0
  29. package/ai-exported/integration/recipes/wallet-connectivity.md +101 -0
  30. package/ai-exported/integration/reference/README.md +12 -0
  31. package/ai-exported/integration/reference/glossary.md +188 -0
  32. package/ai-exported/integration/reference/hooks-index.md +190 -0
  33. package/ai-exported/integration/reference/public-api.md +110 -0
  34. package/ai-exported/integration/reference/querykey-conventions.md +179 -0
  35. package/ai-exported/migration/README.md +60 -0
  36. package/ai-exported/migration/ai-rules.md +81 -0
  37. package/ai-exported/migration/breaking-changes/hook-signatures.md +233 -0
  38. package/ai-exported/migration/breaking-changes/querykey-conventions.md +108 -0
  39. package/ai-exported/migration/breaking-changes/result-handling.md +211 -0
  40. package/ai-exported/migration/breaking-changes/sdk-leakage.md +165 -0
  41. package/ai-exported/migration/checklist.md +89 -0
  42. package/ai-exported/migration/features/README.md +34 -0
  43. package/ai-exported/migration/features/auxiliary-services.md +114 -0
  44. package/ai-exported/migration/features/bitcoin.md +88 -0
  45. package/ai-exported/migration/features/bridge.md +123 -0
  46. package/ai-exported/migration/features/dex.md +101 -0
  47. package/ai-exported/migration/features/migration.md +120 -0
  48. package/ai-exported/migration/features/money-market.md +97 -0
  49. package/ai-exported/migration/features/staking.md +109 -0
  50. package/ai-exported/migration/features/swap.md +118 -0
  51. package/ai-exported/migration/recipes.md +188 -0
  52. package/ai-exported/migration/reference/README.md +15 -0
  53. package/ai-exported/migration/reference/deleted-hooks.md +110 -0
  54. package/ai-exported/migration/reference/error-shape-crosswalk.md +144 -0
  55. package/ai-exported/migration/reference/renamed-hooks.md +66 -0
  56. package/dist/index.cjs +2642 -0
  57. package/dist/index.cjs.map +1 -0
  58. package/dist/index.d.cts +1550 -0
  59. package/dist/index.d.ts +1020 -2051
  60. package/dist/index.mjs +1581 -1531
  61. package/dist/index.mjs.map +1 -1
  62. package/package.json +21 -11
  63. package/src/contexts/index.ts +0 -3
  64. package/src/hooks/_mutationContract.test.ts +99 -0
  65. package/src/hooks/backend/README.md +2 -2
  66. package/src/hooks/backend/index.ts +13 -13
  67. package/src/hooks/backend/unwrapResult.ts +1 -0
  68. package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +13 -45
  69. package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +29 -59
  70. package/src/hooks/backend/useBackendIntentByHash.ts +21 -47
  71. package/src/hooks/backend/useBackendIntentByTxHash.ts +23 -50
  72. package/src/hooks/backend/useBackendMoneyMarketAsset.ts +21 -54
  73. package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +30 -57
  74. package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +31 -58
  75. package/src/hooks/backend/useBackendMoneyMarketPosition.ts +22 -38
  76. package/src/hooks/backend/useBackendOrderbook.ts +27 -49
  77. package/src/hooks/backend/useBackendSubmitSwapTx.ts +30 -36
  78. package/src/hooks/backend/useBackendSubmitSwapTxStatus.ts +38 -58
  79. package/src/hooks/backend/useBackendUserIntents.ts +25 -63
  80. package/src/hooks/bitcoin/index.ts +9 -8
  81. package/src/hooks/bitcoin/useBitcoinBalance.ts +20 -5
  82. package/src/hooks/bitcoin/useExpiredUtxos.ts +26 -16
  83. package/src/hooks/bitcoin/useFundTradingWallet.ts +33 -30
  84. package/src/hooks/bitcoin/useRadfiAuth.ts +43 -40
  85. package/src/hooks/bitcoin/useRadfiSession.ts +53 -59
  86. package/src/hooks/bitcoin/useRadfiWithdraw.ts +35 -53
  87. package/src/hooks/bitcoin/useRenewUtxos.ts +30 -50
  88. package/src/hooks/bitcoin/useTradingWallet.ts +1 -1
  89. package/src/hooks/bitcoin/useTradingWalletBalance.ts +25 -14
  90. package/src/hooks/bridge/index.ts +5 -5
  91. package/src/hooks/bridge/useBridge.ts +29 -55
  92. package/src/hooks/bridge/useBridgeAllowance.ts +38 -38
  93. package/src/hooks/bridge/useBridgeApprove.ts +32 -57
  94. package/src/hooks/bridge/useGetBridgeableAmount.ts +23 -37
  95. package/src/hooks/bridge/useGetBridgeableTokens.ts +27 -50
  96. package/src/hooks/dex/index.ts +16 -16
  97. package/src/hooks/dex/useClaimRewards.ts +35 -54
  98. package/src/hooks/dex/useCreateDecreaseLiquidityParams.ts +7 -20
  99. package/src/hooks/dex/useCreateDepositParams.ts +7 -21
  100. package/src/hooks/dex/useCreateSupplyLiquidityParams.ts +13 -28
  101. package/src/hooks/dex/useCreateWithdrawParams.ts +7 -20
  102. package/src/hooks/dex/useDecreaseLiquidity.ts +40 -66
  103. package/src/hooks/dex/useDexAllowance.ts +29 -75
  104. package/src/hooks/dex/useDexApprove.ts +32 -43
  105. package/src/hooks/dex/useDexDeposit.ts +42 -49
  106. package/src/hooks/dex/useDexWithdraw.ts +32 -43
  107. package/src/hooks/dex/useLiquidityAmounts.ts +13 -82
  108. package/src/hooks/dex/usePoolBalances.ts +50 -72
  109. package/src/hooks/dex/usePoolData.ts +17 -43
  110. package/src/hooks/dex/usePools.ts +11 -38
  111. package/src/hooks/dex/usePositionInfo.ts +27 -62
  112. package/src/hooks/dex/useSupplyLiquidity.ts +80 -75
  113. package/src/hooks/index.ts +12 -10
  114. package/src/hooks/migrate/index.ts +13 -4
  115. package/src/hooks/migrate/useMigrateBaln.ts +42 -0
  116. package/src/hooks/migrate/useMigrateIcxToSoda.ts +44 -0
  117. package/src/hooks/migrate/useMigratebnUSD.ts +47 -0
  118. package/src/hooks/migrate/useMigrationAllowance.ts +76 -0
  119. package/src/hooks/migrate/useMigrationApprove.ts +66 -0
  120. package/src/hooks/migrate/useRevertMigrateSodaToIcx.ts +39 -0
  121. package/src/hooks/mm/index.ts +14 -12
  122. package/src/hooks/mm/useAToken.ts +25 -41
  123. package/src/hooks/mm/useATokensBalances.ts +29 -60
  124. package/src/hooks/mm/useBorrow.ts +38 -56
  125. package/src/hooks/mm/useMMAllowance.ts +37 -73
  126. package/src/hooks/mm/useMMApprove.ts +36 -43
  127. package/src/hooks/mm/useRepay.ts +33 -53
  128. package/src/hooks/mm/useReservesData.ts +12 -38
  129. package/src/hooks/mm/useReservesHumanized.ts +12 -31
  130. package/src/hooks/mm/useReservesList.ts +11 -31
  131. package/src/hooks/mm/useReservesUsdFormat.ts +15 -35
  132. package/src/hooks/mm/useSupply.ts +45 -51
  133. package/src/hooks/mm/useUserFormattedSummary.ts +32 -84
  134. package/src/hooks/mm/useUserReservesData.ts +27 -77
  135. package/src/hooks/mm/useWithdraw.ts +38 -54
  136. package/src/hooks/partner/index.ts +6 -0
  137. package/src/hooks/partner/useApproveToken.ts +42 -0
  138. package/src/hooks/partner/useFeeClaimSwap.ts +38 -0
  139. package/src/hooks/partner/useFetchAssetsBalances.ts +37 -0
  140. package/src/hooks/partner/useGetAutoSwapPreferences.ts +37 -0
  141. package/src/hooks/partner/useIsTokenApproved.ts +39 -0
  142. package/src/hooks/partner/useSetSwapPreference.ts +50 -0
  143. package/src/hooks/provider/index.ts +1 -2
  144. package/src/hooks/provider/useHubProvider.ts +1 -1
  145. package/src/hooks/recovery/index.ts +2 -0
  146. package/src/hooks/recovery/useHubAssetBalances.ts +43 -0
  147. package/src/hooks/recovery/useWithdrawHubAsset.ts +48 -0
  148. package/src/hooks/shared/index.ts +10 -6
  149. package/src/hooks/shared/types.ts +77 -0
  150. package/src/hooks/shared/unwrapResult.ts +19 -0
  151. package/src/hooks/shared/useDeriveUserWalletAddress.ts +22 -40
  152. package/src/hooks/shared/useEstimateGas.ts +18 -15
  153. package/src/hooks/shared/useGetUserHubWalletAddress.ts +25 -26
  154. package/src/hooks/shared/useRequestTrustline.ts +28 -61
  155. package/src/hooks/shared/useSafeMutation.test.ts +43 -0
  156. package/src/hooks/shared/useSafeMutation.ts +68 -0
  157. package/src/hooks/shared/useSodaxContext.ts +1 -1
  158. package/src/hooks/shared/useStellarTrustlineCheck.ts +30 -64
  159. package/src/hooks/shared/useXBalances.test.ts +113 -0
  160. package/src/hooks/shared/useXBalances.ts +61 -0
  161. package/src/hooks/staking/index.ts +18 -18
  162. package/src/hooks/staking/useCancelUnstake.ts +30 -41
  163. package/src/hooks/staking/useClaim.ts +27 -36
  164. package/src/hooks/staking/useConvertedAssets.ts +24 -34
  165. package/src/hooks/staking/useInstantUnstake.ts +33 -40
  166. package/src/hooks/staking/useInstantUnstakeAllowance.ts +37 -45
  167. package/src/hooks/staking/useInstantUnstakeApprove.ts +42 -42
  168. package/src/hooks/staking/useInstantUnstakeRatio.ts +24 -41
  169. package/src/hooks/staking/useStake.ts +32 -37
  170. package/src/hooks/staking/useStakeAllowance.ts +30 -43
  171. package/src/hooks/staking/useStakeApprove.ts +40 -40
  172. package/src/hooks/staking/useStakeRatio.ts +24 -40
  173. package/src/hooks/staking/useStakingConfig.ts +14 -27
  174. package/src/hooks/staking/useStakingInfo.ts +30 -38
  175. package/src/hooks/staking/useUnstake.ts +29 -43
  176. package/src/hooks/staking/useUnstakeAllowance.ts +37 -44
  177. package/src/hooks/staking/useUnstakeApprove.ts +40 -43
  178. package/src/hooks/staking/useUnstakingInfo.ts +29 -41
  179. package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +31 -47
  180. package/src/hooks/swap/index.ts +8 -8
  181. package/src/hooks/swap/useCancelLimitOrder.ts +24 -41
  182. package/src/hooks/swap/useCancelSwap.ts +24 -33
  183. package/src/hooks/swap/useCreateLimitOrder.ts +29 -62
  184. package/src/hooks/swap/useQuote.ts +17 -43
  185. package/src/hooks/swap/useStatus.ts +22 -29
  186. package/src/hooks/swap/useSwap.ts +31 -49
  187. package/src/hooks/swap/useSwapAllowance.ts +38 -35
  188. package/src/hooks/swap/useSwapApprove.ts +48 -57
  189. package/src/index.ts +5 -3
  190. package/src/providers/SodaxProvider.tsx +17 -11
  191. package/src/providers/createSodaxQueryClient.ts +96 -0
  192. package/src/providers/index.ts +2 -1
  193. package/src/utils/dex-utils.ts +27 -5
  194. package/src/utils/index.ts +1 -1
  195. package/dist/index.d.mts +0 -2581
  196. package/dist/index.js +0 -2574
  197. package/dist/index.js.map +0 -1
  198. package/src/hooks/migrate/types.ts +0 -15
  199. package/src/hooks/migrate/useMigrate.tsx +0 -110
  200. package/src/hooks/migrate/useMigrationAllowance.tsx +0 -79
  201. package/src/hooks/migrate/useMigrationApprove.tsx +0 -129
  202. package/src/hooks/provider/useSpokeProvider.ts +0 -172
@@ -1,70 +1,37 @@
1
- // packages/dapp-kit/src/hooks/backend/useMoneyMarketAsset.ts
2
- import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
2
  import type { MoneyMarketAsset } from '@sodax/sdk';
4
- import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from './unwrapResult.js';
5
+ import type { ReadHookParams } from '../shared/types.js';
5
6
 
6
- export type UseBackendMoneyMarketAssetParams = {
7
- params: {
7
+ export type UseBackendMoneyMarketAssetParams = ReadHookParams<
8
+ MoneyMarketAsset | undefined,
9
+ {
8
10
  reserveAddress: string | undefined;
9
- };
10
- queryOptions?: UseQueryOptions<MoneyMarketAsset | undefined, Error>;
11
- };
11
+ }
12
+ >;
12
13
 
13
14
  /**
14
15
  * React hook to fetch a specific money market asset from the backend API.
15
16
  *
16
- * @param params - The hook input parameter object (may be undefined):
17
- * - `params`: An object containing:
18
- * - `reserveAddress` (string | undefined): Reserve contract address to fetch asset details. Disables query if undefined or empty.
19
- * - `queryOptions` (optional): React Query options for advanced configuration (e.g. caching, staleTime, retry, etc.).
20
- *
21
- * @returns A React Query result object: {@link UseQueryResult} for {@link MoneyMarketAsset} or `undefined` on error or if disabled,
22
- * including:
23
- * - `data`: The money market asset (when available) or `undefined`.
24
- * - `isLoading`: Whether the query is running.
25
- * - `error`: An error encountered by the query (if any).
26
- * - `refetch`: Function to manually refetch the asset.
27
- *
28
17
  * @example
29
- * const { data: asset, isLoading, error } = useBackendMoneyMarketAsset({
30
- * params: { reserveAddress: '0xabc...' },
31
- * });
32
- * if (isLoading) return <div>Loading asset...</div>;
33
- * if (error) return <div>Error: {error.message}</div>;
34
- * if (asset) {
35
- * console.log('Asset symbol:', asset.symbol);
36
- * console.log('Liquidity rate:', asset.liquidityRate);
37
- * console.log('Variable borrow rate:', asset.variableBorrowRate);
38
- * }
39
- *
40
- * @remarks
41
- * - Query is disabled if `params`, `params.params`, or `params.params.reserveAddress` is missing or empty.
42
- * - Uses React Query for caching and background-state management.
43
- * - Loading and error handling are managed automatically.
18
+ * const { data: asset } = useBackendMoneyMarketAsset({ params: { reserveAddress: '0xabc...' } });
44
19
  */
45
- export const useBackendMoneyMarketAsset = (
46
- params: UseBackendMoneyMarketAssetParams | undefined,
47
- ): UseQueryResult<MoneyMarketAsset | undefined, Error> => {
20
+ export const useBackendMoneyMarketAsset = ({
21
+ params,
22
+ queryOptions,
23
+ }: UseBackendMoneyMarketAssetParams = {}): UseQueryResult<MoneyMarketAsset | undefined, Error> => {
48
24
  const { sodax } = useSodaxContext();
49
-
50
- const defaultQueryOptions = {
51
- queryKey: ['api', 'mm', 'asset', params?.params?.reserveAddress],
52
- enabled: !!params?.params?.reserveAddress && params?.params?.reserveAddress.length > 0,
53
- retry: 3,
54
- };
55
- const queryOptions = {
56
- ...defaultQueryOptions,
57
- ...params?.queryOptions,
58
- };
25
+ const reserveAddress = params?.reserveAddress;
59
26
 
60
27
  return useQuery({
61
- ...queryOptions,
28
+ queryKey: ['backend', 'mm', 'asset', reserveAddress],
62
29
  queryFn: async (): Promise<MoneyMarketAsset | undefined> => {
63
- if (!params?.params?.reserveAddress) {
64
- return undefined;
65
- }
66
-
67
- return sodax.backendApi.getMoneyMarketAsset(params.params.reserveAddress);
30
+ if (!reserveAddress) return undefined;
31
+ return unwrapResult(await sodax.backendApi.getMoneyMarketAsset(reserveAddress));
68
32
  },
33
+ enabled: !!reserveAddress && reserveAddress.length > 0,
34
+ retry: 3,
35
+ ...queryOptions,
69
36
  });
70
37
  };
@@ -1,76 +1,49 @@
1
- // packages/dapp-kit/src/hooks/backend/useMoneyMarketAssetBorrowers.ts
2
- import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
2
  import type { MoneyMarketAssetBorrowers } from '@sodax/sdk';
4
- import { useSodaxContext } from '../shared/useSodaxContext';
5
- import type { BackendPaginationParams } from './types';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from './unwrapResult.js';
5
+ import type { BackendPaginationParams } from './types.js';
6
+ import type { ReadHookParams } from '../shared/types.js';
6
7
 
7
- export type UseBackendMoneyMarketAssetBorrowersParams = {
8
- params: {
8
+ export type UseBackendMoneyMarketAssetBorrowersParams = ReadHookParams<
9
+ MoneyMarketAssetBorrowers | undefined,
10
+ {
9
11
  reserveAddress: string | undefined;
10
- };
11
- pagination: BackendPaginationParams;
12
- queryOptions?: UseQueryOptions<MoneyMarketAssetBorrowers | undefined, Error>;
13
- };
12
+ pagination: BackendPaginationParams;
13
+ }
14
+ >;
14
15
 
15
16
  /**
16
17
  * React hook for fetching borrowers for a specific money market asset from the backend API with pagination.
17
18
  *
18
- * @param {UseBackendMoneyMarketAssetBorrowersParams | undefined} params - Query parameters:
19
- * - `params`: Object containing:
20
- * - `reserveAddress`: Reserve contract address for which to fetch borrowers, or `undefined` to disable query.
21
- * - `pagination`: Pagination controls with `offset` and `limit` (both required as strings).
22
- * - `queryOptions` (optional): React Query options to override defaults (e.g. `staleTime`, `enabled`, etc.).
23
- *
24
- * @returns {UseQueryResult<MoneyMarketAssetBorrowers | undefined, Error>} React Query result object including:
25
- * - `data`: The money market asset borrowers data, or `undefined` if not available.
26
- * - `isLoading`: Boolean indicating whether the query is loading.
27
- * - `error`: An Error instance if the request failed.
28
- * - `refetch`: Function to manually trigger a data refresh.
29
- *
30
19
  * @example
31
- * const { data: borrowers, isLoading, error } = useBackendMoneyMarketAssetBorrowers({
32
- * params: { reserveAddress: '0xabc...' },
33
- * pagination: { offset: '0', limit: '20' }
20
+ * const { data: borrowers } = useBackendMoneyMarketAssetBorrowers({
21
+ * params: { reserveAddress: '0xabc...', pagination: { offset: '0', limit: '20' } },
34
22
  * });
35
- *
36
- * if (isLoading) return <div>Loading borrowers...</div>;
37
- * if (error) return <div>Error: {error.message}</div>;
38
- * if (borrowers) {
39
- * console.log('Total borrowers:', borrowers.total);
40
- * console.log('Borrowers:', borrowers.borrowers);
41
- * }
42
- *
43
- * @remarks
44
- * - The query is disabled if `reserveAddress`, `offset`, or `limit` are not provided.
45
- * - Uses React Query for caching, retries, and auto error/loading management.
46
- * - Pagination is handled via `pagination.offset` and `pagination.limit`.
47
23
  */
48
- export const useBackendMoneyMarketAssetBorrowers = (
49
- params: UseBackendMoneyMarketAssetBorrowersParams | undefined,
50
- ): UseQueryResult<MoneyMarketAssetBorrowers | undefined, Error> => {
24
+ export const useBackendMoneyMarketAssetBorrowers = ({
25
+ params,
26
+ queryOptions,
27
+ }: UseBackendMoneyMarketAssetBorrowersParams = {}): UseQueryResult<MoneyMarketAssetBorrowers | undefined, Error> => {
51
28
  const { sodax } = useSodaxContext();
52
-
53
- const defaultQueryOptions = {
54
- queryKey: ['api', 'mm', 'asset', 'borrowers', params],
55
- enabled: !!params?.params?.reserveAddress && !!params.pagination.offset && !!params.pagination.limit,
56
- retry: 3,
57
- };
58
- const queryOptions = {
59
- ...defaultQueryOptions,
60
- ...params?.queryOptions,
61
- };
29
+ const reserveAddress = params?.reserveAddress;
30
+ const pagination = params?.pagination;
62
31
 
63
32
  return useQuery({
64
- ...queryOptions,
33
+ queryKey: ['backend', 'mm', 'asset', 'borrowers', reserveAddress, pagination],
65
34
  queryFn: async (): Promise<MoneyMarketAssetBorrowers | undefined> => {
66
- if (!params?.params?.reserveAddress || !params.pagination.offset || !params.pagination.limit) {
35
+ if (!reserveAddress || !pagination?.offset || !pagination?.limit) {
67
36
  return undefined;
68
37
  }
69
-
70
- return sodax.backendApi.getMoneyMarketAssetBorrowers(params.params.reserveAddress, {
71
- offset: params.pagination.offset,
72
- limit: params.pagination.limit,
73
- });
38
+ return unwrapResult(
39
+ await sodax.backendApi.getMoneyMarketAssetBorrowers(reserveAddress, {
40
+ offset: pagination.offset,
41
+ limit: pagination.limit,
42
+ }),
43
+ );
74
44
  },
45
+ enabled: !!reserveAddress && !!pagination?.offset && !!pagination?.limit,
46
+ retry: 3,
47
+ ...queryOptions,
75
48
  });
76
49
  };
@@ -1,76 +1,49 @@
1
- // packages/dapp-kit/src/hooks/backend/useMoneyMarketAssetSuppliers.ts
2
- import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
2
  import type { MoneyMarketAssetSuppliers } from '@sodax/sdk';
4
- import { useSodaxContext } from '../shared/useSodaxContext';
5
- import type { BackendPaginationParams } from './types';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from './unwrapResult.js';
5
+ import type { BackendPaginationParams } from './types.js';
6
+ import type { ReadHookParams } from '../shared/types.js';
6
7
 
7
- export type UseBackendMoneyMarketAssetSuppliersParams = {
8
- params: {
8
+ export type UseBackendMoneyMarketAssetSuppliersParams = ReadHookParams<
9
+ MoneyMarketAssetSuppliers | undefined,
10
+ {
9
11
  reserveAddress: string | undefined;
10
- };
11
- pagination: BackendPaginationParams;
12
- queryOptions?: UseQueryOptions<MoneyMarketAssetSuppliers | undefined, Error>;
13
- };
12
+ pagination: BackendPaginationParams;
13
+ }
14
+ >;
14
15
 
15
16
  /**
16
- * React hook for fetching suppliers for a specific money market asset from the backend API, with pagination support.
17
- *
18
- * @param {UseBackendMoneyMarketAssetSuppliersParams | undefined} params - Hook parameters:
19
- * - `params`: Object containing:
20
- * - `reserveAddress`: The reserve contract address to query, or undefined to disable the query.
21
- * - `pagination`: Backend pagination controls (`offset` and `limit` as strings).
22
- * - `queryOptions` (optional): React Query options to override defaults.
23
- *
24
- * @returns {UseQueryResult<MoneyMarketAssetSuppliers | undefined, Error>} - Query result object with:
25
- * - `data`: The asset suppliers data when available.
26
- * - `isLoading`: Indicates if the request is in progress.
27
- * - `error`: Error object if the request failed.
28
- * - `refetch`: Function to trigger a manual data refresh.
17
+ * React hook for fetching suppliers for a specific money market asset from the backend API.
29
18
  *
30
19
  * @example
31
- * const { data: suppliers, isLoading, error } = useBackendMoneyMarketAssetSuppliers({
32
- * params: { reserveAddress: '0xabc...' },
33
- * pagination: { offset: '0', limit: '20' }
20
+ * const { data: suppliers } = useBackendMoneyMarketAssetSuppliers({
21
+ * params: { reserveAddress: '0xabc...', pagination: { offset: '0', limit: '20' } },
34
22
  * });
35
- *
36
- * if (isLoading) return <div>Loading suppliers...</div>;
37
- * if (error) return <div>Error: {error.message}</div>;
38
- * if (suppliers) {
39
- * console.log('Total suppliers:', suppliers.total);
40
- * console.log('Suppliers:', suppliers.suppliers);
41
- * }
42
- *
43
- * @remarks
44
- * - The query is disabled if `reserveAddress`, `offset`, or `limit` are not provided.
45
- * - Uses React Query for efficient caching, automatic retries, and error/loading handling.
46
- * - Pagination is handled via `pagination.offset` and `pagination.limit`.
47
23
  */
48
- export const useBackendMoneyMarketAssetSuppliers = (
49
- params: UseBackendMoneyMarketAssetSuppliersParams | undefined,
50
- ): UseQueryResult<MoneyMarketAssetSuppliers | undefined, Error> => {
24
+ export const useBackendMoneyMarketAssetSuppliers = ({
25
+ params,
26
+ queryOptions,
27
+ }: UseBackendMoneyMarketAssetSuppliersParams = {}): UseQueryResult<MoneyMarketAssetSuppliers | undefined, Error> => {
51
28
  const { sodax } = useSodaxContext();
52
-
53
- const defaultQueryOptions = {
54
- queryKey: ['api', 'mm', 'asset', 'suppliers', params],
55
- enabled: !!params?.params?.reserveAddress && !!params.pagination.offset && !!params.pagination.limit,
56
- retry: 3,
57
- };
58
- const queryOptions = {
59
- ...defaultQueryOptions,
60
- ...params?.queryOptions,
61
- };
29
+ const reserveAddress = params?.reserveAddress;
30
+ const pagination = params?.pagination;
62
31
 
63
32
  return useQuery({
64
- ...queryOptions,
33
+ queryKey: ['backend', 'mm', 'asset', 'suppliers', reserveAddress, pagination],
65
34
  queryFn: async (): Promise<MoneyMarketAssetSuppliers | undefined> => {
66
- if (!params?.params?.reserveAddress || !params.pagination.offset || !params.pagination.limit) {
35
+ if (!reserveAddress || !pagination?.offset || !pagination?.limit) {
67
36
  return undefined;
68
37
  }
69
-
70
- return sodax.backendApi.getMoneyMarketAssetSuppliers(params.params.reserveAddress, {
71
- offset: params.pagination.offset,
72
- limit: params.pagination.limit,
73
- });
38
+ return unwrapResult(
39
+ await sodax.backendApi.getMoneyMarketAssetSuppliers(reserveAddress, {
40
+ offset: pagination.offset,
41
+ limit: pagination.limit,
42
+ }),
43
+ );
74
44
  },
45
+ enabled: !!reserveAddress && !!pagination?.offset && !!pagination?.limit,
46
+ retry: 3,
47
+ ...queryOptions,
75
48
  });
76
49
  };
@@ -1,53 +1,37 @@
1
- import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
2
  import type { MoneyMarketPosition } from '@sodax/sdk';
3
- import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from './unwrapResult.js';
5
+ import type { ReadHookParams } from '../shared/types.js';
4
6
 
5
- export type UseBackendMoneyMarketPositionParams = {
6
- userAddress: string | undefined;
7
- queryOptions?: UseQueryOptions<MoneyMarketPosition | undefined, Error>;
8
- };
7
+ export type UseBackendMoneyMarketPositionParams = ReadHookParams<
8
+ MoneyMarketPosition | undefined,
9
+ {
10
+ userAddress: string | undefined;
11
+ }
12
+ >;
9
13
 
10
14
  /**
11
15
  * React hook for fetching a user's money market position from the backend API.
12
16
  *
13
- * @param {UseBackendMoneyMarketPositionParams | undefined} params - Parameters object:
14
- * - userAddress: The user's wallet address to fetch positions for. If undefined or empty, the query is disabled.
15
- * - queryOptions: (Optional) React Query options to customize behavior (e.g., staleTime, enabled).
16
- *
17
- * @returns {UseQueryResult<MoneyMarketPosition | undefined, Error>} - React Query result object with:
18
- * - data: The user's money market position data, or undefined if not available.
19
- * - isLoading: Loading state.
20
- * - error: An Error instance if fetching failed.
21
- * - refetch: Function to manually trigger a refetch.
22
- *
23
17
  * @example
24
- * const { data, isLoading, error } = useBackendMoneyMarketPosition({
25
- * userAddress: '0xabc...',
26
- * queryOptions: { staleTime: 60000 },
27
- * });
18
+ * const { data } = useBackendMoneyMarketPosition({ params: { userAddress: '0xabc...' } });
28
19
  */
29
- export const useBackendMoneyMarketPosition = (
30
- params: UseBackendMoneyMarketPositionParams | undefined,
31
- ): UseQueryResult<MoneyMarketPosition | undefined, Error> => {
20
+ export const useBackendMoneyMarketPosition = ({
21
+ params,
22
+ queryOptions,
23
+ }: UseBackendMoneyMarketPositionParams = {}): UseQueryResult<MoneyMarketPosition | undefined, Error> => {
32
24
  const { sodax } = useSodaxContext();
33
-
34
- const defaultQueryOptions = {
35
- queryKey: ['api', 'mm', 'position', params?.userAddress],
36
- enabled: !!params?.userAddress && params?.userAddress.length > 0,
37
- retry: 3,
38
- };
39
- const queryOptions = {
40
- ...defaultQueryOptions,
41
- ...params?.queryOptions,
42
- };
25
+ const userAddress = params?.userAddress;
43
26
 
44
27
  return useQuery({
45
- ...queryOptions,
28
+ queryKey: ['backend', 'mm', 'position', userAddress],
46
29
  queryFn: async (): Promise<MoneyMarketPosition | undefined> => {
47
- if (!params?.userAddress) {
48
- return undefined;
49
- }
50
- return sodax.backendApi.getMoneyMarketPosition(params.userAddress);
30
+ if (!userAddress) return undefined;
31
+ return unwrapResult(await sodax.backendApi.getMoneyMarketPosition(userAddress));
51
32
  },
33
+ enabled: !!userAddress && userAddress.length > 0,
34
+ retry: 3,
35
+ ...queryOptions,
52
36
  });
53
37
  };
@@ -1,63 +1,41 @@
1
- import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
2
  import type { OrderbookResponse } from '@sodax/sdk';
3
- import { useSodaxContext } from '../shared/useSodaxContext';
4
- import type { BackendPaginationParams } from './types';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from './unwrapResult.js';
5
+ import type { BackendPaginationParams } from './types.js';
6
+ import type { ReadHookParams } from '../shared/types.js';
5
7
 
6
- export type UseBackendOrderbookParams = {
7
- queryOptions?: UseQueryOptions<OrderbookResponse | undefined, Error>;
8
- pagination?: BackendPaginationParams;
9
- };
8
+ export type UseBackendOrderbookParams = ReadHookParams<
9
+ OrderbookResponse,
10
+ {
11
+ pagination: BackendPaginationParams;
12
+ }
13
+ >;
10
14
 
11
15
  /**
12
16
  * Hook for fetching the solver orderbook from the backend API.
13
17
  *
14
- * @param {UseBackendOrderbookParams | undefined} params - Optional parameters:
15
- * - `pagination`: Pagination configuration (see `BackendPaginationParams`), including
16
- * `offset` and `limit` (both required for fetch to be enabled).
17
- * - `queryOptions`: Optional React Query options to override default behavior.
18
- *
19
- * @returns {UseQueryResult<OrderbookResponse | undefined, Error>} React Query result object:
20
- * - `data`: The orderbook response, or undefined if unavailable.
21
- * - `isLoading`: Loading state.
22
- * - `error`: Error instance if the query failed.
23
- * - `refetch`: Function to re-trigger the query.
24
- *
25
18
  * @example
26
- * const { data, isLoading, error } = useBackendOrderbook({
27
- * pagination: { offset: '0', limit: '10' },
28
- * queryOptions: { staleTime: 60000 },
29
- * });
30
- *
31
- * @remarks
32
- * - Query is disabled if `params?.pagination`, `offset`, or `limit` are missing/empty.
33
- * - Caches and manages server state using React Query.
34
- * - Default `staleTime` is 30 seconds to support near-real-time updates.
19
+ * const { data } = useBackendOrderbook({ params: { pagination: { offset: '0', limit: '10' } } });
35
20
  */
36
- export const useBackendOrderbook = (
37
- params: UseBackendOrderbookParams | undefined,
38
- ): UseQueryResult<OrderbookResponse | undefined> => {
21
+ export const useBackendOrderbook = ({
22
+ params,
23
+ queryOptions,
24
+ }: UseBackendOrderbookParams = {}): UseQueryResult<OrderbookResponse> => {
39
25
  const { sodax } = useSodaxContext();
26
+ const pagination = params?.pagination;
40
27
 
41
- const defaultQueryOptions = {
42
- queryKey: ['api', 'solver', 'orderbook', params?.pagination?.offset, params?.pagination?.limit],
43
- enabled: !!params?.pagination && !!params?.pagination.offset && !!params?.pagination.limit,
44
- staleTime: 30 * 1000, // 30 seconds for real-time data
45
- retry: 3,
46
- };
47
-
48
- const queryOptions = {
49
- ...defaultQueryOptions,
50
- ...params?.queryOptions, // override default query options if provided
51
- };
52
-
53
- return useQuery({
54
- ...queryOptions,
55
- queryFn: async (): Promise<OrderbookResponse | undefined> => {
56
- if (!params?.pagination || !params?.pagination.offset || !params?.pagination.limit) {
57
- return undefined;
28
+ return useQuery<OrderbookResponse, Error>({
29
+ queryKey: ['backend', 'orderbook', pagination?.offset, pagination?.limit],
30
+ queryFn: async (): Promise<OrderbookResponse> => {
31
+ if (!pagination?.offset || !pagination?.limit) {
32
+ throw new Error('Pagination offset and limit are required');
58
33
  }
59
-
60
- return sodax.backendApi.getOrderbook(params.pagination);
34
+ return unwrapResult(await sodax.backendApi.getOrderbook(pagination));
61
35
  },
36
+ enabled: !!pagination?.offset && !!pagination?.limit,
37
+ staleTime: 30 * 1000,
38
+ retry: 3,
39
+ ...queryOptions,
62
40
  });
63
41
  };
@@ -1,55 +1,49 @@
1
- import { useMutation, type UseMutationOptions, type UseMutationResult } from '@tanstack/react-query';
2
- import type { SubmitSwapTxRequest, SubmitSwapTxResponse } from '@sodax/types';
3
- import type { RequestOverrideConfig } from '@sodax/sdk';
4
- import { useSodaxContext } from '../shared/useSodaxContext';
1
+ // packages/dapp-kit/src/hooks/backend/useBackendSubmitSwapTx.ts
2
+ import type { RequestOverrideConfig, SubmitSwapTxRequest, SubmitSwapTxResponse } from '@sodax/sdk';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from './unwrapResult.js';
5
+ import type { MutationHookParams } from '../shared/types.js';
6
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
5
7
 
6
- export type UseBackendSubmitSwapTxParams = {
8
+ /**
9
+ * Mutation variables for {@link useBackendSubmitSwapTx}. The per-request `apiConfig` override
10
+ * (e.g. base URL) belongs here rather than at the hook level — different submissions in the same
11
+ * component can target different endpoints without re-rendering.
12
+ */
13
+ export type UseBackendSubmitSwapTxVars = {
14
+ request: SubmitSwapTxRequest;
7
15
  apiConfig?: RequestOverrideConfig;
8
- mutationOptions?: UseMutationOptions<SubmitSwapTxResponse, Error, SubmitSwapTxRequest>;
9
16
  };
10
17
 
11
18
  /**
12
- * React hook for submitting a swap transaction to be processed by the backend
13
- * (relay, post execution to solver, etc.).
14
- *
15
- * @param {UseBackendSubmitSwapTxParams | undefined} params - Optional parameters:
16
- * - `mutationOptions`: React Query mutation options to customize behavior (e.g., onSuccess, onError, retry).
19
+ * React hook for submitting a swap transaction to be processed by the backend (relay, post
20
+ * execution to solver, etc.).
17
21
  *
18
- * @returns {UseMutationResult<SubmitSwapTxResponse, Error, SubmitSwapTxRequest>} React Query mutation result:
19
- * - `mutate` / `mutateAsync`: Functions to trigger the submission.
20
- * - `data`: The submit response on success.
21
- * - `isPending`: Loading state.
22
- * - `error`: Error instance if the mutation failed.
22
+ * Pure mutation: pass `{ request, apiConfig? }` to `mutate({...})`. Default `retry: 3` is applied
23
+ * at the hook level consumers can override via `mutationOptions.retry`.
23
24
  *
24
25
  * @example
25
26
  * const { mutateAsync: submitSwapTx, isPending, error } = useBackendSubmitSwapTx();
26
27
  *
27
28
  * const result = await submitSwapTx({
28
- * txHash: '0x123...',
29
- * srcChainId: '1',
30
- * walletAddress: '0xabc...',
31
- * intent: { ... },
32
- * relayData: '0x...',
29
+ * request: { txHash: '0x123...', srcChainKey: 'sonic', walletAddress: '0xabc...', intent: { ... }, relayData: '0x...' },
30
+ * apiConfig: { baseURL: 'https://...' },
33
31
  * });
34
32
  */
35
- export const useBackendSubmitSwapTx = (
36
- params?: UseBackendSubmitSwapTxParams,
37
- ): UseMutationResult<SubmitSwapTxResponse, Error, SubmitSwapTxRequest> => {
33
+ export const useBackendSubmitSwapTx = ({
34
+ mutationOptions,
35
+ }: MutationHookParams<SubmitSwapTxResponse, UseBackendSubmitSwapTxVars> = {}): SafeUseMutationResult<
36
+ SubmitSwapTxResponse,
37
+ Error,
38
+ UseBackendSubmitSwapTxVars
39
+ > => {
38
40
  const { sodax } = useSodaxContext();
39
41
 
40
- const defaultMutationOptions = {
42
+ return useSafeMutation<SubmitSwapTxResponse, Error, UseBackendSubmitSwapTxVars>({
43
+ mutationKey: ['backend', 'submitSwapTx'],
41
44
  retry: 3,
42
- };
43
-
44
- const mutationOptions = {
45
- ...defaultMutationOptions,
46
- ...params?.mutationOptions,
47
- };
48
-
49
- return useMutation({
50
45
  ...mutationOptions,
51
- mutationFn: async (request: SubmitSwapTxRequest): Promise<SubmitSwapTxResponse> => {
52
- return sodax.backendApi.submitSwapTx(request, params?.apiConfig);
53
- },
46
+ mutationFn: async ({ request, apiConfig }): Promise<SubmitSwapTxResponse> =>
47
+ unwrapResult(await sodax.backendApi.submitSwapTx(request, apiConfig)),
54
48
  });
55
49
  };