@sodax/dapp-kit 1.2.4-beta → 1.2.6-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 +2 -1
- package/dist/index.d.mts +38 -22
- package/dist/index.d.ts +38 -22
- package/dist/index.js +39 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +39 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/hooks/mm/useUserFormattedSummary.ts +51 -22
- package/src/hooks/mm/useUserReservesData.ts +47 -13
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sodax/dapp-kit",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.6-beta",
|
|
5
5
|
"description": "dapp-kit of New World",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"viem": "2.29.2",
|
|
19
|
-
"@sodax/
|
|
20
|
-
"@sodax/
|
|
19
|
+
"@sodax/sdk": "1.2.6-beta",
|
|
20
|
+
"@sodax/types": "1.2.6-beta"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/react": "19.0.8",
|
|
@@ -1,28 +1,53 @@
|
|
|
1
|
+
import type { SpokeChainId } from '@sodax/types';
|
|
1
2
|
import type { FormatUserSummaryResponse, FormatReserveUSDResponse, SpokeProvider } from '@sodax/sdk';
|
|
2
3
|
import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
|
|
3
4
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
spokeProvider?: SpokeProvider;
|
|
7
|
-
address?: string;
|
|
6
|
+
type BaseQueryOptions = {
|
|
8
7
|
queryOptions?: UseQueryOptions<FormatUserSummaryResponse<FormatReserveUSDResponse>, Error>;
|
|
9
8
|
};
|
|
10
9
|
|
|
10
|
+
type NewParams = BaseQueryOptions & {
|
|
11
|
+
/** Spoke chain id (e.g. '0xa86a.avax') */
|
|
12
|
+
spokeChainId: SpokeChainId | undefined;
|
|
13
|
+
/** User wallet address on the spoke chain */
|
|
14
|
+
userAddress: string | undefined;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** @deprecated Use `{ spokeChainId, userAddress }` instead */
|
|
18
|
+
type LegacyParams = BaseQueryOptions & {
|
|
19
|
+
/** @deprecated Use `spokeChainId` instead */
|
|
20
|
+
spokeProvider: SpokeProvider | undefined;
|
|
21
|
+
/** @deprecated Use `userAddress` instead */
|
|
22
|
+
address: string | undefined;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type UseUserFormattedSummaryParams = NewParams | LegacyParams;
|
|
26
|
+
|
|
27
|
+
function isLegacyParams(params: UseUserFormattedSummaryParams): params is LegacyParams {
|
|
28
|
+
return 'spokeProvider' in params || 'address' in params;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function resolveParams(params: UseUserFormattedSummaryParams): {
|
|
32
|
+
spokeChainId: SpokeChainId | undefined;
|
|
33
|
+
userAddress: string | undefined;
|
|
34
|
+
} {
|
|
35
|
+
if (isLegacyParams(params)) {
|
|
36
|
+
return {
|
|
37
|
+
spokeChainId: params.spokeProvider?.chainConfig.chain.id as SpokeChainId | undefined,
|
|
38
|
+
userAddress: params.address,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return { spokeChainId: params.spokeChainId, userAddress: params.userAddress };
|
|
42
|
+
}
|
|
43
|
+
|
|
11
44
|
/**
|
|
12
45
|
* React hook to fetch a formatted summary of a user's Sodax money market portfolio.
|
|
13
46
|
*
|
|
14
|
-
*
|
|
15
|
-
* -
|
|
16
|
-
* -
|
|
17
|
-
* -
|
|
18
|
-
*
|
|
19
|
-
* The hook returns a React Query result object containing the formatted summary, loading and error state.
|
|
20
|
-
* The query is enabled only if both the spokeProvider and address are provided.
|
|
21
|
-
*
|
|
22
|
-
* @param params Optional parameters:
|
|
23
|
-
* - spokeProvider: SpokeProvider to query chain data (required for enabled query)
|
|
24
|
-
* - address: User account address (required for enabled query)
|
|
25
|
-
* - queryOptions: React Query options for customization (optional)
|
|
47
|
+
* @param params (optional) - Object including:
|
|
48
|
+
* - spokeChainId: The spoke chain id whose data will be fetched. If not provided, data fetching is disabled.
|
|
49
|
+
* - userAddress: The user's address (string) whose summary will be fetched. If not provided, data fetching is disabled.
|
|
50
|
+
* - queryOptions: (optional) Custom React Query options such as `queryKey`, `enabled`, or cache policy.
|
|
26
51
|
*
|
|
27
52
|
* @returns {UseQueryResult<FormatUserSummaryResponse<FormatReserveUSDResponse>, Error>}
|
|
28
53
|
* A result object from React Query including:
|
|
@@ -32,28 +57,29 @@ export type UseUserFormattedSummaryParams = {
|
|
|
32
57
|
* - error: Error if thrown in fetching
|
|
33
58
|
*
|
|
34
59
|
* @example
|
|
35
|
-
* const { data, isLoading, error } = useUserFormattedSummary({
|
|
60
|
+
* const { data, isLoading, error } = useUserFormattedSummary({ spokeChainId, userAddress });
|
|
36
61
|
*/
|
|
37
62
|
export function useUserFormattedSummary(
|
|
38
63
|
params?: UseUserFormattedSummaryParams,
|
|
39
64
|
): UseQueryResult<FormatUserSummaryResponse<FormatReserveUSDResponse>, Error> {
|
|
40
65
|
const { sodax } = useSodaxContext();
|
|
66
|
+
const resolved = params ? resolveParams(params) : { spokeChainId: undefined, userAddress: undefined };
|
|
41
67
|
const defaultQueryOptions = {
|
|
42
|
-
queryKey: ['mm', 'userFormattedSummary',
|
|
43
|
-
enabled: !!
|
|
68
|
+
queryKey: ['mm', 'userFormattedSummary', resolved.spokeChainId, resolved.userAddress],
|
|
69
|
+
enabled: !!resolved.spokeChainId && !!resolved.userAddress,
|
|
44
70
|
refetchInterval: 5000,
|
|
45
71
|
};
|
|
46
72
|
|
|
47
73
|
const queryOptions = {
|
|
48
74
|
...defaultQueryOptions,
|
|
49
75
|
...params?.queryOptions, // override default query options if provided
|
|
50
|
-
}
|
|
76
|
+
};
|
|
51
77
|
|
|
52
78
|
return useQuery({
|
|
53
79
|
...queryOptions,
|
|
54
80
|
queryFn: async () => {
|
|
55
|
-
if (!
|
|
56
|
-
throw new Error('
|
|
81
|
+
if (!resolved.spokeChainId || !resolved.userAddress) {
|
|
82
|
+
throw new Error('spokeChainId or userAddress is not defined');
|
|
57
83
|
}
|
|
58
84
|
|
|
59
85
|
// fetch reserves and hub wallet address
|
|
@@ -65,7 +91,10 @@ export function useUserFormattedSummary(
|
|
|
65
91
|
);
|
|
66
92
|
|
|
67
93
|
// fetch user reserves
|
|
68
|
-
const userReserves = await sodax.moneyMarket.data.getUserReservesHumanized(
|
|
94
|
+
const userReserves = await sodax.moneyMarket.data.getUserReservesHumanized(
|
|
95
|
+
resolved.spokeChainId,
|
|
96
|
+
resolved.userAddress,
|
|
97
|
+
);
|
|
69
98
|
|
|
70
99
|
// format user summary
|
|
71
100
|
return sodax.moneyMarket.data.formatUserSummary(
|
|
@@ -1,19 +1,52 @@
|
|
|
1
|
+
import type { SpokeChainId } from '@sodax/types';
|
|
1
2
|
import type { SpokeProvider, UserReserveData } from '@sodax/sdk';
|
|
2
3
|
import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
|
|
3
4
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
type BaseQueryOptions = {
|
|
7
|
+
queryOptions?: UseQueryOptions<readonly [readonly UserReserveData[], number], Error>;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type NewParams = BaseQueryOptions & {
|
|
11
|
+
/** Spoke chain id (e.g. '0xa86a.avax') */
|
|
12
|
+
spokeChainId: SpokeChainId | undefined;
|
|
13
|
+
/** User wallet address on the spoke chain */
|
|
14
|
+
userAddress: string | undefined;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** @deprecated Use `{ spokeChainId, userAddress }` instead */
|
|
18
|
+
type LegacyParams = BaseQueryOptions & {
|
|
19
|
+
/** @deprecated Use `spokeChainId` instead */
|
|
6
20
|
spokeProvider: SpokeProvider | undefined;
|
|
21
|
+
/** @deprecated Use `userAddress` instead */
|
|
7
22
|
address: string | undefined;
|
|
8
|
-
queryOptions?: UseQueryOptions<readonly [readonly UserReserveData[], number], Error>;
|
|
9
23
|
};
|
|
10
24
|
|
|
25
|
+
export type UseUserReservesDataParams = NewParams | LegacyParams;
|
|
26
|
+
|
|
27
|
+
function isLegacyParams(params: UseUserReservesDataParams): params is LegacyParams {
|
|
28
|
+
return 'spokeProvider' in params || 'address' in params;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function resolveParams(params: UseUserReservesDataParams): {
|
|
32
|
+
spokeChainId: SpokeChainId | undefined;
|
|
33
|
+
userAddress: string | undefined;
|
|
34
|
+
} {
|
|
35
|
+
if (isLegacyParams(params)) {
|
|
36
|
+
return {
|
|
37
|
+
spokeChainId: params.spokeProvider?.chainConfig.chain.id as SpokeChainId | undefined,
|
|
38
|
+
userAddress: params.address,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return { spokeChainId: params.spokeChainId, userAddress: params.userAddress };
|
|
42
|
+
}
|
|
43
|
+
|
|
11
44
|
/**
|
|
12
45
|
* Hook for fetching user reserves data from the Sodax money market.
|
|
13
46
|
*
|
|
14
47
|
* @param params (optional) - Object including:
|
|
15
|
-
* -
|
|
16
|
-
* -
|
|
48
|
+
* - spokeChainId: The spoke chain id whose reserves data will be fetched. If not provided, data fetching is disabled.
|
|
49
|
+
* - userAddress: The user's address (string) whose reserves data will be fetched. If not provided, data fetching is disabled.
|
|
17
50
|
* - queryOptions: (optional) Custom React Query options such as `queryKey`, `enabled`, or cache policy.
|
|
18
51
|
*
|
|
19
52
|
* @returns {UseQueryResult<readonly [readonly UserReserveData[], number], Error>} React Query result object containing:
|
|
@@ -24,32 +57,33 @@ export type UseUserReservesDataParams = {
|
|
|
24
57
|
*
|
|
25
58
|
* @example
|
|
26
59
|
* const { data: userReservesData, isLoading, error } = useUserReservesData({
|
|
27
|
-
*
|
|
28
|
-
*
|
|
60
|
+
* spokeChainId,
|
|
61
|
+
* userAddress,
|
|
29
62
|
* });
|
|
30
63
|
*/
|
|
31
64
|
export function useUserReservesData(
|
|
32
65
|
params?: UseUserReservesDataParams,
|
|
33
66
|
): UseQueryResult<readonly [readonly UserReserveData[], number], Error> {
|
|
34
67
|
const { sodax } = useSodaxContext();
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
68
|
+
const resolved = params ? resolveParams(params) : { spokeChainId: undefined, userAddress: undefined };
|
|
69
|
+
const defaultQueryOptions = {
|
|
70
|
+
queryKey: ['mm', 'userReservesData', resolved.spokeChainId, resolved.userAddress],
|
|
71
|
+
enabled: !!resolved.spokeChainId && !!resolved.userAddress,
|
|
38
72
|
refetchInterval: 5000,
|
|
39
73
|
};
|
|
40
74
|
const queryOptions = {
|
|
41
75
|
...defaultQueryOptions,
|
|
42
|
-
...params?.queryOptions,
|
|
76
|
+
...params?.queryOptions, // override default query options if provided
|
|
43
77
|
};
|
|
44
78
|
|
|
45
79
|
return useQuery({
|
|
46
80
|
...queryOptions,
|
|
47
81
|
queryFn: async () => {
|
|
48
|
-
if (!
|
|
49
|
-
throw new Error('
|
|
82
|
+
if (!resolved.spokeChainId || !resolved.userAddress) {
|
|
83
|
+
throw new Error('spokeChainId or userAddress is not defined');
|
|
50
84
|
}
|
|
51
85
|
|
|
52
|
-
return await sodax.moneyMarket.data.getUserReservesData(
|
|
86
|
+
return await sodax.moneyMarket.data.getUserReservesData(resolved.spokeChainId, resolved.userAddress);
|
|
53
87
|
},
|
|
54
88
|
});
|
|
55
89
|
}
|