@sodax/dapp-kit 0.0.1-rc.3 → 0.0.1-rc.31
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 +1514 -0
- package/dist/index.d.ts +1514 -4
- package/dist/index.js +1141 -189
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1096 -187
- 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 +4 -0
- package/src/hooks/migrate/index.ts +4 -0
- package/src/hooks/migrate/types.ts +15 -0
- package/src/hooks/migrate/useMigrate.tsx +110 -0
- package/src/hooks/migrate/useMigrationAllowance.tsx +79 -0
- package/src/hooks/migrate/useMigrationApprove.tsx +129 -0
- package/src/hooks/mm/index.ts +3 -1
- package/src/hooks/mm/useBorrow.ts +20 -10
- package/src/hooks/mm/useMMAllowance.ts +56 -0
- package/src/hooks/mm/useMMApprove.ts +68 -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 +103 -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,58 @@
|
|
|
1
|
+
// packages/dapp-kit/src/hooks/staking/useStakeAllowance.ts
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
|
+
import type { UnstakeParams, SpokeProvider } from '@sodax/sdk';
|
|
4
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook for checking xSODA token allowance for unstaking operations.
|
|
8
|
+
* Uses React Query for efficient caching and state management.
|
|
9
|
+
*
|
|
10
|
+
* @param {Omit<UnstakeParams, 'action'> | undefined} params - The unstaking parameters. If undefined, the query will be disabled.
|
|
11
|
+
* @param {SpokeProvider | undefined} spokeProvider - The spoke provider to use for the allowance check
|
|
12
|
+
* @returns {UseQueryResult<boolean, Error>} Query result object containing allowance data and state
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const { data: hasAllowed, isLoading } = useUnstakeAllowance(
|
|
17
|
+
* {
|
|
18
|
+
* amount: 1000000000000000000n, // 1 xSODA
|
|
19
|
+
* account: '0x...'
|
|
20
|
+
* },
|
|
21
|
+
* spokeProvider
|
|
22
|
+
* );
|
|
23
|
+
*
|
|
24
|
+
* if (isLoading) return <div>Checking allowance...</div>;
|
|
25
|
+
* if (hasAllowed) {
|
|
26
|
+
* console.log('Sufficient allowance for unstaking');
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function useUnstakeAllowance(
|
|
31
|
+
params: Omit<UnstakeParams, 'action'> | undefined,
|
|
32
|
+
spokeProvider: SpokeProvider | undefined,
|
|
33
|
+
): UseQueryResult<boolean, Error> {
|
|
34
|
+
const { sodax } = useSodaxContext();
|
|
35
|
+
|
|
36
|
+
return useQuery({
|
|
37
|
+
queryKey: ['soda', 'unstakeAllowance', params, spokeProvider?.chainConfig.chain.id],
|
|
38
|
+
queryFn: async () => {
|
|
39
|
+
if (!params || !spokeProvider) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const result = await sodax.staking.isAllowanceValid({
|
|
44
|
+
params: { ...params, action: 'unstake' },
|
|
45
|
+
spokeProvider,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (!result.ok) {
|
|
49
|
+
console.error(`Unstake allowance check failed: ${result.error.code}, error: ${result.error.error}`);
|
|
50
|
+
throw new Error(`Unstake allowance check failed: ${result.error.code}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return result.value;
|
|
54
|
+
},
|
|
55
|
+
enabled: !!params && !!spokeProvider,
|
|
56
|
+
refetchInterval: 5000, // Refetch every 5 seconds
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// packages/dapp-kit/src/hooks/staking/useStakeApprove.ts
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
|
+
import type { TxReturnType, UnstakeParams } from '@sodax/sdk';
|
|
4
|
+
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
5
|
+
import type { SpokeProvider } from '@sodax/sdk';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Hook for approving xSODA token spending for unstaking operations.
|
|
9
|
+
* Uses React Query's useMutation for better state management and caching.
|
|
10
|
+
*
|
|
11
|
+
* @param {SpokeProvider | undefined} spokeProvider - The spoke provider to use for the approval
|
|
12
|
+
* @returns {UseMutationResult<TxReturnType<SpokeProvider, false>, Error, Omit<UnstakeParams, 'action'>>} Mutation result object containing mutation function and state
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const { mutateAsync: approve, isPending } = useUnstakeApprove(spokeProvider);
|
|
17
|
+
*
|
|
18
|
+
* const handleApprove = async () => {
|
|
19
|
+
* const result = await approve({
|
|
20
|
+
* amount: 1000000000000000000n, // 1 xSODA
|
|
21
|
+
* account: '0x...'
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* console.log('Approval successful:', result);
|
|
25
|
+
* };
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function useUnstakeApprove(
|
|
29
|
+
spokeProvider: SpokeProvider | undefined,
|
|
30
|
+
): UseMutationResult<TxReturnType<SpokeProvider, false>, Error, Omit<UnstakeParams, 'action'>> {
|
|
31
|
+
const { sodax } = useSodaxContext();
|
|
32
|
+
|
|
33
|
+
return useMutation<TxReturnType<SpokeProvider, false>, Error, Omit<UnstakeParams, 'action'>>({
|
|
34
|
+
mutationFn: async (params: Omit<UnstakeParams, 'action'>) => {
|
|
35
|
+
console.log('useUnstakeApprove called with params:', params);
|
|
36
|
+
if (!spokeProvider) {
|
|
37
|
+
throw new Error('Spoke provider not found');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const result = await sodax.staking.approve({
|
|
41
|
+
params: { ...params, action: 'unstake' },
|
|
42
|
+
spokeProvider,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (!result.ok) {
|
|
46
|
+
throw new Error(`Unstake approval failed: ${result.error.code}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return result.value;
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// packages/dapp-kit/src/hooks/staking/useUnstakingInfoWithPenalty.ts
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
|
+
import type { UnstakingInfo, SpokeProvider } from '@sodax/sdk';
|
|
4
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook for fetching unstaking information from the stakedSoda contract.
|
|
8
|
+
* Uses React Query for efficient caching and state management.
|
|
9
|
+
*
|
|
10
|
+
* @param {string | undefined} userAddress - The user address to fetch unstaking info for
|
|
11
|
+
* @param {SpokeProvider | undefined} spokeProvider - The spoke provider instance
|
|
12
|
+
* @param {number} refetchInterval - The interval in milliseconds to refetch data (default: 5000)
|
|
13
|
+
* @returns {UseQueryResult<UnstakingInfo, Error>} Query result object containing unstaking info and state
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const { data: unstakingInfo, isLoading, error } = useUnstakingInfo(userAddress, spokeProvider);
|
|
18
|
+
*
|
|
19
|
+
* if (isLoading) return <div>Loading unstaking info...</div>;
|
|
20
|
+
* if (unstakingInfo) {
|
|
21
|
+
* console.log('Total unstaking:', unstakingInfo.totalUnstaking);
|
|
22
|
+
* unstakingInfo.userUnstakeSodaRequests.forEach(request => {
|
|
23
|
+
* console.log('Request amount:', request.request.amount);
|
|
24
|
+
* });
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function useUnstakingInfo(
|
|
29
|
+
userAddress: string | undefined,
|
|
30
|
+
spokeProvider: SpokeProvider | undefined,
|
|
31
|
+
refetchInterval = 5000,
|
|
32
|
+
): UseQueryResult<UnstakingInfo, Error> {
|
|
33
|
+
const { sodax } = useSodaxContext();
|
|
34
|
+
|
|
35
|
+
return useQuery({
|
|
36
|
+
queryKey: ['soda', 'unstakingInfoWithPenalty', spokeProvider?.chainConfig.chain.id, userAddress],
|
|
37
|
+
queryFn: async () => {
|
|
38
|
+
if (!spokeProvider || !userAddress) {
|
|
39
|
+
throw new Error('Spoke provider or user address not found');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const result = await sodax.staking.getUnstakingInfo(spokeProvider);
|
|
43
|
+
|
|
44
|
+
if (!result.ok) {
|
|
45
|
+
throw new Error(`Failed to fetch unstaking info: ${result.error.code}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return result.value;
|
|
49
|
+
},
|
|
50
|
+
enabled: !!spokeProvider && !!userAddress,
|
|
51
|
+
refetchInterval,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// packages/dapp-kit/src/hooks/staking/useUnstakingInfoWithPenalty.ts
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
3
|
+
import type { UnstakingInfo, UnstakeRequestWithPenalty, SpokeProvider } from '@sodax/sdk';
|
|
4
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
5
|
+
|
|
6
|
+
export type UnstakingInfoWithPenalty = UnstakingInfo & {
|
|
7
|
+
requestsWithPenalty: UnstakeRequestWithPenalty[];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Hook for fetching unstaking information with penalty calculations from the stakedSoda contract.
|
|
12
|
+
* Uses React Query for efficient caching and state management.
|
|
13
|
+
*
|
|
14
|
+
* @param {string | undefined} userAddress - The user address to fetch unstaking info for
|
|
15
|
+
* @param {SpokeProvider | undefined} spokeProvider - The spoke provider instance
|
|
16
|
+
* @param {number} refetchInterval - The interval in milliseconds to refetch data (default: 5000)
|
|
17
|
+
* @returns {UseQueryResult<UnstakingInfoWithPenalty, Error>} Query result object containing unstaking info with penalties and state
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const { data: unstakingInfo, isLoading, error } = useUnstakingInfoWithPenalty(userAddress, spokeProvider);
|
|
22
|
+
*
|
|
23
|
+
* if (isLoading) return <div>Loading unstaking info...</div>;
|
|
24
|
+
* if (unstakingInfo) {
|
|
25
|
+
* console.log('Total unstaking:', unstakingInfo.totalUnstaking);
|
|
26
|
+
* unstakingInfo.requestsWithPenalty.forEach(request => {
|
|
27
|
+
* console.log('Penalty:', request.penaltyPercentage + '%');
|
|
28
|
+
* console.log('Claimable amount:', request.claimableAmount);
|
|
29
|
+
* });
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function useUnstakingInfoWithPenalty(
|
|
34
|
+
userAddress: string | undefined,
|
|
35
|
+
spokeProvider: SpokeProvider | undefined,
|
|
36
|
+
refetchInterval = 5000,
|
|
37
|
+
): UseQueryResult<UnstakingInfoWithPenalty, Error> {
|
|
38
|
+
const { sodax } = useSodaxContext();
|
|
39
|
+
|
|
40
|
+
return useQuery({
|
|
41
|
+
queryKey: ['soda', 'unstakingInfoWithPenalty', spokeProvider?.chainConfig.chain.id, userAddress],
|
|
42
|
+
queryFn: async () => {
|
|
43
|
+
if (!spokeProvider) {
|
|
44
|
+
throw new Error('Spoke provider not found');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Get unstaking info with penalty calculations
|
|
48
|
+
const penaltyResult = await sodax.staking.getUnstakingInfoWithPenalty(spokeProvider);
|
|
49
|
+
|
|
50
|
+
if (!penaltyResult.ok) {
|
|
51
|
+
throw new Error(`Failed to fetch unstaking info with penalty: ${penaltyResult.error.code}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return penaltyResult.value;
|
|
55
|
+
},
|
|
56
|
+
enabled: !!spokeProvider && !!userAddress,
|
|
57
|
+
refetchInterval,
|
|
58
|
+
});
|
|
59
|
+
}
|
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,15 +1,16 @@
|
|
|
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
|
+
import { useMemo } from 'react';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Hook for fetching a quote for an intent-based swap.
|
|
7
8
|
*
|
|
8
9
|
* This hook provides real-time quote data for an intent-based swap.
|
|
9
10
|
*
|
|
10
|
-
* @param {
|
|
11
|
+
* @param {SolverIntentQuoteRequest | undefined} payload - The intent quote request parameters. If undefined, the query will be disabled.
|
|
11
12
|
*
|
|
12
|
-
* @returns {UseQueryResult<Result<
|
|
13
|
+
* @returns {UseQueryResult<Result<SolverIntentQuoteResponse, SolverErrorResponse> | undefined>} A query result object containing:
|
|
13
14
|
* - data: The quote result from the solver
|
|
14
15
|
* - isLoading: Boolean indicating if the quote is being fetched
|
|
15
16
|
* - error: Error object if the quote request failed
|
|
@@ -38,11 +39,24 @@ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
|
38
39
|
* - Uses React Query for efficient caching and state management
|
|
39
40
|
*/
|
|
40
41
|
export const useQuote = (
|
|
41
|
-
payload:
|
|
42
|
-
): UseQueryResult<Result<
|
|
42
|
+
payload: SolverIntentQuoteRequest | undefined,
|
|
43
|
+
): UseQueryResult<Result<SolverIntentQuoteResponse, SolverErrorResponse> | undefined> => {
|
|
43
44
|
const { sodax } = useSodaxContext();
|
|
45
|
+
|
|
46
|
+
// Create a serializable query key by converting BigInt to string
|
|
47
|
+
const queryKey = useMemo(() => {
|
|
48
|
+
if (!payload) return ['quote', undefined];
|
|
49
|
+
return [
|
|
50
|
+
'quote',
|
|
51
|
+
{
|
|
52
|
+
...payload,
|
|
53
|
+
amount: payload.amount.toString(),
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
}, [payload]);
|
|
57
|
+
|
|
44
58
|
return useQuery({
|
|
45
|
-
queryKey
|
|
59
|
+
queryKey,
|
|
46
60
|
queryFn: async () => {
|
|
47
61
|
if (!payload) {
|
|
48
62
|
return undefined;
|
|
@@ -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],
|
|
@@ -1,35 +1,31 @@
|
|
|
1
1
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
2
2
|
import type {
|
|
3
3
|
CreateIntentParams,
|
|
4
|
-
|
|
5
|
-
IntentExecutionResponse,
|
|
4
|
+
SolverExecutionResponse,
|
|
6
5
|
Result,
|
|
7
|
-
|
|
6
|
+
IntentErrorCode,
|
|
8
7
|
Intent,
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
IntentError,
|
|
9
|
+
SpokeProvider,
|
|
10
|
+
IntentDeliveryInfo,
|
|
11
11
|
} from '@sodax/sdk';
|
|
12
|
-
import {
|
|
13
|
-
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
12
|
+
import { useMutation, type UseMutationResult, useQueryClient } from '@tanstack/react-query';
|
|
14
13
|
|
|
15
|
-
type CreateIntentResult = Result<
|
|
16
|
-
[IntentExecutionResponse, Intent, PacketData],
|
|
17
|
-
IntentSubmitError<IntentSubmitErrorCode>
|
|
18
|
-
>;
|
|
14
|
+
type CreateIntentResult = Result<[SolverExecutionResponse, Intent, IntentDeliveryInfo], IntentError<IntentErrorCode>>;
|
|
19
15
|
|
|
20
16
|
/**
|
|
21
|
-
* Hook for creating and submitting an intent order for cross-chain swaps.
|
|
17
|
+
* Hook for creating and submitting an swap intent order for cross-chain swaps.
|
|
22
18
|
* Uses React Query's useMutation for better state management and caching.
|
|
23
19
|
*
|
|
24
|
-
* @param {
|
|
20
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for the swap
|
|
25
21
|
* @returns {UseMutationResult} Mutation result object containing mutation function and state
|
|
26
22
|
*
|
|
27
23
|
* @example
|
|
28
24
|
* ```typescript
|
|
29
|
-
* const { mutateAsync:
|
|
25
|
+
* const { mutateAsync: swap, isPending } = useSwap(spokeProvider);
|
|
30
26
|
*
|
|
31
27
|
* const handleSwap = async () => {
|
|
32
|
-
* const result = await
|
|
28
|
+
* const result = await swap({
|
|
33
29
|
* token_src: '0x...',
|
|
34
30
|
* token_src_blockchain_id: 'arbitrum',
|
|
35
31
|
* token_dst: '0x...',
|
|
@@ -40,18 +36,25 @@ type CreateIntentResult = Result<
|
|
|
40
36
|
* };
|
|
41
37
|
* ```
|
|
42
38
|
*/
|
|
43
|
-
export function
|
|
44
|
-
|
|
39
|
+
export function useSwap(
|
|
40
|
+
spokeProvider: SpokeProvider | undefined,
|
|
45
41
|
): UseMutationResult<CreateIntentResult, Error, CreateIntentParams> {
|
|
46
42
|
const { sodax } = useSodaxContext();
|
|
47
|
-
const
|
|
43
|
+
const queryClient = useQueryClient();
|
|
48
44
|
|
|
49
45
|
return useMutation<CreateIntentResult, Error, CreateIntentParams>({
|
|
50
46
|
mutationFn: async (params: CreateIntentParams) => {
|
|
51
47
|
if (!spokeProvider) {
|
|
52
48
|
throw new Error('Spoke provider not found');
|
|
53
49
|
}
|
|
54
|
-
return sodax.solver.
|
|
50
|
+
return sodax.solver.swap({
|
|
51
|
+
intentParams: params,
|
|
52
|
+
spokeProvider,
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
onSuccess: () => {
|
|
56
|
+
// Invalidate balance queries to refresh both source and destination token balances
|
|
57
|
+
queryClient.invalidateQueries({ queryKey: ['xBalances'] });
|
|
55
58
|
},
|
|
56
59
|
});
|
|
57
60
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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({
|
|
37
|
+
intentParams: params,
|
|
38
|
+
spokeProvider,
|
|
39
|
+
});
|
|
40
|
+
if (allowance.ok) {
|
|
41
|
+
return allowance.value;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
},
|
|
45
|
+
enabled: !!spokeProvider && !!params,
|
|
46
|
+
refetchInterval: 2000,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
2
|
+
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
import type { CreateIntentParams, SpokeProvider } from '@sodax/sdk';
|
|
4
|
+
|
|
5
|
+
interface UseApproveReturn {
|
|
6
|
+
approve: ({ params }: { params: CreateIntentParams }) => Promise<boolean>;
|
|
7
|
+
isLoading: boolean;
|
|
8
|
+
error: Error | null;
|
|
9
|
+
resetError: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Hook for approving token spending for money market actions
|
|
14
|
+
* @param token The token to approve spending for
|
|
15
|
+
* @param spokeProvider The spoke provider instance for the chain
|
|
16
|
+
* @returns Object containing approve function, loading state, error state and reset function
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* const { approve, isLoading, error } = useApprove(token, spokeProvider);
|
|
20
|
+
*
|
|
21
|
+
* // Approve tokens for supply action
|
|
22
|
+
* await approve({ amount: "100", action: "supply" });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export function useSwapApprove(
|
|
27
|
+
params: CreateIntentParams | undefined,
|
|
28
|
+
spokeProvider: SpokeProvider | undefined,
|
|
29
|
+
): UseApproveReturn {
|
|
30
|
+
const { sodax } = useSodaxContext();
|
|
31
|
+
const queryClient = useQueryClient();
|
|
32
|
+
|
|
33
|
+
const {
|
|
34
|
+
mutateAsync: approve,
|
|
35
|
+
isPending,
|
|
36
|
+
error,
|
|
37
|
+
reset: resetError,
|
|
38
|
+
} = useMutation({
|
|
39
|
+
mutationFn: async ({ params }: { params: CreateIntentParams | undefined }) => {
|
|
40
|
+
if (!spokeProvider) {
|
|
41
|
+
throw new Error('Spoke provider not found');
|
|
42
|
+
}
|
|
43
|
+
if (!params) {
|
|
44
|
+
throw new Error('Swap Params not found');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const allowance = await sodax.solver.approve({
|
|
48
|
+
intentParams: params,
|
|
49
|
+
spokeProvider,
|
|
50
|
+
});
|
|
51
|
+
if (!allowance.ok) {
|
|
52
|
+
throw new Error('Failed to approve input token');
|
|
53
|
+
}
|
|
54
|
+
return allowance.ok;
|
|
55
|
+
},
|
|
56
|
+
onSuccess: () => {
|
|
57
|
+
// Invalidate allowance query to refetch the new allowance
|
|
58
|
+
queryClient.invalidateQueries({ queryKey: ['allowance', params] });
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
approve,
|
|
64
|
+
isLoading: isPending,
|
|
65
|
+
error: error,
|
|
66
|
+
resetError,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import type { ReactNode, ReactElement } from 'react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
2
4
|
import { Sodax, type SodaxConfig } from '@sodax/sdk';
|
|
3
5
|
import { SodaxContext } from '@/contexts';
|
|
4
|
-
import
|
|
6
|
+
import type { RpcConfig } from '@sodax/types';
|
|
5
7
|
|
|
6
8
|
interface SodaxProviderProps {
|
|
7
9
|
children: ReactNode;
|
|
8
10
|
testnet?: boolean;
|
|
9
|
-
config
|
|
11
|
+
config?: SodaxConfig;
|
|
12
|
+
rpcConfig: RpcConfig;
|
|
10
13
|
}
|
|
11
14
|
|
|
12
|
-
export const SodaxProvider = ({ children, testnet = false, config }: SodaxProviderProps): ReactElement => {
|
|
15
|
+
export const SodaxProvider = ({ children, testnet = false, config, rpcConfig }: SodaxProviderProps): ReactElement => {
|
|
13
16
|
const sodax = new Sodax(config);
|
|
14
17
|
|
|
15
|
-
return <SodaxContext.Provider value={{ sodax, testnet }}>{children}</SodaxContext.Provider>;
|
|
18
|
+
return <SodaxContext.Provider value={{ sodax, testnet, rpcConfig }}>{children}</SodaxContext.Provider>;
|
|
16
19
|
};
|
package/dist/contexts/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/contexts/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,KAAK,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,eAAO,MAAM,YAAY,kDAA+C,CAAC"}
|
package/dist/core/index.d.ts
DELETED
package/dist/core/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAIpD,eAAO,MAAM,UAAU,EAAE,MAAM,EAAO,CAAC;AAyBvC,eAAO,MAAM,2BAA2B,iBAAkB,OAAO,SAAS,MAAM,WAU/E,CAAC"}
|
package/dist/hooks/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,MAAM,CAAC;AACrB,cAAc,QAAQ,CAAC"}
|
package/dist/hooks/mm/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/mm/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC"}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { ChainId, XToken } from '@sodax/types';
|
|
2
|
-
import { type UseMutationResult } from '@tanstack/react-query';
|
|
3
|
-
interface BorrowResponse {
|
|
4
|
-
ok: true;
|
|
5
|
-
value: [`0x${string}`, `0x${string}`];
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Hook for borrowing tokens from the Sodax money market.
|
|
9
|
-
*
|
|
10
|
-
* This hook provides functionality to borrow tokens from the money market protocol,
|
|
11
|
-
* handling the entire borrow process including transaction creation, submission,
|
|
12
|
-
* and cross-chain communication.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* const { mutateAsync: borrow, isPending, error } = useBorrow(hubToken, spokeChainId);
|
|
17
|
-
* await borrow('100');
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @throws {Error} When:
|
|
21
|
-
* - spokeProvider is not available
|
|
22
|
-
* - Transaction execution fails
|
|
23
|
-
*/
|
|
24
|
-
export declare function useBorrow(hubToken: XToken, spokeChainId: ChainId): UseMutationResult<BorrowResponse, Error, string>;
|
|
25
|
-
export {};
|
|
26
|
-
//# sourceMappingURL=useBorrow.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useBorrow.d.ts","sourceRoot":"","sources":["../../../src/hooks/mm/useBorrow.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAe,KAAK,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAK5E,UAAU,cAAc;IACtB,EAAE,EAAE,IAAI,CAAC;IACT,KAAK,EAAE,CAAC,KAAK,MAAM,EAAE,EAAE,KAAK,MAAM,EAAE,CAAC,CAAC;CACvC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,GAAG,iBAAiB,CAAC,cAAc,EAAE,KAAK,EAAE,MAAM,CAAC,CA0BnH"}
|