@sodax/dapp-kit 0.0.1-rc.9 → 1.0.0-rc.2

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 +142 -44
  3. package/dist/index.d.mts +1514 -0
  4. package/dist/index.d.ts +1514 -4
  5. package/dist/index.js +997 -82
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +957 -84
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +7 -8
  10. package/src/contexts/index.ts +3 -2
  11. package/src/hooks/backend/README.md +135 -0
  12. package/src/hooks/backend/index.ts +23 -0
  13. package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +49 -0
  14. package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +61 -0
  15. package/src/hooks/backend/useBackendIntentByHash.ts +53 -0
  16. package/src/hooks/backend/useBackendIntentByTxHash.ts +52 -0
  17. package/src/hooks/backend/useBackendMoneyMarketAsset.ts +57 -0
  18. package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +67 -0
  19. package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +67 -0
  20. package/src/hooks/backend/useBackendMoneyMarketPosition.ts +56 -0
  21. package/src/hooks/backend/useBackendOrderbook.ts +63 -0
  22. package/src/hooks/bridge/index.ts +5 -0
  23. package/src/hooks/bridge/useBridge.ts +57 -0
  24. package/src/hooks/bridge/useBridgeAllowance.ts +49 -0
  25. package/src/hooks/bridge/useBridgeApprove.ts +68 -0
  26. package/src/hooks/bridge/useGetBridgeableAmount.ts +50 -0
  27. package/src/hooks/bridge/useGetBridgeableTokens.ts +62 -0
  28. package/src/hooks/index.ts +4 -0
  29. package/src/hooks/migrate/index.ts +4 -0
  30. package/src/hooks/migrate/types.ts +15 -0
  31. package/src/hooks/migrate/useMigrate.tsx +110 -0
  32. package/src/hooks/migrate/useMigrationAllowance.tsx +79 -0
  33. package/src/hooks/migrate/useMigrationApprove.tsx +129 -0
  34. package/src/hooks/mm/useMMAllowance.ts +2 -1
  35. package/src/hooks/mm/useMMApprove.ts +2 -1
  36. package/src/hooks/mm/useReservesData.ts +1 -8
  37. package/src/hooks/mm/useReservesHumanized.ts +30 -0
  38. package/src/hooks/mm/useReservesList.ts +29 -0
  39. package/src/hooks/mm/useReservesUsdFormat.ts +38 -0
  40. package/src/hooks/mm/useUserFormattedSummary.ts +54 -0
  41. package/src/hooks/mm/useUserReservesData.ts +30 -31
  42. package/src/hooks/provider/useHubProvider.ts +3 -3
  43. package/src/hooks/provider/useSpokeProvider.ts +45 -21
  44. package/src/hooks/shared/index.ts +4 -0
  45. package/src/hooks/shared/useDeriveUserWalletAddress.ts +44 -0
  46. package/src/hooks/shared/useEstimateGas.ts +18 -0
  47. package/src/hooks/shared/useRequestTrustline.ts +103 -0
  48. package/src/hooks/shared/useStellarTrustlineCheck.ts +71 -0
  49. package/src/hooks/staking/index.ts +19 -0
  50. package/src/hooks/staking/useCancelUnstake.ts +52 -0
  51. package/src/hooks/staking/useClaim.ts +46 -0
  52. package/src/hooks/staking/useConvertedAssets.ts +47 -0
  53. package/src/hooks/staking/useInstantUnstake.ts +50 -0
  54. package/src/hooks/staking/useInstantUnstakeAllowance.ts +59 -0
  55. package/src/hooks/staking/useInstantUnstakeApprove.ts +52 -0
  56. package/src/hooks/staking/useInstantUnstakeRatio.ts +54 -0
  57. package/src/hooks/staking/useStake.ts +47 -0
  58. package/src/hooks/staking/useStakeAllowance.ts +57 -0
  59. package/src/hooks/staking/useStakeApprove.ts +50 -0
  60. package/src/hooks/staking/useStakeRatio.ts +53 -0
  61. package/src/hooks/staking/useStakingConfig.ts +40 -0
  62. package/src/hooks/staking/useStakingInfo.ts +50 -0
  63. package/src/hooks/staking/useUnstake.ts +54 -0
  64. package/src/hooks/staking/useUnstakeAllowance.ts +58 -0
  65. package/src/hooks/staking/useUnstakeApprove.ts +52 -0
  66. package/src/hooks/staking/useUnstakingInfo.ts +53 -0
  67. package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +59 -0
  68. package/src/hooks/swap/index.ts +2 -1
  69. package/src/hooks/swap/useCancelSwap.ts +44 -0
  70. package/src/hooks/swap/useQuote.ts +16 -2
  71. package/src/hooks/swap/useStatus.ts +1 -1
  72. package/src/hooks/swap/{useCreateIntentOrder.ts → useSwap.ts} +16 -11
  73. package/src/hooks/swap/useSwapAllowance.ts +5 -1
  74. package/src/hooks/swap/useSwapApprove.ts +14 -14
  75. package/src/providers/SodaxProvider.tsx +8 -20
  76. package/dist/contexts/index.d.ts +0 -8
  77. package/dist/contexts/index.d.ts.map +0 -1
  78. package/dist/core/index.d.ts +0 -3
  79. package/dist/core/index.d.ts.map +0 -1
  80. package/dist/hooks/index.d.ts +0 -5
  81. package/dist/hooks/index.d.ts.map +0 -1
  82. package/dist/hooks/mm/index.d.ts +0 -9
  83. package/dist/hooks/mm/index.d.ts.map +0 -1
  84. package/dist/hooks/mm/useBorrow.d.ts +0 -35
  85. package/dist/hooks/mm/useBorrow.d.ts.map +0 -1
  86. package/dist/hooks/mm/useMMAllowance.d.ts +0 -26
  87. package/dist/hooks/mm/useMMAllowance.d.ts.map +0 -1
  88. package/dist/hooks/mm/useMMApprove.d.ts +0 -27
  89. package/dist/hooks/mm/useMMApprove.d.ts.map +0 -1
  90. package/dist/hooks/mm/useRepay.d.ts +0 -35
  91. package/dist/hooks/mm/useRepay.d.ts.map +0 -1
  92. package/dist/hooks/mm/useReservesData.d.ts +0 -19
  93. package/dist/hooks/mm/useReservesData.d.ts.map +0 -1
  94. package/dist/hooks/mm/useSupply.d.ts +0 -34
  95. package/dist/hooks/mm/useSupply.d.ts.map +0 -1
  96. package/dist/hooks/mm/useUserReservesData.d.ts +0 -3
  97. package/dist/hooks/mm/useUserReservesData.d.ts.map +0 -1
  98. package/dist/hooks/mm/useWithdraw.d.ts +0 -33
  99. package/dist/hooks/mm/useWithdraw.d.ts.map +0 -1
  100. package/dist/hooks/provider/index.d.ts +0 -3
  101. package/dist/hooks/provider/index.d.ts.map +0 -1
  102. package/dist/hooks/provider/useHubProvider.d.ts +0 -3
  103. package/dist/hooks/provider/useHubProvider.d.ts.map +0 -1
  104. package/dist/hooks/provider/useSpokeProvider.d.ts +0 -18
  105. package/dist/hooks/provider/useSpokeProvider.d.ts.map +0 -1
  106. package/dist/hooks/shared/index.d.ts +0 -2
  107. package/dist/hooks/shared/index.d.ts.map +0 -1
  108. package/dist/hooks/shared/useSodaxContext.d.ts +0 -8
  109. package/dist/hooks/shared/useSodaxContext.d.ts.map +0 -1
  110. package/dist/hooks/swap/index.d.ts +0 -6
  111. package/dist/hooks/swap/index.d.ts.map +0 -1
  112. package/dist/hooks/swap/useCreateIntentOrder.d.ts +0 -33
  113. package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +0 -1
  114. package/dist/hooks/swap/useQuote.d.ts +0 -39
  115. package/dist/hooks/swap/useQuote.d.ts.map +0 -1
  116. package/dist/hooks/swap/useStatus.d.ts +0 -31
  117. package/dist/hooks/swap/useStatus.d.ts.map +0 -1
  118. package/dist/hooks/swap/useSwapAllowance.d.ts +0 -23
  119. package/dist/hooks/swap/useSwapAllowance.d.ts.map +0 -1
  120. package/dist/hooks/swap/useSwapApprove.d.ts +0 -26
  121. package/dist/hooks/swap/useSwapApprove.d.ts.map +0 -1
  122. package/dist/index.d.ts.map +0 -1
  123. package/dist/providers/SodaxProvider.d.ts +0 -10
  124. package/dist/providers/SodaxProvider.d.ts.map +0 -1
  125. package/dist/providers/index.d.ts +0 -2
  126. package/dist/providers/index.d.ts.map +0 -1
@@ -0,0 +1,50 @@
1
+ // packages/dapp-kit/src/hooks/staking/useStakeApprove.ts
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import type { StakeParams, TxReturnType, SpokeProvider } from '@sodax/sdk';
4
+ import { useMutation, type UseMutationResult } from '@tanstack/react-query';
5
+
6
+ /**
7
+ * Hook for approving SODA token spending for staking operations.
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 approval
11
+ * @returns {UseMutationResult<TxReturnType<SpokeProvider, false>, Error, Omit<StakeParams, 'action'>>} Mutation result object containing mutation function and state
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const { mutateAsync: approve, isPending } = useStakeApprove(spokeProvider);
16
+ *
17
+ * const handleApprove = async () => {
18
+ * const result = await approve({
19
+ * amount: 1000000000000000000n, // 1 SODA
20
+ * account: '0x...'
21
+ * });
22
+ *
23
+ * console.log('Approval successful:', result);
24
+ * };
25
+ * ```
26
+ */
27
+ export function useStakeApprove(
28
+ spokeProvider: SpokeProvider | undefined,
29
+ ): UseMutationResult<TxReturnType<SpokeProvider, false>, Error, Omit<StakeParams, 'action'>> {
30
+ const { sodax } = useSodaxContext();
31
+
32
+ return useMutation<TxReturnType<SpokeProvider, false>, Error, Omit<StakeParams, 'action'>>({
33
+ mutationFn: async (params: Omit<StakeParams, 'action'>) => {
34
+ if (!spokeProvider) {
35
+ throw new Error('Spoke provider not found');
36
+ }
37
+
38
+ const result = await sodax.staking.approve({
39
+ params: { ...params, action: 'stake' },
40
+ spokeProvider,
41
+ });
42
+
43
+ if (!result.ok) {
44
+ throw new Error(`Stake approval failed: ${result.error.code}`);
45
+ }
46
+
47
+ return result.value;
48
+ },
49
+ });
50
+ }
@@ -0,0 +1,53 @@
1
+ // packages/dapp-kit/src/hooks/staking/useStakeRatio.ts
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
4
+
5
+ /**
6
+ * Hook for fetching stake ratio estimates (xSoda amount and preview deposit).
7
+ * Uses React Query for efficient caching and state management.
8
+ *
9
+ * @param {bigint | undefined} amount - The amount of SODA to estimate stake for
10
+ * @param {number} refetchInterval - The interval in milliseconds to refetch data (default: 10000)
11
+ * @returns {UseQueryResult<[bigint, bigint], Error>} Query result object containing stake ratio estimates and state
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const { data: stakeRatio, isLoading, error } = useStakeRatio(1000000000000000000n); // 1 SODA
16
+ *
17
+ * if (isLoading) return <div>Loading stake ratio...</div>;
18
+ * if (stakeRatio) {
19
+ * const [xSodaAmount, previewDepositAmount] = stakeRatio;
20
+ * console.log('xSoda amount:', xSodaAmount);
21
+ * console.log('Preview deposit:', previewDepositAmount);
22
+ * }
23
+ * ```
24
+ */
25
+ export function useStakeRatio(
26
+ amount: bigint | undefined,
27
+ refetchInterval = 10000,
28
+ ): UseQueryResult<[bigint, bigint], Error> {
29
+ const { sodax } = useSodaxContext();
30
+
31
+ return useQuery({
32
+ queryKey: ['soda', 'stakeRatio', amount?.toString()],
33
+ queryFn: async () => {
34
+ if (!amount || amount <= 0n) {
35
+ throw new Error('Amount must be greater than 0');
36
+ }
37
+
38
+ if (!sodax?.staking) {
39
+ throw new Error('Staking service not available');
40
+ }
41
+
42
+ const result = await sodax.staking.getStakeRatio(amount);
43
+
44
+ if (!result.ok) {
45
+ throw new Error(`Failed to fetch stake ratio: ${result.error.code}`);
46
+ }
47
+
48
+ return result.value;
49
+ },
50
+ enabled: !!amount && amount > 0n && !!sodax?.staking,
51
+ refetchInterval,
52
+ });
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,5 +1,6 @@
1
1
  export * from './useQuote';
2
- export * from './useCreateIntentOrder';
2
+ export * from './useSwap';
3
3
  export * from './useStatus';
4
4
  export * from './useSwapAllowance';
5
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.swap.cancelIntent(intent, spokeProvider, raw);
42
+ },
43
+ });
44
+ }
@@ -1,6 +1,7 @@
1
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.
@@ -41,13 +42,26 @@ export const useQuote = (
41
42
  payload: SolverIntentQuoteRequest | undefined,
42
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;
49
63
  }
50
- return sodax.solver.getQuote(payload);
64
+ return sodax.swap.getQuote(payload);
51
65
  },
52
66
  enabled: !!payload,
53
67
  refetchInterval: 3000,
@@ -37,7 +37,7 @@ export const useStatus = (
37
37
  return useQuery({
38
38
  queryKey: [intent_tx_hash],
39
39
  queryFn: async () => {
40
- return sodax.solver.getStatus({ intent_tx_hash });
40
+ return sodax.swap.getStatus({ intent_tx_hash });
41
41
  },
42
42
  refetchInterval: 3000, // 3s
43
43
  });
@@ -5,19 +5,16 @@ import type {
5
5
  Result,
6
6
  IntentErrorCode,
7
7
  Intent,
8
- PacketData,
9
8
  IntentError,
10
9
  SpokeProvider,
10
+ IntentDeliveryInfo,
11
11
  } from '@sodax/sdk';
12
- import { useMutation, type UseMutationResult } from '@tanstack/react-query';
12
+ import { useMutation, type UseMutationResult, useQueryClient } from '@tanstack/react-query';
13
13
 
14
- type CreateIntentResult = Result<
15
- [SolverExecutionResponse, Intent, PacketData],
16
- IntentError<IntentErrorCode>
17
- >;
14
+ type CreateIntentResult = Result<[SolverExecutionResponse, Intent, IntentDeliveryInfo], IntentError<IntentErrorCode>>;
18
15
 
19
16
  /**
20
- * 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.
21
18
  * Uses React Query's useMutation for better state management and caching.
22
19
  *
23
20
  * @param {SpokeProvider} spokeProvider - The spoke provider to use for the swap
@@ -25,10 +22,10 @@ type CreateIntentResult = Result<
25
22
  *
26
23
  * @example
27
24
  * ```typescript
28
- * const { mutateAsync: createIntent, isPending } = useCreateIntentOrder(spokeProvider);
25
+ * const { mutateAsync: swap, isPending } = useSwap(spokeProvider);
29
26
  *
30
27
  * const handleSwap = async () => {
31
- * const result = await createIntent({
28
+ * const result = await swap({
32
29
  * token_src: '0x...',
33
30
  * token_src_blockchain_id: 'arbitrum',
34
31
  * token_dst: '0x...',
@@ -39,17 +36,25 @@ type CreateIntentResult = Result<
39
36
  * };
40
37
  * ```
41
38
  */
42
- export function useCreateIntentOrder(
39
+ export function useSwap(
43
40
  spokeProvider: SpokeProvider | undefined,
44
41
  ): UseMutationResult<CreateIntentResult, Error, CreateIntentParams> {
45
42
  const { sodax } = useSodaxContext();
43
+ const queryClient = useQueryClient();
46
44
 
47
45
  return useMutation<CreateIntentResult, Error, CreateIntentParams>({
48
46
  mutationFn: async (params: CreateIntentParams) => {
49
47
  if (!spokeProvider) {
50
48
  throw new Error('Spoke provider not found');
51
49
  }
52
- return sodax.solver.createAndSubmitIntent(params, spokeProvider);
50
+ return sodax.swap.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'] });
53
58
  },
54
59
  });
55
60
  }
@@ -33,12 +33,16 @@ export function useSwapAllowance(
33
33
  if (!spokeProvider || !params) {
34
34
  return false;
35
35
  }
36
- const allowance = await sodax.solver.isAllowanceValid(params, spokeProvider);
36
+ const allowance = await sodax.swap.isAllowanceValid({
37
+ intentParams: params,
38
+ spokeProvider,
39
+ });
37
40
  if (allowance.ok) {
38
41
  return allowance.value;
39
42
  }
40
43
  return false;
41
44
  },
42
45
  enabled: !!spokeProvider && !!params,
46
+ refetchInterval: 2000,
43
47
  });
44
48
  }