@sodax/dapp-kit 1.3.1-beta-rc2 → 1.3.1-beta-rc3

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sodax/dapp-kit",
3
3
  "license": "MIT",
4
- "version": "1.3.1-beta-rc2",
4
+ "version": "1.3.1-beta-rc3",
5
5
  "description": "dapp-kit of New World",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -16,8 +16,8 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "viem": "2.29.2",
19
- "@sodax/sdk": "1.3.1-beta-rc2",
20
- "@sodax/types": "1.3.1-beta-rc2"
19
+ "@sodax/sdk": "1.3.1-beta-rc3",
20
+ "@sodax/types": "1.3.1-beta-rc3"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/react": "19.0.8",
@@ -12,6 +12,10 @@ export { useBackendIntentByTxHash } from './useBackendIntentByTxHash';
12
12
  export { useBackendIntentByHash } from './useBackendIntentByHash';
13
13
  export { useBackendUserIntents } from './useBackendUserIntents';
14
14
 
15
+ // Swap submit-tx hooks
16
+ export { useBackendSubmitSwapTx } from './useBackendSubmitSwapTx';
17
+ export { useBackendSubmitSwapTxStatus } from './useBackendSubmitSwapTxStatus';
18
+
15
19
  // Solver hooks
16
20
  export { useBackendOrderbook } from './useBackendOrderbook';
17
21
 
@@ -0,0 +1,55 @@
1
+ import { useMutation, type UseMutationOptions, type UseMutationResult } from '@tanstack/react-query';
2
+ import type { SubmitSwapTxRequest, SubmitSwapTxResponse } from '@sodax/types';
3
+ import type { RequestOverrideConfig } from '@sodax/sdk';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ export type UseBackendSubmitSwapTxParams = {
7
+ apiConfig?: RequestOverrideConfig;
8
+ mutationOptions?: UseMutationOptions<SubmitSwapTxResponse, Error, SubmitSwapTxRequest>;
9
+ };
10
+
11
+ /**
12
+ * React hook for submitting a swap transaction to be processed by the backend
13
+ * (relay, post execution to solver, etc.).
14
+ *
15
+ * @param {UseBackendSubmitSwapTxParams | undefined} params - Optional parameters:
16
+ * - `mutationOptions`: React Query mutation options to customize behavior (e.g., onSuccess, onError, retry).
17
+ *
18
+ * @returns {UseMutationResult<SubmitSwapTxResponse, Error, SubmitSwapTxRequest>} React Query mutation result:
19
+ * - `mutate` / `mutateAsync`: Functions to trigger the submission.
20
+ * - `data`: The submit response on success.
21
+ * - `isPending`: Loading state.
22
+ * - `error`: Error instance if the mutation failed.
23
+ *
24
+ * @example
25
+ * const { mutateAsync: submitSwapTx, isPending, error } = useBackendSubmitSwapTx();
26
+ *
27
+ * const result = await submitSwapTx({
28
+ * txHash: '0x123...',
29
+ * srcChainId: '1',
30
+ * walletAddress: '0xabc...',
31
+ * intent: { ... },
32
+ * relayData: '0x...',
33
+ * });
34
+ */
35
+ export const useBackendSubmitSwapTx = (
36
+ params?: UseBackendSubmitSwapTxParams,
37
+ ): UseMutationResult<SubmitSwapTxResponse, Error, SubmitSwapTxRequest> => {
38
+ const { sodax } = useSodaxContext();
39
+
40
+ const defaultMutationOptions = {
41
+ retry: 3,
42
+ };
43
+
44
+ const mutationOptions = {
45
+ ...defaultMutationOptions,
46
+ ...params?.mutationOptions,
47
+ };
48
+
49
+ return useMutation({
50
+ ...mutationOptions,
51
+ mutationFn: async (request: SubmitSwapTxRequest): Promise<SubmitSwapTxResponse> => {
52
+ return sodax.backendApi.submitSwapTx(request, params?.apiConfig);
53
+ },
54
+ });
55
+ };
@@ -0,0 +1,79 @@
1
+ import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { SubmitSwapTxStatusResponse } from '@sodax/types';
3
+ import type { RequestOverrideConfig } from '@sodax/sdk';
4
+ import { useSodaxContext } from '../shared/useSodaxContext';
5
+
6
+ export type UseBackendSubmitSwapTxStatusParams = {
7
+ params: {
8
+ txHash: string | undefined;
9
+ srcChainId?: string;
10
+ };
11
+ apiConfig?: RequestOverrideConfig;
12
+ queryOptions?: UseQueryOptions<SubmitSwapTxStatusResponse | undefined, Error>;
13
+ };
14
+
15
+ /**
16
+ * React hook for polling the processing status of a submitted swap transaction.
17
+ *
18
+ * @param {UseBackendSubmitSwapTxStatusParams | undefined} params - Parameters for the query:
19
+ * - `params.txHash`: The transaction hash of the submitted swap; query is disabled if undefined or empty.
20
+ * - `params.srcChainId`: Optional source chain ID to narrow the status lookup.
21
+ * - `queryOptions`: Optional React Query options to override default behavior (e.g., refetchInterval, retry).
22
+ *
23
+ * @returns {UseQueryResult<SubmitSwapTxStatusResponse | undefined, Error>} React Query result object:
24
+ * - `data`: The status response or undefined if unavailable.
25
+ * - `isLoading`: Loading state.
26
+ * - `error`: Error instance if the query failed.
27
+ * - `refetch`: Function to re-trigger the query.
28
+ *
29
+ * @example
30
+ * const { data: status, isLoading, error } = useBackendSubmitSwapTxStatus({
31
+ * params: { txHash: '0x123...', srcChainId: '1' },
32
+ * });
33
+ *
34
+ * if (status?.data.status === 'executed') {
35
+ * console.log('Swap completed!', status.data.result);
36
+ * }
37
+ *
38
+ * @remarks
39
+ * - Query is disabled if `params` is undefined or `txHash` is undefined/empty.
40
+ * - Default refetch interval is 1 second for real-time status polling.
41
+ * - Uses React Query for state management, caching, and retries.
42
+ */
43
+ export const useBackendSubmitSwapTxStatus = (
44
+ params: UseBackendSubmitSwapTxStatusParams | undefined,
45
+ ): UseQueryResult<SubmitSwapTxStatusResponse | undefined, Error> => {
46
+ const { sodax } = useSodaxContext();
47
+
48
+ const defaultQueryOptions = {
49
+ queryKey: ['api', 'swaps', 'submit-tx', 'status', params?.params?.txHash, params?.params?.srcChainId],
50
+ enabled: !!params?.params?.txHash && params.params.txHash.length > 0,
51
+ retry: 3,
52
+ refetchInterval: (query: { state: { data: SubmitSwapTxStatusResponse | undefined } }) => {
53
+ const status = query.state.data?.data?.status;
54
+ if (status === 'executed' || status === 'failed') return false;
55
+ return 1000;
56
+ },
57
+ };
58
+
59
+ const queryOptions = {
60
+ ...defaultQueryOptions,
61
+ ...params?.queryOptions,
62
+ };
63
+
64
+ return useQuery({
65
+ ...queryOptions,
66
+ queryFn: async (): Promise<SubmitSwapTxStatusResponse | undefined> => {
67
+ if (!params?.params?.txHash) {
68
+ return undefined;
69
+ }
70
+ return sodax.backendApi.getSubmitSwapTxStatus(
71
+ {
72
+ txHash: params.params.txHash,
73
+ srcChainId: params.params.srcChainId,
74
+ },
75
+ params.apiConfig,
76
+ );
77
+ },
78
+ });
79
+ };