@sodax/dapp-kit 0.0.1-rc.21 → 0.0.1-rc.23

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 (80) hide show
  1. package/README.md +83 -1
  2. package/dist/contexts/index.d.ts +1 -2
  3. package/dist/contexts/index.d.ts.map +1 -1
  4. package/dist/hooks/backend/index.d.ts +17 -0
  5. package/dist/hooks/backend/index.d.ts.map +1 -0
  6. package/dist/hooks/backend/useBackendAllMoneyMarketAssets.d.ts +37 -0
  7. package/dist/hooks/backend/useBackendAllMoneyMarketAssets.d.ts.map +1 -0
  8. package/dist/hooks/backend/useBackendAllMoneyMarketBorrowers.d.ts +45 -0
  9. package/dist/hooks/backend/useBackendAllMoneyMarketBorrowers.d.ts.map +1 -0
  10. package/dist/hooks/backend/useBackendIntentByHash.d.ts +36 -0
  11. package/dist/hooks/backend/useBackendIntentByHash.d.ts.map +1 -0
  12. package/dist/hooks/backend/useBackendIntentByTxHash.d.ts +35 -0
  13. package/dist/hooks/backend/useBackendIntentByTxHash.d.ts.map +1 -0
  14. package/dist/hooks/backend/useBackendMoneyMarketAsset.d.ts +38 -0
  15. package/dist/hooks/backend/useBackendMoneyMarketAsset.d.ts.map +1 -0
  16. package/dist/hooks/backend/useBackendMoneyMarketAssetBorrowers.d.ts +47 -0
  17. package/dist/hooks/backend/useBackendMoneyMarketAssetBorrowers.d.ts.map +1 -0
  18. package/dist/hooks/backend/useBackendMoneyMarketAssetSuppliers.d.ts +47 -0
  19. package/dist/hooks/backend/useBackendMoneyMarketAssetSuppliers.d.ts.map +1 -0
  20. package/dist/hooks/backend/useBackendMoneyMarketPosition.d.ts +37 -0
  21. package/dist/hooks/backend/useBackendMoneyMarketPosition.d.ts.map +1 -0
  22. package/dist/hooks/backend/useBackendOrderbook.d.ts +46 -0
  23. package/dist/hooks/backend/useBackendOrderbook.d.ts.map +1 -0
  24. package/dist/hooks/bridge/index.d.ts +6 -0
  25. package/dist/hooks/bridge/index.d.ts.map +1 -0
  26. package/dist/hooks/bridge/useBridge.d.ts +33 -0
  27. package/dist/hooks/bridge/useBridge.d.ts.map +1 -0
  28. package/dist/hooks/bridge/useBridgeAllowance.d.ts +23 -0
  29. package/dist/hooks/bridge/useBridgeAllowance.d.ts.map +1 -0
  30. package/dist/hooks/bridge/useBridgeApprove.d.ts +29 -0
  31. package/dist/hooks/bridge/useBridgeApprove.d.ts.map +1 -0
  32. package/dist/hooks/bridge/useGetBridgeableAmount.d.ts +26 -0
  33. package/dist/hooks/bridge/useGetBridgeableAmount.d.ts.map +1 -0
  34. package/dist/hooks/bridge/useGetBridgeableTokens.d.ts +37 -0
  35. package/dist/hooks/bridge/useGetBridgeableTokens.d.ts.map +1 -0
  36. package/dist/hooks/index.d.ts +2 -0
  37. package/dist/hooks/index.d.ts.map +1 -1
  38. package/dist/hooks/mm/useUserFormattedSummary.d.ts +1 -1
  39. package/dist/hooks/mm/useUserFormattedSummary.d.ts.map +1 -1
  40. package/dist/hooks/mm/useUserReservesData.d.ts +1 -1
  41. package/dist/hooks/mm/useUserReservesData.d.ts.map +1 -1
  42. package/dist/hooks/provider/useHubProvider.d.ts +1 -1
  43. package/dist/hooks/provider/useHubProvider.d.ts.map +1 -1
  44. package/dist/hooks/provider/useSpokeProvider.d.ts.map +1 -1
  45. package/dist/hooks/shared/index.d.ts +1 -0
  46. package/dist/hooks/shared/index.d.ts.map +1 -1
  47. package/dist/hooks/shared/useDeriveUserWalletAddress.d.ts +24 -0
  48. package/dist/hooks/shared/useDeriveUserWalletAddress.d.ts.map +1 -0
  49. package/dist/index.js +278 -19
  50. package/dist/index.js.map +1 -1
  51. package/dist/index.mjs +265 -21
  52. package/dist/index.mjs.map +1 -1
  53. package/dist/providers/SodaxProvider.d.ts.map +1 -1
  54. package/package.json +2 -2
  55. package/src/contexts/index.ts +1 -2
  56. package/src/hooks/backend/README.md +135 -0
  57. package/src/hooks/backend/index.ts +23 -0
  58. package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +49 -0
  59. package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +61 -0
  60. package/src/hooks/backend/useBackendIntentByHash.ts +53 -0
  61. package/src/hooks/backend/useBackendIntentByTxHash.ts +52 -0
  62. package/src/hooks/backend/useBackendMoneyMarketAsset.ts +57 -0
  63. package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +67 -0
  64. package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +67 -0
  65. package/src/hooks/backend/useBackendMoneyMarketPosition.ts +56 -0
  66. package/src/hooks/backend/useBackendOrderbook.ts +63 -0
  67. package/src/hooks/bridge/index.ts +5 -0
  68. package/src/hooks/bridge/useBridge.ts +57 -0
  69. package/src/hooks/bridge/useBridgeAllowance.ts +49 -0
  70. package/src/hooks/bridge/useBridgeApprove.ts +68 -0
  71. package/src/hooks/bridge/useGetBridgeableAmount.ts +50 -0
  72. package/src/hooks/bridge/useGetBridgeableTokens.ts +62 -0
  73. package/src/hooks/index.ts +2 -0
  74. package/src/hooks/mm/useUserFormattedSummary.ts +1 -1
  75. package/src/hooks/mm/useUserReservesData.ts +1 -1
  76. package/src/hooks/provider/useHubProvider.ts +3 -3
  77. package/src/hooks/provider/useSpokeProvider.ts +10 -4
  78. package/src/hooks/shared/index.ts +1 -0
  79. package/src/hooks/shared/useDeriveUserWalletAddress.ts +44 -0
  80. package/src/providers/SodaxProvider.tsx +4 -18
@@ -0,0 +1,53 @@
1
+ // packages/dapp-kit/src/hooks/backend/useIntentByHash.ts
2
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
+ import type { IntentResponse } from '@sodax/sdk';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ /**
7
+ * Hook for fetching intent details by intent hash from the backend API.
8
+ *
9
+ * This hook provides access to intent data using the intent hash directly,
10
+ * including intent details, events, and transaction information. The data is automatically
11
+ * fetched and cached using React Query.
12
+ *
13
+ * @param {string | undefined} intentHash - The intent hash to fetch intent for. If undefined, the query will be disabled.
14
+ *
15
+ * @returns {UseQueryResult<IntentResponse | undefined>} A query result object containing:
16
+ * - data: The intent response data when available
17
+ * - isLoading: Boolean indicating if the request is in progress
18
+ * - error: Error object if the request failed
19
+ * - refetch: Function to manually trigger a data refresh
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const { data: intent, isLoading, error } = useIntentByHash('0xabc...');
24
+ *
25
+ * if (isLoading) return <div>Loading intent...</div>;
26
+ * if (error) return <div>Error: {error.message}</div>;
27
+ * if (intent) {
28
+ * console.log('Intent found:', intent.intentHash);
29
+ * console.log('Open status:', intent.open);
30
+ * }
31
+ * ```
32
+ *
33
+ * @remarks
34
+ * - The query is disabled when intentHash is undefined or empty
35
+ * - Uses React Query for efficient caching and state management
36
+ * - Automatically handles error states and loading indicators
37
+ */
38
+ export const useBackendIntentByHash = (intentHash: string | undefined): UseQueryResult<IntentResponse | undefined> => {
39
+ const { sodax } = useSodaxContext();
40
+
41
+ return useQuery({
42
+ queryKey: ['backend', 'intent', 'hash', intentHash],
43
+ queryFn: async (): Promise<IntentResponse | undefined> => {
44
+ if (!intentHash) {
45
+ return undefined;
46
+ }
47
+
48
+ return sodax.backendApiService.getIntentByHash(intentHash);
49
+ },
50
+ enabled: !!intentHash && intentHash.length > 0,
51
+ retry: 3,
52
+ });
53
+ };
@@ -0,0 +1,52 @@
1
+ // packages/dapp-kit/src/hooks/backend/useIntentByTxHash.ts
2
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
+ import type { IntentResponse } from '@sodax/sdk';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ /**
7
+ * Hook for fetching intent details by transaction hash from the backend API.
8
+ *
9
+ * This hook provides access to intent data associated with a specific transaction hash,
10
+ * including intent details, events, and transaction information. The data is automatically
11
+ * fetched and cached using React Query.
12
+ *
13
+ * @param {string | undefined} txHash - The transaction hash to fetch intent for. If undefined, the query will be disabled.
14
+ *
15
+ * @returns {UseQueryResult<IntentResponse | undefined>} A query result object containing:
16
+ * - data: The intent response data when available
17
+ * - isLoading: Boolean indicating if the request is in progress
18
+ * - error: Error object if the request failed
19
+ * - refetch: Function to manually trigger a data refresh
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const { data: intent, isLoading, error } = useIntentByTxHash('0x123...');
24
+ *
25
+ * if (isLoading) return <div>Loading intent...</div>;
26
+ * if (error) return <div>Error: {error.message}</div>;
27
+ * if (intent) {
28
+ * console.log('Intent found:', intent.intentHash);
29
+ * }
30
+ * ```
31
+ *
32
+ * @remarks
33
+ * - The query is disabled when txHash is undefined or empty
34
+ * - Uses React Query for efficient caching and state management
35
+ * - Automatically handles error states and loading indicators
36
+ */
37
+ export const useBackendIntentByTxHash = (txHash: string | undefined): UseQueryResult<IntentResponse | undefined> => {
38
+ const { sodax } = useSodaxContext();
39
+
40
+ return useQuery({
41
+ queryKey: ['backend', 'intent', 'txHash', txHash],
42
+ queryFn: async (): Promise<IntentResponse | undefined> => {
43
+ if (!txHash) {
44
+ return undefined;
45
+ }
46
+
47
+ return sodax.backendApiService.getIntentByTxHash(txHash);
48
+ },
49
+ enabled: !!txHash && txHash.length > 0,
50
+ retry: 3,
51
+ });
52
+ };
@@ -0,0 +1,57 @@
1
+ // packages/dapp-kit/src/hooks/backend/useMoneyMarketAsset.ts
2
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
+ import type { MoneyMarketAsset } from '@sodax/sdk';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ /**
7
+ * Hook for fetching specific money market asset details from the backend API.
8
+ *
9
+ * This hook provides access to detailed information for a specific money market asset,
10
+ * including reserve information, liquidity rates, borrow rates, and market statistics.
11
+ * The data is automatically fetched and cached using React Query.
12
+ *
13
+ * @param {string | undefined} reserveAddress - The reserve contract address. If undefined, the query will be disabled.
14
+ *
15
+ * @returns {UseQueryResult<MoneyMarketAsset | undefined>} A query result object containing:
16
+ * - data: The money market asset data when available
17
+ * - isLoading: Boolean indicating if the request is in progress
18
+ * - error: Error object if the request failed
19
+ * - refetch: Function to manually trigger a data refresh
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const { data: asset, isLoading, error } = useMoneyMarketAsset('0xabc...');
24
+ *
25
+ * if (isLoading) return <div>Loading asset...</div>;
26
+ * if (error) return <div>Error: {error.message}</div>;
27
+ * if (asset) {
28
+ * console.log('Asset symbol:', asset.symbol);
29
+ * console.log('Liquidity rate:', asset.liquidityRate);
30
+ * console.log('Variable borrow rate:', asset.variableBorrowRate);
31
+ * }
32
+ * ```
33
+ *
34
+ * @remarks
35
+ * - The query is disabled when reserveAddress is undefined or empty
36
+ * - Uses React Query for efficient caching and state management
37
+ * - Automatically handles error states and loading indicators
38
+ * - Returns comprehensive asset information for the specified reserve
39
+ */
40
+ export const useBackendMoneyMarketAsset = (
41
+ reserveAddress: string | undefined,
42
+ ): UseQueryResult<MoneyMarketAsset | undefined> => {
43
+ const { sodax } = useSodaxContext();
44
+
45
+ return useQuery({
46
+ queryKey: ['backend', 'moneymarket', 'asset', reserveAddress],
47
+ queryFn: async (): Promise<MoneyMarketAsset | undefined> => {
48
+ if (!reserveAddress) {
49
+ return undefined;
50
+ }
51
+
52
+ return sodax.backendApiService.getMoneyMarketAsset(reserveAddress);
53
+ },
54
+ enabled: !!reserveAddress && reserveAddress.length > 0,
55
+ retry: 3,
56
+ });
57
+ };
@@ -0,0 +1,67 @@
1
+ // packages/dapp-kit/src/hooks/backend/useMoneyMarketAssetBorrowers.ts
2
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
+ import type { MoneyMarketAssetBorrowers } from '@sodax/sdk';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ /**
7
+ * Hook for fetching borrowers for a specific money market asset from the backend API.
8
+ *
9
+ * This hook provides access to the list of borrowers for a specific money market asset,
10
+ * with pagination support. The data is automatically fetched and cached using React Query.
11
+ *
12
+ * @param {Object} params - Parameters for fetching asset borrowers
13
+ * @param {string | undefined} params.reserveAddress - The reserve contract address. If undefined, the query will be disabled.
14
+ * @param {string} params.offset - The offset for pagination (number as string)
15
+ * @param {string} params.limit - The limit for pagination (number as string)
16
+ *
17
+ * @returns {UseQueryResult<MoneyMarketAssetBorrowers | undefined>} A query result object containing:
18
+ * - data: The asset borrowers data when available
19
+ * - isLoading: Boolean indicating if the request is in progress
20
+ * - error: Error object if the request failed
21
+ * - refetch: Function to manually trigger a data refresh
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const { data: borrowers, isLoading, error } = useMoneyMarketAssetBorrowers({
26
+ * reserveAddress: '0xabc...',
27
+ * offset: '0',
28
+ * limit: '20'
29
+ * });
30
+ *
31
+ * if (isLoading) return <div>Loading borrowers...</div>;
32
+ * if (error) return <div>Error: {error.message}</div>;
33
+ * if (borrowers) {
34
+ * console.log('Total borrowers:', borrowers.total);
35
+ * console.log('Borrowers:', borrowers.borrowers);
36
+ * }
37
+ * ```
38
+ *
39
+ * @remarks
40
+ * - The query is disabled when reserveAddress is undefined or empty
41
+ * - Uses React Query for efficient caching and state management
42
+ * - Automatically handles error states and loading indicators
43
+ * - Supports pagination through offset and limit parameters
44
+ */
45
+ export const useBackendMoneyMarketAssetBorrowers = (params: {
46
+ reserveAddress: string | undefined;
47
+ offset: string;
48
+ limit: string;
49
+ }): UseQueryResult<MoneyMarketAssetBorrowers | undefined> => {
50
+ const { sodax } = useSodaxContext();
51
+
52
+ return useQuery({
53
+ queryKey: ['backend', 'moneymarket', 'asset', 'borrowers', params],
54
+ queryFn: async (): Promise<MoneyMarketAssetBorrowers | undefined> => {
55
+ if (!params.reserveAddress || !params.offset || !params.limit) {
56
+ return undefined;
57
+ }
58
+
59
+ return sodax.backendApiService.getMoneyMarketAssetBorrowers(params.reserveAddress, {
60
+ offset: params.offset,
61
+ limit: params.limit,
62
+ });
63
+ },
64
+ enabled: !!params.reserveAddress && !!params.offset && !!params.limit,
65
+ retry: 3,
66
+ });
67
+ };
@@ -0,0 +1,67 @@
1
+ // packages/dapp-kit/src/hooks/backend/useMoneyMarketAssetSuppliers.ts
2
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
+ import type { MoneyMarketAssetSuppliers } from '@sodax/sdk';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ /**
7
+ * Hook for fetching suppliers for a specific money market asset from the backend API.
8
+ *
9
+ * This hook provides access to the list of suppliers for a specific money market asset,
10
+ * with pagination support. The data is automatically fetched and cached using React Query.
11
+ *
12
+ * @param {Object} params - Parameters for fetching asset suppliers
13
+ * @param {string | undefined} params.reserveAddress - The reserve contract address. If undefined, the query will be disabled.
14
+ * @param {string} params.offset - The offset for pagination (number as string)
15
+ * @param {string} params.limit - The limit for pagination (number as string)
16
+ *
17
+ * @returns {UseQueryResult<MoneyMarketAssetSuppliers | undefined>} A query result object containing:
18
+ * - data: The asset suppliers data when available
19
+ * - isLoading: Boolean indicating if the request is in progress
20
+ * - error: Error object if the request failed
21
+ * - refetch: Function to manually trigger a data refresh
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const { data: suppliers, isLoading, error } = useMoneyMarketAssetSuppliers({
26
+ * reserveAddress: '0xabc...',
27
+ * offset: '0',
28
+ * limit: '20'
29
+ * });
30
+ *
31
+ * if (isLoading) return <div>Loading suppliers...</div>;
32
+ * if (error) return <div>Error: {error.message}</div>;
33
+ * if (suppliers) {
34
+ * console.log('Total suppliers:', suppliers.total);
35
+ * console.log('Suppliers:', suppliers.suppliers);
36
+ * }
37
+ * ```
38
+ *
39
+ * @remarks
40
+ * - The query is disabled when reserveAddress is undefined or empty
41
+ * - Uses React Query for efficient caching and state management
42
+ * - Automatically handles error states and loading indicators
43
+ * - Supports pagination through offset and limit parameters
44
+ */
45
+ export const useBackendMoneyMarketAssetSuppliers = (params: {
46
+ reserveAddress: string | undefined;
47
+ offset: string;
48
+ limit: string;
49
+ }): UseQueryResult<MoneyMarketAssetSuppliers | undefined> => {
50
+ const { sodax } = useSodaxContext();
51
+
52
+ return useQuery({
53
+ queryKey: ['backend', 'moneymarket', 'asset', 'suppliers', params],
54
+ queryFn: async (): Promise<MoneyMarketAssetSuppliers | undefined> => {
55
+ if (!params.reserveAddress || !params.offset || !params.limit) {
56
+ return undefined;
57
+ }
58
+
59
+ return sodax.backendApiService.getMoneyMarketAssetSuppliers(params.reserveAddress, {
60
+ offset: params.offset,
61
+ limit: params.limit,
62
+ });
63
+ },
64
+ enabled: !!params.reserveAddress && !!params.offset && !!params.limit,
65
+ retry: 3,
66
+ });
67
+ };
@@ -0,0 +1,56 @@
1
+ // packages/dapp-kit/src/hooks/backend/useMoneyMarketPosition.ts
2
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
+ import type { MoneyMarketPosition } from '@sodax/sdk';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ /**
7
+ * Hook for fetching money market position for a specific user from the backend API.
8
+ *
9
+ * This hook provides access to a user's money market positions, including their
10
+ * aToken balances, variable debt token balances, and associated reserve information.
11
+ * The data is automatically fetched and cached using React Query.
12
+ *
13
+ * @param {string | undefined} userAddress - The user's wallet address. If undefined, the query will be disabled.
14
+ *
15
+ * @returns {UseQueryResult<MoneyMarketPosition | undefined>} A query result object containing:
16
+ * - data: The money market position data when available
17
+ * - isLoading: Boolean indicating if the request is in progress
18
+ * - error: Error object if the request failed
19
+ * - refetch: Function to manually trigger a data refresh
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const { data: position, isLoading, error } = useMoneyMarketPosition('0x123...');
24
+ *
25
+ * if (isLoading) return <div>Loading position...</div>;
26
+ * if (error) return <div>Error: {error.message}</div>;
27
+ * if (position) {
28
+ * console.log('User address:', position.userAddress);
29
+ * console.log('Positions:', position.positions);
30
+ * }
31
+ * ```
32
+ *
33
+ * @remarks
34
+ * - The query is disabled when userAddress is undefined or empty
35
+ * - Uses React Query for efficient caching and state management
36
+ * - Automatically handles error states and loading indicators
37
+ * - Includes user's aToken and debt token balances across all reserves
38
+ */
39
+ export const useBackendMoneyMarketPosition = (
40
+ userAddress: string | undefined,
41
+ ): UseQueryResult<MoneyMarketPosition | undefined> => {
42
+ const { sodax } = useSodaxContext();
43
+
44
+ return useQuery({
45
+ queryKey: ['backend', 'moneymarket', 'position', userAddress],
46
+ queryFn: async (): Promise<MoneyMarketPosition | undefined> => {
47
+ if (!userAddress) {
48
+ return undefined;
49
+ }
50
+
51
+ return sodax.backendApiService.getMoneyMarketPosition(userAddress);
52
+ },
53
+ enabled: !!userAddress && userAddress.length > 0,
54
+ retry: 3,
55
+ });
56
+ };
@@ -0,0 +1,63 @@
1
+ // packages/dapp-kit/src/hooks/backend/useOrderbook.ts
2
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
3
+ import type { OrderbookResponse } from '@sodax/sdk';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ /**
7
+ * Hook for fetching the solver orderbook from the backend API.
8
+ *
9
+ * This hook provides access to the solver orderbook data, including intent states
10
+ * and intent data for all available intents. The data is automatically fetched
11
+ * and cached using React Query with pagination support.
12
+ *
13
+ * @param {Object} params - Pagination parameters for the orderbook
14
+ * @param {string} params.offset - The offset for pagination (number as string)
15
+ * @param {string} params.limit - The limit for pagination (number as string)
16
+ *
17
+ * @returns {UseQueryResult<OrderbookResponse | undefined>} A query result object containing:
18
+ * - data: The orderbook response data when available
19
+ * - isLoading: Boolean indicating if the request is in progress
20
+ * - error: Error object if the request failed
21
+ * - refetch: Function to manually trigger a data refresh
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const { data: orderbook, isLoading, error } = useOrderbook({
26
+ * offset: '0',
27
+ * limit: '10'
28
+ * });
29
+ *
30
+ * if (isLoading) return <div>Loading orderbook...</div>;
31
+ * if (error) return <div>Error: {error.message}</div>;
32
+ * if (orderbook) {
33
+ * console.log('Total intents:', orderbook.total);
34
+ * console.log('Intents:', orderbook.data);
35
+ * }
36
+ * ```
37
+ *
38
+ * @remarks
39
+ * - The query is disabled when params are undefined or invalid
40
+ * - Uses React Query for efficient caching and state management
41
+ * - Automatically handles error states and loading indicators
42
+ * - Stale time of 30 seconds for real-time orderbook data
43
+ * - Supports pagination through offset and limit parameters
44
+ */
45
+ export const useBackendOrderbook = (
46
+ params: { offset: string; limit: string } | undefined,
47
+ ): UseQueryResult<OrderbookResponse | undefined> => {
48
+ const { sodax } = useSodaxContext();
49
+
50
+ return useQuery({
51
+ queryKey: ['backend', 'solver', 'orderbook', params],
52
+ queryFn: async (): Promise<OrderbookResponse | undefined> => {
53
+ if (!params || !params.offset || !params.limit) {
54
+ return undefined;
55
+ }
56
+
57
+ return sodax.backendApiService.getOrderbook(params);
58
+ },
59
+ enabled: !!params && !!params.offset && !!params.limit,
60
+ staleTime: 30 * 1000, // 30 seconds for real-time data
61
+ retry: 3,
62
+ });
63
+ };
@@ -0,0 +1,5 @@
1
+ export * from './useBridgeAllowance';
2
+ export * from './useBridgeApprove';
3
+ export * from './useBridge';
4
+ export * from './useGetBridgeableAmount';
5
+ export * from './useGetBridgeableTokens';
@@ -0,0 +1,57 @@
1
+ import { useSodaxContext } from '../shared/useSodaxContext';
2
+ import type { BridgeError, BridgeErrorCode, SpokeTxHash, HubTxHash, Result, CreateBridgeIntentParams } from '@sodax/sdk';
3
+ import { useMutation, type UseMutationResult } from '@tanstack/react-query';
4
+ import type { SpokeProvider } from '@sodax/sdk';
5
+
6
+ /**
7
+ * Hook for executing bridge transactions to transfer tokens between chains.
8
+ * Uses React Query's useMutation for better state management and caching.
9
+ *
10
+ * @param {SpokeProvider} spokeProvider - The spoke provider to use for the bridge
11
+ * @returns {UseMutationResult} Mutation result object containing mutation function and state
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const { mutateAsync: bridge, isPending } = useBridge(spokeProvider);
16
+ *
17
+ * const handleBridge = async () => {
18
+ * const result = await bridge({
19
+ * srcChainId: '0x2105.base',
20
+ * srcAsset: '0x...',
21
+ * amount: 1000n,
22
+ * dstChainId: '0x89.polygon',
23
+ * dstAsset: '0x...',
24
+ * recipient: '0x...'
25
+ * });
26
+ *
27
+ * console.log('Bridge transaction hashes:', {
28
+ * spokeTxHash: result.spokeTxHash,
29
+ * hubTxHash: result.hubTxHash
30
+ * });
31
+ * };
32
+ * ```
33
+ */
34
+ export function useBridge(
35
+ spokeProvider: SpokeProvider | undefined,
36
+ ): UseMutationResult<Result<[SpokeTxHash, HubTxHash], BridgeError<BridgeErrorCode>>, Error, CreateBridgeIntentParams> {
37
+ const { sodax } = useSodaxContext();
38
+
39
+ return useMutation<Result<[SpokeTxHash, HubTxHash], BridgeError<BridgeErrorCode>>, Error, CreateBridgeIntentParams>({
40
+ mutationFn: async (params: CreateBridgeIntentParams) => {
41
+ if (!spokeProvider) {
42
+ throw new Error('Spoke provider not found');
43
+ }
44
+
45
+ const result = await sodax.bridge.bridge({
46
+ params,
47
+ spokeProvider,
48
+ });
49
+
50
+ if (!result.ok) {
51
+ throw new Error(`Bridge failed: ${result.error.code}`);
52
+ }
53
+
54
+ return result;
55
+ },
56
+ });
57
+ }
@@ -0,0 +1,49 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import type { SpokeProvider, CreateBridgeIntentParams } from '@sodax/sdk';
4
+
5
+ /**
6
+ * Hook for checking token allowance for bridge operations.
7
+ *
8
+ * This hook verifies if the user has approved enough tokens for a specific bridge action.
9
+ * It automatically queries and tracks the allowance status.
10
+ *
11
+ * @param {BridgeParams} params - The parameters for the bridge to check allowance for.
12
+ * @param {SpokeProvider} spokeProvider - The spoke provider to use for allowance checks
13
+ *
14
+ * @returns {UseQueryResult<boolean, Error>} A React Query result containing:
15
+ * - data: Boolean indicating if allowance is sufficient
16
+ * - isLoading: Loading state indicator
17
+ * - error: Any error that occurred during the check
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const { data: hasAllowed, isLoading } = useBridgeAllowance(params, spokeProvider);
22
+ * ```
23
+ */
24
+ export function useBridgeAllowance(
25
+ params: CreateBridgeIntentParams | undefined,
26
+ spokeProvider: SpokeProvider | undefined,
27
+ ): UseQueryResult<boolean, Error> {
28
+ const { sodax } = useSodaxContext();
29
+
30
+ return useQuery({
31
+ queryKey: ['bridge-allowance', params],
32
+ queryFn: async () => {
33
+ if (!spokeProvider || !params) {
34
+ return false;
35
+ }
36
+
37
+ const allowance = await sodax.bridge.isAllowanceValid({
38
+ params,
39
+ spokeProvider,
40
+ });
41
+
42
+ if (allowance.ok) {
43
+ return allowance.value;
44
+ }
45
+ return false;
46
+ },
47
+ enabled: !!spokeProvider && !!params,
48
+ });
49
+ }
@@ -0,0 +1,68 @@
1
+ import { useSodaxContext } from '../shared/useSodaxContext';
2
+ import type { CreateBridgeIntentParams, SpokeProvider } from '@sodax/sdk';
3
+ import { useMutation, useQueryClient } from '@tanstack/react-query';
4
+
5
+ interface UseBridgeApproveReturn {
6
+ approve: (params: CreateBridgeIntentParams) => Promise<boolean>;
7
+ isLoading: boolean;
8
+ error: Error | null;
9
+ resetError: () => void;
10
+ }
11
+
12
+ /**
13
+ * Hook for approving token spending for bridge actions
14
+ * @param spokeProvider The spoke provider instance for the chain
15
+ * @returns Object containing approve function, loading state, error state and reset function
16
+ * @example
17
+ * ```tsx
18
+ * const { approve, isLoading, error } = useBridgeApprove(spokeProvider);
19
+ *
20
+ * // Approve tokens for bridge action
21
+ * await approve({
22
+ * srcChainId: '0x2105.base',
23
+ * srcAsset: '0x...',
24
+ * amount: 1000n,
25
+ * dstChainId: '0x89.polygon',
26
+ * dstAsset: '0x...',
27
+ * recipient: '0x...'
28
+ * });
29
+ * ```
30
+ */
31
+ export function useBridgeApprove(spokeProvider: SpokeProvider | undefined): UseBridgeApproveReturn {
32
+ const { sodax } = useSodaxContext();
33
+ const queryClient = useQueryClient();
34
+
35
+ const {
36
+ mutateAsync: approve,
37
+ isPending,
38
+ error,
39
+ reset: resetError,
40
+ } = useMutation({
41
+ mutationFn: async (params: CreateBridgeIntentParams) => {
42
+ if (!spokeProvider) {
43
+ throw new Error('Spoke provider not found');
44
+ }
45
+
46
+ const allowance = await sodax.bridge.approve({
47
+ params,
48
+ spokeProvider,
49
+ });
50
+
51
+ if (!allowance.ok) {
52
+ throw new Error('Failed to approve tokens for bridge');
53
+ }
54
+ return true;
55
+ },
56
+ onSuccess: (_, params) => {
57
+ // Invalidate allowance query to refetch the new allowance
58
+ queryClient.invalidateQueries({ queryKey: ['bridge-allowance', params] });
59
+ },
60
+ });
61
+
62
+ return {
63
+ approve,
64
+ isLoading: isPending,
65
+ error: error,
66
+ resetError,
67
+ };
68
+ }
@@ -0,0 +1,50 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import { useSodaxContext } from '../shared/useSodaxContext';
3
+ import type { Result, SpokeChainId, XToken } from '@sodax/sdk';
4
+
5
+ /**
6
+ * Hook for getting the amount available to be bridged.
7
+ *
8
+ * This hook is used to check if a target chain has enough balance to bridge when bridging.
9
+ * It automatically queries and tracks the available amount to be bridged.
10
+ *
11
+ * @param {SpokeChainId | undefined} chainId - The chain ID to get the balance for
12
+ * @param {string | undefined} token - The token address to get the balance for
13
+ *
14
+ * @returns {UseQueryResult<bigint, Error>} A React Query result containing:
15
+ * - data: The available amount to be bridged (bigint)
16
+ * - error: Any error that occurred during the check
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const { data: balance, isLoading } = useSpokeAssetManagerTokenBalance(chainId, tokenAddress);
21
+ *
22
+ * if (balance) {
23
+ * console.log('Asset manager token balance:', balance.toString());
24
+ * }
25
+ * ```
26
+ */
27
+ export function useGetBridgeableAmount(
28
+ from: XToken | undefined,
29
+ to: XToken | undefined,
30
+ ): UseQueryResult<bigint, Error> {
31
+ const { sodax } = useSodaxContext();
32
+
33
+ return useQuery({
34
+ queryKey: ['spoke-asset-manager-token-balance', from, to],
35
+ queryFn: async () => {
36
+ if (!from || !to) {
37
+ return 0n;
38
+ }
39
+
40
+ const result = await sodax.bridge.getBridgeableAmount(from, to);
41
+ if (result.ok) {
42
+ return result.value;
43
+ }
44
+
45
+ console.error('Error getting bridgeable amount:', result.error);
46
+ return 0n;
47
+ },
48
+ enabled: !!from && !!to,
49
+ });
50
+ }