@sodax/dapp-kit 0.0.1-rc.2 → 0.0.1-rc.21
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/LICENSE +21 -0
- package/README.md +30 -37
- package/dist/contexts/index.d.ts +4 -1
- package/dist/contexts/index.d.ts.map +1 -1
- package/dist/core/index.d.ts +2 -3
- package/dist/core/index.d.ts.map +1 -1
- package/dist/hooks/mm/index.d.ts +3 -1
- package/dist/hooks/mm/index.d.ts.map +1 -1
- package/dist/hooks/mm/useBorrow.d.ts +13 -4
- package/dist/hooks/mm/useBorrow.d.ts.map +1 -1
- package/dist/hooks/mm/useMMAllowance.d.ts +26 -0
- package/dist/hooks/mm/useMMAllowance.d.ts.map +1 -0
- package/dist/hooks/mm/useMMApprove.d.ts +27 -0
- package/dist/hooks/mm/useMMApprove.d.ts.map +1 -0
- package/dist/hooks/mm/useRepay.d.ts +13 -4
- package/dist/hooks/mm/useRepay.d.ts.map +1 -1
- package/dist/hooks/mm/useReservesData.d.ts +19 -0
- package/dist/hooks/mm/useReservesData.d.ts.map +1 -0
- package/dist/hooks/mm/useReservesHumanized.d.ts +21 -0
- package/dist/hooks/mm/useReservesHumanized.d.ts.map +1 -0
- package/dist/hooks/mm/useReservesList.d.ts +18 -0
- package/dist/hooks/mm/useReservesList.d.ts.map +1 -0
- package/dist/hooks/mm/useReservesUsdFormat.d.ts +23 -0
- package/dist/hooks/mm/useReservesUsdFormat.d.ts.map +1 -0
- package/dist/hooks/mm/useSupply.d.ts +4 -2
- package/dist/hooks/mm/useSupply.d.ts.map +1 -1
- package/dist/hooks/mm/useUserFormattedSummary.d.ts +21 -0
- package/dist/hooks/mm/useUserFormattedSummary.d.ts.map +1 -0
- package/dist/hooks/mm/useUserReservesData.d.ts +19 -8
- package/dist/hooks/mm/useUserReservesData.d.ts.map +1 -1
- package/dist/hooks/mm/useWithdraw.d.ts +11 -4
- package/dist/hooks/mm/useWithdraw.d.ts.map +1 -1
- package/dist/hooks/provider/useHubProvider.d.ts +1 -1
- package/dist/hooks/provider/useHubProvider.d.ts.map +1 -1
- package/dist/hooks/provider/useSpokeProvider.d.ts +16 -2
- package/dist/hooks/provider/useSpokeProvider.d.ts.map +1 -1
- package/dist/hooks/shared/index.d.ts +1 -2
- package/dist/hooks/shared/index.d.ts.map +1 -1
- package/dist/hooks/shared/useEstimateGas.d.ts +4 -0
- package/dist/hooks/shared/useEstimateGas.d.ts.map +1 -0
- package/dist/hooks/swap/index.d.ts +4 -1
- package/dist/hooks/swap/index.d.ts.map +1 -1
- package/dist/hooks/swap/useCancelSwap.d.ts +29 -0
- package/dist/hooks/swap/useCancelSwap.d.ts.map +1 -0
- package/dist/hooks/swap/useQuote.d.ts +4 -4
- package/dist/hooks/swap/useQuote.d.ts.map +1 -1
- package/dist/hooks/swap/useStatus.d.ts +3 -3
- package/dist/hooks/swap/useStatus.d.ts.map +1 -1
- package/dist/hooks/swap/useSwap.d.ts +29 -0
- package/dist/hooks/swap/useSwap.d.ts.map +1 -0
- package/dist/hooks/swap/useSwapAllowance.d.ts +23 -0
- package/dist/hooks/swap/useSwapAllowance.d.ts.map +1 -0
- package/dist/hooks/swap/useSwapApprove.d.ts +25 -0
- package/dist/hooks/swap/useSwapApprove.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +244 -189
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +238 -186
- package/dist/index.mjs.map +1 -1
- package/dist/providers/SodaxProvider.d.ts +4 -2
- package/dist/providers/SodaxProvider.d.ts.map +1 -1
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +5 -6
- package/src/contexts/index.ts +4 -1
- package/src/core/index.ts +5 -33
- package/src/hooks/mm/index.ts +3 -1
- package/src/hooks/mm/useBorrow.ts +20 -10
- package/src/hooks/mm/useMMAllowance.ts +55 -0
- package/src/hooks/mm/useMMApprove.ts +67 -0
- package/src/hooks/mm/useRepay.ts +20 -10
- package/src/hooks/mm/useReservesData.ts +30 -0
- package/src/hooks/mm/useReservesHumanized.ts +30 -0
- package/src/hooks/mm/useReservesList.ts +29 -0
- package/src/hooks/mm/useReservesUsdFormat.ts +38 -0
- package/src/hooks/mm/useSupply.ts +9 -6
- package/src/hooks/mm/useUserFormattedSummary.ts +54 -0
- package/src/hooks/mm/useUserReservesData.ts +30 -48
- package/src/hooks/mm/useWithdraw.ts +17 -11
- package/src/hooks/provider/useHubProvider.ts +2 -20
- package/src/hooks/provider/useSpokeProvider.ts +91 -6
- package/src/hooks/shared/index.ts +1 -2
- package/src/hooks/shared/useEstimateGas.ts +18 -0
- package/src/hooks/swap/index.ts +4 -1
- package/src/hooks/swap/useCancelSwap.ts +44 -0
- package/src/hooks/swap/useQuote.ts +5 -5
- package/src/hooks/swap/useStatus.ts +3 -3
- package/src/hooks/swap/{useCreateIntentOrder.ts → useSwap.ts} +16 -18
- package/src/hooks/swap/useSwapAllowance.ts +47 -0
- package/src/hooks/swap/useSwapApprove.ts +68 -0
- package/src/index.ts +1 -0
- package/src/providers/SodaxProvider.tsx +22 -5
- package/src/types.ts +22 -0
- package/dist/hooks/mm/useHubWalletAddress.d.ts +0 -24
- package/dist/hooks/mm/useHubWalletAddress.d.ts.map +0 -1
- package/dist/hooks/shared/useAllowance.d.ts +0 -3
- package/dist/hooks/shared/useAllowance.d.ts.map +0 -1
- package/dist/hooks/shared/useApprove.d.ts +0 -10
- package/dist/hooks/shared/useApprove.d.ts.map +0 -1
- package/dist/hooks/swap/useCreateIntentOrder.d.ts +0 -33
- package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +0 -1
- package/src/hooks/mm/useHubWalletAddress.ts +0 -49
- package/src/hooks/shared/useAllowance.ts +0 -31
- package/src/hooks/shared/useApprove.ts +0 -53
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ReservesDataHumanized } from '@sodax/sdk';
|
|
2
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
|
+
/**
|
|
5
|
+
* Hook for fetching humanized reserves data from the Sodax money market.
|
|
6
|
+
*
|
|
7
|
+
* This hook provides access to the current state of all reserves (humanized format) in the money market protocol,
|
|
8
|
+
* including liquidity, interest rates, and other key metrics. The data is automatically
|
|
9
|
+
* fetched and cached using React Query.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const { data: reservesHumanized, isLoading, error } = useReservesHumanized();
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @returns A React Query result object containing:
|
|
17
|
+
* - data: The reserves humanized data when available
|
|
18
|
+
* - isLoading: Loading state indicator
|
|
19
|
+
* - error: Any error that occurred during data fetching
|
|
20
|
+
*/
|
|
21
|
+
export function useReservesHumanized(): UseQueryResult<ReservesDataHumanized, Error> {
|
|
22
|
+
const { sodax } = useSodaxContext();
|
|
23
|
+
|
|
24
|
+
return useQuery({
|
|
25
|
+
queryKey: ['reservesHumanized'],
|
|
26
|
+
queryFn: async () => {
|
|
27
|
+
return await sodax.moneyMarket.data.getReservesHumanized();
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useQuery } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hook for fetching list of reserves from the Sodax money market.
|
|
6
|
+
*
|
|
7
|
+
* This hook provides access to the list of addresses of all reserves in the money market protocol.
|
|
8
|
+
* The data is automatically fetched and cached using React Query.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const { data: reservesList, isLoading, error } = useReservesList();
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @returns A React Query result object containing:
|
|
16
|
+
* - data: The reserves list when available
|
|
17
|
+
* - isLoading: Loading state indicator
|
|
18
|
+
* - error: Any error that occurred during data fetching
|
|
19
|
+
*/
|
|
20
|
+
export function useReservesList() {
|
|
21
|
+
const { sodax } = useSodaxContext();
|
|
22
|
+
|
|
23
|
+
return useQuery({
|
|
24
|
+
queryKey: ['reservesList'],
|
|
25
|
+
queryFn: async () => {
|
|
26
|
+
return await sodax.moneyMarket.data.getReservesList();
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { FormatReserveUSDResponse, ReserveData } from '@sodax/sdk';
|
|
2
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for fetching formatted summary of Sodax user portfolio (holdings, total liquidity,
|
|
7
|
+
* collateral, borrows, liquidation threshold, health factor, available borrowing power, etc..).
|
|
8
|
+
*
|
|
9
|
+
* This hook provides access to the current state of user portfolio in the money market protocol.
|
|
10
|
+
* The data is automatically fetched and cached using React Query.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const { data: userFormattedSummary, isLoading, error } = useUserFormattedSummary();
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @returns A React Query result object containing:
|
|
18
|
+
* - data: The formatted summary of Sodax user portfolio when available
|
|
19
|
+
* - isLoading: Loading state indicator
|
|
20
|
+
* - error: Any error that occurred during data fetching
|
|
21
|
+
*/
|
|
22
|
+
export function useReservesUsdFormat(): UseQueryResult<
|
|
23
|
+
(ReserveData & { priceInMarketReferenceCurrency: string } & FormatReserveUSDResponse)[],
|
|
24
|
+
Error
|
|
25
|
+
> {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
|
|
28
|
+
return useQuery({
|
|
29
|
+
queryKey: ['reservesUsdFormat'],
|
|
30
|
+
queryFn: async () => {
|
|
31
|
+
// fetch reserves and hub wallet address
|
|
32
|
+
const reserves = await sodax.moneyMarket.data.getReservesHumanized();
|
|
33
|
+
|
|
34
|
+
// format reserves
|
|
35
|
+
return sodax.moneyMarket.data.formatReservesUSD(sodax.moneyMarket.data.buildReserveDataWithPrice(reserves));
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SpokeProvider } from '@sodax/sdk';
|
|
2
2
|
import type { XToken } from '@sodax/types';
|
|
3
3
|
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
4
4
|
import { parseUnits } from 'viem';
|
|
5
|
-
import { useSpokeProvider } from '../provider/useSpokeProvider';
|
|
6
5
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
7
6
|
|
|
8
7
|
interface SupplyResponse {
|
|
9
8
|
ok: true;
|
|
10
|
-
value: [
|
|
9
|
+
value: [string, string];
|
|
11
10
|
}
|
|
12
11
|
|
|
13
12
|
/**
|
|
@@ -18,6 +17,7 @@ interface SupplyResponse {
|
|
|
18
17
|
* and cross-chain communication.
|
|
19
18
|
*
|
|
20
19
|
* @param {XToken} spokeToken - The token to supply on the spoke chain. Must be an XToken with valid address and chain information.
|
|
20
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for the supply transaction. Must be a valid SpokeProvider instance.
|
|
21
21
|
*
|
|
22
22
|
* @returns {UseMutationResult<SupplyResponse, Error, string>} A mutation result object with the following properties:
|
|
23
23
|
* - mutateAsync: Function to execute the supply transaction
|
|
@@ -33,9 +33,11 @@ interface SupplyResponse {
|
|
|
33
33
|
* @throws {Error} When:
|
|
34
34
|
* - spokeProvider is not available
|
|
35
35
|
*/
|
|
36
|
-
export function useSupply(
|
|
36
|
+
export function useSupply(
|
|
37
|
+
spokeToken: XToken,
|
|
38
|
+
spokeProvider: SpokeProvider | undefined,
|
|
39
|
+
): UseMutationResult<SupplyResponse, Error, string> {
|
|
37
40
|
const { sodax } = useSodaxContext();
|
|
38
|
-
const spokeProvider = useSpokeProvider(spokeToken.xChainId as SpokeChainId);
|
|
39
41
|
|
|
40
42
|
return useMutation<SupplyResponse, Error, string>({
|
|
41
43
|
mutationFn: async (amount: string) => {
|
|
@@ -43,10 +45,11 @@ export function useSupply(spokeToken: XToken): UseMutationResult<SupplyResponse,
|
|
|
43
45
|
throw new Error('spokeProvider is not found');
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
const response = await sodax.moneyMarket.
|
|
48
|
+
const response = await sodax.moneyMarket.supply(
|
|
47
49
|
{
|
|
48
50
|
token: spokeToken.address,
|
|
49
51
|
amount: parseUnits(amount, spokeToken.decimals),
|
|
52
|
+
action: 'supply',
|
|
50
53
|
},
|
|
51
54
|
spokeProvider,
|
|
52
55
|
);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type FormatUserSummaryResponse, type FormatReserveUSDResponse, SpokeProvider } from '@sodax/sdk';
|
|
2
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for fetching formatted summary of Sodax user portfolio (holdings, total liquidity,
|
|
7
|
+
* collateral, borrows, liquidation threshold, health factor, available borrowing power, etc..).
|
|
8
|
+
*
|
|
9
|
+
* This hook provides access to the current state of user portfolio in the money market protocol.
|
|
10
|
+
* The data is automatically fetched and cached using React Query.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const { data: userFormattedSummary, isLoading, error } = useUserFormattedSummary(spokeProvider, address);
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @returns A React Query result object containing:
|
|
18
|
+
* - data: The formatted summary of Sodax user portfolio when available
|
|
19
|
+
* - isLoading: Loading state indicator
|
|
20
|
+
* - error: Any error that occurred during data fetching
|
|
21
|
+
*/
|
|
22
|
+
export function useUserFormattedSummary(
|
|
23
|
+
spokeProvider: SpokeProvider | undefined,
|
|
24
|
+
address: string | undefined,
|
|
25
|
+
): UseQueryResult<FormatUserSummaryResponse<FormatReserveUSDResponse>, Error> {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
|
|
28
|
+
return useQuery({
|
|
29
|
+
queryKey: ['userFormattedSummary', spokeProvider?.chainConfig.chain.id, address],
|
|
30
|
+
queryFn: async () => {
|
|
31
|
+
if (!spokeProvider || !address) {
|
|
32
|
+
throw new Error('Spoke provider or address is not defined');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// fetch reserves and hub wallet address
|
|
36
|
+
const reserves = await sodax.moneyMarket.data.getReservesHumanized();
|
|
37
|
+
|
|
38
|
+
// format reserves
|
|
39
|
+
const formattedReserves = sodax.moneyMarket.data.formatReservesUSD(
|
|
40
|
+
sodax.moneyMarket.data.buildReserveDataWithPrice(reserves),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
// fetch user reserves
|
|
44
|
+
const userReserves = await sodax.moneyMarket.data.getUserReservesHumanized(spokeProvider);
|
|
45
|
+
|
|
46
|
+
// format user summary
|
|
47
|
+
return sodax.moneyMarket.data.formatUserSummary(
|
|
48
|
+
sodax.moneyMarket.data.buildUserSummaryRequest(reserves, formattedReserves, userReserves),
|
|
49
|
+
);
|
|
50
|
+
},
|
|
51
|
+
enabled: !!spokeProvider && !!address,
|
|
52
|
+
refetchInterval: 5000,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -1,58 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type { HubChainId, SpokeChainId } from '@sodax/types';
|
|
4
|
-
import { useXAccount, useWalletProvider } from '@sodax/wallet-sdk';
|
|
5
|
-
import type { ChainId } from '@sodax/types';
|
|
6
|
-
import { useQuery } from '@tanstack/react-query';
|
|
7
|
-
import { useHubProvider } from '../provider/useHubProvider';
|
|
8
|
-
import { useHubWalletAddress } from './useHubWalletAddress';
|
|
1
|
+
import { SpokeProvider, type UserReserveData } from '@sodax/sdk';
|
|
2
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
9
3
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
10
4
|
|
|
11
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Hook for fetching user reserves data from the Sodax money market.
|
|
7
|
+
*
|
|
8
|
+
* This hook provides access to the current state of user reserves in the money market protocol.
|
|
9
|
+
* The data is automatically fetched and cached using React Query.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const { data: userReservesData, isLoading, error } = useUserReservesData(spokeProvider, address);
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @returns A React Query result object containing:
|
|
17
|
+
* - data: The user reserves data when available
|
|
18
|
+
* - isLoading: Loading state indicator
|
|
19
|
+
* - error: Any error that occurred during data fetching
|
|
20
|
+
*/
|
|
21
|
+
export function useUserReservesData(
|
|
22
|
+
spokeProvider: SpokeProvider | undefined,
|
|
23
|
+
address: string | undefined,
|
|
24
|
+
refetchInterval = 5000,
|
|
25
|
+
): UseQueryResult<readonly [readonly UserReserveData[], number], Error> {
|
|
12
26
|
const { sodax } = useSodaxContext();
|
|
13
|
-
const hubChainId = (sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? 'sonic') as HubChainId;
|
|
14
|
-
const hubWalletProvider = useWalletProvider(hubChainId);
|
|
15
|
-
const hubProvider = useHubProvider();
|
|
16
|
-
const { address } = useXAccount(spokeChainId);
|
|
17
|
-
const { data: hubWalletAddress } = useHubWalletAddress(
|
|
18
|
-
spokeChainId as SpokeChainId,
|
|
19
|
-
address,
|
|
20
|
-
hubProvider as EvmHubProvider,
|
|
21
|
-
);
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
queryKey: ['userReserves',
|
|
28
|
+
return useQuery({
|
|
29
|
+
queryKey: ['userReserves', spokeProvider?.chainConfig.chain.id, address],
|
|
25
30
|
queryFn: async () => {
|
|
26
|
-
if (!
|
|
27
|
-
|
|
31
|
+
if (!spokeProvider) {
|
|
32
|
+
throw new Error('Spoke provider or address is not defined');
|
|
28
33
|
}
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const moneyMarketConfig = getMoneyMarketConfig(hubChainId);
|
|
35
|
-
try {
|
|
36
|
-
const [res] = await sodax.moneyMarket.getUserReservesData(
|
|
37
|
-
hubWalletAddress as `0x${string}`,
|
|
38
|
-
moneyMarketConfig.uiPoolDataProvider,
|
|
39
|
-
moneyMarketConfig.poolAddressesProvider,
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
return res?.map(r => {
|
|
43
|
-
return {
|
|
44
|
-
...r,
|
|
45
|
-
token: allXTokens.find(t => t.address === r.underlyingAsset),
|
|
46
|
-
};
|
|
47
|
-
});
|
|
48
|
-
} catch (error) {
|
|
49
|
-
console.log('error', error);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
35
|
+
return await sodax.moneyMarket.data.getUserReservesData(spokeProvider);
|
|
52
36
|
},
|
|
53
|
-
enabled: !!
|
|
54
|
-
refetchInterval
|
|
37
|
+
enabled: !!spokeProvider && !!address,
|
|
38
|
+
refetchInterval,
|
|
55
39
|
});
|
|
56
|
-
|
|
57
|
-
return userReserves;
|
|
58
40
|
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { SpokeProvider } from '@sodax/sdk';
|
|
2
|
+
import type { XToken } from '@sodax/types';
|
|
3
3
|
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
4
4
|
import { parseUnits } from 'viem';
|
|
5
|
-
import { useSpokeProvider } from '../provider/useSpokeProvider';
|
|
6
5
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
7
6
|
|
|
8
7
|
interface WithdrawResponse {
|
|
9
8
|
ok: true;
|
|
10
|
-
value: [
|
|
9
|
+
value: [string, string];
|
|
11
10
|
}
|
|
12
11
|
|
|
13
12
|
/**
|
|
@@ -17,9 +16,15 @@ interface WithdrawResponse {
|
|
|
17
16
|
* handling the entire withdrawal process including transaction creation, submission,
|
|
18
17
|
* and cross-chain communication.
|
|
19
18
|
*
|
|
19
|
+
* @param {XToken} spokeToken - The token to withdraw from the spoke chain. Must be an XToken with valid address and chain information.
|
|
20
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for the withdraw transaction. Must be a valid SpokeProvider instance.
|
|
21
|
+
*
|
|
22
|
+
* @returns {UseMutationResult<WithdrawResponse, Error, string>} A mutation result object with the following properties:
|
|
23
|
+
* - mutateAsync: Function to execute the withdraw transaction
|
|
24
|
+
* - isPending: Boolean indicating if a transaction is in progress
|
|
20
25
|
* @example
|
|
21
26
|
* ```typescript
|
|
22
|
-
* const { mutateAsync: withdraw, isPending, error } = useWithdraw(
|
|
27
|
+
* const { mutateAsync: withdraw, isPending, error } = useWithdraw(spokeToken);
|
|
23
28
|
* await withdraw('100');
|
|
24
29
|
* ```
|
|
25
30
|
*
|
|
@@ -28,11 +33,10 @@ interface WithdrawResponse {
|
|
|
28
33
|
* - Transaction execution fails
|
|
29
34
|
*/
|
|
30
35
|
export function useWithdraw(
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
spokeToken: XToken,
|
|
37
|
+
spokeProvider: SpokeProvider | undefined,
|
|
33
38
|
): UseMutationResult<WithdrawResponse, Error, string> {
|
|
34
39
|
const { sodax } = useSodaxContext();
|
|
35
|
-
const spokeProvider = useSpokeProvider(spokeChainId as SpokeChainId);
|
|
36
40
|
|
|
37
41
|
return useMutation<WithdrawResponse, Error, string>({
|
|
38
42
|
mutationFn: async (amount: string) => {
|
|
@@ -40,10 +44,12 @@ export function useWithdraw(
|
|
|
40
44
|
throw new Error('spokeProvider is not found');
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
const response = await sodax.moneyMarket.
|
|
47
|
+
const response = await sodax.moneyMarket.withdraw(
|
|
44
48
|
{
|
|
45
|
-
token:
|
|
46
|
-
|
|
49
|
+
token: spokeToken.address,
|
|
50
|
+
// vault token on hub chain decimals is 18
|
|
51
|
+
amount: parseUnits(amount, 18),
|
|
52
|
+
action: 'withdraw',
|
|
47
53
|
},
|
|
48
54
|
spokeProvider,
|
|
49
55
|
);
|
|
@@ -1,26 +1,8 @@
|
|
|
1
|
-
import { EvmHubProvider
|
|
2
|
-
import { getXChainType } from '@sodax/wallet-sdk';
|
|
3
|
-
import { useMemo } from 'react';
|
|
1
|
+
import type { EvmHubProvider } from '@sodax/sdk';
|
|
4
2
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
5
3
|
|
|
6
4
|
export function useHubProvider(): EvmHubProvider | undefined {
|
|
7
|
-
const {
|
|
8
|
-
const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id;
|
|
9
|
-
const hubRpcUrl = sodax.config?.hubProviderConfig?.hubRpcUrl;
|
|
10
|
-
const xChainType = getXChainType(hubChainId);
|
|
11
|
-
const hubProvider = useMemo(() => {
|
|
12
|
-
if (xChainType === 'EVM' && hubChainId && hubRpcUrl) {
|
|
13
|
-
const hubChainCfg = getHubChainConfig(hubChainId);
|
|
14
|
-
|
|
15
|
-
if (!hubChainCfg) return undefined;
|
|
16
|
-
|
|
17
|
-
return new EvmHubProvider({
|
|
18
|
-
hubRpcUrl: hubRpcUrl,
|
|
19
|
-
chainConfig: hubChainCfg,
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
return undefined;
|
|
23
|
-
}, [xChainType, hubChainId, hubRpcUrl]);
|
|
5
|
+
const { hubProvider } = useSodaxContext();
|
|
24
6
|
|
|
25
7
|
return hubProvider;
|
|
26
8
|
}
|
|
@@ -1,33 +1,118 @@
|
|
|
1
|
+
import { useSodaxContext } from '@/index';
|
|
1
2
|
import {
|
|
2
3
|
EvmSpokeProvider,
|
|
3
4
|
spokeChainConfig,
|
|
4
5
|
type SuiSpokeChainConfig,
|
|
5
6
|
SuiSpokeProvider,
|
|
6
7
|
type EvmSpokeChainConfig,
|
|
8
|
+
IconSpokeProvider,
|
|
9
|
+
type IconSpokeChainConfig,
|
|
10
|
+
InjectiveSpokeProvider,
|
|
11
|
+
type InjectiveSpokeChainConfig,
|
|
12
|
+
StellarSpokeProvider,
|
|
13
|
+
type StellarSpokeChainConfig,
|
|
14
|
+
type SpokeProvider,
|
|
15
|
+
type IWalletProvider,
|
|
16
|
+
SolanaSpokeProvider,
|
|
17
|
+
type SolanaChainConfig,
|
|
18
|
+
SONIC_MAINNET_CHAIN_ID,
|
|
19
|
+
SonicSpokeProvider,
|
|
20
|
+
type SonicSpokeChainConfig,
|
|
7
21
|
} from '@sodax/sdk';
|
|
8
|
-
import type {
|
|
9
|
-
|
|
22
|
+
import type {
|
|
23
|
+
IEvmWalletProvider,
|
|
24
|
+
IIconWalletProvider,
|
|
25
|
+
ISuiWalletProvider,
|
|
26
|
+
SpokeChainId,
|
|
27
|
+
IInjectiveWalletProvider,
|
|
28
|
+
IStellarWalletProvider,
|
|
29
|
+
ISolanaWalletProvider,
|
|
30
|
+
} from '@sodax/types';
|
|
10
31
|
import { useMemo } from 'react';
|
|
11
32
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Hook to get the appropriate spoke provider based on the chain type.
|
|
35
|
+
* Supports EVM, SUI, ICON and INJECTIVE chains.
|
|
36
|
+
*
|
|
37
|
+
* @param {SpokeChainId | undefined} spokeChainId - The spoke chain ID to get the provider for
|
|
38
|
+
* @param {IWalletProvider | undefined} walletProvider - The wallet provider to use
|
|
39
|
+
* @returns {SpokeProvider | undefined} The appropriate spoke provider instance for the given chain ID, or undefined if invalid/unsupported
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```tsx
|
|
43
|
+
* // Using a specific SpokeChainId and wallet provider
|
|
44
|
+
* const spokeProvider = useSpokeProvider(spokeChainId, walletProvider);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function useSpokeProvider(
|
|
48
|
+
spokeChainId: SpokeChainId | undefined,
|
|
49
|
+
walletProvider?: IWalletProvider | undefined,
|
|
50
|
+
): SpokeProvider | undefined {
|
|
51
|
+
const { rpcConfig } = useSodaxContext();
|
|
52
|
+
const xChainType = spokeChainId ? spokeChainConfig[spokeChainId]?.chain.type : undefined;
|
|
53
|
+
|
|
15
54
|
const spokeProvider = useMemo(() => {
|
|
16
55
|
if (!walletProvider) return undefined;
|
|
56
|
+
if (!spokeChainId) return undefined;
|
|
57
|
+
if (!xChainType) return undefined;
|
|
58
|
+
if (!rpcConfig) return undefined;
|
|
59
|
+
|
|
17
60
|
if (xChainType === 'EVM') {
|
|
61
|
+
if (spokeChainId === SONIC_MAINNET_CHAIN_ID) {
|
|
62
|
+
return new SonicSpokeProvider(
|
|
63
|
+
walletProvider as IEvmWalletProvider,
|
|
64
|
+
spokeChainConfig[spokeChainId] as SonicSpokeChainConfig,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
18
67
|
return new EvmSpokeProvider(
|
|
19
68
|
walletProvider as IEvmWalletProvider,
|
|
20
69
|
spokeChainConfig[spokeChainId] as EvmSpokeChainConfig,
|
|
21
70
|
);
|
|
22
71
|
}
|
|
72
|
+
|
|
23
73
|
if (xChainType === 'SUI') {
|
|
24
74
|
return new SuiSpokeProvider(
|
|
25
75
|
spokeChainConfig[spokeChainId] as SuiSpokeChainConfig,
|
|
26
76
|
walletProvider as ISuiWalletProvider,
|
|
27
77
|
);
|
|
28
78
|
}
|
|
79
|
+
|
|
80
|
+
if (xChainType === 'ICON') {
|
|
81
|
+
return new IconSpokeProvider(
|
|
82
|
+
walletProvider as IIconWalletProvider,
|
|
83
|
+
spokeChainConfig[spokeChainId] as IconSpokeChainConfig,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (xChainType === 'INJECTIVE') {
|
|
88
|
+
return new InjectiveSpokeProvider(
|
|
89
|
+
spokeChainConfig[spokeChainId] as InjectiveSpokeChainConfig,
|
|
90
|
+
walletProvider as IInjectiveWalletProvider,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (xChainType === 'STELLAR') {
|
|
95
|
+
const stellarConfig = spokeChainConfig[spokeChainId] as StellarSpokeChainConfig;
|
|
96
|
+
return new StellarSpokeProvider(walletProvider as IStellarWalletProvider, stellarConfig, {
|
|
97
|
+
horizonRpcUrl: stellarConfig.horizonRpcUrl,
|
|
98
|
+
sorobanRpcUrl: stellarConfig.sorobanRpcUrl,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (xChainType === 'SOLANA') {
|
|
103
|
+
return new SolanaSpokeProvider(
|
|
104
|
+
walletProvider as ISolanaWalletProvider,
|
|
105
|
+
rpcConfig.solana
|
|
106
|
+
? ({
|
|
107
|
+
...spokeChainConfig[spokeChainId],
|
|
108
|
+
rpcUrl: rpcConfig.solana,
|
|
109
|
+
} as SolanaChainConfig)
|
|
110
|
+
: (spokeChainConfig[spokeChainId] as SolanaChainConfig),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
29
114
|
return undefined;
|
|
30
|
-
}, [
|
|
115
|
+
}, [spokeChainId, xChainType, walletProvider, rpcConfig]);
|
|
31
116
|
|
|
32
117
|
return spokeProvider;
|
|
33
118
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type GetEstimateGasReturnType, type SpokeProvider, SpokeService, type TxReturnType } from '@sodax/sdk';
|
|
2
|
+
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
3
|
+
|
|
4
|
+
export function useEstimateGas<T extends SpokeProvider = SpokeProvider>(
|
|
5
|
+
spokeProvider: T | undefined,
|
|
6
|
+
): UseMutationResult<GetEstimateGasReturnType<T>, Error, TxReturnType<T, true>> {
|
|
7
|
+
return useMutation<GetEstimateGasReturnType<T>, Error, TxReturnType<T, true>>({
|
|
8
|
+
mutationFn: async (rawTx: TxReturnType<T, true>) => {
|
|
9
|
+
if (!spokeProvider) {
|
|
10
|
+
throw new Error('spokeProvider is not found');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const response = await SpokeService.estimateGas(rawTx, spokeProvider);
|
|
14
|
+
|
|
15
|
+
return response;
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
}
|
package/src/hooks/swap/index.ts
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
2
|
+
import type { Intent, Result, SpokeProvider, TxReturnType } from '@sodax/sdk';
|
|
3
|
+
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
4
|
+
|
|
5
|
+
type CancelIntentParams = {
|
|
6
|
+
intent: Intent;
|
|
7
|
+
raw?: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type CancelIntentResult = Result<TxReturnType<SpokeProvider, boolean>>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Hook for canceling a swap intent order.
|
|
14
|
+
* Uses React Query's useMutation for better state management and caching.
|
|
15
|
+
*
|
|
16
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for canceling the intent
|
|
17
|
+
* @returns {UseMutationResult} Mutation result object containing mutation function and state
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const { mutateAsync: cancelSwap, isPending } = useCancelSwap(spokeProvider);
|
|
22
|
+
*
|
|
23
|
+
* const handleCancelSwap = async () => {
|
|
24
|
+
* const result = await cancelSwap({
|
|
25
|
+
* intent: intentObject,
|
|
26
|
+
* raw: false // optional, defaults to false
|
|
27
|
+
* });
|
|
28
|
+
* };
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function useCancelSwap(
|
|
32
|
+
spokeProvider: SpokeProvider | undefined,
|
|
33
|
+
): UseMutationResult<CancelIntentResult, Error, CancelIntentParams> {
|
|
34
|
+
const { sodax } = useSodaxContext();
|
|
35
|
+
|
|
36
|
+
return useMutation<CancelIntentResult, Error, CancelIntentParams>({
|
|
37
|
+
mutationFn: async ({ intent, raw = false }: CancelIntentParams) => {
|
|
38
|
+
if (!spokeProvider) {
|
|
39
|
+
throw new Error('Spoke provider not found');
|
|
40
|
+
}
|
|
41
|
+
return sodax.solver.cancelIntent(intent, spokeProvider, raw);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SolverErrorResponse, SolverIntentQuoteRequest, SolverIntentQuoteResponse, Result } from '@sodax/sdk';
|
|
2
2
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
3
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
4
4
|
|
|
@@ -7,9 +7,9 @@ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
|
7
7
|
*
|
|
8
8
|
* This hook provides real-time quote data for an intent-based swap.
|
|
9
9
|
*
|
|
10
|
-
* @param {
|
|
10
|
+
* @param {SolverIntentQuoteRequest | undefined} payload - The intent quote request parameters. If undefined, the query will be disabled.
|
|
11
11
|
*
|
|
12
|
-
* @returns {UseQueryResult<Result<
|
|
12
|
+
* @returns {UseQueryResult<Result<SolverIntentQuoteResponse, SolverErrorResponse> | undefined>} A query result object containing:
|
|
13
13
|
* - data: The quote result from the solver
|
|
14
14
|
* - isLoading: Boolean indicating if the quote is being fetched
|
|
15
15
|
* - error: Error object if the quote request failed
|
|
@@ -38,8 +38,8 @@ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
|
38
38
|
* - Uses React Query for efficient caching and state management
|
|
39
39
|
*/
|
|
40
40
|
export const useQuote = (
|
|
41
|
-
payload:
|
|
42
|
-
): UseQueryResult<Result<
|
|
41
|
+
payload: SolverIntentQuoteRequest | undefined,
|
|
42
|
+
): UseQueryResult<Result<SolverIntentQuoteResponse, SolverErrorResponse> | undefined> => {
|
|
43
43
|
const { sodax } = useSodaxContext();
|
|
44
44
|
return useQuery({
|
|
45
45
|
queryKey: [payload],
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Hex,
|
|
1
|
+
import type { Hex, SolverErrorResponse, SolverIntentStatusResponse, Result } from '@sodax/sdk';
|
|
2
2
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
3
3
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
4
|
|
|
@@ -9,7 +9,7 @@ import { useSodaxContext } from '../shared/useSodaxContext';
|
|
|
9
9
|
*
|
|
10
10
|
* @param {Hex} intent_tx_hash - The transaction hash of the intent order on the hub chain
|
|
11
11
|
*
|
|
12
|
-
* @returns {UseQueryResult<Result<
|
|
12
|
+
* @returns {UseQueryResult<Result<SolverIntentStatusResponse, SolverErrorResponse> | undefined>} A query result object containing:
|
|
13
13
|
* - data: The status result from the solver
|
|
14
14
|
* - isLoading: Boolean indicating if the status is being fetched
|
|
15
15
|
* - error: Error object if the status request failed
|
|
@@ -32,7 +32,7 @@ import { useSodaxContext } from '../shared/useSodaxContext';
|
|
|
32
32
|
|
|
33
33
|
export const useStatus = (
|
|
34
34
|
intent_tx_hash: Hex,
|
|
35
|
-
): UseQueryResult<Result<
|
|
35
|
+
): UseQueryResult<Result<SolverIntentStatusResponse, SolverErrorResponse> | undefined> => {
|
|
36
36
|
const { sodax } = useSodaxContext();
|
|
37
37
|
return useQuery({
|
|
38
38
|
queryKey: [intent_tx_hash],
|