@sodax/dapp-kit 0.0.1-rc.1

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 (78) hide show
  1. package/README.md +260 -0
  2. package/dist/contexts/index.d.ts +7 -0
  3. package/dist/contexts/index.d.ts.map +1 -0
  4. package/dist/core/index.d.ts +4 -0
  5. package/dist/core/index.d.ts.map +1 -0
  6. package/dist/hooks/index.d.ts +5 -0
  7. package/dist/hooks/index.d.ts.map +1 -0
  8. package/dist/hooks/mm/index.d.ts +7 -0
  9. package/dist/hooks/mm/index.d.ts.map +1 -0
  10. package/dist/hooks/mm/useBorrow.d.ts +26 -0
  11. package/dist/hooks/mm/useBorrow.d.ts.map +1 -0
  12. package/dist/hooks/mm/useHubWalletAddress.d.ts +24 -0
  13. package/dist/hooks/mm/useHubWalletAddress.d.ts.map +1 -0
  14. package/dist/hooks/mm/useRepay.d.ts +26 -0
  15. package/dist/hooks/mm/useRepay.d.ts.map +1 -0
  16. package/dist/hooks/mm/useSupply.d.ts +32 -0
  17. package/dist/hooks/mm/useSupply.d.ts.map +1 -0
  18. package/dist/hooks/mm/useUserReservesData.d.ts +9 -0
  19. package/dist/hooks/mm/useUserReservesData.d.ts.map +1 -0
  20. package/dist/hooks/mm/useWithdraw.d.ts +26 -0
  21. package/dist/hooks/mm/useWithdraw.d.ts.map +1 -0
  22. package/dist/hooks/provider/index.d.ts +3 -0
  23. package/dist/hooks/provider/index.d.ts.map +1 -0
  24. package/dist/hooks/provider/useHubProvider.d.ts +3 -0
  25. package/dist/hooks/provider/useHubProvider.d.ts.map +1 -0
  26. package/dist/hooks/provider/useSpokeProvider.d.ts +4 -0
  27. package/dist/hooks/provider/useSpokeProvider.d.ts.map +1 -0
  28. package/dist/hooks/shared/index.d.ts +4 -0
  29. package/dist/hooks/shared/index.d.ts.map +1 -0
  30. package/dist/hooks/shared/useAllowance.d.ts +3 -0
  31. package/dist/hooks/shared/useAllowance.d.ts.map +1 -0
  32. package/dist/hooks/shared/useApprove.d.ts +10 -0
  33. package/dist/hooks/shared/useApprove.d.ts.map +1 -0
  34. package/dist/hooks/shared/useSodaxContext.d.ts +8 -0
  35. package/dist/hooks/shared/useSodaxContext.d.ts.map +1 -0
  36. package/dist/hooks/swap/index.d.ts +4 -0
  37. package/dist/hooks/swap/index.d.ts.map +1 -0
  38. package/dist/hooks/swap/useCreateIntentOrder.d.ts +33 -0
  39. package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +1 -0
  40. package/dist/hooks/swap/useQuote.d.ts +39 -0
  41. package/dist/hooks/swap/useQuote.d.ts.map +1 -0
  42. package/dist/hooks/swap/useStatus.d.ts +31 -0
  43. package/dist/hooks/swap/useStatus.d.ts.map +1 -0
  44. package/dist/index.d.ts +4 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +363 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/index.mjs +341 -0
  49. package/dist/index.mjs.map +1 -0
  50. package/dist/providers/SodaxProvider.d.ts +10 -0
  51. package/dist/providers/SodaxProvider.d.ts.map +1 -0
  52. package/dist/providers/index.d.ts +2 -0
  53. package/dist/providers/index.d.ts.map +1 -0
  54. package/package.json +54 -0
  55. package/src/contexts/index.ts +9 -0
  56. package/src/core/index.ts +42 -0
  57. package/src/hooks/index.ts +4 -0
  58. package/src/hooks/mm/index.ts +6 -0
  59. package/src/hooks/mm/useBorrow.ts +55 -0
  60. package/src/hooks/mm/useHubWalletAddress.ts +49 -0
  61. package/src/hooks/mm/useRepay.ts +56 -0
  62. package/src/hooks/mm/useSupply.ts +62 -0
  63. package/src/hooks/mm/useUserReservesData.ts +58 -0
  64. package/src/hooks/mm/useWithdraw.ts +59 -0
  65. package/src/hooks/provider/index.ts +2 -0
  66. package/src/hooks/provider/useHubProvider.ts +27 -0
  67. package/src/hooks/provider/useSpokeProvider.ts +23 -0
  68. package/src/hooks/shared/index.ts +3 -0
  69. package/src/hooks/shared/useAllowance.ts +31 -0
  70. package/src/hooks/shared/useApprove.ts +53 -0
  71. package/src/hooks/shared/useSodaxContext.ts +16 -0
  72. package/src/hooks/swap/index.ts +3 -0
  73. package/src/hooks/swap/useCreateIntentOrder.ts +57 -0
  74. package/src/hooks/swap/useQuote.ts +55 -0
  75. package/src/hooks/swap/useStatus.ts +44 -0
  76. package/src/index.ts +3 -0
  77. package/src/providers/SodaxProvider.tsx +16 -0
  78. package/src/providers/index.ts +1 -0
@@ -0,0 +1,56 @@
1
+ import type { SpokeChainId } from '@sodax/sdk';
2
+ import type { ChainId, XToken } from '@sodax/types';
3
+ import { useMutation, type UseMutationResult } from '@tanstack/react-query';
4
+ import { parseUnits } from 'viem';
5
+ import { useSpokeProvider } from '../provider/useSpokeProvider';
6
+ import { useSodaxContext } from '../shared/useSodaxContext';
7
+
8
+ interface RepayResponse {
9
+ ok: true;
10
+ value: [`0x${string}`, `0x${string}`];
11
+ }
12
+
13
+ /**
14
+ * Hook for repaying borrowed tokens to the Sodax money market.
15
+ *
16
+ * This hook provides functionality to repay borrowed tokens back to the money market protocol,
17
+ * handling the entire repayment process including transaction creation, submission,
18
+ * and cross-chain communication.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * const { mutateAsync: repay, isPending, error } = useRepay(hubToken, spokeChainId);
23
+ * await repay('100');
24
+ * ```
25
+ *
26
+ * @throws {Error} When:
27
+ * - spokeProvider is not available
28
+ * - Transaction execution fails
29
+ */
30
+ export function useRepay(hubToken: XToken, spokeChainId: ChainId): UseMutationResult<RepayResponse, Error, string> {
31
+ const { sodax } = useSodaxContext();
32
+ const spokeProvider = useSpokeProvider(spokeChainId as SpokeChainId);
33
+
34
+ return useMutation<RepayResponse, Error, string>({
35
+ mutationFn: async (amount: string) => {
36
+ if (!spokeProvider) {
37
+ throw new Error('spokeProvider is not found');
38
+ }
39
+
40
+ const response = await sodax.moneyMarket.repayAndSubmit(
41
+ {
42
+ token: hubToken.address,
43
+ amount: parseUnits(amount, hubToken.decimals),
44
+ },
45
+ spokeProvider,
46
+ );
47
+
48
+ if (!response.ok) {
49
+ throw new Error('Failed to repay tokens');
50
+ }
51
+
52
+ console.log('Repay transaction submitted:', response);
53
+ return response;
54
+ },
55
+ });
56
+ }
@@ -0,0 +1,62 @@
1
+ import type { SpokeChainId } from '@sodax/sdk';
2
+ import type { XToken } from '@sodax/types';
3
+ import { useMutation, type UseMutationResult } from '@tanstack/react-query';
4
+ import { parseUnits } from 'viem';
5
+ import { useSpokeProvider } from '../provider/useSpokeProvider';
6
+ import { useSodaxContext } from '../shared/useSodaxContext';
7
+
8
+ interface SupplyResponse {
9
+ ok: true;
10
+ value: [`0x${string}`, `0x${string}`];
11
+ }
12
+
13
+ /**
14
+ * Hook for supplying tokens to the Sodax money market.
15
+ *
16
+ * This hook provides functionality to supply tokens to the money market protocol,
17
+ * handling the entire supply process including transaction creation, submission,
18
+ * and cross-chain communication.
19
+ *
20
+ * @param {XToken} spokeToken - The token to supply on the spoke chain. Must be an XToken with valid address and chain information.
21
+ *
22
+ * @returns {UseMutationResult<SupplyResponse, Error, string>} A mutation result object with the following properties:
23
+ * - mutateAsync: Function to execute the supply transaction
24
+ * - isPending: Boolean indicating if a transaction is in progress
25
+ * - error: Error object if the last transaction failed, null otherwise
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const { mutateAsync: supply, isPending, error } = useSupply(spokeToken);
30
+ * await supply('100');
31
+ * ```
32
+ *
33
+ * @throws {Error} When:
34
+ * - spokeProvider is not available
35
+ */
36
+ export function useSupply(spokeToken: XToken): UseMutationResult<SupplyResponse, Error, string> {
37
+ const { sodax } = useSodaxContext();
38
+ const spokeProvider = useSpokeProvider(spokeToken.xChainId as SpokeChainId);
39
+
40
+ return useMutation<SupplyResponse, Error, string>({
41
+ mutationFn: async (amount: string) => {
42
+ if (!spokeProvider) {
43
+ throw new Error('spokeProvider is not found');
44
+ }
45
+
46
+ const response = await sodax.moneyMarket.supplyAndSubmit(
47
+ {
48
+ token: spokeToken.address,
49
+ amount: parseUnits(amount, spokeToken.decimals),
50
+ },
51
+ spokeProvider,
52
+ );
53
+
54
+ if (!response.ok) {
55
+ throw new Error('Failed to supply tokens');
56
+ }
57
+
58
+ console.log('Supply transaction submitted:', response);
59
+ return response;
60
+ },
61
+ });
62
+ }
@@ -0,0 +1,58 @@
1
+ import { allXTokens } from '@/core';
2
+ import { getMoneyMarketConfig, type EvmHubProvider } from '@sodax/sdk';
3
+ import type { HubChainId, SpokeChainId } from '@sodax/types';
4
+ import { useXAccount, useWalletProvider } from '@sodax/wallet-sdk';
5
+ import type { ChainId } from '@sodax/types';
6
+ import { useQuery } from '@tanstack/react-query';
7
+ import { useHubProvider } from '../provider/useHubProvider';
8
+ import { useHubWalletAddress } from './useHubWalletAddress';
9
+ import { useSodaxContext } from '../shared/useSodaxContext';
10
+
11
+ export function useUserReservesData(spokeChainId: ChainId) {
12
+ const { sodax } = useSodaxContext();
13
+ const hubChainId = (sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? 'sonic') as HubChainId;
14
+ const hubWalletProvider = useWalletProvider(hubChainId);
15
+ const hubProvider = useHubProvider();
16
+ const { address } = useXAccount(spokeChainId);
17
+ const { data: hubWalletAddress } = useHubWalletAddress(
18
+ spokeChainId as SpokeChainId,
19
+ address,
20
+ hubProvider as EvmHubProvider,
21
+ );
22
+
23
+ const { data: userReserves } = useQuery({
24
+ queryKey: ['userReserves', hubWalletAddress],
25
+ queryFn: async () => {
26
+ if (!hubWalletProvider) {
27
+ return;
28
+ }
29
+
30
+ if (!hubWalletAddress) {
31
+ return;
32
+ }
33
+
34
+ const moneyMarketConfig = getMoneyMarketConfig(hubChainId);
35
+ try {
36
+ const [res] = await sodax.moneyMarket.getUserReservesData(
37
+ hubWalletAddress as `0x${string}`,
38
+ moneyMarketConfig.uiPoolDataProvider,
39
+ moneyMarketConfig.poolAddressesProvider,
40
+ );
41
+
42
+ return res?.map(r => {
43
+ return {
44
+ ...r,
45
+ token: allXTokens.find(t => t.address === r.underlyingAsset),
46
+ };
47
+ });
48
+ } catch (error) {
49
+ console.log('error', error);
50
+ return;
51
+ }
52
+ },
53
+ enabled: !!address && !!hubWalletProvider && !!hubWalletAddress,
54
+ refetchInterval: 5000,
55
+ });
56
+
57
+ return userReserves;
58
+ }
@@ -0,0 +1,59 @@
1
+ import type { SpokeChainId } from '@sodax/sdk';
2
+ import type { ChainId, XToken } from '@sodax/types';
3
+ import { useMutation, type UseMutationResult } from '@tanstack/react-query';
4
+ import { parseUnits } from 'viem';
5
+ import { useSpokeProvider } from '../provider/useSpokeProvider';
6
+ import { useSodaxContext } from '../shared/useSodaxContext';
7
+
8
+ interface WithdrawResponse {
9
+ ok: true;
10
+ value: [`0x${string}`, `0x${string}`];
11
+ }
12
+
13
+ /**
14
+ * Hook for withdrawing supplied tokens from the Sodax money market.
15
+ *
16
+ * This hook provides functionality to withdraw previously supplied tokens from the money market protocol,
17
+ * handling the entire withdrawal process including transaction creation, submission,
18
+ * and cross-chain communication.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * const { mutateAsync: withdraw, isPending, error } = useWithdraw(hubToken, spokeChainId);
23
+ * await withdraw('100');
24
+ * ```
25
+ *
26
+ * @throws {Error} When:
27
+ * - spokeProvider is not available
28
+ * - Transaction execution fails
29
+ */
30
+ export function useWithdraw(
31
+ hubToken: XToken,
32
+ spokeChainId: ChainId,
33
+ ): UseMutationResult<WithdrawResponse, Error, string> {
34
+ const { sodax } = useSodaxContext();
35
+ const spokeProvider = useSpokeProvider(spokeChainId as SpokeChainId);
36
+
37
+ return useMutation<WithdrawResponse, Error, string>({
38
+ mutationFn: async (amount: string) => {
39
+ if (!spokeProvider) {
40
+ throw new Error('spokeProvider is not found');
41
+ }
42
+
43
+ const response = await sodax.moneyMarket.withdrawAndSubmit(
44
+ {
45
+ token: hubToken.address,
46
+ amount: parseUnits(amount, hubToken.decimals),
47
+ },
48
+ spokeProvider,
49
+ );
50
+
51
+ if (!response.ok) {
52
+ throw new Error('Failed to withdraw tokens');
53
+ }
54
+
55
+ console.log('Withdraw transaction submitted:', response);
56
+ return response;
57
+ },
58
+ });
59
+ }
@@ -0,0 +1,2 @@
1
+ export * from './useHubProvider';
2
+ export * from './useSpokeProvider';
@@ -0,0 +1,27 @@
1
+ import { EvmHubProvider, getHubChainConfig, type HubChainId } from '@sodax/sdk';
2
+ import { getXChainType } from '@sodax/wallet-sdk';
3
+ import { useMemo } from 'react';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ export function useHubProvider(): EvmHubProvider | undefined {
7
+ const { sodax } = useSodaxContext();
8
+ const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id;
9
+ const hubRpcUrl = sodax.config?.hubProviderConfig?.hubRpcUrl;
10
+ const xChainType = getXChainType(hubChainId);
11
+ const hubProvider = useMemo(() => {
12
+ if (xChainType === 'EVM' && hubChainId && hubRpcUrl) {
13
+ // @ts-ignore
14
+ const hubChainCfg = getHubChainConfig(hubChainId);
15
+
16
+ if (!hubChainCfg) return undefined;
17
+
18
+ return new EvmHubProvider({
19
+ hubRpcUrl: hubRpcUrl,
20
+ chainConfig: hubChainCfg,
21
+ });
22
+ }
23
+ return undefined;
24
+ }, [xChainType, hubChainId, hubRpcUrl]);
25
+
26
+ return hubProvider;
27
+ }
@@ -0,0 +1,23 @@
1
+ import { EvmSpokeProvider, spokeChainConfig, SuiSpokeProvider } from '@sodax/sdk';
2
+ import type { SpokeChainId } from '@sodax/types';
3
+ import { getXChainType, useWalletProvider } from '@sodax/wallet-sdk';
4
+ import { useMemo } from 'react';
5
+
6
+ export function useSpokeProvider(spokeChainId: SpokeChainId) {
7
+ const xChainType = getXChainType(spokeChainId);
8
+ const walletProvider = useWalletProvider(spokeChainId);
9
+ const spokeProvider = useMemo(() => {
10
+ if (!walletProvider) return undefined;
11
+ if (xChainType === 'EVM') {
12
+ // @ts-ignore
13
+ return new EvmSpokeProvider(walletProvider, spokeChainConfig[spokeChainId]);
14
+ }
15
+ if (xChainType === 'SUI') {
16
+ // @ts-ignore
17
+ return new SuiSpokeProvider(spokeChainConfig[spokeChainId], walletProvider);
18
+ }
19
+ return undefined;
20
+ }, [walletProvider, xChainType, spokeChainId]);
21
+
22
+ return spokeProvider;
23
+ }
@@ -0,0 +1,3 @@
1
+ export * from './useSodaxContext';
2
+ export * from './useAllowance';
3
+ export * from './useApprove';
@@ -0,0 +1,31 @@
1
+ import { useQuery } from '@tanstack/react-query';
2
+ import type { SpokeChainId, XToken } from '@sodax/types';
3
+ import { useSodaxContext } from '../shared/useSodaxContext';
4
+ import { useSpokeProvider } from '../provider/useSpokeProvider';
5
+ import { parseUnits } from 'viem';
6
+
7
+ export function useAllowance(token: XToken, amount: string) {
8
+ const { sodax } = useSodaxContext();
9
+ const spokeProvider = useSpokeProvider(token.xChainId as SpokeChainId);
10
+
11
+ return useQuery({
12
+ queryKey: ['allowance', token.address, amount],
13
+ queryFn: async () => {
14
+ if (!spokeProvider) {
15
+ return false;
16
+ }
17
+ const allowance = await sodax.moneyMarket.isAllowanceValid(
18
+ {
19
+ token: token.address,
20
+ amount: parseUnits(amount, token.decimals),
21
+ },
22
+ spokeProvider,
23
+ );
24
+ if (allowance.ok) {
25
+ return allowance.value;
26
+ }
27
+ return false;
28
+ },
29
+ enabled: !!spokeProvider,
30
+ });
31
+ }
@@ -0,0 +1,53 @@
1
+ import { useSodaxContext } from '../shared/useSodaxContext';
2
+ import { useSpokeProvider } from '../provider/useSpokeProvider';
3
+ import type { SpokeChainId, XToken } from '@sodax/types';
4
+ import { parseUnits } from 'viem';
5
+ import { useMutation, useQueryClient } from '@tanstack/react-query';
6
+ import type { Address } from '@sodax/sdk';
7
+
8
+ interface UseApproveReturn {
9
+ approve: (amount: string) => Promise<boolean>;
10
+ isLoading: boolean;
11
+ error: Error | null;
12
+ resetError: () => void;
13
+ }
14
+
15
+ export function useApprove(token: XToken): UseApproveReturn {
16
+ const { sodax } = useSodaxContext();
17
+ const spokeProvider = useSpokeProvider(token.xChainId as SpokeChainId);
18
+ const queryClient = useQueryClient();
19
+
20
+ const {
21
+ mutateAsync: approve,
22
+ isPending,
23
+ error,
24
+ reset: resetError,
25
+ } = useMutation({
26
+ mutationFn: async (amount: string) => {
27
+ if (!spokeProvider) {
28
+ throw new Error('Spoke provider not found');
29
+ }
30
+ const allowance = await sodax.moneyMarket.approve(
31
+ token.address as Address,
32
+ parseUnits(amount, token.decimals),
33
+ spokeProvider.chainConfig.addresses.assetManager as Address,
34
+ spokeProvider,
35
+ );
36
+ if (!allowance.ok) {
37
+ throw new Error('Failed to approve tokens');
38
+ }
39
+ return allowance.ok;
40
+ },
41
+ onSuccess: () => {
42
+ // Invalidate allowance query to refetch the new allowance
43
+ queryClient.invalidateQueries({ queryKey: ['allowance', token.address] });
44
+ },
45
+ });
46
+
47
+ return {
48
+ approve,
49
+ isLoading: isPending,
50
+ error: error as Error | null,
51
+ resetError,
52
+ };
53
+ }
@@ -0,0 +1,16 @@
1
+ import { SodaxContext, type SodaxContextType } from '@/contexts';
2
+ import { useContext } from 'react';
3
+
4
+ /**
5
+ * Hook to access the Sodax context which provides access to the Sodax SDK instance and chain configuration
6
+ * @throws {Error} If used outside of a SodaxProvider
7
+ * @returns {SodaxContextType} The Sodax context containing SDK instance and configuration
8
+ */
9
+
10
+ export const useSodaxContext = (): SodaxContextType => {
11
+ const context = useContext(SodaxContext);
12
+ if (!context) {
13
+ throw new Error('useSodaxContext must be used within a SodaxProvider');
14
+ }
15
+ return context;
16
+ };
@@ -0,0 +1,3 @@
1
+ export * from './useQuote';
2
+ export * from './useCreateIntentOrder';
3
+ export * from './useStatus';
@@ -0,0 +1,57 @@
1
+ import { useSodaxContext } from '../shared/useSodaxContext';
2
+ import type {
3
+ CreateIntentParams,
4
+ SpokeChainId,
5
+ IntentExecutionResponse,
6
+ Result,
7
+ IntentSubmitErrorCode,
8
+ Intent,
9
+ PacketData,
10
+ IntentSubmitError,
11
+ } from '@sodax/sdk';
12
+ import { useSpokeProvider } from '../provider/useSpokeProvider';
13
+ import { useMutation, type UseMutationResult } from '@tanstack/react-query';
14
+
15
+ type CreateIntentResult = Result<
16
+ [IntentExecutionResponse, Intent, PacketData],
17
+ IntentSubmitError<IntentSubmitErrorCode>
18
+ >;
19
+
20
+ /**
21
+ * Hook for creating and submitting an intent order for cross-chain swaps.
22
+ * Uses React Query's useMutation for better state management and caching.
23
+ *
24
+ * @param {SpokeChainId} chainId - The source chain ID where the swap will originate
25
+ * @returns {UseMutationResult} Mutation result object containing mutation function and state
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const { mutateAsync: createIntent, isPending } = useCreateIntentOrder('0xa4b1.arbitrum');
30
+ *
31
+ * const handleSwap = async () => {
32
+ * const result = await createIntent({
33
+ * token_src: '0x...',
34
+ * token_src_blockchain_id: 'arbitrum',
35
+ * token_dst: '0x...',
36
+ * token_dst_blockchain_id: 'polygon',
37
+ * amount: '1000000000000000000',
38
+ * min_output_amount: '900000000000000000'
39
+ * });
40
+ * };
41
+ * ```
42
+ */
43
+ export function useCreateIntentOrder(
44
+ chainId: SpokeChainId,
45
+ ): UseMutationResult<CreateIntentResult, Error, CreateIntentParams> {
46
+ const { sodax } = useSodaxContext();
47
+ const spokeProvider = useSpokeProvider(chainId);
48
+
49
+ return useMutation<CreateIntentResult, Error, CreateIntentParams>({
50
+ mutationFn: async (params: CreateIntentParams) => {
51
+ if (!spokeProvider) {
52
+ throw new Error('Spoke provider not found');
53
+ }
54
+ return sodax.solver.createAndSubmitIntent(params, spokeProvider);
55
+ },
56
+ });
57
+ }
@@ -0,0 +1,55 @@
1
+ import type { IntentErrorResponse, IntentQuoteRequest, IntentQuoteResponse, Result } from '@sodax/sdk';
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
4
+
5
+ /**
6
+ * Hook for fetching a quote for an intent-based swap.
7
+ *
8
+ * This hook provides real-time quote data for an intent-based swap.
9
+ *
10
+ * @param {IntentQuoteRequest | undefined} payload - The intent quote request parameters. If undefined, the query will be disabled.
11
+ *
12
+ * @returns {UseQueryResult<Result<IntentQuoteResponse, IntentErrorResponse> | undefined>} A query result object containing:
13
+ * - data: The quote result from the solver
14
+ * - isLoading: Boolean indicating if the quote is being fetched
15
+ * - error: Error object if the quote request failed
16
+ * - refetch: Function to manually trigger a quote refresh
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const { data: quote, isLoading } = useQuote({
21
+ * token_src: '0x...',
22
+ * token_src_blockchain_id: '1',
23
+ * token_dst: '0x...',
24
+ * token_dst_blockchain_id: '2',
25
+ * amount: '1000000000000000000',
26
+ * quote_type: 'exact_input',
27
+ * });
28
+ *
29
+ * if (isLoading) return <div>Loading quote...</div>;
30
+ * if (quote) {
31
+ * console.log('Quote received:', quote);
32
+ * }
33
+ * ```
34
+ *
35
+ * @remarks
36
+ * - The quote is automatically refreshed every 3 seconds
37
+ * - The query is disabled when payload is undefined
38
+ * - Uses React Query for efficient caching and state management
39
+ */
40
+ export const useQuote = (
41
+ payload: IntentQuoteRequest | undefined,
42
+ ): UseQueryResult<Result<IntentQuoteResponse, IntentErrorResponse> | undefined> => {
43
+ const { sodax } = useSodaxContext();
44
+ return useQuery({
45
+ queryKey: [payload],
46
+ queryFn: async () => {
47
+ if (!payload) {
48
+ return undefined;
49
+ }
50
+ return sodax.solver.getQuote(payload);
51
+ },
52
+ enabled: !!payload,
53
+ refetchInterval: 3000,
54
+ });
55
+ };
@@ -0,0 +1,44 @@
1
+ import type { Hex, IntentErrorResponse, IntentStatusResponse, Result } from '@sodax/sdk';
2
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
+ import { useSodaxContext } from '../shared/useSodaxContext';
4
+
5
+ /**
6
+ * Hook for monitoring the status of an intent-based swap.
7
+ *
8
+ * This hook provides real-time status updates for an intent-based swap transaction.
9
+ *
10
+ * @param {Hex} intent_tx_hash - The transaction hash of the intent order on the hub chain
11
+ *
12
+ * @returns {UseQueryResult<Result<IntentStatusResponse, IntentErrorResponse> | undefined>} A query result object containing:
13
+ * - data: The status result from the solver
14
+ * - isLoading: Boolean indicating if the status is being fetched
15
+ * - error: Error object if the status request failed
16
+ * - refetch: Function to manually trigger a status refresh
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const { data: status, isLoading } = useStatus('0x...');
21
+ *
22
+ * if (isLoading) return <div>Loading status...</div>;
23
+ * if (status?.ok) {
24
+ * console.log('Status:', status.value);
25
+ * }
26
+ * ```
27
+ *
28
+ * @remarks
29
+ * - The status is automatically refreshed every 3 seconds
30
+ * - Uses React Query for efficient caching and state management
31
+ */
32
+
33
+ export const useStatus = (
34
+ intent_tx_hash: Hex,
35
+ ): UseQueryResult<Result<IntentStatusResponse, IntentErrorResponse> | undefined> => {
36
+ const { sodax } = useSodaxContext();
37
+ return useQuery({
38
+ queryKey: [intent_tx_hash],
39
+ queryFn: async () => {
40
+ return sodax.solver.getStatus({ intent_tx_hash });
41
+ },
42
+ refetchInterval: 3000, // 3s
43
+ });
44
+ };
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './hooks';
2
+ export * from './providers';
3
+ export * from './core';
@@ -0,0 +1,16 @@
1
+ import type { ReactNode, ReactElement } from 'react';
2
+ import { Sodax, type SodaxConfig } from '@sodax/sdk';
3
+ import { SodaxContext } from '@/contexts';
4
+ import React from 'react';
5
+
6
+ interface SodaxProviderProps {
7
+ children: ReactNode;
8
+ testnet?: boolean;
9
+ config: SodaxConfig;
10
+ }
11
+
12
+ export const SodaxProvider = ({ children, testnet = false, config }: SodaxProviderProps): ReactElement => {
13
+ const sodax = new Sodax(config);
14
+
15
+ return <SodaxContext.Provider value={{ sodax, testnet }}>{children}</SodaxContext.Provider>;
16
+ };
@@ -0,0 +1 @@
1
+ export { SodaxProvider } from './SodaxProvider';