@sodax/dapp-kit 0.0.1-rc.6 → 0.0.1-rc.8
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 +11 -10
- package/dist/hooks/mm/index.d.ts +2 -0
- package/dist/hooks/mm/index.d.ts.map +1 -1
- package/dist/hooks/mm/useBorrow.d.ts +10 -1
- 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 +10 -1
- package/dist/hooks/mm/useRepay.d.ts.map +1 -1
- package/dist/hooks/mm/useSupply.d.ts +3 -1
- package/dist/hooks/mm/useSupply.d.ts.map +1 -1
- package/dist/hooks/mm/useUserReservesData.d.ts +1 -1
- package/dist/hooks/mm/useUserReservesData.d.ts.map +1 -1
- package/dist/hooks/mm/useWithdraw.d.ts +8 -1
- package/dist/hooks/mm/useWithdraw.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 +0 -2
- package/dist/hooks/shared/index.d.ts.map +1 -1
- package/dist/hooks/swap/index.d.ts +2 -0
- package/dist/hooks/swap/index.d.ts.map +1 -1
- package/dist/hooks/swap/useCreateIntentOrder.d.ts +4 -4
- package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +1 -1
- 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 +26 -0
- package/dist/hooks/swap/useSwapApprove.d.ts.map +1 -0
- package/dist/index.js +161 -101
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +159 -101
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/hooks/mm/index.ts +2 -0
- package/src/hooks/mm/useBorrow.ts +14 -5
- package/src/hooks/mm/useMMAllowance.ts +55 -0
- package/src/hooks/mm/useMMApprove.ts +67 -0
- package/src/hooks/mm/useRepay.ts +14 -4
- package/src/hooks/mm/useSupply.ts +7 -4
- package/src/hooks/mm/useUserReservesData.ts +6 -12
- package/src/hooks/mm/useWithdraw.ts +12 -4
- package/src/hooks/provider/useSpokeProvider.ts +44 -8
- package/src/hooks/shared/index.ts +0 -2
- package/src/hooks/swap/index.ts +2 -0
- package/src/hooks/swap/useCreateIntentOrder.ts +4 -6
- package/src/hooks/swap/useSwapAllowance.ts +44 -0
- package/src/hooks/swap/useSwapApprove.ts +68 -0
- 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/src/hooks/shared/useAllowance.ts +0 -31
- package/src/hooks/shared/useApprove.ts +0 -53
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type { XToken } from '@sodax/types';
|
|
2
2
|
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
3
3
|
import { parseUnits } from 'viem';
|
|
4
|
-
import { useSpokeProvider } from '../provider/useSpokeProvider';
|
|
5
4
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
6
|
-
import type {
|
|
5
|
+
import type { SpokeProvider } from '@sodax/sdk';
|
|
7
6
|
interface BorrowResponse {
|
|
8
7
|
ok: true;
|
|
9
8
|
value: [`0x${string}`, `0x${string}`];
|
|
@@ -16,6 +15,14 @@ interface BorrowResponse {
|
|
|
16
15
|
* handling the entire borrow process including transaction creation, submission,
|
|
17
16
|
* and cross-chain communication.
|
|
18
17
|
*
|
|
18
|
+
* @param {XToken} spokeToken - The token to borrow from the spoke chain. Must be an XToken with valid address and chain information.
|
|
19
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for the borrow transaction. Must be a valid SpokeProvider instance.
|
|
20
|
+
*
|
|
21
|
+
* @returns {UseMutationResult<BorrowResponse, Error, string>} A mutation result object with the following properties:
|
|
22
|
+
* - mutateAsync: Function to execute the borrow transaction
|
|
23
|
+
* - isPending: Boolean indicating if a transaction is in progress
|
|
24
|
+
* - error: Error object if the last transaction failed, null otherwise
|
|
25
|
+
*
|
|
19
26
|
* @example
|
|
20
27
|
* ```typescript
|
|
21
28
|
* const { mutateAsync: borrow, isPending, error } = useBorrow(spokeToken);
|
|
@@ -26,9 +33,11 @@ interface BorrowResponse {
|
|
|
26
33
|
* - spokeProvider is not available
|
|
27
34
|
* - Transaction execution fails
|
|
28
35
|
*/
|
|
29
|
-
export function useBorrow(
|
|
36
|
+
export function useBorrow(
|
|
37
|
+
spokeToken: XToken,
|
|
38
|
+
spokeProvider: SpokeProvider | undefined,
|
|
39
|
+
): UseMutationResult<BorrowResponse, Error, string> {
|
|
30
40
|
const { sodax } = useSodaxContext();
|
|
31
|
-
const spokeProvider = useSpokeProvider(spokeToken.xChainId as SpokeChainId);
|
|
32
41
|
|
|
33
42
|
return useMutation<BorrowResponse, Error, string>({
|
|
34
43
|
mutationFn: async (amount: string) => {
|
|
@@ -40,12 +49,12 @@ export function useBorrow(spokeToken: XToken): UseMutationResult<BorrowResponse,
|
|
|
40
49
|
{
|
|
41
50
|
token: spokeToken.address,
|
|
42
51
|
amount: parseUnits(amount, 18),
|
|
52
|
+
action: 'borrow',
|
|
43
53
|
},
|
|
44
54
|
spokeProvider,
|
|
45
55
|
);
|
|
46
56
|
|
|
47
57
|
if (!response.ok) {
|
|
48
|
-
console.log('Failed to borrow tokens', response);
|
|
49
58
|
throw new Error('Failed to borrow tokens');
|
|
50
59
|
}
|
|
51
60
|
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { XToken } from '@sodax/types';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
|
+
import { parseUnits } from 'viem';
|
|
5
|
+
import type { MoneyMarketAction, SpokeProvider } from '@sodax/sdk';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Hook for checking token allowance for money market operations.
|
|
9
|
+
*
|
|
10
|
+
* This hook verifies if the user has approved enough tokens for a specific money market action
|
|
11
|
+
* (borrow/repay). It automatically queries and tracks the allowance status.
|
|
12
|
+
*
|
|
13
|
+
* @param {XToken} token - The token to check allowance for. Must be an XToken with valid address and chain information.
|
|
14
|
+
* @param {string} amount - The amount to check allowance for, as a decimal string
|
|
15
|
+
* @param {MoneyMarketAction} action - The money market action to check allowance for ('borrow' or 'repay')
|
|
16
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for allowance checks
|
|
17
|
+
*
|
|
18
|
+
* @returns {UseQueryResult<boolean, Error>} A React Query result containing:
|
|
19
|
+
* - data: Boolean indicating if allowance is sufficient
|
|
20
|
+
* - isLoading: Loading state indicator
|
|
21
|
+
* - error: Any error that occurred during the check
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const { data: hasAllowed, isLoading } = useMMAllowance(token, "100", "repay", provider);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function useMMAllowance(
|
|
29
|
+
token: XToken,
|
|
30
|
+
amount: string,
|
|
31
|
+
action: MoneyMarketAction,
|
|
32
|
+
spokeProvider: SpokeProvider | undefined,
|
|
33
|
+
): UseQueryResult<boolean, Error> {
|
|
34
|
+
const { sodax } = useSodaxContext();
|
|
35
|
+
|
|
36
|
+
return useQuery({
|
|
37
|
+
queryKey: ['allowance', token.address, amount, action],
|
|
38
|
+
queryFn: async () => {
|
|
39
|
+
if (!spokeProvider) throw new Error('Spoke provider is required');
|
|
40
|
+
const allowance = await sodax.moneyMarket.isAllowanceValid(
|
|
41
|
+
{
|
|
42
|
+
token: token.address,
|
|
43
|
+
amount: parseUnits(amount, token.decimals),
|
|
44
|
+
action,
|
|
45
|
+
},
|
|
46
|
+
spokeProvider,
|
|
47
|
+
);
|
|
48
|
+
if (allowance.ok) {
|
|
49
|
+
return allowance.value;
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
},
|
|
53
|
+
enabled: !!spokeProvider,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
2
|
+
import type { XToken } from '@sodax/types';
|
|
3
|
+
import { parseUnits } from 'viem';
|
|
4
|
+
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
5
|
+
import type { MoneyMarketAction, SpokeProvider } from '@sodax/sdk';
|
|
6
|
+
|
|
7
|
+
interface UseApproveReturn {
|
|
8
|
+
approve: ({ amount, action }: { amount: string; action: MoneyMarketAction }) => Promise<boolean>;
|
|
9
|
+
isLoading: boolean;
|
|
10
|
+
error: Error | null;
|
|
11
|
+
resetError: () => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Hook for approving token spending for money market actions
|
|
16
|
+
* @param token The token to approve spending for
|
|
17
|
+
* @param spokeProvider The spoke provider instance for the chain
|
|
18
|
+
* @returns Object containing approve function, loading state, error state and reset function
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* const { approve, isLoading, error } = useMMApprove(token, spokeProvider);
|
|
22
|
+
*
|
|
23
|
+
* // Approve tokens for supply action
|
|
24
|
+
* await approve({ amount: "100", action: "supply" });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
export function useMMApprove(token: XToken, spokeProvider: SpokeProvider | undefined): UseApproveReturn {
|
|
29
|
+
const { sodax } = useSodaxContext();
|
|
30
|
+
const queryClient = useQueryClient();
|
|
31
|
+
|
|
32
|
+
const {
|
|
33
|
+
mutateAsync: approve,
|
|
34
|
+
isPending,
|
|
35
|
+
error,
|
|
36
|
+
reset: resetError,
|
|
37
|
+
} = useMutation({
|
|
38
|
+
mutationFn: async ({ amount, action }: { amount: string; action: MoneyMarketAction }) => {
|
|
39
|
+
if (!spokeProvider) {
|
|
40
|
+
throw new Error('Spoke provider not found');
|
|
41
|
+
}
|
|
42
|
+
const allowance = await sodax.moneyMarket.approve(
|
|
43
|
+
{
|
|
44
|
+
token: token.address,
|
|
45
|
+
amount: parseUnits(amount, token.decimals),
|
|
46
|
+
action,
|
|
47
|
+
},
|
|
48
|
+
spokeProvider,
|
|
49
|
+
);
|
|
50
|
+
if (!allowance.ok) {
|
|
51
|
+
throw new Error('Failed to approve tokens');
|
|
52
|
+
}
|
|
53
|
+
return allowance.ok;
|
|
54
|
+
},
|
|
55
|
+
onSuccess: () => {
|
|
56
|
+
// Invalidate allowance query to refetch the new allowance
|
|
57
|
+
queryClient.invalidateQueries({ queryKey: ['allowance', token.address] });
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
approve,
|
|
63
|
+
isLoading: isPending,
|
|
64
|
+
error: error,
|
|
65
|
+
resetError,
|
|
66
|
+
};
|
|
67
|
+
}
|
package/src/hooks/mm/useRepay.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
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 RepayResponse {
|
|
@@ -17,6 +16,14 @@ interface RepayResponse {
|
|
|
17
16
|
* handling the entire repayment process including transaction creation, submission,
|
|
18
17
|
* and cross-chain communication.
|
|
19
18
|
*
|
|
19
|
+
* @param {XToken} spokeToken - The token to repay 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 repay transaction. Must be a valid SpokeProvider instance.
|
|
21
|
+
*
|
|
22
|
+
* @returns {UseMutationResult<RepayResponse, Error, string>} A mutation result object with the following properties:
|
|
23
|
+
* - mutateAsync: Function to execute the repay transaction
|
|
24
|
+
* - isPending: Boolean indicating if a transaction is in progress
|
|
25
|
+
* - error: Error object if the last transaction failed, null otherwise
|
|
26
|
+
*
|
|
20
27
|
* @example
|
|
21
28
|
* ```typescript
|
|
22
29
|
* const { mutateAsync: repay, isPending, error } = useRepay(spokeToken);
|
|
@@ -27,9 +34,11 @@ interface RepayResponse {
|
|
|
27
34
|
* - spokeProvider is not available
|
|
28
35
|
* - Transaction execution fails
|
|
29
36
|
*/
|
|
30
|
-
export function useRepay(
|
|
37
|
+
export function useRepay(
|
|
38
|
+
spokeToken: XToken,
|
|
39
|
+
spokeProvider: SpokeProvider | undefined,
|
|
40
|
+
): UseMutationResult<RepayResponse, Error, string> {
|
|
31
41
|
const { sodax } = useSodaxContext();
|
|
32
|
-
const spokeProvider = useSpokeProvider(spokeToken.xChainId as SpokeChainId);
|
|
33
42
|
|
|
34
43
|
return useMutation<RepayResponse, Error, string>({
|
|
35
44
|
mutationFn: async (amount: string) => {
|
|
@@ -41,6 +50,7 @@ export function useRepay(spokeToken: XToken): UseMutationResult<RepayResponse, E
|
|
|
41
50
|
{
|
|
42
51
|
token: spokeToken.address,
|
|
43
52
|
amount: parseUnits(amount, spokeToken.decimals),
|
|
53
|
+
action: 'repay',
|
|
44
54
|
},
|
|
45
55
|
spokeProvider,
|
|
46
56
|
);
|
|
@@ -1,8 +1,7 @@
|
|
|
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 {
|
|
@@ -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) => {
|
|
@@ -47,6 +49,7 @@ export function useSupply(spokeToken: XToken): UseMutationResult<SupplyResponse,
|
|
|
47
49
|
{
|
|
48
50
|
token: spokeToken.address,
|
|
49
51
|
amount: parseUnits(amount, spokeToken.decimals),
|
|
52
|
+
action: 'supply',
|
|
50
53
|
},
|
|
51
54
|
spokeProvider,
|
|
52
55
|
);
|
|
@@ -1,30 +1,24 @@
|
|
|
1
1
|
import { allXTokens } from '@/core';
|
|
2
|
-
import { EvmWalletAbstraction, getMoneyMarketConfig, type EvmHubProvider } from '@sodax/sdk';
|
|
2
|
+
import { encodeAddress, EvmWalletAbstraction, getMoneyMarketConfig, type EvmHubProvider } from '@sodax/sdk';
|
|
3
3
|
import type { HubChainId, SpokeChainId } from '@sodax/types';
|
|
4
4
|
import type { ChainId } from '@sodax/types';
|
|
5
5
|
import { useQuery } from '@tanstack/react-query';
|
|
6
6
|
import { useHubProvider } from '../provider/useHubProvider';
|
|
7
7
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
8
|
-
import { useSpokeProvider } from '../provider/useSpokeProvider';
|
|
9
8
|
|
|
10
|
-
export function useUserReservesData(spokeChainId: ChainId) {
|
|
9
|
+
export function useUserReservesData(spokeChainId: ChainId, address: string | undefined) {
|
|
11
10
|
const { sodax } = useSodaxContext();
|
|
12
11
|
const hubChainId = (sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? 'sonic') as HubChainId;
|
|
13
12
|
const hubProvider = useHubProvider();
|
|
14
|
-
const spokeProvider = useSpokeProvider(spokeChainId as SpokeChainId);
|
|
15
13
|
|
|
16
14
|
const { data: userReserves } = useQuery({
|
|
17
|
-
queryKey: ['userReserves', spokeChainId],
|
|
15
|
+
queryKey: ['userReserves', spokeChainId, address],
|
|
18
16
|
queryFn: async () => {
|
|
19
|
-
if (!hubProvider) {
|
|
17
|
+
if (!hubProvider || !address) {
|
|
20
18
|
return;
|
|
21
19
|
}
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const addressBytes = await spokeProvider.walletProvider.getWalletAddressBytes();
|
|
21
|
+
const addressBytes = encodeAddress(spokeChainId, address);
|
|
28
22
|
const hubWalletAddress = await EvmWalletAbstraction.getUserHubWalletAddress(
|
|
29
23
|
spokeChainId as SpokeChainId,
|
|
30
24
|
addressBytes,
|
|
@@ -45,7 +39,7 @@ export function useUserReservesData(spokeChainId: ChainId) {
|
|
|
45
39
|
};
|
|
46
40
|
});
|
|
47
41
|
},
|
|
48
|
-
enabled: !!
|
|
42
|
+
enabled: !!spokeChainId && !!hubProvider && !!address,
|
|
49
43
|
refetchInterval: 5000,
|
|
50
44
|
});
|
|
51
45
|
|
|
@@ -1,8 +1,7 @@
|
|
|
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 WithdrawResponse {
|
|
@@ -17,6 +16,12 @@ 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
27
|
* const { mutateAsync: withdraw, isPending, error } = useWithdraw(spokeToken);
|
|
@@ -27,9 +32,11 @@ interface WithdrawResponse {
|
|
|
27
32
|
* - spokeProvider is not available
|
|
28
33
|
* - Transaction execution fails
|
|
29
34
|
*/
|
|
30
|
-
export function useWithdraw(
|
|
35
|
+
export function useWithdraw(
|
|
36
|
+
spokeToken: XToken,
|
|
37
|
+
spokeProvider: SpokeProvider | undefined,
|
|
38
|
+
): UseMutationResult<WithdrawResponse, Error, string> {
|
|
31
39
|
const { sodax } = useSodaxContext();
|
|
32
|
-
const spokeProvider = useSpokeProvider(spokeToken.xChainId as SpokeChainId);
|
|
33
40
|
|
|
34
41
|
return useMutation<WithdrawResponse, Error, string>({
|
|
35
42
|
mutationFn: async (amount: string) => {
|
|
@@ -42,6 +49,7 @@ export function useWithdraw(spokeToken: XToken): UseMutationResult<WithdrawRespo
|
|
|
42
49
|
token: spokeToken.address,
|
|
43
50
|
// vault token on hub chain decimals is 18
|
|
44
51
|
amount: parseUnits(amount, 18),
|
|
52
|
+
action: 'withdraw',
|
|
45
53
|
},
|
|
46
54
|
spokeProvider,
|
|
47
55
|
);
|
|
@@ -8,6 +8,10 @@ import {
|
|
|
8
8
|
type IconSpokeChainConfig,
|
|
9
9
|
CWSpokeProvider,
|
|
10
10
|
type CosmosSpokeChainConfig,
|
|
11
|
+
StellarSpokeProvider,
|
|
12
|
+
type StellarSpokeChainConfig,
|
|
13
|
+
type SpokeProvider,
|
|
14
|
+
type IWalletProvider,
|
|
11
15
|
} from '@sodax/sdk';
|
|
12
16
|
import type {
|
|
13
17
|
IEvmWalletProvider,
|
|
@@ -15,42 +19,74 @@ import type {
|
|
|
15
19
|
ISuiWalletProvider,
|
|
16
20
|
SpokeChainId,
|
|
17
21
|
IInjectiveWalletProvider,
|
|
22
|
+
IStellarWalletProvider,
|
|
18
23
|
} from '@sodax/types';
|
|
19
24
|
import { getXChainType, useWalletProvider } from '@sodax/wallet-sdk';
|
|
20
25
|
import { useMemo } from 'react';
|
|
21
26
|
|
|
22
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Hook to get the appropriate spoke provider based on the chain type.
|
|
29
|
+
* Supports EVM, SUI, ICON and INJECTIVE chains.
|
|
30
|
+
*
|
|
31
|
+
* @param {SpokeChainId | undefined} spokeChainId - The spoke chain ID to get the provider for
|
|
32
|
+
* @param {IWalletProvider | undefined} walletProvider - The wallet provider to use
|
|
33
|
+
* @returns {SpokeProvider | undefined} The appropriate spoke provider instance for the given chain ID, or undefined if invalid/unsupported
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```tsx
|
|
37
|
+
* // Using a specific SpokeChainId and wallet provider
|
|
38
|
+
* const spokeProvider = useSpokeProvider(spokeChainId, walletProvider);
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function useSpokeProvider(
|
|
42
|
+
spokeChainId: SpokeChainId | undefined,
|
|
43
|
+
walletProvider?: IWalletProvider | undefined,
|
|
44
|
+
): SpokeProvider | undefined {
|
|
23
45
|
const xChainType = getXChainType(spokeChainId);
|
|
24
|
-
const
|
|
46
|
+
const walletProvider_ = useWalletProvider(spokeChainId);
|
|
47
|
+
const _walletProvider = walletProvider ?? walletProvider_;
|
|
48
|
+
|
|
25
49
|
const spokeProvider = useMemo(() => {
|
|
26
|
-
if (!
|
|
50
|
+
if (!_walletProvider) return undefined;
|
|
51
|
+
if (!spokeChainId) return undefined;
|
|
52
|
+
|
|
27
53
|
if (xChainType === 'EVM') {
|
|
28
54
|
return new EvmSpokeProvider(
|
|
29
|
-
|
|
55
|
+
_walletProvider as IEvmWalletProvider,
|
|
30
56
|
spokeChainConfig[spokeChainId] as EvmSpokeChainConfig,
|
|
31
57
|
);
|
|
32
58
|
}
|
|
33
59
|
if (xChainType === 'SUI') {
|
|
34
60
|
return new SuiSpokeProvider(
|
|
35
61
|
spokeChainConfig[spokeChainId] as SuiSpokeChainConfig,
|
|
36
|
-
|
|
62
|
+
_walletProvider as ISuiWalletProvider,
|
|
37
63
|
);
|
|
38
64
|
}
|
|
39
65
|
if (xChainType === 'ICON') {
|
|
40
66
|
return new IconSpokeProvider(
|
|
41
|
-
|
|
67
|
+
_walletProvider as IIconWalletProvider,
|
|
42
68
|
spokeChainConfig[spokeChainId] as IconSpokeChainConfig,
|
|
43
69
|
);
|
|
44
70
|
}
|
|
45
71
|
if (xChainType === 'INJECTIVE') {
|
|
46
72
|
return new CWSpokeProvider(
|
|
47
73
|
spokeChainConfig[spokeChainId] as CosmosSpokeChainConfig,
|
|
48
|
-
|
|
74
|
+
_walletProvider as IInjectiveWalletProvider,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (xChainType === 'STELLAR') {
|
|
79
|
+
const stellarConfig = spokeChainConfig[spokeChainId] as StellarSpokeChainConfig;
|
|
80
|
+
return new StellarSpokeProvider(
|
|
81
|
+
_walletProvider as IStellarWalletProvider,
|
|
82
|
+
stellarConfig.addresses.assetManager,
|
|
83
|
+
stellarConfig,
|
|
84
|
+
stellarConfig.rpc_url,
|
|
49
85
|
);
|
|
50
86
|
}
|
|
51
87
|
|
|
52
88
|
return undefined;
|
|
53
|
-
}, [
|
|
89
|
+
}, [spokeChainId, xChainType, _walletProvider]);
|
|
54
90
|
|
|
55
91
|
return spokeProvider;
|
|
56
92
|
}
|
package/src/hooks/swap/index.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
2
2
|
import type {
|
|
3
3
|
CreateIntentParams,
|
|
4
|
-
SpokeChainId,
|
|
5
4
|
IntentExecutionResponse,
|
|
6
5
|
Result,
|
|
7
6
|
IntentSubmitErrorCode,
|
|
8
7
|
Intent,
|
|
9
8
|
PacketData,
|
|
10
9
|
IntentSubmitError,
|
|
10
|
+
SpokeProvider,
|
|
11
11
|
} from '@sodax/sdk';
|
|
12
|
-
import { useSpokeProvider } from '../provider/useSpokeProvider';
|
|
13
12
|
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
14
13
|
|
|
15
14
|
type CreateIntentResult = Result<
|
|
@@ -21,12 +20,12 @@ type CreateIntentResult = Result<
|
|
|
21
20
|
* Hook for creating and submitting an intent order for cross-chain swaps.
|
|
22
21
|
* Uses React Query's useMutation for better state management and caching.
|
|
23
22
|
*
|
|
24
|
-
* @param {
|
|
23
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for the swap
|
|
25
24
|
* @returns {UseMutationResult} Mutation result object containing mutation function and state
|
|
26
25
|
*
|
|
27
26
|
* @example
|
|
28
27
|
* ```typescript
|
|
29
|
-
* const { mutateAsync: createIntent, isPending } = useCreateIntentOrder(
|
|
28
|
+
* const { mutateAsync: createIntent, isPending } = useCreateIntentOrder(spokeProvider);
|
|
30
29
|
*
|
|
31
30
|
* const handleSwap = async () => {
|
|
32
31
|
* const result = await createIntent({
|
|
@@ -41,10 +40,9 @@ type CreateIntentResult = Result<
|
|
|
41
40
|
* ```
|
|
42
41
|
*/
|
|
43
42
|
export function useCreateIntentOrder(
|
|
44
|
-
|
|
43
|
+
spokeProvider: SpokeProvider | undefined,
|
|
45
44
|
): UseMutationResult<CreateIntentResult, Error, CreateIntentParams> {
|
|
46
45
|
const { sodax } = useSodaxContext();
|
|
47
|
-
const spokeProvider = useSpokeProvider(chainId);
|
|
48
46
|
|
|
49
47
|
return useMutation<CreateIntentResult, Error, CreateIntentParams>({
|
|
50
48
|
mutationFn: async (params: CreateIntentParams) => {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
|
+
import type { CreateIntentParams, SpokeProvider } from '@sodax/sdk';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for checking token allowance for money market operations.
|
|
7
|
+
*
|
|
8
|
+
* This hook verifies if the user has approved enough tokens for a specific money market action
|
|
9
|
+
* (borrow/repay). It automatically queries and tracks the allowance status.
|
|
10
|
+
*
|
|
11
|
+
* @param {CreateIntentParams} params - The parameters for the intent to check allowance for.
|
|
12
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for allowance checks
|
|
13
|
+
*
|
|
14
|
+
* @returns {UseQueryResult<boolean, Error>} A React Query result containing:
|
|
15
|
+
* - data: Boolean indicating if allowance is sufficient
|
|
16
|
+
* - isLoading: Loading state indicator
|
|
17
|
+
* - error: Any error that occurred during the check
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const { data: hasAllowed, isLoading } = useMMAllowance(params, spokeProvider);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function useSwapAllowance(
|
|
25
|
+
params: CreateIntentParams | undefined,
|
|
26
|
+
spokeProvider: SpokeProvider | undefined,
|
|
27
|
+
): UseQueryResult<boolean, Error> {
|
|
28
|
+
const { sodax } = useSodaxContext();
|
|
29
|
+
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ['allowance', params],
|
|
32
|
+
queryFn: async () => {
|
|
33
|
+
if (!spokeProvider || !params) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
const allowance = await sodax.solver.isAllowanceValid(params, spokeProvider);
|
|
37
|
+
if (allowance.ok) {
|
|
38
|
+
return allowance.value;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
},
|
|
42
|
+
enabled: !!spokeProvider && !!params,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
2
|
+
import type { Token } from '@sodax/types';
|
|
3
|
+
import { type Address, parseUnits } from 'viem';
|
|
4
|
+
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
5
|
+
import type { SpokeProvider } from '@sodax/sdk';
|
|
6
|
+
|
|
7
|
+
interface UseApproveReturn {
|
|
8
|
+
approve: ({ amount }: { amount: string }) => Promise<boolean>;
|
|
9
|
+
isLoading: boolean;
|
|
10
|
+
error: Error | null;
|
|
11
|
+
resetError: () => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Hook for approving token spending for money market actions
|
|
16
|
+
* @param token The token to approve spending for
|
|
17
|
+
* @param spokeProvider The spoke provider instance for the chain
|
|
18
|
+
* @returns Object containing approve function, loading state, error state and reset function
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* const { approve, isLoading, error } = useApprove(token, spokeProvider);
|
|
22
|
+
*
|
|
23
|
+
* // Approve tokens for supply action
|
|
24
|
+
* await approve({ amount: "100", action: "supply" });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
export function useSwapApprove(token: Token | undefined, spokeProvider: SpokeProvider | undefined): UseApproveReturn {
|
|
29
|
+
const { sodax } = useSodaxContext();
|
|
30
|
+
const queryClient = useQueryClient();
|
|
31
|
+
|
|
32
|
+
const {
|
|
33
|
+
mutateAsync: approve,
|
|
34
|
+
isPending,
|
|
35
|
+
error,
|
|
36
|
+
reset: resetError,
|
|
37
|
+
} = useMutation({
|
|
38
|
+
mutationFn: async ({ amount }: { amount: string }) => {
|
|
39
|
+
if (!spokeProvider) {
|
|
40
|
+
throw new Error('Spoke provider not found');
|
|
41
|
+
}
|
|
42
|
+
if (!token) {
|
|
43
|
+
throw new Error('Token not found');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const allowance = await sodax.solver.approve(
|
|
47
|
+
token.address as Address,
|
|
48
|
+
parseUnits(amount, token.decimals),
|
|
49
|
+
spokeProvider,
|
|
50
|
+
);
|
|
51
|
+
if (!allowance.ok) {
|
|
52
|
+
throw new Error('Failed to approve tokens');
|
|
53
|
+
}
|
|
54
|
+
return allowance.ok;
|
|
55
|
+
},
|
|
56
|
+
onSuccess: () => {
|
|
57
|
+
// Invalidate allowance query to refetch the new allowance
|
|
58
|
+
queryClient.invalidateQueries({ queryKey: ['allowance', token?.address] });
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
approve,
|
|
64
|
+
isLoading: isPending,
|
|
65
|
+
error: error,
|
|
66
|
+
resetError,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAllowance.d.ts","sourceRoot":"","sources":["../../../src/hooks/shared/useAllowance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAgB,MAAM,EAAE,MAAM,cAAc,CAAC;AAKzD,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kEAwBzD"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { XToken } from '@sodax/types';
|
|
2
|
-
interface UseApproveReturn {
|
|
3
|
-
approve: (amount: string) => Promise<boolean>;
|
|
4
|
-
isLoading: boolean;
|
|
5
|
-
error: Error | null;
|
|
6
|
-
resetError: () => void;
|
|
7
|
-
}
|
|
8
|
-
export declare function useApprove(token: XToken): UseApproveReturn;
|
|
9
|
-
export {};
|
|
10
|
-
//# sourceMappingURL=useApprove.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useApprove.d.ts","sourceRoot":"","sources":["../../../src/hooks/shared/useApprove.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgB,MAAM,EAAE,MAAM,cAAc,CAAC;AAKzD,UAAU,gBAAgB;IACxB,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9C,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,UAAU,EAAE,MAAM,IAAI,CAAC;CACxB;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB,CAsC1D"}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { useQuery } from '@tanstack/react-query';
|
|
2
|
-
import type { SpokeChainId, XToken } from '@sodax/types';
|
|
3
|
-
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
|
-
import { useSpokeProvider } from '../provider/useSpokeProvider';
|
|
5
|
-
import { parseUnits } from 'viem';
|
|
6
|
-
|
|
7
|
-
export function useAllowance(token: XToken, amount: string) {
|
|
8
|
-
const { sodax } = useSodaxContext();
|
|
9
|
-
const spokeProvider = useSpokeProvider(token.xChainId as SpokeChainId);
|
|
10
|
-
|
|
11
|
-
return useQuery({
|
|
12
|
-
queryKey: ['allowance', token.address, amount],
|
|
13
|
-
queryFn: async () => {
|
|
14
|
-
if (!spokeProvider) {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
const allowance = await sodax.moneyMarket.isAllowanceValid(
|
|
18
|
-
{
|
|
19
|
-
token: token.address,
|
|
20
|
-
amount: parseUnits(amount, token.decimals),
|
|
21
|
-
},
|
|
22
|
-
spokeProvider,
|
|
23
|
-
);
|
|
24
|
-
if (allowance.ok) {
|
|
25
|
-
return allowance.value;
|
|
26
|
-
}
|
|
27
|
-
return false;
|
|
28
|
-
},
|
|
29
|
-
enabled: !!spokeProvider,
|
|
30
|
-
});
|
|
31
|
-
}
|