@sodax/dapp-kit 2.0.0-rc.18 → 2.0.0-rc.19

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 (34) hide show
  1. package/README.md +12 -3
  2. package/dist/index.d.ts +371 -46
  3. package/dist/index.mjs +370 -46
  4. package/package.json +2 -2
  5. package/src/hooks/_mutationContract.test.ts +6 -1
  6. package/src/hooks/backend/index.ts +1 -5
  7. package/src/hooks/bitcoin/index.ts +1 -0
  8. package/src/hooks/bitcoin/useBitcoinTradingSetup.ts +45 -0
  9. package/src/hooks/index.ts +1 -0
  10. package/src/hooks/swapsApi/index.ts +41 -0
  11. package/src/hooks/swapsApi/isTerminalSwapIntentStatus.test.ts +24 -0
  12. package/src/hooks/swapsApi/isTerminalSwapIntentStatus.ts +11 -0
  13. package/src/hooks/swapsApi/useSwapsApiAllowance.ts +40 -0
  14. package/src/hooks/swapsApi/useSwapsApiApprove.ts +42 -0
  15. package/src/hooks/swapsApi/useSwapsApiCancelIntent.ts +41 -0
  16. package/src/hooks/swapsApi/useSwapsApiCreateIntent.ts +41 -0
  17. package/src/hooks/swapsApi/useSwapsApiCreateLimitOrder.ts +41 -0
  18. package/src/hooks/swapsApi/useSwapsApiDeadline.ts +36 -0
  19. package/src/hooks/swapsApi/useSwapsApiEstimateGas.ts +40 -0
  20. package/src/hooks/swapsApi/useSwapsApiFilledIntent.ts +41 -0
  21. package/src/hooks/swapsApi/useSwapsApiIntent.ts +40 -0
  22. package/src/hooks/swapsApi/useSwapsApiIntentExtraData.ts +41 -0
  23. package/src/hooks/swapsApi/useSwapsApiIntentHash.ts +40 -0
  24. package/src/hooks/swapsApi/useSwapsApiIntentPacket.ts +43 -0
  25. package/src/hooks/swapsApi/useSwapsApiPartnerFee.ts +40 -0
  26. package/src/hooks/swapsApi/useSwapsApiQuote.ts +55 -0
  27. package/src/hooks/swapsApi/useSwapsApiSolverFee.ts +40 -0
  28. package/src/hooks/swapsApi/useSwapsApiStatus.ts +45 -0
  29. package/src/hooks/swapsApi/useSwapsApiSubmitIntent.ts +43 -0
  30. package/src/hooks/{backend/useBackendSubmitSwapTx.ts → swapsApi/useSwapsApiSubmitTx.ts} +16 -17
  31. package/src/hooks/swapsApi/useSwapsApiSubmitTxStatus.ts +55 -0
  32. package/src/hooks/swapsApi/useSwapsApiTokens.ts +34 -0
  33. package/src/hooks/swapsApi/useSwapsApiTokensByChain.ts +40 -0
  34. package/src/hooks/backend/useBackendSubmitSwapTxStatus.ts +0 -59
@@ -0,0 +1,45 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { RequestOverrideConfig, StatusResponseV2 } from '@sodax/sdk';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from '../shared/unwrapResult.js';
5
+ import type { ReadHookParams } from '../shared/types.js';
6
+ import { isTerminalSwapIntentStatus } from './isTerminalSwapIntentStatus.js';
7
+
8
+ export type UseSwapsApiStatusParams = ReadHookParams<
9
+ StatusResponseV2 | undefined,
10
+ {
11
+ intentTxHash: string | undefined;
12
+ apiConfig?: RequestOverrideConfig;
13
+ }
14
+ >;
15
+
16
+ /**
17
+ * React hook for polling the solver intent status by hub-chain intent tx hash via the swaps API —
18
+ * `sodax.api.swaps.getStatus`. Returns `{ status, fillTxHash? }` (`fillTxHash` set when `status === 3`).
19
+ *
20
+ * @example
21
+ * const { data } = useSwapsApiStatus({ params: { intentTxHash: '0x123...' } });
22
+ *
23
+ * @remarks
24
+ * - Default refetch interval is 1 second; stops once `status` is `3` (SOLVED) or `4` (FAILED).
25
+ */
26
+ export const useSwapsApiStatus = ({
27
+ params,
28
+ queryOptions,
29
+ }: UseSwapsApiStatusParams = {}): UseQueryResult<StatusResponseV2 | undefined, Error> => {
30
+ const { sodax } = useSodaxContext();
31
+ const intentTxHash = params?.intentTxHash;
32
+ const apiConfig = params?.apiConfig;
33
+
34
+ return useQuery({
35
+ queryKey: ['swapsApi', 'status', intentTxHash],
36
+ queryFn: async (): Promise<StatusResponseV2 | undefined> => {
37
+ if (!intentTxHash) return undefined;
38
+ return unwrapResult(await sodax.api.swaps.getStatus({ intentTxHash }, apiConfig));
39
+ },
40
+ enabled: !!intentTxHash && intentTxHash.length > 0,
41
+ retry: 3,
42
+ refetchInterval: query => (isTerminalSwapIntentStatus(query.state.data?.status) ? false : 1000),
43
+ ...queryOptions,
44
+ });
45
+ };
@@ -0,0 +1,43 @@
1
+ import type { RequestOverrideConfig, SubmitIntentRequestV2, SubmitIntentResponseV2 } from '@sodax/sdk';
2
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
3
+ import { unwrapResult } from '../shared/unwrapResult.js';
4
+ import type { MutationHookParams } from '../shared/types.js';
5
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
6
+
7
+ /**
8
+ * Mutation variables for {@link useSwapsApiSubmitIntent}. The per-request `apiConfig` override
9
+ * belongs here rather than at the hook level.
10
+ */
11
+ export type UseSwapsApiSubmitIntentVars = {
12
+ body: SubmitIntentRequestV2;
13
+ apiConfig?: RequestOverrideConfig;
14
+ };
15
+
16
+ /**
17
+ * React hook to submit the broadcast intent tx to the relay via the swaps API —
18
+ * `sodax.api.swaps.submitIntent`. Returns `{ result }` (opaque relay response).
19
+ *
20
+ * No auto-retry (`retry: false`): submitting is non-idempotent — a retry after a lost response
21
+ * could double-submit to the relay. Override via `mutationOptions.retry` if your relay dedupes.
22
+ *
23
+ * @example
24
+ * const { mutateAsync: submitIntent } = useSwapsApiSubmitIntent();
25
+ * const { result } = await submitIntent({ body: { chainId: '146', txHash: '0x123...' } });
26
+ */
27
+ export const useSwapsApiSubmitIntent = ({
28
+ mutationOptions,
29
+ }: MutationHookParams<SubmitIntentResponseV2, UseSwapsApiSubmitIntentVars> = {}): SafeUseMutationResult<
30
+ SubmitIntentResponseV2,
31
+ Error,
32
+ UseSwapsApiSubmitIntentVars
33
+ > => {
34
+ const { sodax } = useSodaxContext();
35
+
36
+ return useSafeMutation<SubmitIntentResponseV2, Error, UseSwapsApiSubmitIntentVars>({
37
+ mutationKey: ['swapsApi', 'submitIntent'],
38
+ retry: false,
39
+ ...mutationOptions,
40
+ mutationFn: async ({ body, apiConfig }): Promise<SubmitIntentResponseV2> =>
41
+ unwrapResult(await sodax.api.swaps.submitIntent(body, apiConfig)),
42
+ });
43
+ };
@@ -1,49 +1,48 @@
1
- // packages/dapp-kit/src/hooks/backend/useBackendSubmitSwapTx.ts
2
- import type { RequestOverrideConfig, SubmitSwapTxRequest, SubmitSwapTxResponse } from '@sodax/sdk';
1
+ import type { RequestOverrideConfig, SubmitTxRequestV2, SubmitTxResponseV2 } from '@sodax/sdk';
3
2
  import { useSodaxContext } from '../shared/useSodaxContext.js';
4
- import { unwrapResult } from './unwrapResult.js';
3
+ import { unwrapResult } from '../shared/unwrapResult.js';
5
4
  import type { MutationHookParams } from '../shared/types.js';
6
5
  import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
7
6
 
8
7
  /**
9
- * Mutation variables for {@link useBackendSubmitSwapTx}. The per-request `apiConfig` override
8
+ * Mutation variables for {@link useSwapsApiSubmitTx}. The per-request `apiConfig` override
10
9
  * (e.g. base URL) belongs here rather than at the hook level — different submissions in the same
11
10
  * component can target different endpoints without re-rendering.
12
11
  */
13
- export type UseBackendSubmitSwapTxVars = {
14
- request: SubmitSwapTxRequest;
12
+ export type UseSwapsApiSubmitTxVars = {
13
+ request: SubmitTxRequestV2;
15
14
  apiConfig?: RequestOverrideConfig;
16
15
  };
17
16
 
18
17
  /**
19
- * React hook for submitting a swap transaction to be processed by the backend (relay, post
20
- * execution to solver, etc.).
18
+ * React hook for submitting a swap transaction to be processed (relay, post execution to the
19
+ * solver, etc.) via the swaps API — `sodax.api.swaps.submitTx`.
21
20
  *
22
21
  * Pure mutation: pass `{ request, apiConfig? }` to `mutate({...})`. Default `retry: 3` is applied
23
22
  * at the hook level — consumers can override via `mutationOptions.retry`.
24
23
  *
25
24
  * @example
26
- * const { mutateAsync: submitSwapTx, isPending, error } = useBackendSubmitSwapTx();
25
+ * const { mutateAsync: submitSwapTx, isPending, error } = useSwapsApiSubmitTx();
27
26
  *
28
27
  * const result = await submitSwapTx({
29
28
  * request: { txHash: '0x123...', srcChainKey: 'sonic', walletAddress: '0xabc...', intent: { ... }, relayData: '0x...' },
30
29
  * apiConfig: { baseURL: 'https://...' },
31
30
  * });
32
31
  */
33
- export const useBackendSubmitSwapTx = ({
32
+ export const useSwapsApiSubmitTx = ({
34
33
  mutationOptions,
35
- }: MutationHookParams<SubmitSwapTxResponse, UseBackendSubmitSwapTxVars> = {}): SafeUseMutationResult<
36
- SubmitSwapTxResponse,
34
+ }: MutationHookParams<SubmitTxResponseV2, UseSwapsApiSubmitTxVars> = {}): SafeUseMutationResult<
35
+ SubmitTxResponseV2,
37
36
  Error,
38
- UseBackendSubmitSwapTxVars
37
+ UseSwapsApiSubmitTxVars
39
38
  > => {
40
39
  const { sodax } = useSodaxContext();
41
40
 
42
- return useSafeMutation<SubmitSwapTxResponse, Error, UseBackendSubmitSwapTxVars>({
43
- mutationKey: ['backend', 'submitSwapTx'],
41
+ return useSafeMutation<SubmitTxResponseV2, Error, UseSwapsApiSubmitTxVars>({
42
+ mutationKey: ['swapsApi', 'submitTx'],
44
43
  retry: 3,
45
44
  ...mutationOptions,
46
- mutationFn: async ({ request, apiConfig }): Promise<SubmitSwapTxResponse> =>
47
- unwrapResult(await sodax.backendApi.submitSwapTx(request, apiConfig)),
45
+ mutationFn: async ({ request, apiConfig }): Promise<SubmitTxResponseV2> =>
46
+ unwrapResult(await sodax.api.swaps.submitTx(request, apiConfig)),
48
47
  });
49
48
  };
@@ -0,0 +1,55 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { RequestOverrideConfig, SubmitTxStatusResponseV2 } from '@sodax/sdk';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from '../shared/unwrapResult.js';
5
+ import type { ReadHookParams } from '../shared/types.js';
6
+
7
+ export type UseSwapsApiSubmitTxStatusParams = ReadHookParams<
8
+ SubmitTxStatusResponseV2 | undefined,
9
+ {
10
+ txHash: string | undefined;
11
+ srcChainKey?: string;
12
+ apiConfig?: RequestOverrideConfig;
13
+ }
14
+ >;
15
+
16
+ /**
17
+ * React hook for polling the processing status of a submitted swap transaction via the swaps API —
18
+ * `sodax.api.swaps.getSubmitTxStatus`. Both `txHash` and `srcChainKey` are required for the query
19
+ * to run (the swaps API v2 status query requires the source chain key).
20
+ *
21
+ * @example
22
+ * const { data: status } = useSwapsApiSubmitTxStatus({
23
+ * params: { txHash: '0x123...', srcChainKey: 'sonic' },
24
+ * });
25
+ *
26
+ * @remarks
27
+ * - Default refetch interval is 1 second; stops on 'executed' or 'failed' status.
28
+ */
29
+ export const useSwapsApiSubmitTxStatus = ({
30
+ params,
31
+ queryOptions,
32
+ }: UseSwapsApiSubmitTxStatusParams = {}): UseQueryResult<SubmitTxStatusResponseV2 | undefined, Error> => {
33
+ const { sodax } = useSodaxContext();
34
+ const txHash = params?.txHash;
35
+ const srcChainKey = params?.srcChainKey;
36
+ const apiConfig = params?.apiConfig;
37
+
38
+ return useQuery({
39
+ queryKey: ['swapsApi', 'submitTx', 'status', txHash, srcChainKey],
40
+ queryFn: async (): Promise<SubmitTxStatusResponseV2 | undefined> => {
41
+ // `srcChainKey` is required by the swaps API v2 status query. The `enabled` gate below
42
+ // ensures both are present; this guard also narrows the types for the call.
43
+ if (!txHash || !srcChainKey) return undefined;
44
+ return unwrapResult(await sodax.api.swaps.getSubmitTxStatus({ txHash, srcChainKey }, apiConfig));
45
+ },
46
+ enabled: !!txHash && txHash.length > 0 && !!srcChainKey,
47
+ retry: 3,
48
+ refetchInterval: query => {
49
+ const status = query.state.data?.data?.status;
50
+ if (status === 'executed' || status === 'failed') return false;
51
+ return 1000;
52
+ },
53
+ ...queryOptions,
54
+ });
55
+ };
@@ -0,0 +1,34 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { GetSwapTokensResponseV2, RequestOverrideConfig } from '@sodax/sdk';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from '../shared/unwrapResult.js';
5
+ import type { ReadHookParams } from '../shared/types.js';
6
+
7
+ export type UseSwapsApiTokensParams = ReadHookParams<
8
+ GetSwapTokensResponseV2,
9
+ {
10
+ apiConfig?: RequestOverrideConfig;
11
+ }
12
+ >;
13
+
14
+ /**
15
+ * React hook to fetch all supported swap tokens grouped by SpokeChainKey via the swaps API —
16
+ * `sodax.api.swaps.getTokens`.
17
+ *
18
+ * @example
19
+ * const { data: tokensByChain } = useSwapsApiTokens();
20
+ */
21
+ export const useSwapsApiTokens = ({
22
+ params,
23
+ queryOptions,
24
+ }: UseSwapsApiTokensParams = {}): UseQueryResult<GetSwapTokensResponseV2, Error> => {
25
+ const { sodax } = useSodaxContext();
26
+ const apiConfig = params?.apiConfig;
27
+
28
+ return useQuery<GetSwapTokensResponseV2, Error>({
29
+ queryKey: ['swapsApi', 'tokens'],
30
+ queryFn: async (): Promise<GetSwapTokensResponseV2> => unwrapResult(await sodax.api.swaps.getTokens(apiConfig)),
31
+ retry: 3,
32
+ ...queryOptions,
33
+ });
34
+ };
@@ -0,0 +1,40 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { GetSwapTokensByChainResponseV2, RequestOverrideConfig } from '@sodax/sdk';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { unwrapResult } from '../shared/unwrapResult.js';
5
+ import type { ReadHookParams } from '../shared/types.js';
6
+
7
+ export type UseSwapsApiTokensByChainParams = ReadHookParams<
8
+ GetSwapTokensByChainResponseV2 | undefined,
9
+ {
10
+ chainKey: string | undefined;
11
+ apiConfig?: RequestOverrideConfig;
12
+ }
13
+ >;
14
+
15
+ /**
16
+ * React hook to fetch supported swap tokens for a single SpokeChainKey via the swaps API —
17
+ * `sodax.api.swaps.getTokensByChain`.
18
+ *
19
+ * @example
20
+ * const { data: tokens } = useSwapsApiTokensByChain({ params: { chainKey: '0xa4b1.arbitrum' } });
21
+ */
22
+ export const useSwapsApiTokensByChain = ({
23
+ params,
24
+ queryOptions,
25
+ }: UseSwapsApiTokensByChainParams = {}): UseQueryResult<GetSwapTokensByChainResponseV2 | undefined, Error> => {
26
+ const { sodax } = useSodaxContext();
27
+ const chainKey = params?.chainKey;
28
+ const apiConfig = params?.apiConfig;
29
+
30
+ return useQuery({
31
+ queryKey: ['swapsApi', 'tokens', chainKey],
32
+ queryFn: async (): Promise<GetSwapTokensByChainResponseV2 | undefined> => {
33
+ if (!chainKey) return undefined;
34
+ return unwrapResult(await sodax.api.swaps.getTokensByChain(chainKey, apiConfig));
35
+ },
36
+ enabled: !!chainKey && chainKey.length > 0,
37
+ retry: 3,
38
+ ...queryOptions,
39
+ });
40
+ };
@@ -1,59 +0,0 @@
1
- import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
- import type { RequestOverrideConfig, SubmitSwapTxStatusResponse } from '@sodax/sdk';
3
- import { useSodaxContext } from '../shared/useSodaxContext.js';
4
- import { unwrapResult } from './unwrapResult.js';
5
- import type { ReadHookParams } from '../shared/types.js';
6
-
7
- export type UseBackendSubmitSwapTxStatusParams = ReadHookParams<
8
- SubmitSwapTxStatusResponse | undefined,
9
- {
10
- txHash: string | undefined;
11
- srcChainKey?: string;
12
- apiConfig?: RequestOverrideConfig;
13
- }
14
- >;
15
-
16
- /**
17
- * React hook for polling the processing status of a submitted swap transaction.
18
- *
19
- * @example
20
- * const { data: status } = useBackendSubmitSwapTxStatus({
21
- * params: { txHash: '0x123...', srcChainKey: 'sonic' },
22
- * });
23
- *
24
- * @remarks
25
- * - Default refetch interval is 1 second; stops on 'executed' or 'failed' status.
26
- */
27
- export const useBackendSubmitSwapTxStatus = ({
28
- params,
29
- queryOptions,
30
- }: UseBackendSubmitSwapTxStatusParams = {}): UseQueryResult<SubmitSwapTxStatusResponse | undefined, Error> => {
31
- const { sodax } = useSodaxContext();
32
- const txHash = params?.txHash;
33
- const srcChainKey = params?.srcChainKey;
34
- const apiConfig = params?.apiConfig;
35
-
36
- return useQuery({
37
- queryKey: ['backend', 'submitSwapTx', 'status', txHash, srcChainKey],
38
- queryFn: async (): Promise<SubmitSwapTxStatusResponse | undefined> => {
39
- if (!txHash) return undefined;
40
- return unwrapResult(
41
- await sodax.backendApi.getSubmitSwapTxStatus(
42
- {
43
- txHash,
44
- srcChainKey,
45
- },
46
- apiConfig,
47
- ),
48
- );
49
- },
50
- enabled: !!txHash && txHash.length > 0,
51
- retry: 3,
52
- refetchInterval: query => {
53
- const status = query.state.data?.data?.status;
54
- if (status === 'executed' || status === 'failed') return false;
55
- return 1000;
56
- },
57
- ...queryOptions,
58
- });
59
- };