@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.
Files changed (126) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +152 -53
  3. package/dist/index.d.mts +1425 -0
  4. package/dist/index.d.ts +1425 -4
  5. package/dist/index.js +931 -194
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +890 -191
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +7 -8
  10. package/src/contexts/index.ts +2 -0
  11. package/src/core/index.ts +5 -33
  12. package/src/hooks/backend/README.md +135 -0
  13. package/src/hooks/backend/index.ts +23 -0
  14. package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +49 -0
  15. package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +61 -0
  16. package/src/hooks/backend/useBackendIntentByHash.ts +53 -0
  17. package/src/hooks/backend/useBackendIntentByTxHash.ts +52 -0
  18. package/src/hooks/backend/useBackendMoneyMarketAsset.ts +57 -0
  19. package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +67 -0
  20. package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +67 -0
  21. package/src/hooks/backend/useBackendMoneyMarketPosition.ts +56 -0
  22. package/src/hooks/backend/useBackendOrderbook.ts +63 -0
  23. package/src/hooks/bridge/index.ts +5 -0
  24. package/src/hooks/bridge/useBridge.ts +57 -0
  25. package/src/hooks/bridge/useBridgeAllowance.ts +49 -0
  26. package/src/hooks/bridge/useBridgeApprove.ts +68 -0
  27. package/src/hooks/bridge/useGetBridgeableAmount.ts +50 -0
  28. package/src/hooks/bridge/useGetBridgeableTokens.ts +62 -0
  29. package/src/hooks/index.ts +3 -0
  30. package/src/hooks/mm/index.ts +3 -1
  31. package/src/hooks/mm/useBorrow.ts +20 -10
  32. package/src/hooks/mm/useMMAllowance.ts +55 -0
  33. package/src/hooks/mm/useMMApprove.ts +67 -0
  34. package/src/hooks/mm/useRepay.ts +20 -10
  35. package/src/hooks/mm/useReservesData.ts +30 -0
  36. package/src/hooks/mm/useReservesHumanized.ts +30 -0
  37. package/src/hooks/mm/useReservesList.ts +29 -0
  38. package/src/hooks/mm/useReservesUsdFormat.ts +38 -0
  39. package/src/hooks/mm/useSupply.ts +9 -6
  40. package/src/hooks/mm/useUserFormattedSummary.ts +54 -0
  41. package/src/hooks/mm/useUserReservesData.ts +30 -48
  42. package/src/hooks/mm/useWithdraw.ts +17 -11
  43. package/src/hooks/provider/useHubProvider.ts +3 -21
  44. package/src/hooks/provider/useSpokeProvider.ts +97 -6
  45. package/src/hooks/shared/index.ts +4 -2
  46. package/src/hooks/shared/useDeriveUserWalletAddress.ts +44 -0
  47. package/src/hooks/shared/useEstimateGas.ts +18 -0
  48. package/src/hooks/shared/useRequestTrustline.ts +82 -0
  49. package/src/hooks/shared/useStellarTrustlineCheck.ts +71 -0
  50. package/src/hooks/staking/index.ts +19 -0
  51. package/src/hooks/staking/useCancelUnstake.ts +52 -0
  52. package/src/hooks/staking/useClaim.ts +46 -0
  53. package/src/hooks/staking/useConvertedAssets.ts +47 -0
  54. package/src/hooks/staking/useInstantUnstake.ts +50 -0
  55. package/src/hooks/staking/useInstantUnstakeAllowance.ts +59 -0
  56. package/src/hooks/staking/useInstantUnstakeApprove.ts +52 -0
  57. package/src/hooks/staking/useInstantUnstakeRatio.ts +54 -0
  58. package/src/hooks/staking/useStake.ts +47 -0
  59. package/src/hooks/staking/useStakeAllowance.ts +57 -0
  60. package/src/hooks/staking/useStakeApprove.ts +50 -0
  61. package/src/hooks/staking/useStakeRatio.ts +53 -0
  62. package/src/hooks/staking/useStakingConfig.ts +40 -0
  63. package/src/hooks/staking/useStakingInfo.ts +50 -0
  64. package/src/hooks/staking/useUnstake.ts +54 -0
  65. package/src/hooks/staking/useUnstakeAllowance.ts +58 -0
  66. package/src/hooks/staking/useUnstakeApprove.ts +52 -0
  67. package/src/hooks/staking/useUnstakingInfo.ts +53 -0
  68. package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +59 -0
  69. package/src/hooks/swap/index.ts +4 -1
  70. package/src/hooks/swap/useCancelSwap.ts +44 -0
  71. package/src/hooks/swap/useQuote.ts +20 -6
  72. package/src/hooks/swap/useStatus.ts +3 -3
  73. package/src/hooks/swap/{useCreateIntentOrder.ts → useSwap.ts} +22 -19
  74. package/src/hooks/swap/useSwapAllowance.ts +48 -0
  75. package/src/hooks/swap/useSwapApprove.ts +68 -0
  76. package/src/providers/SodaxProvider.tsx +7 -4
  77. package/dist/contexts/index.d.ts +0 -7
  78. package/dist/contexts/index.d.ts.map +0 -1
  79. package/dist/core/index.d.ts +0 -4
  80. package/dist/core/index.d.ts.map +0 -1
  81. package/dist/hooks/index.d.ts +0 -5
  82. package/dist/hooks/index.d.ts.map +0 -1
  83. package/dist/hooks/mm/index.d.ts +0 -7
  84. package/dist/hooks/mm/index.d.ts.map +0 -1
  85. package/dist/hooks/mm/useBorrow.d.ts +0 -26
  86. package/dist/hooks/mm/useBorrow.d.ts.map +0 -1
  87. package/dist/hooks/mm/useHubWalletAddress.d.ts +0 -24
  88. package/dist/hooks/mm/useHubWalletAddress.d.ts.map +0 -1
  89. package/dist/hooks/mm/useRepay.d.ts +0 -26
  90. package/dist/hooks/mm/useRepay.d.ts.map +0 -1
  91. package/dist/hooks/mm/useSupply.d.ts +0 -32
  92. package/dist/hooks/mm/useSupply.d.ts.map +0 -1
  93. package/dist/hooks/mm/useUserReservesData.d.ts +0 -9
  94. package/dist/hooks/mm/useUserReservesData.d.ts.map +0 -1
  95. package/dist/hooks/mm/useWithdraw.d.ts +0 -26
  96. package/dist/hooks/mm/useWithdraw.d.ts.map +0 -1
  97. package/dist/hooks/provider/index.d.ts +0 -3
  98. package/dist/hooks/provider/index.d.ts.map +0 -1
  99. package/dist/hooks/provider/useHubProvider.d.ts +0 -3
  100. package/dist/hooks/provider/useHubProvider.d.ts.map +0 -1
  101. package/dist/hooks/provider/useSpokeProvider.d.ts +0 -4
  102. package/dist/hooks/provider/useSpokeProvider.d.ts.map +0 -1
  103. package/dist/hooks/shared/index.d.ts +0 -4
  104. package/dist/hooks/shared/index.d.ts.map +0 -1
  105. package/dist/hooks/shared/useAllowance.d.ts +0 -3
  106. package/dist/hooks/shared/useAllowance.d.ts.map +0 -1
  107. package/dist/hooks/shared/useApprove.d.ts +0 -10
  108. package/dist/hooks/shared/useApprove.d.ts.map +0 -1
  109. package/dist/hooks/shared/useSodaxContext.d.ts +0 -8
  110. package/dist/hooks/shared/useSodaxContext.d.ts.map +0 -1
  111. package/dist/hooks/swap/index.d.ts +0 -4
  112. package/dist/hooks/swap/index.d.ts.map +0 -1
  113. package/dist/hooks/swap/useCreateIntentOrder.d.ts +0 -33
  114. package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +0 -1
  115. package/dist/hooks/swap/useQuote.d.ts +0 -39
  116. package/dist/hooks/swap/useQuote.d.ts.map +0 -1
  117. package/dist/hooks/swap/useStatus.d.ts +0 -31
  118. package/dist/hooks/swap/useStatus.d.ts.map +0 -1
  119. package/dist/index.d.ts.map +0 -1
  120. package/dist/providers/SodaxProvider.d.ts +0 -10
  121. package/dist/providers/SodaxProvider.d.ts.map +0 -1
  122. package/dist/providers/index.d.ts +0 -2
  123. package/dist/providers/index.d.ts.map +0 -1
  124. package/src/hooks/mm/useHubWalletAddress.ts +0 -49
  125. package/src/hooks/shared/useAllowance.ts +0 -31
  126. package/src/hooks/shared/useApprove.ts +0 -53
@@ -0,0 +1,40 @@
1
+ // packages/dapp-kit/src/hooks/staking/useStakingConfig.ts
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import type { StakingConfig } from '@sodax/sdk';
4
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
5
+
6
+ /**
7
+ * Hook for fetching staking configuration from the stakedSoda contract.
8
+ * Uses React Query for efficient caching and state management.
9
+ *
10
+ * @param {number} refetchInterval - The interval in milliseconds to refetch data (default: 30000)
11
+ * @returns {UseQueryResult<StakingConfig, Error>} Query result object containing staking config and state
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const { data: stakingConfig, isLoading, error } = useStakingConfig();
16
+ *
17
+ * if (isLoading) return <div>Loading staking config...</div>;
18
+ * if (stakingConfig) {
19
+ * console.log('Unstaking period (days):', stakingConfig.unstakingPeriod / 86400n);
20
+ * console.log('Max penalty (%):', stakingConfig.maxPenalty);
21
+ * }
22
+ * ```
23
+ */
24
+ export function useStakingConfig(refetchInterval = 30000): UseQueryResult<StakingConfig, Error> {
25
+ const { sodax } = useSodaxContext();
26
+
27
+ return useQuery({
28
+ queryKey: ['soda', 'stakingConfig'],
29
+ queryFn: async () => {
30
+ const result = await sodax.staking.getStakingConfig();
31
+
32
+ if (!result.ok) {
33
+ throw new Error(`Failed to fetch staking config: ${result.error.code}`);
34
+ }
35
+
36
+ return result.value;
37
+ },
38
+ refetchInterval,
39
+ });
40
+ }
@@ -0,0 +1,50 @@
1
+ // packages/dapp-kit/src/hooks/staking/useStakingInfo.ts
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import type { StakingInfo, SpokeProvider } from '@sodax/sdk';
4
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
5
+
6
+ /**
7
+ * Hook for fetching comprehensive staking information for a user.
8
+ * Uses React Query for efficient caching and state management.
9
+ *
10
+ * @param {SpokeProvider | undefined} spokeProvider - The spoke provider to use for the query
11
+ * @param {number} refetchInterval - The interval in milliseconds to refetch data (default: 5000)
12
+ * @returns {UseQueryResult<StakingInfo, Error>} Query result object containing staking info and state
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const { data: stakingInfo, isLoading, error } = useStakingInfo(spokeProvider);
17
+ *
18
+ * if (isLoading) return <div>Loading staking info...</div>;
19
+ * if (stakingInfo) {
20
+ * console.log('Total staked:', stakingInfo.totalStaked);
21
+ * console.log('User staked:', stakingInfo.userStaked);
22
+ * console.log('xSODA balance:', stakingInfo.userXSodaBalance);
23
+ * }
24
+ * ```
25
+ */
26
+ export function useStakingInfo(
27
+ spokeProvider: SpokeProvider | undefined,
28
+ refetchInterval = 5000,
29
+ ): UseQueryResult<StakingInfo, Error> {
30
+ const { sodax } = useSodaxContext();
31
+
32
+ return useQuery({
33
+ queryKey: ['soda', 'stakingInfo', spokeProvider?.chainConfig.chain.id],
34
+ queryFn: async () => {
35
+ if (!spokeProvider) {
36
+ throw new Error('Spoke provider not found');
37
+ }
38
+
39
+ const result = await sodax.staking.getStakingInfoFromSpoke(spokeProvider);
40
+
41
+ if (!result.ok) {
42
+ throw new Error(`Failed to fetch staking info: ${result.error.code}`);
43
+ }
44
+
45
+ return result.value;
46
+ },
47
+ enabled: !!spokeProvider,
48
+ refetchInterval,
49
+ });
50
+ }
@@ -0,0 +1,54 @@
1
+ // packages/dapp-kit/src/hooks/staking/useUnstake.ts
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import type { UnstakeParams, SpokeTxHash, HubTxHash, SpokeProvider } from '@sodax/sdk';
4
+ import { useMutation, useQueryClient, type UseMutationResult } from '@tanstack/react-query';
5
+
6
+ /**
7
+ * Hook for executing unstake transactions to unstake xSODA shares.
8
+ * Uses React Query's useMutation for better state management and caching.
9
+ *
10
+ * @param {SpokeProvider | undefined} spokeProvider - The spoke provider to use for the unstake
11
+ * @returns {UseMutationResult<[SpokeTxHash, HubTxHash], Error, Omit<UnstakeParams, 'action'>>} Mutation result object containing mutation function and state
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const { mutateAsync: unstake, isPending } = useUnstake(spokeProvider);
16
+ *
17
+ * const handleUnstake = async () => {
18
+ * const result = await unstake({
19
+ * amount: 1000000000000000000n, // 1 xSODA
20
+ * account: '0x...'
21
+ * });
22
+ *
23
+ * console.log('Unstake successful:', result);
24
+ * };
25
+ * ```
26
+ */
27
+ export function useUnstake(
28
+ spokeProvider: SpokeProvider | undefined,
29
+ ): UseMutationResult<[SpokeTxHash, HubTxHash], Error, Omit<UnstakeParams, 'action'>> {
30
+ const { sodax } = useSodaxContext();
31
+ const queryClient = useQueryClient();
32
+
33
+ return useMutation<[SpokeTxHash, HubTxHash], Error, Omit<UnstakeParams, 'action'>>({
34
+ mutationFn: async (params: Omit<UnstakeParams, 'action'>) => {
35
+ if (!spokeProvider) {
36
+ throw new Error('Spoke provider not found');
37
+ }
38
+
39
+ const result = await sodax.staking.unstake({ ...params, action: 'unstake' }, spokeProvider);
40
+
41
+ if (!result.ok) {
42
+ throw new Error(`Unstake failed: ${result.error.code}`);
43
+ }
44
+
45
+ return result.value;
46
+ },
47
+ onSuccess: () => {
48
+ // Invalidate relevant queries to refresh data
49
+ queryClient.invalidateQueries({ queryKey: ['stakingInfo'] });
50
+ queryClient.invalidateQueries({ queryKey: ['unstakingInfo'] });
51
+ queryClient.invalidateQueries({ queryKey: ['unstakingInfoWithPenalty'] });
52
+ },
53
+ });
54
+ }
@@ -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
+ }
@@ -1,3 +1,6 @@
1
1
  export * from './useQuote';
2
- export * from './useCreateIntentOrder';
2
+ export * from './useSwap';
3
3
  export * from './useStatus';
4
+ export * from './useSwapAllowance';
5
+ export * from './useSwapApprove';
6
+ export * from './useCancelSwap';
@@ -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 { IntentErrorResponse, IntentQuoteRequest, IntentQuoteResponse, Result } from '@sodax/sdk';
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 {IntentQuoteRequest | undefined} payload - The intent quote request parameters. If undefined, the query will be disabled.
11
+ * @param {SolverIntentQuoteRequest | undefined} payload - The intent quote request parameters. If undefined, the query will be disabled.
11
12
  *
12
- * @returns {UseQueryResult<Result<IntentQuoteResponse, IntentErrorResponse> | undefined>} A query result object containing:
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: IntentQuoteRequest | undefined,
42
- ): UseQueryResult<Result<IntentQuoteResponse, IntentErrorResponse> | undefined> => {
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: [payload],
59
+ queryKey,
46
60
  queryFn: async () => {
47
61
  if (!payload) {
48
62
  return undefined;
@@ -1,4 +1,4 @@
1
- import type { Hex, IntentErrorResponse, IntentStatusResponse, Result } from '@sodax/sdk';
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<IntentStatusResponse, IntentErrorResponse> | undefined>} A query result object containing:
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<IntentStatusResponse, IntentErrorResponse> | undefined> => {
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
- SpokeChainId,
5
- IntentExecutionResponse,
4
+ SolverExecutionResponse,
6
5
  Result,
7
- IntentSubmitErrorCode,
6
+ IntentErrorCode,
8
7
  Intent,
9
- PacketData,
10
- IntentSubmitError,
8
+ IntentError,
9
+ SpokeProvider,
10
+ IntentDeliveryInfo,
11
11
  } from '@sodax/sdk';
12
- import { useSpokeProvider } from '../provider/useSpokeProvider';
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 {SpokeChainId} chainId - The source chain ID where the swap will originate
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: createIntent, isPending } = useCreateIntentOrder('0xa4b1.arbitrum');
25
+ * const { mutateAsync: swap, isPending } = useSwap(spokeProvider);
30
26
  *
31
27
  * const handleSwap = async () => {
32
- * const result = await createIntent({
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 useCreateIntentOrder(
44
- chainId: SpokeChainId,
39
+ export function useSwap(
40
+ spokeProvider: SpokeProvider | undefined,
45
41
  ): UseMutationResult<CreateIntentResult, Error, CreateIntentParams> {
46
42
  const { sodax } = useSodaxContext();
47
- const spokeProvider = useSpokeProvider(chainId);
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.createAndSubmitIntent(params, spokeProvider);
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
+ }