@sodax/dapp-kit 1.0.1-beta → 1.0.2-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +130 -39
  2. package/dist/index.d.mts +541 -347
  3. package/dist/index.d.ts +541 -347
  4. package/dist/index.js +317 -176
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +315 -178
  7. package/dist/index.mjs.map +1 -1
  8. package/package.json +3 -3
  9. package/src/hooks/backend/README.md +148 -49
  10. package/src/hooks/backend/index.ts +2 -0
  11. package/src/hooks/backend/types.ts +4 -0
  12. package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +31 -20
  13. package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +25 -7
  14. package/src/hooks/backend/useBackendIntentByHash.ts +36 -26
  15. package/src/hooks/backend/useBackendIntentByTxHash.ts +41 -29
  16. package/src/hooks/backend/useBackendMoneyMarketAsset.ts +40 -27
  17. package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +45 -36
  18. package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +45 -36
  19. package/src/hooks/backend/useBackendMoneyMarketPosition.ts +34 -37
  20. package/src/hooks/backend/useBackendOrderbook.ts +38 -38
  21. package/src/hooks/backend/useBackendUserIntents.ts +81 -0
  22. package/src/hooks/mm/index.ts +1 -0
  23. package/src/hooks/mm/useAToken.ts +37 -20
  24. package/src/hooks/mm/useBorrow.ts +36 -36
  25. package/src/hooks/mm/useMMAllowance.ts +33 -24
  26. package/src/hooks/mm/useMMApprove.ts +43 -48
  27. package/src/hooks/mm/useRepay.ts +32 -36
  28. package/src/hooks/mm/useReservesData.ts +35 -16
  29. package/src/hooks/mm/useReservesHumanized.ts +15 -3
  30. package/src/hooks/mm/useReservesList.ts +28 -15
  31. package/src/hooks/mm/useReservesUsdFormat.ts +30 -21
  32. package/src/hooks/mm/useSupply.ts +34 -36
  33. package/src/hooks/mm/useUserFormattedSummary.ts +42 -20
  34. package/src/hooks/mm/useUserReservesData.ts +34 -19
  35. package/src/hooks/mm/useWithdraw.ts +33 -35
  36. package/src/hooks/swap/index.ts +2 -0
  37. package/src/hooks/swap/useCancelLimitOrder.ts +53 -0
  38. package/src/hooks/swap/useCreateLimitOrder.ts +72 -0
  39. package/src/hooks/swap/useSwapAllowance.ts +7 -7
  40. package/src/hooks/swap/useSwapApprove.ts +6 -6
@@ -1,27 +1,34 @@
1
1
  // packages/dapp-kit/src/hooks/backend/useMoneyMarketAsset.ts
2
- import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
3
3
  import type { MoneyMarketAsset } from '@sodax/sdk';
4
4
  import { useSodaxContext } from '../shared/useSodaxContext';
5
5
 
6
+ export type UseBackendMoneyMarketAssetParams = {
7
+ params: {
8
+ reserveAddress: string | undefined;
9
+ };
10
+ queryOptions?: UseQueryOptions<MoneyMarketAsset | undefined, Error>;
11
+ };
12
+
6
13
  /**
7
- * Hook for fetching specific money market asset details from the backend API.
14
+ * React hook to fetch a specific money market asset from the backend API.
8
15
  *
9
- * This hook provides access to detailed information for a specific money market asset,
10
- * including reserve information, liquidity rates, borrow rates, and market statistics.
11
- * The data is automatically fetched and cached using React Query.
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.).
12
20
  *
13
- * @param {string | undefined} reserveAddress - The reserve contract address. If undefined, the query will be disabled.
14
- *
15
- * @returns {UseQueryResult<MoneyMarketAsset | undefined>} A query result object containing:
16
- * - data: The money market asset data when available
17
- * - isLoading: Boolean indicating if the request is in progress
18
- * - error: Error object if the request failed
19
- * - refetch: Function to manually trigger a data refresh
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.
20
27
  *
21
28
  * @example
22
- * ```typescript
23
- * const { data: asset, isLoading, error } = useMoneyMarketAsset('0xabc...');
24
- *
29
+ * const { data: asset, isLoading, error } = useBackendMoneyMarketAsset({
30
+ * params: { reserveAddress: '0xabc...' },
31
+ * });
25
32
  * if (isLoading) return <div>Loading asset...</div>;
26
33
  * if (error) return <div>Error: {error.message}</div>;
27
34
  * if (asset) {
@@ -29,29 +36,35 @@ import { useSodaxContext } from '../shared/useSodaxContext';
29
36
  * console.log('Liquidity rate:', asset.liquidityRate);
30
37
  * console.log('Variable borrow rate:', asset.variableBorrowRate);
31
38
  * }
32
- * ```
33
39
  *
34
40
  * @remarks
35
- * - The query is disabled when reserveAddress is undefined or empty
36
- * - Uses React Query for efficient caching and state management
37
- * - Automatically handles error states and loading indicators
38
- * - Returns comprehensive asset information for the specified reserve
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.
39
44
  */
40
45
  export const useBackendMoneyMarketAsset = (
41
- reserveAddress: string | undefined,
42
- ): UseQueryResult<MoneyMarketAsset | undefined> => {
46
+ params: UseBackendMoneyMarketAssetParams | undefined,
47
+ ): UseQueryResult<MoneyMarketAsset | undefined, Error> => {
43
48
  const { sodax } = useSodaxContext();
44
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
+ };
59
+
45
60
  return useQuery({
46
- queryKey: ['backend', 'moneymarket', 'asset', reserveAddress],
61
+ ...queryOptions,
47
62
  queryFn: async (): Promise<MoneyMarketAsset | undefined> => {
48
- if (!reserveAddress) {
63
+ if (!params?.params?.reserveAddress) {
49
64
  return undefined;
50
65
  }
51
66
 
52
- return sodax.backendApi.getMoneyMarketAsset(reserveAddress);
67
+ return sodax.backendApi.getMoneyMarketAsset(params.params.reserveAddress);
53
68
  },
54
- enabled: !!reserveAddress && reserveAddress.length > 0,
55
- retry: 3,
56
69
  });
57
70
  };
@@ -1,31 +1,36 @@
1
1
  // packages/dapp-kit/src/hooks/backend/useMoneyMarketAssetBorrowers.ts
2
- import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
3
3
  import type { MoneyMarketAssetBorrowers } from '@sodax/sdk';
4
4
  import { useSodaxContext } from '../shared/useSodaxContext';
5
+ import type { BackendPaginationParams } from './types';
6
+
7
+ export type UseBackendMoneyMarketAssetBorrowersParams = {
8
+ params: {
9
+ reserveAddress: string | undefined;
10
+ };
11
+ pagination: BackendPaginationParams;
12
+ queryOptions?: UseQueryOptions<MoneyMarketAssetBorrowers | undefined, Error>;
13
+ };
5
14
 
6
15
  /**
7
- * Hook for fetching borrowers for a specific money market asset from the backend API.
8
- *
9
- * This hook provides access to the list of borrowers for a specific money market asset,
10
- * with pagination support. The data is automatically fetched and cached using React Query.
16
+ * React hook for fetching borrowers for a specific money market asset from the backend API with pagination.
11
17
  *
12
- * @param {Object} params - Parameters for fetching asset borrowers
13
- * @param {string | undefined} params.reserveAddress - The reserve contract address. If undefined, the query will be disabled.
14
- * @param {string} params.offset - The offset for pagination (number as string)
15
- * @param {string} params.limit - The limit for pagination (number as string)
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.).
16
23
  *
17
- * @returns {UseQueryResult<MoneyMarketAssetBorrowers | undefined>} A query result object containing:
18
- * - data: The asset borrowers data when available
19
- * - isLoading: Boolean indicating if the request is in progress
20
- * - error: Error object if the request failed
21
- * - refetch: Function to manually trigger a data refresh
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.
22
29
  *
23
30
  * @example
24
- * ```typescript
25
- * const { data: borrowers, isLoading, error } = useMoneyMarketAssetBorrowers({
26
- * reserveAddress: '0xabc...',
27
- * offset: '0',
28
- * limit: '20'
31
+ * const { data: borrowers, isLoading, error } = useBackendMoneyMarketAssetBorrowers({
32
+ * params: { reserveAddress: '0xabc...' },
33
+ * pagination: { offset: '0', limit: '20' }
29
34
  * });
30
35
  *
31
36
  * if (isLoading) return <div>Loading borrowers...</div>;
@@ -34,34 +39,38 @@ import { useSodaxContext } from '../shared/useSodaxContext';
34
39
  * console.log('Total borrowers:', borrowers.total);
35
40
  * console.log('Borrowers:', borrowers.borrowers);
36
41
  * }
37
- * ```
38
42
  *
39
43
  * @remarks
40
- * - The query is disabled when reserveAddress is undefined or empty
41
- * - Uses React Query for efficient caching and state management
42
- * - Automatically handles error states and loading indicators
43
- * - Supports pagination through offset and limit parameters
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`.
44
47
  */
45
- export const useBackendMoneyMarketAssetBorrowers = (params: {
46
- reserveAddress: string | undefined;
47
- offset: string;
48
- limit: string;
49
- }): UseQueryResult<MoneyMarketAssetBorrowers | undefined> => {
48
+ export const useBackendMoneyMarketAssetBorrowers = (
49
+ params: UseBackendMoneyMarketAssetBorrowersParams | undefined,
50
+ ): UseQueryResult<MoneyMarketAssetBorrowers | undefined, Error> => {
50
51
  const { sodax } = useSodaxContext();
51
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
+ };
62
+
52
63
  return useQuery({
53
- queryKey: ['backend', 'moneymarket', 'asset', 'borrowers', params],
64
+ ...queryOptions,
54
65
  queryFn: async (): Promise<MoneyMarketAssetBorrowers | undefined> => {
55
- if (!params.reserveAddress || !params.offset || !params.limit) {
66
+ if (!params?.params?.reserveAddress || !params.pagination.offset || !params.pagination.limit) {
56
67
  return undefined;
57
68
  }
58
69
 
59
- return sodax.backendApi.getMoneyMarketAssetBorrowers(params.reserveAddress, {
60
- offset: params.offset,
61
- limit: params.limit,
70
+ return sodax.backendApi.getMoneyMarketAssetBorrowers(params.params.reserveAddress, {
71
+ offset: params.pagination.offset,
72
+ limit: params.pagination.limit,
62
73
  });
63
74
  },
64
- enabled: !!params.reserveAddress && !!params.offset && !!params.limit,
65
- retry: 3,
66
75
  });
67
76
  };
@@ -1,31 +1,36 @@
1
1
  // packages/dapp-kit/src/hooks/backend/useMoneyMarketAssetSuppliers.ts
2
- import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
3
3
  import type { MoneyMarketAssetSuppliers } from '@sodax/sdk';
4
4
  import { useSodaxContext } from '../shared/useSodaxContext';
5
+ import type { BackendPaginationParams } from './types';
6
+
7
+ export type UseBackendMoneyMarketAssetSuppliersParams = {
8
+ params: {
9
+ reserveAddress: string | undefined;
10
+ };
11
+ pagination: BackendPaginationParams;
12
+ queryOptions?: UseQueryOptions<MoneyMarketAssetSuppliers | undefined, Error>;
13
+ };
5
14
 
6
15
  /**
7
- * Hook for fetching suppliers for a specific money market asset from the backend API.
8
- *
9
- * This hook provides access to the list of suppliers for a specific money market asset,
10
- * with pagination support. The data is automatically fetched and cached using React Query.
16
+ * React hook for fetching suppliers for a specific money market asset from the backend API, with pagination support.
11
17
  *
12
- * @param {Object} params - Parameters for fetching asset suppliers
13
- * @param {string | undefined} params.reserveAddress - The reserve contract address. If undefined, the query will be disabled.
14
- * @param {string} params.offset - The offset for pagination (number as string)
15
- * @param {string} params.limit - The limit for pagination (number as string)
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.
16
23
  *
17
- * @returns {UseQueryResult<MoneyMarketAssetSuppliers | undefined>} A query result object containing:
18
- * - data: The asset suppliers data when available
19
- * - isLoading: Boolean indicating if the request is in progress
20
- * - error: Error object if the request failed
21
- * - refetch: Function to manually trigger a data refresh
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.
22
29
  *
23
30
  * @example
24
- * ```typescript
25
- * const { data: suppliers, isLoading, error } = useMoneyMarketAssetSuppliers({
26
- * reserveAddress: '0xabc...',
27
- * offset: '0',
28
- * limit: '20'
31
+ * const { data: suppliers, isLoading, error } = useBackendMoneyMarketAssetSuppliers({
32
+ * params: { reserveAddress: '0xabc...' },
33
+ * pagination: { offset: '0', limit: '20' }
29
34
  * });
30
35
  *
31
36
  * if (isLoading) return <div>Loading suppliers...</div>;
@@ -34,34 +39,38 @@ import { useSodaxContext } from '../shared/useSodaxContext';
34
39
  * console.log('Total suppliers:', suppliers.total);
35
40
  * console.log('Suppliers:', suppliers.suppliers);
36
41
  * }
37
- * ```
38
42
  *
39
43
  * @remarks
40
- * - The query is disabled when reserveAddress is undefined or empty
41
- * - Uses React Query for efficient caching and state management
42
- * - Automatically handles error states and loading indicators
43
- * - Supports pagination through offset and limit parameters
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`.
44
47
  */
45
- export const useBackendMoneyMarketAssetSuppliers = (params: {
46
- reserveAddress: string | undefined;
47
- offset: string;
48
- limit: string;
49
- }): UseQueryResult<MoneyMarketAssetSuppliers | undefined> => {
48
+ export const useBackendMoneyMarketAssetSuppliers = (
49
+ params: UseBackendMoneyMarketAssetSuppliersParams | undefined,
50
+ ): UseQueryResult<MoneyMarketAssetSuppliers | undefined, Error> => {
50
51
  const { sodax } = useSodaxContext();
51
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
+ };
62
+
52
63
  return useQuery({
53
- queryKey: ['backend', 'moneymarket', 'asset', 'suppliers', params],
64
+ ...queryOptions,
54
65
  queryFn: async (): Promise<MoneyMarketAssetSuppliers | undefined> => {
55
- if (!params.reserveAddress || !params.offset || !params.limit) {
66
+ if (!params?.params?.reserveAddress || !params.pagination.offset || !params.pagination.limit) {
56
67
  return undefined;
57
68
  }
58
69
 
59
- return sodax.backendApi.getMoneyMarketAssetSuppliers(params.reserveAddress, {
60
- offset: params.offset,
61
- limit: params.limit,
70
+ return sodax.backendApi.getMoneyMarketAssetSuppliers(params.params.reserveAddress, {
71
+ offset: params.pagination.offset,
72
+ limit: params.pagination.limit,
62
73
  });
63
74
  },
64
- enabled: !!params.reserveAddress && !!params.offset && !!params.limit,
65
- retry: 3,
66
75
  });
67
76
  };
@@ -1,56 +1,53 @@
1
- // packages/dapp-kit/src/hooks/backend/useMoneyMarketPosition.ts
2
- import { useQuery, type UseQueryResult } from '@tanstack/react-query';
1
+ import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
3
2
  import type { MoneyMarketPosition } from '@sodax/sdk';
4
3
  import { useSodaxContext } from '../shared/useSodaxContext';
5
4
 
5
+ export type UseBackendMoneyMarketPositionParams = {
6
+ userAddress: string | undefined;
7
+ queryOptions?: UseQueryOptions<MoneyMarketPosition | undefined, Error>;
8
+ };
9
+
6
10
  /**
7
- * Hook for fetching money market position for a specific user from the backend API.
8
- *
9
- * This hook provides access to a user's money market positions, including their
10
- * aToken balances, variable debt token balances, and associated reserve information.
11
- * The data is automatically fetched and cached using React Query.
11
+ * React hook for fetching a user's money market position from the backend API.
12
12
  *
13
- * @param {string | undefined} userAddress - The user's wallet address. If undefined, the query will be disabled.
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).
14
16
  *
15
- * @returns {UseQueryResult<MoneyMarketPosition | undefined>} A query result object containing:
16
- * - data: The money market position data when available
17
- * - isLoading: Boolean indicating if the request is in progress
18
- * - error: Error object if the request failed
19
- * - refetch: Function to manually trigger a data refresh
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.
20
22
  *
21
23
  * @example
22
- * ```typescript
23
- * const { data: position, isLoading, error } = useMoneyMarketPosition('0x123...');
24
- *
25
- * if (isLoading) return <div>Loading position...</div>;
26
- * if (error) return <div>Error: {error.message}</div>;
27
- * if (position) {
28
- * console.log('User address:', position.userAddress);
29
- * console.log('Positions:', position.positions);
30
- * }
31
- * ```
32
- *
33
- * @remarks
34
- * - The query is disabled when userAddress is undefined or empty
35
- * - Uses React Query for efficient caching and state management
36
- * - Automatically handles error states and loading indicators
37
- * - Includes user's aToken and debt token balances across all reserves
24
+ * const { data, isLoading, error } = useBackendMoneyMarketPosition({
25
+ * userAddress: '0xabc...',
26
+ * queryOptions: { staleTime: 60000 },
27
+ * });
38
28
  */
39
29
  export const useBackendMoneyMarketPosition = (
40
- userAddress: string | undefined,
41
- ): UseQueryResult<MoneyMarketPosition | undefined> => {
30
+ params: UseBackendMoneyMarketPositionParams | undefined,
31
+ ): UseQueryResult<MoneyMarketPosition | undefined, Error> => {
42
32
  const { sodax } = useSodaxContext();
43
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
+ };
43
+
44
44
  return useQuery({
45
- queryKey: ['backend', 'moneymarket', 'position', userAddress],
45
+ ...queryOptions,
46
46
  queryFn: async (): Promise<MoneyMarketPosition | undefined> => {
47
- if (!userAddress) {
47
+ if (!params?.userAddress) {
48
48
  return undefined;
49
49
  }
50
-
51
- return sodax.backendApi.getMoneyMarketPosition(userAddress);
50
+ return sodax.backendApi.getMoneyMarketPosition(params.userAddress);
52
51
  },
53
- enabled: !!userAddress && userAddress.length > 0,
54
- retry: 3,
55
52
  });
56
53
  };
@@ -1,63 +1,63 @@
1
- // packages/dapp-kit/src/hooks/backend/useOrderbook.ts
2
- import { useQuery, type UseQueryResult } from '@tanstack/react-query';
1
+ import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
3
2
  import type { OrderbookResponse } from '@sodax/sdk';
4
3
  import { useSodaxContext } from '../shared/useSodaxContext';
4
+ import type { BackendPaginationParams } from './types';
5
+
6
+ export type UseBackendOrderbookParams = {
7
+ queryOptions?: UseQueryOptions<OrderbookResponse | undefined, Error>;
8
+ pagination?: BackendPaginationParams;
9
+ };
5
10
 
6
11
  /**
7
12
  * Hook for fetching the solver orderbook from the backend API.
8
13
  *
9
- * This hook provides access to the solver orderbook data, including intent states
10
- * and intent data for all available intents. The data is automatically fetched
11
- * and cached using React Query with pagination support.
12
- *
13
- * @param {Object} params - Pagination parameters for the orderbook
14
- * @param {string} params.offset - The offset for pagination (number as string)
15
- * @param {string} params.limit - The limit for pagination (number as string)
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.
16
18
  *
17
- * @returns {UseQueryResult<OrderbookResponse | undefined>} A query result object containing:
18
- * - data: The orderbook response data when available
19
- * - isLoading: Boolean indicating if the request is in progress
20
- * - error: Error object if the request failed
21
- * - refetch: Function to manually trigger a data refresh
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.
22
24
  *
23
25
  * @example
24
- * ```typescript
25
- * const { data: orderbook, isLoading, error } = useOrderbook({
26
- * offset: '0',
27
- * limit: '10'
26
+ * const { data, isLoading, error } = useBackendOrderbook({
27
+ * pagination: { offset: '0', limit: '10' },
28
+ * queryOptions: { staleTime: 60000 },
28
29
  * });
29
30
  *
30
- * if (isLoading) return <div>Loading orderbook...</div>;
31
- * if (error) return <div>Error: {error.message}</div>;
32
- * if (orderbook) {
33
- * console.log('Total intents:', orderbook.total);
34
- * console.log('Intents:', orderbook.data);
35
- * }
36
- * ```
37
- *
38
31
  * @remarks
39
- * - The query is disabled when params are undefined or invalid
40
- * - Uses React Query for efficient caching and state management
41
- * - Automatically handles error states and loading indicators
42
- * - Stale time of 30 seconds for real-time orderbook data
43
- * - Supports pagination through offset and limit parameters
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.
44
35
  */
45
36
  export const useBackendOrderbook = (
46
- params: { offset: string; limit: string } | undefined,
37
+ params: UseBackendOrderbookParams | undefined,
47
38
  ): UseQueryResult<OrderbookResponse | undefined> => {
48
39
  const { sodax } = useSodaxContext();
49
40
 
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
+
50
53
  return useQuery({
51
- queryKey: ['backend', 'solver', 'orderbook', params],
54
+ ...queryOptions,
52
55
  queryFn: async (): Promise<OrderbookResponse | undefined> => {
53
- if (!params || !params.offset || !params.limit) {
56
+ if (!params?.pagination || !params?.pagination.offset || !params?.pagination.limit) {
54
57
  return undefined;
55
58
  }
56
59
 
57
- return sodax.backendApi.getOrderbook(params);
60
+ return sodax.backendApi.getOrderbook(params.pagination);
58
61
  },
59
- enabled: !!params && !!params.offset && !!params.limit,
60
- staleTime: 30 * 1000, // 30 seconds for real-time data
61
- retry: 3,
62
62
  });
63
63
  };
@@ -0,0 +1,81 @@
1
+ import type { UserIntentsResponse, Address } from '@sodax/sdk';
2
+ // packages/dapp-kit/src/hooks/backend/useBackendUserIntents.ts
3
+ import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+ import type { BackendPaginationParams } from './types';
6
+
7
+ export type GetUserIntentsParams = {
8
+ userAddress: Address;
9
+ startDate?: number;
10
+ endDate?: number;
11
+ };
12
+
13
+ export type UseBackendUserIntentsParams = {
14
+ params?: GetUserIntentsParams;
15
+ queryOptions?: UseQueryOptions<UserIntentsResponse | undefined, Error>;
16
+ pagination?: BackendPaginationParams;
17
+ };
18
+
19
+ /**
20
+ * React hook for fetching user-created intents from the backend API for a given user address,
21
+ * with optional support for a date filtering range.
22
+ *
23
+ * @param {UseBackendUserIntentsParams} args - Query configuration.
24
+ * @param {GetUserIntentsParams | undefined} args.params - User intent filter parameters.
25
+ * @param {Address} args.params.userAddress - The wallet address of the user (required).
26
+ * @param {number} [args.params.startDate] - Include intents created after this timestamp (ms).
27
+ * @param {number} [args.params.endDate] - Include intents created before this timestamp (ms).
28
+ * @param {UseQueryOptions<UserIntentsResponse | undefined, Error>} [args.queryOptions] - Optional React Query options.
29
+ * @param {BackendPaginationParams} [args.pagination] - (currently ignored) Pagination options.
30
+ *
31
+ * @returns {UseQueryResult<UserIntentsResponse | undefined, Error>} React Query result:
32
+ * - `data`: The user intent response, or undefined if unavailable.
33
+ * - `isLoading`: `true` if loading.
34
+ * - `error`: An Error instance if any occurred.
35
+ * - `refetch`: Function to refetch data.
36
+ *
37
+ * @example
38
+ * const { data: userIntents, isLoading, error } = useBackendUserIntents({
39
+ * params: { userAddress: "0x123..." }
40
+ * });
41
+ *
42
+ * @example
43
+ * const { data } = useBackendUserIntents({
44
+ * params: {
45
+ * userAddress: "0xabc...",
46
+ * startDate: Date.now() - 1_000_000,
47
+ * endDate: Date.now(),
48
+ * },
49
+ * });
50
+ *
51
+ * @remarks
52
+ * The query is disabled if `params` or `params.userAddress` is missing or empty. Uses React Query for
53
+ * cache/state management and auto-retries failed requests three times by default.
54
+ */
55
+ export const useBackendUserIntents = ({
56
+ params,
57
+ queryOptions,
58
+ }: UseBackendUserIntentsParams): UseQueryResult<UserIntentsResponse | undefined, Error> => {
59
+ const { sodax } = useSodaxContext();
60
+ const defaultQueryOptions = {
61
+ queryKey: ['api', 'intent', 'user', params],
62
+ enabled: !!params && !!params.userAddress && params.userAddress.length > 0,
63
+ retry: 3,
64
+ };
65
+
66
+ queryOptions = {
67
+ ...defaultQueryOptions,
68
+ ...queryOptions, // override default query options if provided
69
+ };
70
+
71
+ return useQuery({
72
+ ...queryOptions,
73
+ queryFn: async (): Promise<UserIntentsResponse | undefined> => {
74
+ if (!params?.userAddress) {
75
+ return undefined;
76
+ }
77
+
78
+ return sodax.backendApi.getUserIntents(params);
79
+ },
80
+ });
81
+ };
@@ -8,3 +8,4 @@ export * from './useMMAllowance';
8
8
  export * from './useMMApprove';
9
9
  export * from './useAToken';
10
10
  export * from './useReservesUsdFormat';
11
+ export * from './useUserFormattedSummary';