@sodax/dapp-kit 0.0.1-rc.8 → 1.0.0-beta

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 (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +157 -60
  3. package/dist/index.d.mts +1558 -0
  4. package/dist/index.d.ts +1558 -4
  5. package/dist/index.js +1030 -135
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +988 -134
  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 +57 -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/index.ts +2 -1
  35. package/src/hooks/mm/useAToken.ts +47 -0
  36. package/src/hooks/mm/useBorrow.ts +2 -2
  37. package/src/hooks/mm/useMMAllowance.ts +2 -1
  38. package/src/hooks/mm/useMMApprove.ts +2 -1
  39. package/src/hooks/mm/useRepay.ts +2 -2
  40. package/src/hooks/mm/useReservesData.ts +1 -8
  41. package/src/hooks/mm/useReservesHumanized.ts +30 -0
  42. package/src/hooks/mm/useReservesList.ts +29 -0
  43. package/src/hooks/mm/useReservesUsdFormat.ts +38 -0
  44. package/src/hooks/mm/useSupply.ts +2 -2
  45. package/src/hooks/mm/useUserFormattedSummary.ts +54 -0
  46. package/src/hooks/mm/useUserReservesData.ts +30 -37
  47. package/src/hooks/mm/useWithdraw.ts +2 -2
  48. package/src/hooks/provider/useHubProvider.ts +3 -3
  49. package/src/hooks/provider/useSpokeProvider.ts +50 -18
  50. package/src/hooks/shared/index.ts +4 -0
  51. package/src/hooks/shared/useDeriveUserWalletAddress.ts +44 -0
  52. package/src/hooks/shared/useEstimateGas.ts +18 -0
  53. package/src/hooks/shared/useRequestTrustline.ts +103 -0
  54. package/src/hooks/shared/useStellarTrustlineCheck.ts +71 -0
  55. package/src/hooks/staking/index.ts +19 -0
  56. package/src/hooks/staking/useCancelUnstake.ts +52 -0
  57. package/src/hooks/staking/useClaim.ts +46 -0
  58. package/src/hooks/staking/useConvertedAssets.ts +47 -0
  59. package/src/hooks/staking/useInstantUnstake.ts +50 -0
  60. package/src/hooks/staking/useInstantUnstakeAllowance.ts +59 -0
  61. package/src/hooks/staking/useInstantUnstakeApprove.ts +52 -0
  62. package/src/hooks/staking/useInstantUnstakeRatio.ts +54 -0
  63. package/src/hooks/staking/useStake.ts +47 -0
  64. package/src/hooks/staking/useStakeAllowance.ts +57 -0
  65. package/src/hooks/staking/useStakeApprove.ts +50 -0
  66. package/src/hooks/staking/useStakeRatio.ts +53 -0
  67. package/src/hooks/staking/useStakingConfig.ts +40 -0
  68. package/src/hooks/staking/useStakingInfo.ts +50 -0
  69. package/src/hooks/staking/useUnstake.ts +54 -0
  70. package/src/hooks/staking/useUnstakeAllowance.ts +58 -0
  71. package/src/hooks/staking/useUnstakeApprove.ts +52 -0
  72. package/src/hooks/staking/useUnstakingInfo.ts +53 -0
  73. package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +59 -0
  74. package/src/hooks/swap/index.ts +2 -1
  75. package/src/hooks/swap/useCancelSwap.ts +44 -0
  76. package/src/hooks/swap/useQuote.ts +21 -7
  77. package/src/hooks/swap/useStatus.ts +4 -4
  78. package/src/hooks/swap/{useCreateIntentOrder.ts → useSwap.ts} +19 -14
  79. package/src/hooks/swap/useSwapAllowance.ts +5 -1
  80. package/src/hooks/swap/useSwapApprove.ts +14 -14
  81. package/src/index.ts +0 -1
  82. package/src/providers/SodaxProvider.tsx +8 -20
  83. package/dist/contexts/index.d.ts +0 -8
  84. package/dist/contexts/index.d.ts.map +0 -1
  85. package/dist/core/index.d.ts +0 -4
  86. package/dist/core/index.d.ts.map +0 -1
  87. package/dist/hooks/index.d.ts +0 -5
  88. package/dist/hooks/index.d.ts.map +0 -1
  89. package/dist/hooks/mm/index.d.ts +0 -10
  90. package/dist/hooks/mm/index.d.ts.map +0 -1
  91. package/dist/hooks/mm/useBorrow.d.ts +0 -35
  92. package/dist/hooks/mm/useBorrow.d.ts.map +0 -1
  93. package/dist/hooks/mm/useHubWalletAddress.d.ts +0 -24
  94. package/dist/hooks/mm/useHubWalletAddress.d.ts.map +0 -1
  95. package/dist/hooks/mm/useMMAllowance.d.ts +0 -26
  96. package/dist/hooks/mm/useMMAllowance.d.ts.map +0 -1
  97. package/dist/hooks/mm/useMMApprove.d.ts +0 -27
  98. package/dist/hooks/mm/useMMApprove.d.ts.map +0 -1
  99. package/dist/hooks/mm/useRepay.d.ts +0 -35
  100. package/dist/hooks/mm/useRepay.d.ts.map +0 -1
  101. package/dist/hooks/mm/useReservesData.d.ts +0 -19
  102. package/dist/hooks/mm/useReservesData.d.ts.map +0 -1
  103. package/dist/hooks/mm/useSupply.d.ts +0 -34
  104. package/dist/hooks/mm/useSupply.d.ts.map +0 -1
  105. package/dist/hooks/mm/useUserReservesData.d.ts +0 -9
  106. package/dist/hooks/mm/useUserReservesData.d.ts.map +0 -1
  107. package/dist/hooks/mm/useWithdraw.d.ts +0 -33
  108. package/dist/hooks/mm/useWithdraw.d.ts.map +0 -1
  109. package/dist/hooks/provider/index.d.ts +0 -3
  110. package/dist/hooks/provider/index.d.ts.map +0 -1
  111. package/dist/hooks/provider/useHubProvider.d.ts +0 -3
  112. package/dist/hooks/provider/useHubProvider.d.ts.map +0 -1
  113. package/dist/hooks/provider/useSpokeProvider.d.ts +0 -18
  114. package/dist/hooks/provider/useSpokeProvider.d.ts.map +0 -1
  115. package/dist/hooks/shared/index.d.ts +0 -2
  116. package/dist/hooks/shared/index.d.ts.map +0 -1
  117. package/dist/hooks/shared/useSodaxContext.d.ts +0 -8
  118. package/dist/hooks/shared/useSodaxContext.d.ts.map +0 -1
  119. package/dist/hooks/swap/index.d.ts +0 -6
  120. package/dist/hooks/swap/index.d.ts.map +0 -1
  121. package/dist/hooks/swap/useCreateIntentOrder.d.ts +0 -33
  122. package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +0 -1
  123. package/dist/hooks/swap/useQuote.d.ts +0 -39
  124. package/dist/hooks/swap/useQuote.d.ts.map +0 -1
  125. package/dist/hooks/swap/useStatus.d.ts +0 -31
  126. package/dist/hooks/swap/useStatus.d.ts.map +0 -1
  127. package/dist/hooks/swap/useSwapAllowance.d.ts +0 -23
  128. package/dist/hooks/swap/useSwapAllowance.d.ts.map +0 -1
  129. package/dist/hooks/swap/useSwapApprove.d.ts +0 -26
  130. package/dist/hooks/swap/useSwapApprove.d.ts.map +0 -1
  131. package/dist/index.d.ts.map +0 -1
  132. package/dist/providers/SodaxProvider.d.ts +0 -10
  133. package/dist/providers/SodaxProvider.d.ts.map +0 -1
  134. package/dist/providers/index.d.ts +0 -2
  135. package/dist/providers/index.d.ts.map +0 -1
  136. package/src/core/index.ts +0 -35
  137. package/src/hooks/mm/useHubWalletAddress.ts +0 -49
@@ -0,0 +1,52 @@
1
+ // packages/dapp-kit/src/hooks/staking/useStakeApprove.ts
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import type { InstantUnstakeParams, TxReturnType, SpokeProvider } from '@sodax/sdk';
4
+ import { useMutation, type UseMutationResult } from '@tanstack/react-query';
5
+
6
+ /**
7
+ * Hook for approving xSODA token spending for instant unstaking 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<InstantUnstakeParams, 'action'>>} Mutation result object containing mutation function and state
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const { mutateAsync: approve, isPending } = useInstantUnstakeApprove(spokeProvider);
16
+ *
17
+ * const handleApprove = async () => {
18
+ * const result = await approve({
19
+ * amount: 1000000000000000000n, // 1 xSODA
20
+ * minAmount: 950000000000000000n, // 0.95 SODA
21
+ * account: '0x...'
22
+ * });
23
+ *
24
+ * console.log('Approval successful:', result);
25
+ * };
26
+ * ```
27
+ */
28
+ export function useInstantUnstakeApprove(
29
+ spokeProvider: SpokeProvider | undefined,
30
+ ): UseMutationResult<TxReturnType<SpokeProvider, false>, Error, Omit<InstantUnstakeParams, 'action'>> {
31
+ const { sodax } = useSodaxContext();
32
+
33
+ return useMutation<TxReturnType<SpokeProvider, false>, Error, Omit<InstantUnstakeParams, 'action'>>({
34
+ mutationFn: async (params: Omit<InstantUnstakeParams, 'action'>) => {
35
+ console.log('useInstantUnstakeApprove 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: 'instantUnstake' },
42
+ spokeProvider,
43
+ });
44
+
45
+ if (!result.ok) {
46
+ throw new Error(`Instant unstake approval failed: ${result.error.code}`);
47
+ }
48
+
49
+ return result.value;
50
+ },
51
+ });
52
+ }
@@ -0,0 +1,54 @@
1
+ // packages/dapp-kit/src/hooks/staking/useInstantUnstakeRatio.ts
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
4
+
5
+ /**
6
+ * Hook for fetching instant unstake ratio estimates.
7
+ * Uses React Query for efficient caching and state management.
8
+ *
9
+ * @param {bigint | undefined} amount - The amount of xSoda to estimate instant unstake for
10
+ * @param {number} refetchInterval - The interval in milliseconds to refetch data (default: 10000)
11
+ * @returns {UseQueryResult<bigint, Error>} Query result object containing instant unstake ratio and state
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const { data: instantUnstakeRatio, isLoading, error } = useInstantUnstakeRatio(1000000000000000000n); // 1 xSoda
16
+ *
17
+ * if (isLoading) return <div>Loading instant unstake ratio...</div>;
18
+ * if (instantUnstakeRatio) {
19
+ * console.log('Instant unstake ratio:', instantUnstakeRatio);
20
+ * }
21
+ * ```
22
+ */
23
+ export function useInstantUnstakeRatio(
24
+ amount: bigint | undefined,
25
+ refetchInterval = 10000,
26
+ ): UseQueryResult<bigint, Error> {
27
+ const { sodax } = useSodaxContext();
28
+
29
+ console.log('useInstantUnstakeRatio hook called with:', { amount: amount?.toString(), sodax: !!sodax });
30
+
31
+ return useQuery({
32
+ queryKey: ['soda', 'instantUnstakeRatio', amount?.toString()],
33
+ queryFn: async () => {
34
+ console.log('useInstantUnstakeRatio queryFn called with amount:', amount?.toString());
35
+ if (!amount || amount <= 0n) {
36
+ throw new Error('Amount must be greater than 0');
37
+ }
38
+
39
+ if (!sodax?.staking) {
40
+ throw new Error('Staking service not available');
41
+ }
42
+
43
+ const result = await sodax.staking.getInstantUnstakeRatio(amount);
44
+
45
+ if (!result.ok) {
46
+ throw new Error(`Failed to fetch instant unstake ratio: ${result.error.code}`);
47
+ }
48
+
49
+ return result.value;
50
+ },
51
+ enabled: !!amount && amount > 0n && !!sodax?.staking,
52
+ refetchInterval,
53
+ });
54
+ }
@@ -0,0 +1,47 @@
1
+ // packages/dapp-kit/src/hooks/staking/useStake.ts
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import type { StakeParams, SpokeTxHash, HubTxHash, SpokeProvider } from '@sodax/sdk';
4
+ import { useMutation, type UseMutationResult } from '@tanstack/react-query';
5
+
6
+ /**
7
+ * Hook for executing stake transactions to stake SODA tokens and receive 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 stake
11
+ * @returns {UseMutationResult<[SpokeTxHash, HubTxHash], Error, StakeParams>} Mutation result object containing mutation function and state
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const { mutateAsync: stake, isPending } = useStake(spokeProvider);
16
+ *
17
+ * const handleStake = async () => {
18
+ * const result = await stake({
19
+ * amount: 1000000000000000000n, // 1 SODA
20
+ * account: '0x...'
21
+ * });
22
+ *
23
+ * console.log('Stake successful:', result);
24
+ * };
25
+ * ```
26
+ */
27
+ export function useStake(
28
+ spokeProvider: SpokeProvider | undefined,
29
+ ): UseMutationResult<[SpokeTxHash, HubTxHash], Error, StakeParams> {
30
+ const { sodax } = useSodaxContext();
31
+
32
+ return useMutation<[SpokeTxHash, HubTxHash], Error, StakeParams>({
33
+ mutationFn: async (params: StakeParams) => {
34
+ if (!spokeProvider) {
35
+ throw new Error('Spoke provider not found');
36
+ }
37
+
38
+ const result = await sodax.staking.stake(params, spokeProvider);
39
+
40
+ if (!result.ok) {
41
+ throw new Error(`Stake failed: ${result.error.code}`);
42
+ }
43
+
44
+ return result.value;
45
+ },
46
+ });
47
+ }
@@ -0,0 +1,57 @@
1
+ // packages/dapp-kit/src/hooks/staking/useStakeAllowance.ts
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import type { StakeParams, SpokeProvider } from '@sodax/sdk';
4
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
5
+
6
+ /**
7
+ * Hook for checking SODA token allowance for staking operations.
8
+ * Uses React Query for efficient caching and state management.
9
+ *
10
+ * @param {Omit<StakeParams, 'action'> | undefined} params - The staking 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 } = useStakeAllowance(
17
+ * {
18
+ * amount: 1000000000000000000n, // 1 SODA
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 staking');
27
+ * }
28
+ * ```
29
+ */
30
+ export function useStakeAllowance(
31
+ params: Omit<StakeParams, 'action'> | undefined,
32
+ spokeProvider: SpokeProvider | undefined,
33
+ ): UseQueryResult<boolean, Error> {
34
+ const { sodax } = useSodaxContext();
35
+
36
+ return useQuery({
37
+ queryKey: ['soda', 'stakeAllowance', 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: 'stake' },
45
+ spokeProvider,
46
+ });
47
+
48
+ if (!result.ok) {
49
+ throw new Error(`Allowance check failed: ${result.error.code}`);
50
+ }
51
+
52
+ return result.value;
53
+ },
54
+ enabled: !!params && !!spokeProvider,
55
+ refetchInterval: 5000, // Refetch every 5 seconds
56
+ });
57
+ }
@@ -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
+ }