@sodax/dapp-kit 0.0.1-rc.3 → 0.0.1-rc.30
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 +152 -53
- package/dist/index.d.mts +1425 -0
- package/dist/index.d.ts +1425 -4
- package/dist/index.js +931 -194
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +890 -191
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -8
- package/src/contexts/index.ts +2 -0
- package/src/core/index.ts +5 -33
- package/src/hooks/backend/README.md +135 -0
- package/src/hooks/backend/index.ts +23 -0
- package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +49 -0
- package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +61 -0
- package/src/hooks/backend/useBackendIntentByHash.ts +53 -0
- package/src/hooks/backend/useBackendIntentByTxHash.ts +52 -0
- package/src/hooks/backend/useBackendMoneyMarketAsset.ts +57 -0
- package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +67 -0
- package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +67 -0
- package/src/hooks/backend/useBackendMoneyMarketPosition.ts +56 -0
- package/src/hooks/backend/useBackendOrderbook.ts +63 -0
- package/src/hooks/bridge/index.ts +5 -0
- package/src/hooks/bridge/useBridge.ts +57 -0
- package/src/hooks/bridge/useBridgeAllowance.ts +49 -0
- package/src/hooks/bridge/useBridgeApprove.ts +68 -0
- package/src/hooks/bridge/useGetBridgeableAmount.ts +50 -0
- package/src/hooks/bridge/useGetBridgeableTokens.ts +62 -0
- package/src/hooks/index.ts +3 -0
- 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 +3 -21
- package/src/hooks/provider/useSpokeProvider.ts +97 -6
- package/src/hooks/shared/index.ts +4 -2
- package/src/hooks/shared/useDeriveUserWalletAddress.ts +44 -0
- package/src/hooks/shared/useEstimateGas.ts +18 -0
- package/src/hooks/shared/useRequestTrustline.ts +82 -0
- package/src/hooks/shared/useStellarTrustlineCheck.ts +71 -0
- package/src/hooks/staking/index.ts +19 -0
- package/src/hooks/staking/useCancelUnstake.ts +52 -0
- package/src/hooks/staking/useClaim.ts +46 -0
- package/src/hooks/staking/useConvertedAssets.ts +47 -0
- package/src/hooks/staking/useInstantUnstake.ts +50 -0
- package/src/hooks/staking/useInstantUnstakeAllowance.ts +59 -0
- package/src/hooks/staking/useInstantUnstakeApprove.ts +52 -0
- package/src/hooks/staking/useInstantUnstakeRatio.ts +54 -0
- package/src/hooks/staking/useStake.ts +47 -0
- package/src/hooks/staking/useStakeAllowance.ts +57 -0
- package/src/hooks/staking/useStakeApprove.ts +50 -0
- package/src/hooks/staking/useStakeRatio.ts +53 -0
- package/src/hooks/staking/useStakingConfig.ts +40 -0
- package/src/hooks/staking/useStakingInfo.ts +50 -0
- package/src/hooks/staking/useUnstake.ts +54 -0
- package/src/hooks/staking/useUnstakeAllowance.ts +58 -0
- package/src/hooks/staking/useUnstakeApprove.ts +52 -0
- package/src/hooks/staking/useUnstakingInfo.ts +53 -0
- package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +59 -0
- package/src/hooks/swap/index.ts +4 -1
- package/src/hooks/swap/useCancelSwap.ts +44 -0
- package/src/hooks/swap/useQuote.ts +20 -6
- package/src/hooks/swap/useStatus.ts +3 -3
- package/src/hooks/swap/{useCreateIntentOrder.ts → useSwap.ts} +22 -19
- package/src/hooks/swap/useSwapAllowance.ts +48 -0
- package/src/hooks/swap/useSwapApprove.ts +68 -0
- package/src/providers/SodaxProvider.tsx +7 -4
- package/dist/contexts/index.d.ts +0 -7
- package/dist/contexts/index.d.ts.map +0 -1
- package/dist/core/index.d.ts +0 -4
- package/dist/core/index.d.ts.map +0 -1
- package/dist/hooks/index.d.ts +0 -5
- package/dist/hooks/index.d.ts.map +0 -1
- package/dist/hooks/mm/index.d.ts +0 -7
- package/dist/hooks/mm/index.d.ts.map +0 -1
- package/dist/hooks/mm/useBorrow.d.ts +0 -26
- package/dist/hooks/mm/useBorrow.d.ts.map +0 -1
- package/dist/hooks/mm/useHubWalletAddress.d.ts +0 -24
- package/dist/hooks/mm/useHubWalletAddress.d.ts.map +0 -1
- package/dist/hooks/mm/useRepay.d.ts +0 -26
- package/dist/hooks/mm/useRepay.d.ts.map +0 -1
- package/dist/hooks/mm/useSupply.d.ts +0 -32
- package/dist/hooks/mm/useSupply.d.ts.map +0 -1
- package/dist/hooks/mm/useUserReservesData.d.ts +0 -9
- package/dist/hooks/mm/useUserReservesData.d.ts.map +0 -1
- package/dist/hooks/mm/useWithdraw.d.ts +0 -26
- package/dist/hooks/mm/useWithdraw.d.ts.map +0 -1
- package/dist/hooks/provider/index.d.ts +0 -3
- package/dist/hooks/provider/index.d.ts.map +0 -1
- package/dist/hooks/provider/useHubProvider.d.ts +0 -3
- package/dist/hooks/provider/useHubProvider.d.ts.map +0 -1
- package/dist/hooks/provider/useSpokeProvider.d.ts +0 -4
- package/dist/hooks/provider/useSpokeProvider.d.ts.map +0 -1
- package/dist/hooks/shared/index.d.ts +0 -4
- package/dist/hooks/shared/index.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/shared/useSodaxContext.d.ts +0 -8
- package/dist/hooks/shared/useSodaxContext.d.ts.map +0 -1
- package/dist/hooks/swap/index.d.ts +0 -4
- package/dist/hooks/swap/index.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/dist/hooks/swap/useQuote.d.ts +0 -39
- package/dist/hooks/swap/useQuote.d.ts.map +0 -1
- package/dist/hooks/swap/useStatus.d.ts +0 -31
- package/dist/hooks/swap/useStatus.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/providers/SodaxProvider.d.ts +0 -10
- package/dist/providers/SodaxProvider.d.ts.map +0 -1
- package/dist/providers/index.d.ts +0 -2
- package/dist/providers/index.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,63 @@
|
|
|
1
|
+
// packages/dapp-kit/src/hooks/backend/useOrderbook.ts
|
|
2
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
3
|
+
import type { OrderbookResponse } from '@sodax/sdk';
|
|
4
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook for fetching the solver orderbook from the backend API.
|
|
8
|
+
*
|
|
9
|
+
* This hook provides access to the solver orderbook data, including intent states
|
|
10
|
+
* and intent data for all available intents. The data is automatically fetched
|
|
11
|
+
* and cached using React Query with pagination support.
|
|
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)
|
|
16
|
+
*
|
|
17
|
+
* @returns {UseQueryResult<OrderbookResponse | undefined>} A query result object containing:
|
|
18
|
+
* - data: The orderbook response data when available
|
|
19
|
+
* - isLoading: Boolean indicating if the request is in progress
|
|
20
|
+
* - error: Error object if the request failed
|
|
21
|
+
* - refetch: Function to manually trigger a data refresh
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const { data: orderbook, isLoading, error } = useOrderbook({
|
|
26
|
+
* offset: '0',
|
|
27
|
+
* limit: '10'
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
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
|
+
* @remarks
|
|
39
|
+
* - The query is disabled when params are undefined or invalid
|
|
40
|
+
* - Uses React Query for efficient caching and state management
|
|
41
|
+
* - Automatically handles error states and loading indicators
|
|
42
|
+
* - Stale time of 30 seconds for real-time orderbook data
|
|
43
|
+
* - Supports pagination through offset and limit parameters
|
|
44
|
+
*/
|
|
45
|
+
export const useBackendOrderbook = (
|
|
46
|
+
params: { offset: string; limit: string } | undefined,
|
|
47
|
+
): UseQueryResult<OrderbookResponse | undefined> => {
|
|
48
|
+
const { sodax } = useSodaxContext();
|
|
49
|
+
|
|
50
|
+
return useQuery({
|
|
51
|
+
queryKey: ['backend', 'solver', 'orderbook', params],
|
|
52
|
+
queryFn: async (): Promise<OrderbookResponse | undefined> => {
|
|
53
|
+
if (!params || !params.offset || !params.limit) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return sodax.backendApiService.getOrderbook(params);
|
|
58
|
+
},
|
|
59
|
+
enabled: !!params && !!params.offset && !!params.limit,
|
|
60
|
+
staleTime: 30 * 1000, // 30 seconds for real-time data
|
|
61
|
+
retry: 3,
|
|
62
|
+
});
|
|
63
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
2
|
+
import type { BridgeError, BridgeErrorCode, SpokeTxHash, HubTxHash, Result, CreateBridgeIntentParams } from '@sodax/sdk';
|
|
3
|
+
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
4
|
+
import type { SpokeProvider } from '@sodax/sdk';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook for executing bridge transactions to transfer tokens between chains.
|
|
8
|
+
* Uses React Query's useMutation for better state management and caching.
|
|
9
|
+
*
|
|
10
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for the bridge
|
|
11
|
+
* @returns {UseMutationResult} Mutation result object containing mutation function and state
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const { mutateAsync: bridge, isPending } = useBridge(spokeProvider);
|
|
16
|
+
*
|
|
17
|
+
* const handleBridge = async () => {
|
|
18
|
+
* const result = await bridge({
|
|
19
|
+
* srcChainId: '0x2105.base',
|
|
20
|
+
* srcAsset: '0x...',
|
|
21
|
+
* amount: 1000n,
|
|
22
|
+
* dstChainId: '0x89.polygon',
|
|
23
|
+
* dstAsset: '0x...',
|
|
24
|
+
* recipient: '0x...'
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* console.log('Bridge transaction hashes:', {
|
|
28
|
+
* spokeTxHash: result.spokeTxHash,
|
|
29
|
+
* hubTxHash: result.hubTxHash
|
|
30
|
+
* });
|
|
31
|
+
* };
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function useBridge(
|
|
35
|
+
spokeProvider: SpokeProvider | undefined,
|
|
36
|
+
): UseMutationResult<Result<[SpokeTxHash, HubTxHash], BridgeError<BridgeErrorCode>>, Error, CreateBridgeIntentParams> {
|
|
37
|
+
const { sodax } = useSodaxContext();
|
|
38
|
+
|
|
39
|
+
return useMutation<Result<[SpokeTxHash, HubTxHash], BridgeError<BridgeErrorCode>>, Error, CreateBridgeIntentParams>({
|
|
40
|
+
mutationFn: async (params: CreateBridgeIntentParams) => {
|
|
41
|
+
if (!spokeProvider) {
|
|
42
|
+
throw new Error('Spoke provider not found');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const result = await sodax.bridge.bridge({
|
|
46
|
+
params,
|
|
47
|
+
spokeProvider,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (!result.ok) {
|
|
51
|
+
throw new Error(`Bridge failed: ${result.error.code}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return result;
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
|
+
import type { SpokeProvider, CreateBridgeIntentParams } from '@sodax/sdk';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for checking token allowance for bridge operations.
|
|
7
|
+
*
|
|
8
|
+
* This hook verifies if the user has approved enough tokens for a specific bridge action.
|
|
9
|
+
* It automatically queries and tracks the allowance status.
|
|
10
|
+
*
|
|
11
|
+
* @param {BridgeParams} params - The parameters for the bridge 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 } = useBridgeAllowance(params, spokeProvider);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function useBridgeAllowance(
|
|
25
|
+
params: CreateBridgeIntentParams | undefined,
|
|
26
|
+
spokeProvider: SpokeProvider | undefined,
|
|
27
|
+
): UseQueryResult<boolean, Error> {
|
|
28
|
+
const { sodax } = useSodaxContext();
|
|
29
|
+
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ['bridge-allowance', params],
|
|
32
|
+
queryFn: async () => {
|
|
33
|
+
if (!spokeProvider || !params) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const allowance = await sodax.bridge.isAllowanceValid({
|
|
38
|
+
params,
|
|
39
|
+
spokeProvider,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
if (allowance.ok) {
|
|
43
|
+
return allowance.value;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
},
|
|
47
|
+
enabled: !!spokeProvider && !!params,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
2
|
+
import type { CreateBridgeIntentParams, SpokeProvider } from '@sodax/sdk';
|
|
3
|
+
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
4
|
+
|
|
5
|
+
interface UseBridgeApproveReturn {
|
|
6
|
+
approve: (params: CreateBridgeIntentParams) => Promise<boolean>;
|
|
7
|
+
isLoading: boolean;
|
|
8
|
+
error: Error | null;
|
|
9
|
+
resetError: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Hook for approving token spending for bridge actions
|
|
14
|
+
* @param spokeProvider The spoke provider instance for the chain
|
|
15
|
+
* @returns Object containing approve function, loading state, error state and reset function
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* const { approve, isLoading, error } = useBridgeApprove(spokeProvider);
|
|
19
|
+
*
|
|
20
|
+
* // Approve tokens for bridge action
|
|
21
|
+
* await approve({
|
|
22
|
+
* srcChainId: '0x2105.base',
|
|
23
|
+
* srcAsset: '0x...',
|
|
24
|
+
* amount: 1000n,
|
|
25
|
+
* dstChainId: '0x89.polygon',
|
|
26
|
+
* dstAsset: '0x...',
|
|
27
|
+
* recipient: '0x...'
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function useBridgeApprove(spokeProvider: SpokeProvider | undefined): UseBridgeApproveReturn {
|
|
32
|
+
const { sodax } = useSodaxContext();
|
|
33
|
+
const queryClient = useQueryClient();
|
|
34
|
+
|
|
35
|
+
const {
|
|
36
|
+
mutateAsync: approve,
|
|
37
|
+
isPending,
|
|
38
|
+
error,
|
|
39
|
+
reset: resetError,
|
|
40
|
+
} = useMutation({
|
|
41
|
+
mutationFn: async (params: CreateBridgeIntentParams) => {
|
|
42
|
+
if (!spokeProvider) {
|
|
43
|
+
throw new Error('Spoke provider not found');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const allowance = await sodax.bridge.approve({
|
|
47
|
+
params,
|
|
48
|
+
spokeProvider,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (!allowance.ok) {
|
|
52
|
+
throw new Error('Failed to approve tokens for bridge');
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
},
|
|
56
|
+
onSuccess: (_, params) => {
|
|
57
|
+
// Invalidate allowance query to refetch the new allowance
|
|
58
|
+
queryClient.invalidateQueries({ queryKey: ['bridge-allowance', params] });
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
approve,
|
|
64
|
+
isLoading: isPending,
|
|
65
|
+
error: error,
|
|
66
|
+
resetError,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
|
+
import type { XToken } from '@sodax/sdk';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for getting the amount available to be bridged.
|
|
7
|
+
*
|
|
8
|
+
* This hook is used to check if a target chain has enough balance to bridge when bridging.
|
|
9
|
+
* It automatically queries and tracks the available amount to be bridged.
|
|
10
|
+
*
|
|
11
|
+
* @param {SpokeChainId | undefined} chainId - The chain ID to get the balance for
|
|
12
|
+
* @param {string | undefined} token - The token address to get the balance for
|
|
13
|
+
*
|
|
14
|
+
* @returns {UseQueryResult<bigint, Error>} A React Query result containing:
|
|
15
|
+
* - data: The available amount to be bridged (bigint)
|
|
16
|
+
* - error: Any error that occurred during the check
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const { data: balance, isLoading } = useSpokeAssetManagerTokenBalance(chainId, tokenAddress);
|
|
21
|
+
*
|
|
22
|
+
* if (balance) {
|
|
23
|
+
* console.log('Asset manager token balance:', balance.toString());
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function useGetBridgeableAmount(
|
|
28
|
+
from: XToken | undefined,
|
|
29
|
+
to: XToken | undefined,
|
|
30
|
+
): UseQueryResult<bigint, Error> {
|
|
31
|
+
const { sodax } = useSodaxContext();
|
|
32
|
+
|
|
33
|
+
return useQuery({
|
|
34
|
+
queryKey: ['spoke-asset-manager-token-balance', from, to],
|
|
35
|
+
queryFn: async () => {
|
|
36
|
+
if (!from || !to) {
|
|
37
|
+
return 0n;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const result = await sodax.bridge.getBridgeableAmount(from, to);
|
|
41
|
+
if (result.ok) {
|
|
42
|
+
return result.value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
console.error('Error getting bridgeable amount:', result.error);
|
|
46
|
+
return 0n;
|
|
47
|
+
},
|
|
48
|
+
enabled: !!from && !!to,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { XToken, SpokeChainId } from '@sodax/sdk';
|
|
3
|
+
import { useSodaxContext } from '../shared';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
/**
|
|
7
|
+
* Hook for retrieving all bridgeable tokens from a source token on one chain to a destination chain.
|
|
8
|
+
*
|
|
9
|
+
* This hook queries and tracks the set of tokens on the destination chain that can be bridged to,
|
|
10
|
+
* given a source chain, destination chain, and source token address.
|
|
11
|
+
*
|
|
12
|
+
* @param {SpokeChainId | undefined} from - The source chain ID
|
|
13
|
+
* @param {SpokeChainId | undefined} to - The destination chain ID
|
|
14
|
+
* @param {string | undefined} token - The source token address
|
|
15
|
+
*
|
|
16
|
+
* @returns {UseQueryResult<XToken[], Error>} A React Query result containing:
|
|
17
|
+
* - data: Array of bridgeable tokens (XToken[]) on the destination chain
|
|
18
|
+
* - error: Any error that occurred during the query
|
|
19
|
+
*
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const { data: bridgeableTokens, isLoading } = useGetBridgeableTokens(
|
|
24
|
+
* fromChainId,
|
|
25
|
+
* toChainId,
|
|
26
|
+
* fromTokenAddress
|
|
27
|
+
* );
|
|
28
|
+
*
|
|
29
|
+
* if (bridgeableTokens && bridgeableTokens.length > 0) {
|
|
30
|
+
* bridgeableTokens.forEach(token => {
|
|
31
|
+
* console.log(`Bridgeable token: ${token.symbol} (${token.address}) on chain ${token.xChainId}`);
|
|
32
|
+
* });
|
|
33
|
+
* } else {
|
|
34
|
+
* console.log('No bridgeable tokens found for the selected route.');
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function useGetBridgeableTokens(
|
|
39
|
+
from: SpokeChainId | undefined,
|
|
40
|
+
to: SpokeChainId | undefined,
|
|
41
|
+
token: string | undefined,
|
|
42
|
+
): UseQueryResult<XToken[], Error> {
|
|
43
|
+
const { sodax } = useSodaxContext();
|
|
44
|
+
|
|
45
|
+
return useQuery({
|
|
46
|
+
queryKey: ['bridgeable-tokens', from, to, token],
|
|
47
|
+
queryFn: async () => {
|
|
48
|
+
if (!from || !to || !token) {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const result = sodax.bridge.getBridgeableTokens(from, to, token);
|
|
53
|
+
if (result.ok) {
|
|
54
|
+
return result.value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
console.error('Error getting bridgeable tokens:', result.error);
|
|
58
|
+
return [];
|
|
59
|
+
},
|
|
60
|
+
enabled: !!from && !!to && !!token,
|
|
61
|
+
});
|
|
62
|
+
}
|
package/src/hooks/index.ts
CHANGED
package/src/hooks/mm/index.ts
CHANGED
|
@@ -3,4 +3,6 @@ export * from './useRepay';
|
|
|
3
3
|
export * from './useSupply';
|
|
4
4
|
export * from './useWithdraw';
|
|
5
5
|
export * from './useUserReservesData';
|
|
6
|
-
export * from './
|
|
6
|
+
export * from './useReservesData';
|
|
7
|
+
export * from './useMMAllowance';
|
|
8
|
+
export * from './useMMApprove';
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import type {
|
|
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
|
-
value: [
|
|
8
|
+
value: [string, string];
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
/**
|
|
@@ -16,9 +15,17 @@ 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
|
-
* const { mutateAsync: borrow, isPending, error } = useBorrow(
|
|
28
|
+
* const { mutateAsync: borrow, isPending, error } = useBorrow(spokeToken);
|
|
22
29
|
* await borrow('100');
|
|
23
30
|
* ```
|
|
24
31
|
*
|
|
@@ -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(spokeChainId as SpokeChainId);
|
|
32
41
|
|
|
33
42
|
return useMutation<BorrowResponse, Error, string>({
|
|
34
43
|
mutationFn: async (amount: string) => {
|
|
@@ -36,10 +45,11 @@ export function useBorrow(hubToken: XToken, spokeChainId: ChainId): UseMutationR
|
|
|
36
45
|
throw new Error('spokeProvider is not found');
|
|
37
46
|
}
|
|
38
47
|
|
|
39
|
-
const response = await sodax.moneyMarket.
|
|
48
|
+
const response = await sodax.moneyMarket.borrow(
|
|
40
49
|
{
|
|
41
|
-
token:
|
|
42
|
-
amount: parseUnits(amount,
|
|
50
|
+
token: spokeToken.address,
|
|
51
|
+
amount: parseUnits(amount, 18),
|
|
52
|
+
action: 'borrow',
|
|
43
53
|
},
|
|
44
54
|
spokeProvider,
|
|
45
55
|
);
|
|
@@ -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,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 RepayResponse {
|
|
9
8
|
ok: true;
|
|
10
|
-
value: [
|
|
9
|
+
value: [string, string];
|
|
11
10
|
}
|
|
12
11
|
|
|
13
12
|
/**
|
|
@@ -17,9 +16,17 @@ 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
|
-
* const { mutateAsync: repay, isPending, error } = useRepay(
|
|
29
|
+
* const { mutateAsync: repay, isPending, error } = useRepay(spokeToken);
|
|
23
30
|
* await repay('100');
|
|
24
31
|
* ```
|
|
25
32
|
*
|
|
@@ -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(spokeChainId as SpokeChainId);
|
|
33
42
|
|
|
34
43
|
return useMutation<RepayResponse, Error, string>({
|
|
35
44
|
mutationFn: async (amount: string) => {
|
|
@@ -37,10 +46,11 @@ export function useRepay(hubToken: XToken, spokeChainId: ChainId): UseMutationRe
|
|
|
37
46
|
throw new Error('spokeProvider is not found');
|
|
38
47
|
}
|
|
39
48
|
|
|
40
|
-
const response = await sodax.moneyMarket.
|
|
49
|
+
const response = await sodax.moneyMarket.repay(
|
|
41
50
|
{
|
|
42
|
-
token:
|
|
43
|
-
amount: parseUnits(amount,
|
|
51
|
+
token: spokeToken.address,
|
|
52
|
+
amount: parseUnits(amount, spokeToken.decimals),
|
|
53
|
+
action: 'repay',
|
|
44
54
|
},
|
|
45
55
|
spokeProvider,
|
|
46
56
|
);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useQuery } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
|
+
/**
|
|
4
|
+
* Hook for fetching reserves data from the Sodax money market.
|
|
5
|
+
*
|
|
6
|
+
* This hook provides access to the current state of all reserves in the money market protocol,
|
|
7
|
+
* including liquidity, interest rates, and other key metrics. The data is automatically
|
|
8
|
+
* fetched and cached using React Query.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const { data: reservesData, isLoading, error } = useReservesData();
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @returns A React Query result object containing:
|
|
16
|
+
* - data: The reserves data when available
|
|
17
|
+
* - isLoading: Loading state indicator
|
|
18
|
+
* - error: Any error that occurred during data fetching
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export function useReservesData() {
|
|
22
|
+
const { sodax } = useSodaxContext();
|
|
23
|
+
|
|
24
|
+
return useQuery({
|
|
25
|
+
queryKey: ['reservesData'],
|
|
26
|
+
queryFn: async () => {
|
|
27
|
+
return await sodax.moneyMarket.data.getReservesData();
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
}
|