@sodax/dapp-kit 0.0.1-rc.27 → 0.0.1-rc.29

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.
@@ -0,0 +1,71 @@
1
+ import { STELLAR_MAINNET_CHAIN_ID, StellarSpokeProvider, StellarSpokeService } from '@sodax/sdk';
2
+ import type { SpokeChainId, SpokeProvider } from '@sodax/sdk';
3
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
4
+
5
+ /**
6
+ * React hook to check if a Stellar account has a sufficient trustline for a given token and amount.
7
+ *
8
+ * This hook queries the Stellar network (via the provided SpokeProvider) to determine
9
+ * whether the account has established a trustline for the specified token and if the trustline
10
+ * is sufficient for the intended amount. It is useful for gating UI actions that require
11
+ * a trustline to be present and funded.
12
+ *
13
+ * @template T - The type of SpokeProvider, defaults to SpokeProvider.
14
+ * @param {string | undefined} token - The Stellar asset code or token address to check the trustline for.
15
+ * @param {bigint | undefined} amount - The minimum amount required for the trustline.
16
+ * @param {T | undefined} spokeProvider - The provider instance for interacting with the Stellar network.
17
+ * @param {SpokeChainId | undefined} chainId - The chain ID to determine if the check should be performed (only on Stellar mainnet).
18
+ * @returns {UseQueryResult<boolean, Error>} A React Query result object containing:
19
+ * - `data`: `true` if the trustline exists and is sufficient, `false` otherwise.
20
+ * - `error`: Any error encountered during the check.
21
+ * - `isLoading`: Whether the query is in progress.
22
+ * - Other React Query state.
23
+ *
24
+ * @example
25
+ * ```tsx
26
+ * import { useStellarTrustlineCheck } from '@sodax/dapp-kit';
27
+ *
28
+ * const { data: hasTrustline, isLoading, error } = useStellarTrustlineCheck(
29
+ * 'USDC-G...TOKEN',
30
+ * 10000000n,
31
+ * stellarProvider,
32
+ * 'stellar'
33
+ * );
34
+ *
35
+ * if (isLoading) return <span>Checking trustline...</span>;
36
+ * if (error) return <span>Error: {error.message}</span>;
37
+ * if (!hasTrustline) return <span>Trustline not established or insufficient.</span>;
38
+ * return <span>Trustline is sufficient!</span>;
39
+ * ```
40
+ */
41
+
42
+ export function useStellarTrustlineCheck<T extends SpokeProvider = SpokeProvider>(
43
+ token: string | undefined,
44
+ amount: bigint | undefined,
45
+ spokeProvider: T | undefined,
46
+ chainId: SpokeChainId | undefined,
47
+ ): UseQueryResult<boolean, Error> {
48
+ return useQuery({
49
+ queryKey: ['stellar-trustline-check', token],
50
+ queryFn: async () => {
51
+ if (chainId !== STELLAR_MAINNET_CHAIN_ID) {
52
+ return true;
53
+ }
54
+ if (!spokeProvider || !token || !amount || !(spokeProvider instanceof StellarSpokeProvider)) {
55
+ console.error(
56
+ 'Spoke provider, token or amount not found. Details: spokeProvider:',
57
+ spokeProvider,
58
+ 'token:',
59
+ token,
60
+ 'amount:',
61
+ amount,
62
+ );
63
+ return false;
64
+ }
65
+ const response = await StellarSpokeService.hasSufficientTrustline(token, amount, spokeProvider);
66
+
67
+ return response;
68
+ },
69
+ enabled: !!spokeProvider && !!token && !!amount,
70
+ });
71
+ }
@@ -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,8 +42,21 @@ 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;
@@ -9,7 +9,7 @@ import type {
9
9
  SpokeProvider,
10
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
14
  type CreateIntentResult = Result<[SolverExecutionResponse, Intent, IntentDeliveryInfo], IntentError<IntentErrorCode>>;
15
15
 
@@ -40,6 +40,7 @@ export function useSwap(
40
40
  spokeProvider: SpokeProvider | undefined,
41
41
  ): UseMutationResult<CreateIntentResult, Error, CreateIntentParams> {
42
42
  const { sodax } = useSodaxContext();
43
+ const queryClient = useQueryClient();
43
44
 
44
45
  return useMutation<CreateIntentResult, Error, CreateIntentParams>({
45
46
  mutationFn: async (params: CreateIntentParams) => {
@@ -51,5 +52,9 @@ export function useSwap(
51
52
  spokeProvider,
52
53
  });
53
54
  },
55
+ onSuccess: () => {
56
+ // Invalidate balance queries to refresh both source and destination token balances
57
+ queryClient.invalidateQueries({ queryKey: ['xBalances'] });
58
+ },
54
59
  });
55
60
  }
@@ -43,5 +43,6 @@ export function useSwapAllowance(
43
43
  return false;
44
44
  },
45
45
  enabled: !!spokeProvider && !!params,
46
+ refetchInterval: 2000,
46
47
  });
47
48
  }
@@ -55,7 +55,7 @@ export function useSwapApprove(
55
55
  },
56
56
  onSuccess: () => {
57
57
  // Invalidate allowance query to refetch the new allowance
58
- queryClient.invalidateQueries({ queryKey: ['allowance', params?.inputToken] });
58
+ queryClient.invalidateQueries({ queryKey: ['allowance', params] });
59
59
  },
60
60
  });
61
61