@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.
- package/README.md +130 -39
- package/dist/index.d.mts +541 -347
- package/dist/index.d.ts +541 -347
- package/dist/index.js +317 -176
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +315 -178
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/hooks/backend/README.md +148 -49
- package/src/hooks/backend/index.ts +2 -0
- package/src/hooks/backend/types.ts +4 -0
- package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +31 -20
- package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +25 -7
- package/src/hooks/backend/useBackendIntentByHash.ts +36 -26
- package/src/hooks/backend/useBackendIntentByTxHash.ts +41 -29
- package/src/hooks/backend/useBackendMoneyMarketAsset.ts +40 -27
- package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +45 -36
- package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +45 -36
- package/src/hooks/backend/useBackendMoneyMarketPosition.ts +34 -37
- package/src/hooks/backend/useBackendOrderbook.ts +38 -38
- package/src/hooks/backend/useBackendUserIntents.ts +81 -0
- package/src/hooks/mm/index.ts +1 -0
- package/src/hooks/mm/useAToken.ts +37 -20
- package/src/hooks/mm/useBorrow.ts +36 -36
- package/src/hooks/mm/useMMAllowance.ts +33 -24
- package/src/hooks/mm/useMMApprove.ts +43 -48
- package/src/hooks/mm/useRepay.ts +32 -36
- package/src/hooks/mm/useReservesData.ts +35 -16
- package/src/hooks/mm/useReservesHumanized.ts +15 -3
- package/src/hooks/mm/useReservesList.ts +28 -15
- package/src/hooks/mm/useReservesUsdFormat.ts +30 -21
- package/src/hooks/mm/useSupply.ts +34 -36
- package/src/hooks/mm/useUserFormattedSummary.ts +42 -20
- package/src/hooks/mm/useUserReservesData.ts +34 -19
- package/src/hooks/mm/useWithdraw.ts +33 -35
- package/src/hooks/swap/index.ts +2 -0
- package/src/hooks/swap/useCancelLimitOrder.ts +53 -0
- package/src/hooks/swap/useCreateLimitOrder.ts +72 -0
- package/src/hooks/swap/useSwapAllowance.ts +7 -7
- 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
|
-
*
|
|
14
|
+
* React hook to fetch a specific money market asset from the backend API.
|
|
8
15
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
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
|
-
* @
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* -
|
|
17
|
-
* -
|
|
18
|
-
* -
|
|
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
|
-
*
|
|
23
|
-
*
|
|
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
|
-
* -
|
|
36
|
-
* - Uses React Query for
|
|
37
|
-
* -
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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 {
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
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>}
|
|
18
|
-
* - data
|
|
19
|
-
* - isLoading
|
|
20
|
-
* - error
|
|
21
|
-
* - refetch
|
|
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
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
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
|
|
41
|
-
* - Uses React Query for
|
|
42
|
-
* -
|
|
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 = (
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
64
|
+
...queryOptions,
|
|
54
65
|
queryFn: async (): Promise<MoneyMarketAssetBorrowers | undefined> => {
|
|
55
|
-
if (!params
|
|
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
|
-
*
|
|
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 {
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
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>}
|
|
18
|
-
* - data
|
|
19
|
-
* - isLoading
|
|
20
|
-
* - error
|
|
21
|
-
* - refetch
|
|
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
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
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
|
|
41
|
-
* - Uses React Query for efficient caching and
|
|
42
|
-
* -
|
|
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 = (
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
64
|
+
...queryOptions,
|
|
54
65
|
queryFn: async (): Promise<MoneyMarketAssetSuppliers | undefined> => {
|
|
55
|
-
if (!params
|
|
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
|
-
|
|
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
|
-
*
|
|
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 {
|
|
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>}
|
|
16
|
-
* - data: The money market position data
|
|
17
|
-
* - isLoading:
|
|
18
|
-
* - error: Error
|
|
19
|
-
* - refetch: Function to manually trigger a
|
|
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
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* and
|
|
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>}
|
|
18
|
-
* - data
|
|
19
|
-
* - isLoading
|
|
20
|
-
* - error
|
|
21
|
-
* - refetch
|
|
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
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
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
|
-
* -
|
|
40
|
-
* -
|
|
41
|
-
* -
|
|
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:
|
|
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
|
-
|
|
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
|
+
};
|
package/src/hooks/mm/index.ts
CHANGED