@sodax/dapp-kit 1.0.1-beta-rc3 → 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 +4 -4
  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
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sodax/dapp-kit",
3
3
  "license": "MIT",
4
- "version": "1.0.1-beta-rc3",
4
+ "version": "1.0.2-beta",
5
5
  "description": "dapp-kit of New World",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -16,11 +16,11 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "viem": "2.29.2",
19
- "@sodax/sdk": "1.0.1-beta-rc3",
20
- "@sodax/types": "1.0.1-beta-rc3"
19
+ "@sodax/types": "1.0.2-beta",
20
+ "@sodax/sdk": "1.0.2-beta"
21
21
  },
22
22
  "devDependencies": {
23
- "@types/react": "18.3.1",
23
+ "@types/react": "19.0.8",
24
24
  "knip": "5.30.5",
25
25
  "tsup": "^8.4.0",
26
26
  "typescript": "5.5.4"
@@ -6,119 +6,218 @@ This directory contains React hooks for interacting with the Sodax Backend API t
6
6
 
7
7
  ### Intent Hooks
8
8
 
9
- #### `useIntentByTxHash(txHash: string | undefined)`
10
- Fetches intent details by transaction hash.
9
+ #### `useBackendIntentByTxHash(params)`
10
+
11
+ Fetches intent details by transaction hash. The query is disabled if `txHash` is undefined or empty.
11
12
 
12
13
  ```typescript
13
- import { useIntentByTxHash } from '@sodax/dapp-kit';
14
+ import { useBackendIntentByTxHash } from '@sodax/dapp-kit';
14
15
 
15
- const { data: intent, isLoading, error } = useIntentByTxHash('0x123...');
16
+ const { data: intent, isLoading, error } = useBackendIntentByTxHash({
17
+ params: { txHash: '0x123...' },
18
+ queryOptions: { staleTime: 1000 }, // optional
19
+ });
16
20
  ```
17
21
 
18
- #### `useIntentByHash(intentHash: string | undefined)`
19
- Fetches intent details by intent hash.
22
+ **Parameters:**
23
+
24
+ - `params.params.txHash` (string | undefined): Transaction hash used to retrieve the associated intent
25
+ - `params.queryOptions` (optional): React Query options to customize behavior
26
+
27
+ **Note:** Intents are only created on the hub chain, so `txHash` must originate from there. Default refetch interval is 1 second.
28
+
29
+ #### `useBackendIntentByHash(params)`
30
+
31
+ Fetches intent details by intent hash. The query is disabled if `intentHash` is undefined or empty.
20
32
 
21
33
  ```typescript
22
- import { useIntentByHash } from '@sodax/dapp-kit';
34
+ import { useBackendIntentByHash } from '@sodax/dapp-kit';
23
35
 
24
- const { data: intent, isLoading, error } = useIntentByHash('0xabc...');
36
+ const { data: intent, isLoading, error } = useBackendIntentByHash({
37
+ params: { intentHash: '0xabc...' },
38
+ });
25
39
  ```
26
40
 
41
+ **Parameters:**
42
+
43
+ - `params.params.intentHash` (string | undefined): The hash identifying the intent to fetch
44
+ - `params.queryOptions` (optional): React Query options to customize behavior
45
+
46
+ #### `useBackendUserIntents(params)`
47
+
48
+ Fetches user-created intents from the backend API for a given user address, with optional date filtering.
49
+
50
+ ```typescript
51
+ import { useBackendUserIntents } from '@sodax/dapp-kit';
52
+
53
+ const { data: userIntents, isLoading, error } = useBackendUserIntents({
54
+ params: {
55
+ userAddress: '0x123...',
56
+ startDate: Date.now() - 1_000_000, // optional
57
+ endDate: Date.now(), // optional
58
+ },
59
+ });
60
+ ```
61
+
62
+ **Parameters:**
63
+
64
+ - `params.params.userAddress` (Address): The wallet address of the user (required)
65
+ - `params.params.startDate` (number, optional): Include intents created after this timestamp (ms)
66
+ - `params.params.endDate` (number, optional): Include intents created before this timestamp (ms)
67
+ - `params.queryOptions` (optional): React Query options to customize behavior
68
+ - `params.pagination` (optional): Currently ignored
69
+
27
70
  ### Solver Hooks
28
71
 
29
- #### `useOrderbook(params: { offset: string; limit: string } | undefined)`
30
- Fetches the solver orderbook with pagination support.
72
+ #### `useBackendOrderbook(params)`
73
+
74
+ Fetches the solver orderbook with pagination support. The query is disabled if pagination parameters are missing.
31
75
 
32
76
  ```typescript
33
- import { useOrderbook } from '@sodax/dapp-kit';
77
+ import { useBackendOrderbook } from '@sodax/dapp-kit';
34
78
 
35
- const { data: orderbook, isLoading, error } = useOrderbook({
36
- offset: '0',
37
- limit: '10'
79
+ const { data: orderbook, isLoading, error } = useBackendOrderbook({
80
+ pagination: { offset: '0', limit: '10' },
81
+ queryOptions: { staleTime: 60000 }, // optional
38
82
  });
39
83
  ```
40
84
 
85
+ **Parameters:**
86
+
87
+ - `params.pagination.offset` (string): The offset for pagination (required)
88
+ - `params.pagination.limit` (string): The limit for pagination (required)
89
+ - `params.queryOptions` (optional): React Query options to customize behavior
90
+
91
+ **Note:** Default `staleTime` is 30 seconds to support near-real-time updates.
92
+
41
93
  ### Money Market Hooks
42
94
 
43
- #### `useMoneyMarketPosition(userAddress: string | undefined)`
44
- Fetches a user's money market positions.
95
+ #### `useBackendMoneyMarketPosition(params)`
96
+
97
+ Fetches a user's money market position. The query is disabled if `userAddress` is undefined or empty.
45
98
 
46
99
  ```typescript
47
- import { useMoneyMarketPosition } from '@sodax/dapp-kit';
100
+ import { useBackendMoneyMarketPosition } from '@sodax/dapp-kit';
48
101
 
49
- const { data: position, isLoading, error } = useMoneyMarketPosition('0x123...');
102
+ const { data: position, isLoading, error } = useBackendMoneyMarketPosition({
103
+ userAddress: '0x123...',
104
+ queryOptions: { staleTime: 60000 }, // optional
105
+ });
50
106
  ```
51
107
 
52
- #### `useAllMoneyMarketAssets()`
53
- Fetches all available money market assets.
108
+ **Parameters:**
109
+
110
+ - `params.userAddress` (string | undefined): The user's wallet address to fetch positions for
111
+ - `params.queryOptions` (optional): React Query options to customize behavior
112
+
113
+ #### `useBackendAllMoneyMarketAssets(params)`
114
+
115
+ Fetches all available money market assets. No required parameters.
54
116
 
55
117
  ```typescript
56
- import { useAllMoneyMarketAssets } from '@sodax/dapp-kit';
118
+ import { useBackendAllMoneyMarketAssets } from '@sodax/dapp-kit';
57
119
 
58
- const { data: assets, isLoading, error } = useAllMoneyMarketAssets();
120
+ const { data: assets, isLoading, error } = useBackendAllMoneyMarketAssets({
121
+ queryOptions: { staleTime: 60000 }, // optional
122
+ });
59
123
  ```
60
124
 
61
- #### `useMoneyMarketAsset(reserveAddress: string | undefined)`
62
- Fetches details for a specific money market asset.
125
+ **Parameters:**
126
+
127
+ - `params.queryOptions` (optional): React Query options to customize behavior
128
+
129
+ #### `useBackendMoneyMarketAsset(params)`
130
+
131
+ Fetches details for a specific money market asset. The query is disabled if `reserveAddress` is undefined or empty.
63
132
 
64
133
  ```typescript
65
- import { useMoneyMarketAsset } from '@sodax/dapp-kit';
134
+ import { useBackendMoneyMarketAsset } from '@sodax/dapp-kit';
66
135
 
67
- const { data: asset, isLoading, error } = useMoneyMarketAsset('0xabc...');
136
+ const { data: asset, isLoading, error } = useBackendMoneyMarketAsset({
137
+ params: { reserveAddress: '0xabc...' },
138
+ });
68
139
  ```
69
140
 
70
- #### `useMoneyMarketAssetBorrowers(params)`
71
- Fetches borrowers for a specific money market asset.
141
+ **Parameters:**
142
+
143
+ - `params.params.reserveAddress` (string | undefined): Reserve contract address to fetch asset details
144
+ - `params.queryOptions` (optional): React Query options to customize behavior
145
+
146
+ #### `useBackendMoneyMarketAssetBorrowers(params)`
147
+
148
+ Fetches borrowers for a specific money market asset with pagination. The query is disabled if `reserveAddress`, `offset`, or `limit` are missing.
72
149
 
73
150
  ```typescript
74
- import { useMoneyMarketAssetBorrowers } from '@sodax/dapp-kit';
151
+ import { useBackendMoneyMarketAssetBorrowers } from '@sodax/dapp-kit';
75
152
 
76
- const { data: borrowers, isLoading, error } = useMoneyMarketAssetBorrowers({
77
- reserveAddress: '0xabc...',
78
- offset: '0',
79
- limit: '20'
153
+ const { data: borrowers, isLoading, error } = useBackendMoneyMarketAssetBorrowers({
154
+ params: { reserveAddress: '0xabc...' },
155
+ pagination: { offset: '0', limit: '20' },
80
156
  });
81
157
  ```
82
158
 
83
- #### `useMoneyMarketAssetSuppliers(params)`
84
- Fetches suppliers for a specific money market asset.
159
+ **Parameters:**
160
+
161
+ - `params.params.reserveAddress` (string | undefined): Reserve contract address for which to fetch borrowers
162
+ - `params.pagination.offset` (string): The offset for pagination (required)
163
+ - `params.pagination.limit` (string): The limit for pagination (required)
164
+ - `params.queryOptions` (optional): React Query options to customize behavior
165
+
166
+ #### `useBackendMoneyMarketAssetSuppliers(params)`
167
+
168
+ Fetches suppliers for a specific money market asset with pagination. The query is disabled if `reserveAddress`, `offset`, or `limit` are missing.
85
169
 
86
170
  ```typescript
87
- import { useMoneyMarketAssetSuppliers } from '@sodax/dapp-kit';
171
+ import { useBackendMoneyMarketAssetSuppliers } from '@sodax/dapp-kit';
88
172
 
89
- const { data: suppliers, isLoading, error } = useMoneyMarketAssetSuppliers({
90
- reserveAddress: '0xabc...',
91
- offset: '0',
92
- limit: '20'
173
+ const { data: suppliers, isLoading, error } = useBackendMoneyMarketAssetSuppliers({
174
+ params: { reserveAddress: '0xabc...' },
175
+ pagination: { offset: '0', limit: '20' },
93
176
  });
94
177
  ```
95
178
 
96
- #### `useAllMoneyMarketBorrowers(params)`
97
- Fetches all money market borrowers across all assets.
179
+ **Parameters:**
180
+
181
+ - `params.params.reserveAddress` (string | undefined): The reserve contract address to query
182
+ - `params.pagination.offset` (string): The offset for pagination (required)
183
+ - `params.pagination.limit` (string): The limit for pagination (required)
184
+ - `params.queryOptions` (optional): React Query options to customize behavior
185
+
186
+ #### `useBackendAllMoneyMarketBorrowers(params)`
187
+
188
+ Fetches all money market borrowers across all assets with pagination. The query is disabled if pagination parameters are missing.
98
189
 
99
190
  ```typescript
100
- import { useAllMoneyMarketBorrowers } from '@sodax/dapp-kit';
191
+ import { useBackendAllMoneyMarketBorrowers } from '@sodax/dapp-kit';
101
192
 
102
- const { data: borrowers, isLoading, error } = useAllMoneyMarketBorrowers({
103
- offset: '0',
104
- limit: '50'
193
+ const { data: borrowers, isLoading, error } = useBackendAllMoneyMarketBorrowers({
194
+ pagination: { offset: '0', limit: '50' },
105
195
  });
106
196
  ```
107
197
 
198
+ **Parameters:**
199
+
200
+ - `params.pagination.offset` (string): The offset for pagination (required)
201
+ - `params.pagination.limit` (string): The limit for pagination (required)
202
+ - `params.queryOptions` (optional): React Query options to customize behavior
203
+
108
204
  ## Features
109
205
 
110
206
  - **Automatic Caching**: All hooks use React Query for efficient data caching
111
- - **Error Handling**: Built-in error states and retry logic
207
+ - **Error Handling**: Built-in error states and retry logic (default: 3 retries)
112
208
  - **Loading States**: Automatic loading indicators
113
209
  - **TypeScript Support**: Full type safety with proper TypeScript definitions
114
- - **Pagination Support**: Built-in pagination for list endpoints
115
- - **Conditional Queries**: Queries are automatically disabled when required parameters are missing
210
+ - **Pagination Support**: Built-in pagination for list endpoints (offset/limit as strings)
211
+ - **Conditional Queries**: Queries are automatically disabled when required parameters are missing or empty
116
212
  - **Context Integration**: Uses `useSodaxContext` for consistent SDK access across the application
213
+ - **Auto-refetch**: `useBackendIntentByTxHash` has a default refetch interval of 1 second for real-time updates
117
214
 
118
215
  ## Stale Times
119
216
 
120
217
  Different hooks have different stale times optimized for their data types:
121
218
 
219
+ - **`useBackendOrderbook`**: 30 seconds (default) - for near-real-time solver orderbook updates
220
+ - **Other hooks**: No default stale time set - uses React Query defaults. You can customize via `queryOptions.staleTime`
122
221
 
123
222
  ## Error Handling
124
223
 
@@ -10,6 +10,7 @@
10
10
  // Intent hooks
11
11
  export { useBackendIntentByTxHash } from './useBackendIntentByTxHash';
12
12
  export { useBackendIntentByHash } from './useBackendIntentByHash';
13
+ export { useBackendUserIntents } from './useBackendUserIntents';
13
14
 
14
15
  // Solver hooks
15
16
  export { useBackendOrderbook } from './useBackendOrderbook';
@@ -21,3 +22,4 @@ export { useBackendMoneyMarketAsset } from './useBackendMoneyMarketAsset';
21
22
  export { useBackendMoneyMarketAssetBorrowers } from './useBackendMoneyMarketAssetBorrowers';
22
23
  export { useBackendMoneyMarketAssetSuppliers } from './useBackendMoneyMarketAssetSuppliers';
23
24
  export { useBackendAllMoneyMarketBorrowers } from './useBackendAllMoneyMarketBorrowers';
25
+ export * from './types';
@@ -0,0 +1,4 @@
1
+ export type BackendPaginationParams = {
2
+ offset: string;
3
+ limit: string;
4
+ };
@@ -1,24 +1,26 @@
1
1
  // packages/dapp-kit/src/hooks/backend/useAllMoneyMarketAssets.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 UseBackendAllMoneyMarketAssetsParams = {
7
+ queryOptions?: UseQueryOptions<MoneyMarketAsset[], Error>;
8
+ };
9
+
6
10
  /**
7
- * Hook for fetching all money market assets from the backend API.
11
+ * React hook to fetch all money market assets from the backend API.
8
12
  *
9
- * This hook provides access to all available money market assets, including
10
- * their reserve information, liquidity rates, borrow rates, and market statistics.
11
- * The data is automatically fetched and cached using React Query.
13
+ * @param {UseBackendAllMoneyMarketAssetsParams | undefined} params - Optional parameters:
14
+ * - `queryOptions` (optional): React Query options to override default behavior (e.g., caching, retry, etc).
12
15
  *
13
- * @returns {UseQueryResult<MoneyMarketAsset[]>} A query result object containing:
14
- * - data: Array of money market asset data when available
15
- * - isLoading: Boolean indicating if the request is in progress
16
- * - error: Error object if the request failed
17
- * - refetch: Function to manually trigger a data refresh
16
+ * @returns {UseQueryResult<MoneyMarketAsset[], Error>} React Query result object:
17
+ * - `data`: Array of all money market asset data when available.
18
+ * - `isLoading`: Boolean indicating if the request is in progress.
19
+ * - `error`: Error object if the request failed.
20
+ * - `refetch`: Function to manually trigger a data refresh.
18
21
  *
19
22
  * @example
20
- * ```typescript
21
- * const { data: assets, isLoading, error } = useAllMoneyMarketAssets();
23
+ * const { data: assets, isLoading, error } = useBackendAllMoneyMarketAssets();
22
24
  *
23
25
  * if (isLoading) return <div>Loading assets...</div>;
24
26
  * if (error) return <div>Error: {error.message}</div>;
@@ -28,22 +30,31 @@ import { useSodaxContext } from '../shared/useSodaxContext';
28
30
  * console.log(`${asset.symbol}: ${asset.liquidityRate} liquidity rate`);
29
31
  * });
30
32
  * }
31
- * ```
32
33
  *
33
34
  * @remarks
34
- * - Uses React Query for efficient caching and state management
35
- * - Automatically handles error states and loading indicators
36
- * - Returns comprehensive asset information including rates and statistics
37
- * - No parameters required - fetches all available assets
35
+ * - No required parameters fetches all available money market assets from backend.
36
+ * - Uses React Query for caching, retries, and loading/error state management.
37
+ * - Supports overriding React Query config via `queryOptions`.
38
38
  */
39
- export const useBackendAllMoneyMarketAssets = (): UseQueryResult<MoneyMarketAsset[]> => {
39
+ export const useBackendAllMoneyMarketAssets = (
40
+ params: UseBackendAllMoneyMarketAssetsParams | undefined,
41
+ ): UseQueryResult<MoneyMarketAsset[], Error> => {
40
42
  const { sodax } = useSodaxContext();
41
43
 
44
+ const defaultQueryOptions = {
45
+ queryKey: ['api', 'mm', 'assets', 'all'],
46
+ enabled: true,
47
+ retry: 3,
48
+ };
49
+ const queryOptions = {
50
+ ...defaultQueryOptions,
51
+ ...params?.queryOptions,
52
+ };
53
+
42
54
  return useQuery({
43
- queryKey: ['backend', 'moneymarket', 'assets', 'all'],
55
+ ...queryOptions,
44
56
  queryFn: async (): Promise<MoneyMarketAsset[]> => {
45
57
  return sodax.backendApi.getAllMoneyMarketAssets();
46
58
  },
47
- retry: 3,
48
59
  });
49
60
  };
@@ -1,7 +1,13 @@
1
1
  // packages/dapp-kit/src/hooks/backend/useAllMoneyMarketBorrowers.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 { MoneyMarketBorrowers } from '@sodax/sdk';
4
4
  import { useSodaxContext } from '../shared/useSodaxContext';
5
+ import type { BackendPaginationParams } from './types';
6
+
7
+ export type UseBackendAllMoneyMarketBorrowersParams = {
8
+ pagination: BackendPaginationParams;
9
+ queryOptions?: UseQueryOptions<MoneyMarketBorrowers | undefined, Error>;
10
+ };
5
11
 
6
12
  /**
7
13
  * Hook for fetching all money market borrowers from the backend API.
@@ -42,20 +48,32 @@ import { useSodaxContext } from '../shared/useSodaxContext';
42
48
  * - Returns borrowers across all money market assets
43
49
  */
44
50
  export const useBackendAllMoneyMarketBorrowers = (
45
- params: { offset: string; limit: string } | undefined,
51
+ params: UseBackendAllMoneyMarketBorrowersParams | undefined,
46
52
  ): UseQueryResult<MoneyMarketBorrowers | undefined> => {
47
53
  const { sodax } = useSodaxContext();
48
54
 
55
+ const defaultQueryOptions = {
56
+ queryKey: ['api', 'mm', 'borrowers', 'all', params],
57
+ enabled: !!params && !!params.pagination.offset && !!params.pagination.limit,
58
+ retry: 3,
59
+ };
60
+
61
+ const queryOptions = {
62
+ ...defaultQueryOptions,
63
+ ...params?.queryOptions,
64
+ };
65
+
49
66
  return useQuery({
50
- queryKey: ['backend', 'moneymarket', 'borrowers', 'all', params],
67
+ ...queryOptions,
51
68
  queryFn: async (): Promise<MoneyMarketBorrowers | undefined> => {
52
- if (!params || !params.offset || !params.limit) {
69
+ if (!params || !params.pagination.offset || !params.pagination.limit) {
53
70
  return undefined;
54
71
  }
55
72
 
56
- return sodax.backendApi.getAllMoneyMarketBorrowers(params);
73
+ return sodax.backendApi.getAllMoneyMarketBorrowers({
74
+ offset: params.pagination.offset,
75
+ limit: params.pagination.limit,
76
+ });
57
77
  },
58
- enabled: !!params && !!params.offset && !!params.limit,
59
- retry: 3,
60
78
  });
61
79
  };
@@ -1,53 +1,63 @@
1
- // packages/dapp-kit/src/hooks/backend/useIntentByHash.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 { IntentResponse } from '@sodax/sdk';
4
3
  import { useSodaxContext } from '../shared/useSodaxContext';
5
4
 
5
+ export type UseBackendIntentByHashParams = {
6
+ params: {
7
+ intentHash: string | undefined;
8
+ };
9
+ queryOptions?: UseQueryOptions<IntentResponse | undefined, Error>;
10
+ };
11
+
6
12
  /**
7
- * Hook for fetching intent details by intent hash from the backend API.
8
- *
9
- * This hook provides access to intent data using the intent hash directly,
10
- * including intent details, events, and transaction information. The data is automatically
11
- * fetched and cached using React Query.
13
+ * React hook to fetch intent details from the backend API using an intent hash.
12
14
  *
13
- * @param {string | undefined} intentHash - The intent hash to fetch intent for. If undefined, the query will be disabled.
15
+ * @param {UseBackendIntentByHashParams | undefined} params - Parameters for the query:
16
+ * - params: { intentHash: string | undefined }
17
+ * - `intentHash`: The hash identifying the intent to fetch (disables query if undefined or empty).
18
+ * - queryOptions (optional): Options to customize React Query (e.g., staleTime, enabled).
14
19
  *
15
- * @returns {UseQueryResult<IntentResponse | undefined>} A query result object containing:
16
- * - data: The intent response 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
20
+ * @returns {UseQueryResult<IntentResponse | undefined, Error>} React Query result containing intent response data, loading, error, and refetch states.
20
21
  *
21
22
  * @example
22
- * ```typescript
23
- * const { data: intent, isLoading, error } = useIntentByHash('0xabc...');
23
+ * const { data: intent, isLoading, error } = useBackendIntentByHash({
24
+ * params: { intentHash: '0xabc...' },
25
+ * });
24
26
  *
25
27
  * if (isLoading) return <div>Loading intent...</div>;
26
28
  * if (error) return <div>Error: {error.message}</div>;
27
29
  * if (intent) {
28
30
  * console.log('Intent found:', intent.intentHash);
29
- * console.log('Open status:', intent.open);
30
31
  * }
31
- * ```
32
32
  *
33
33
  * @remarks
34
- * - The query is disabled when intentHash is undefined or empty
35
- * - Uses React Query for efficient caching and state management
36
- * - Automatically handles error states and loading indicators
34
+ * - Returns `undefined` data if no intentHash is provided or query is disabled.
35
+ * - Query is cached and managed using React Query.
36
+ * - Use `queryOptions` to customize caching, retry and fetch behavior.
37
37
  */
38
- export const useBackendIntentByHash = (intentHash: string | undefined): UseQueryResult<IntentResponse | undefined> => {
38
+ export const useBackendIntentByHash = (
39
+ params: UseBackendIntentByHashParams | undefined,
40
+ ): UseQueryResult<IntentResponse | undefined, Error> => {
39
41
  const { sodax } = useSodaxContext();
40
42
 
43
+ const defaultQueryOptions = {
44
+ queryKey: ['api', 'intent', 'hash', params?.params?.intentHash],
45
+ enabled: !!params?.params?.intentHash && params?.params?.intentHash.length > 0,
46
+ retry: 3,
47
+ };
48
+ const queryOptions = {
49
+ ...defaultQueryOptions,
50
+ ...params?.queryOptions,
51
+ };
52
+
41
53
  return useQuery({
42
- queryKey: ['backend', 'intent', 'hash', intentHash],
54
+ ...queryOptions,
43
55
  queryFn: async (): Promise<IntentResponse | undefined> => {
44
- if (!intentHash) {
56
+ if (!params?.params?.intentHash) {
45
57
  return undefined;
46
58
  }
47
59
 
48
- return sodax.backendApi.getIntentByHash(intentHash);
60
+ return sodax.backendApi.getIntentByHash(params.params.intentHash);
49
61
  },
50
- enabled: !!intentHash && intentHash.length > 0,
51
- retry: 3,
52
62
  });
53
63
  };
@@ -1,57 +1,69 @@
1
1
  // packages/dapp-kit/src/hooks/backend/useIntentByTxHash.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 { IntentResponse } from '@sodax/sdk';
4
4
  import { useSodaxContext } from '../shared/useSodaxContext';
5
5
 
6
+ export type UseBackendIntentByTxHashParams = {
7
+ params: {
8
+ txHash: string | undefined;
9
+ };
10
+ queryOptions?: UseQueryOptions<IntentResponse | undefined, Error>;
11
+ };
12
+
6
13
  /**
7
- * Hook for fetching intent details by intent created transaction hash from the backend API.
14
+ * React hook for fetching intent details from the backend API using a transaction hash.
8
15
  *
9
- * This hook provides access to intent data associated with the transaction hash from when
10
- * the intent was created on the hub chain, including intent details, events, and transaction
11
- * information. The data is automatically fetched and cached using React Query.
16
+ * @param {UseBackendIntentByTxHashParams | undefined} params - Parameters for the query:
17
+ * - params: { txHash: string | undefined }
18
+ * - `txHash`: Transaction hash used to retrieve the associated intent; query is disabled if undefined or empty.
19
+ * - queryOptions (optional): React Query options to customize request behavior (e.g., caching, retry, refetchInterval, etc.).
12
20
  *
13
- * @param {string | undefined} txHash - The intent created transaction hash from the hub chain to fetch intent for. If undefined, the query will be disabled.
14
- *
15
- * @returns {UseQueryResult<IntentResponse | undefined>} A query result object containing:
16
- * - data: The intent response 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 {UseQueryResult<IntentResponse | undefined, Error>} React Query result object, including:
22
+ * - `data`: The intent response or undefined if unavailable,
23
+ * - `isLoading`: Loading state,
24
+ * - `error`: Error (if request failed),
25
+ * - `refetch`: Function to refetch the data.
20
26
  *
21
27
  * @example
22
- * ```typescript
23
- * const { data: intent, isLoading, error } = useIntentByTxHash('0x123...');
28
+ * const { data: intent, isLoading, error } = useBackendIntentByTxHash({
29
+ * params: { txHash: '0x123...' },
30
+ * });
24
31
  *
25
32
  * if (isLoading) return <div>Loading intent...</div>;
26
33
  * if (error) return <div>Error: {error.message}</div>;
27
34
  * if (intent) {
28
35
  * console.log('Intent found:', intent.intentHash);
29
36
  * }
30
- * ```
31
37
  *
32
38
  * @remarks
33
- * - Intents are only created on the hub chain, so the transaction hash must be from the hub chain
34
- * - The query is disabled when txHash is undefined or empty
35
- * - Uses React Query for efficient caching and state management
36
- * - Automatically handles error states and loading indicators
39
+ * - Intents are only created on the hub chain, so `txHash` must originate from there.
40
+ * - Query is disabled if `params` is undefined, or if `params.params.txHash` is undefined or an empty string.
41
+ * - Default refetch interval is 1 second. Uses React Query for state management, caching, and retries.
37
42
  */
38
43
  export const useBackendIntentByTxHash = (
39
- txHash: string | undefined,
40
- refetchInterval = 1000,
41
- ): UseQueryResult<IntentResponse | undefined> => {
44
+ params: UseBackendIntentByTxHashParams | undefined,
45
+ ): UseQueryResult<IntentResponse | undefined, Error> => {
42
46
  const { sodax } = useSodaxContext();
43
47
 
48
+ const defaultQueryOptions = {
49
+ queryKey: ['api', 'intent', 'txHash', params?.params?.txHash],
50
+ enabled: !!params?.params?.txHash && params?.params?.txHash.length > 0,
51
+ retry: 3,
52
+ refetchInterval: 1000,
53
+ };
54
+
55
+ const queryOptions = {
56
+ ...defaultQueryOptions,
57
+ ...params?.queryOptions,
58
+ };
59
+
44
60
  return useQuery({
45
- queryKey: ['backend', 'intent', 'txHash', txHash],
61
+ ...queryOptions,
46
62
  queryFn: async (): Promise<IntentResponse | undefined> => {
47
- if (!txHash) {
63
+ if (!params?.params?.txHash) {
48
64
  return undefined;
49
65
  }
50
-
51
- return sodax.backendApi.getIntentByTxHash(txHash);
66
+ return sodax.backendApi.getIntentByTxHash(params.params.txHash);
52
67
  },
53
- refetchInterval,
54
- enabled: !!txHash && txHash.length > 0,
55
- retry: 3,
56
68
  });
57
69
  };