@sodax/dapp-kit 2.0.0-rc.9 → 2.1.0-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 (104) hide show
  1. package/README.md +141 -119
  2. package/dist/index.d.ts +939 -63
  3. package/dist/index.mjs +1086 -180
  4. package/package.json +3 -3
  5. package/src/hooks/_mutationContract.test.ts +17 -2
  6. package/src/hooks/backend/index.ts +1 -5
  7. package/src/hooks/backend/types.ts +1 -1
  8. package/src/hooks/bitcoin/index.ts +2 -0
  9. package/src/hooks/bitcoin/resolveBtcReadAddress.test.ts +54 -0
  10. package/src/hooks/bitcoin/resolveBtcReadAddress.ts +21 -0
  11. package/src/hooks/bitcoin/useBitcoinTradingSetup.ts +45 -0
  12. package/src/hooks/bitcoin/useEnsureRadfiAccessToken.ts +57 -0
  13. package/src/hooks/bitcoin/useFundTradingWallet.ts +1 -1
  14. package/src/hooks/bitcoin/useRadfiAuth.ts +2 -2
  15. package/src/hooks/bitcoin/useRadfiWithdraw.ts +2 -2
  16. package/src/hooks/bitcoin/useRenewUtxos.ts +2 -2
  17. package/src/hooks/bitcoin/useTradingWallet.ts +1 -1
  18. package/src/hooks/bridge/useBridgeApprove.ts +2 -1
  19. package/src/hooks/index.ts +3 -0
  20. package/src/hooks/leverageYield/index.ts +9 -0
  21. package/src/hooks/leverageYield/useLeverageYieldDeposit.ts +40 -0
  22. package/src/hooks/leverageYield/useLeverageYieldEffectiveApr.ts +42 -0
  23. package/src/hooks/leverageYield/useLeverageYieldNotifySolver.ts +34 -0
  24. package/src/hooks/leverageYield/useLeverageYieldPosition.ts +40 -0
  25. package/src/hooks/leverageYield/useLeverageYieldPreviewRedeem.ts +43 -0
  26. package/src/hooks/leverageYield/useLeverageYieldShareBalances.ts +74 -0
  27. package/src/hooks/leverageYield/useLeverageYieldTotalAssets.ts +36 -0
  28. package/src/hooks/leverageYield/useLeverageYieldVaultSwap.ts +50 -0
  29. package/src/hooks/leverageYield/useLeverageYieldWithdraw.ts +40 -0
  30. package/src/hooks/migrate/useMigrationApprove.ts +1 -3
  31. package/src/hooks/mm/useATokensBalances.ts +7 -1
  32. package/src/hooks/mm/useUserFormattedSummary.ts +5 -1
  33. package/src/hooks/mm/useUserReservesData.ts +5 -1
  34. package/src/hooks/mm/useWithdraw.ts +5 -1
  35. package/src/hooks/partner/index.ts +4 -0
  36. package/src/hooks/partner/useApproveToken.ts +1 -2
  37. package/src/hooks/partner/useFeeClaimWithdraw.ts +88 -0
  38. package/src/hooks/partner/useGetIntentDetails.ts +39 -0
  39. package/src/hooks/partner/useGetUserIntent.ts +35 -0
  40. package/src/hooks/partner/usePartnerCancelIntent.ts +52 -0
  41. package/src/hooks/shared/index.ts +6 -0
  42. package/src/hooks/shared/isUserRejectedError.test.ts +42 -0
  43. package/src/hooks/shared/isUserRejectedError.ts +30 -0
  44. package/src/hooks/shared/useEstablishTrustline.test.ts +89 -0
  45. package/src/hooks/shared/useEstablishTrustline.ts +72 -0
  46. package/src/hooks/shared/useGetUserHubWalletAddress.ts +5 -1
  47. package/src/hooks/shared/useNearStorageCheck.ts +45 -0
  48. package/src/hooks/shared/useNearStorageGate.ts +60 -0
  49. package/src/hooks/shared/useRegisterNearStorage.ts +46 -0
  50. package/src/hooks/shared/useRequestTrustline.test.ts +115 -0
  51. package/src/hooks/shared/useRequestTrustline.ts +41 -56
  52. package/src/hooks/shared/useSafeMutation.ts +1 -4
  53. package/src/hooks/shared/useStellarGate.test.ts +99 -0
  54. package/src/hooks/shared/useStellarGate.ts +102 -0
  55. package/src/hooks/shared/useStellarTrustlineCheck.test.ts +74 -0
  56. package/src/hooks/shared/useStellarTrustlineCheck.ts +39 -15
  57. package/src/hooks/sponsoring/index.ts +4 -0
  58. package/src/hooks/sponsoring/useActivateStellarAccount.test.ts +74 -0
  59. package/src/hooks/sponsoring/useActivateStellarAccount.ts +38 -0
  60. package/src/hooks/sponsoring/useSponsorConfig.test.ts +46 -0
  61. package/src/hooks/sponsoring/useSponsorConfig.ts +31 -0
  62. package/src/hooks/sponsoring/useStellarAccountActive.test.ts +50 -0
  63. package/src/hooks/sponsoring/useStellarAccountActive.ts +45 -0
  64. package/src/hooks/sponsoring/useStellarAccountStatus.test.ts +90 -0
  65. package/src/hooks/sponsoring/useStellarAccountStatus.ts +57 -0
  66. package/src/hooks/staking/useStakingConfig.ts +1 -3
  67. package/src/hooks/staking/useStakingInfo.ts +4 -4
  68. package/src/hooks/staking/useUnstake.ts +5 -1
  69. package/src/hooks/swap/useCreateLimitOrder.ts +1 -2
  70. package/src/hooks/swap/useSwap.ts +1 -5
  71. package/src/hooks/swapsApi/index.ts +41 -0
  72. package/src/hooks/swapsApi/isTerminalSwapIntentStatus.test.ts +24 -0
  73. package/src/hooks/swapsApi/isTerminalSwapIntentStatus.ts +11 -0
  74. package/src/hooks/swapsApi/useSwapsApiAllowance.ts +40 -0
  75. package/src/hooks/swapsApi/useSwapsApiApprove.ts +42 -0
  76. package/src/hooks/swapsApi/useSwapsApiCancelIntent.ts +41 -0
  77. package/src/hooks/swapsApi/useSwapsApiCreateIntent.ts +41 -0
  78. package/src/hooks/swapsApi/useSwapsApiCreateLimitOrder.ts +41 -0
  79. package/src/hooks/swapsApi/useSwapsApiDeadline.ts +36 -0
  80. package/src/hooks/swapsApi/useSwapsApiEstimateGas.ts +40 -0
  81. package/src/hooks/swapsApi/useSwapsApiFilledIntent.ts +41 -0
  82. package/src/hooks/swapsApi/useSwapsApiIntent.ts +40 -0
  83. package/src/hooks/swapsApi/useSwapsApiIntentExtraData.ts +41 -0
  84. package/src/hooks/swapsApi/useSwapsApiIntentHash.ts +40 -0
  85. package/src/hooks/swapsApi/useSwapsApiIntentPacket.ts +43 -0
  86. package/src/hooks/swapsApi/useSwapsApiPartnerFee.ts +40 -0
  87. package/src/hooks/swapsApi/useSwapsApiQuote.ts +55 -0
  88. package/src/hooks/swapsApi/useSwapsApiSolverFee.ts +40 -0
  89. package/src/hooks/swapsApi/useSwapsApiStatus.ts +45 -0
  90. package/src/hooks/swapsApi/useSwapsApiSubmitIntent.ts +43 -0
  91. package/src/hooks/{backend/useBackendSubmitSwapTx.ts → swapsApi/useSwapsApiSubmitTx.ts} +16 -17
  92. package/src/hooks/swapsApi/useSwapsApiSubmitTxStatus.ts +55 -0
  93. package/src/hooks/swapsApi/useSwapsApiTokens.ts +34 -0
  94. package/src/hooks/swapsApi/useSwapsApiTokensByChain.ts +40 -0
  95. package/src/index.ts +1 -1
  96. package/src/providers/SodaxProvider.tsx +2 -3
  97. package/src/providers/createSodaxQueryClient.ts +1 -5
  98. package/src/utils/dex-utils.ts +1 -4
  99. package/src/utils/index.ts +2 -0
  100. package/src/utils/nearStorageGate.test.ts +46 -0
  101. package/src/utils/nearStorageGate.ts +39 -0
  102. package/src/utils/stellarGate.test.ts +196 -0
  103. package/src/utils/stellarGate.ts +84 -0
  104. package/src/hooks/backend/useBackendSubmitSwapTxStatus.ts +0 -59
@@ -0,0 +1,57 @@
1
+ import { STELLAR_TRUSTLINE_MIN_XLM_STROOPS, type Sodax, type StellarAccountStatus } from '@sodax/sdk';
2
+ import { type UseQueryResult, useQuery } from '@tanstack/react-query';
3
+ import type { ReadHookParams } from '../shared/types.js';
4
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
5
+
6
+ export type UseStellarAccountStatusParams = ReadHookParams<
7
+ StellarAccountStatus,
8
+ {
9
+ address: string | undefined;
10
+ }
11
+ >;
12
+
13
+ type StellarAccountStatusReader = { sponsoring: Pick<Sodax['sponsoring'], 'getStellarAccountStatus'> };
14
+
15
+ export function getStellarAccountStatusQueryOptions({
16
+ sodax,
17
+ address,
18
+ }: {
19
+ sodax: StellarAccountStatusReader;
20
+ address: string | undefined;
21
+ }) {
22
+ return {
23
+ queryKey: ['sponsoring', 'stellarAccountStatus', address] as const,
24
+ queryFn: async (): Promise<StellarAccountStatus> => {
25
+ if (!address) {
26
+ return {
27
+ exists: false,
28
+ nativeBalanceStroops: 0n,
29
+ availableBalanceStroops: 0n,
30
+ canAffordTrustline: false,
31
+ // No account to read the network reserve for; the published value is the best guess.
32
+ trustlineMinXlmStroops: STELLAR_TRUSTLINE_MIN_XLM_STROOPS,
33
+ };
34
+ }
35
+ const result = await sodax.sponsoring.getStellarAccountStatus({ address });
36
+ // A read failure is not evidence that the account is absent.
37
+ if (!result.ok) throw result.error;
38
+ return result.value;
39
+ },
40
+ enabled: !!address,
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Read account existence and trustline affordability in one Horizon request.
46
+ * Prefer {@link useStellarGate} for destination gating.
47
+ */
48
+ export function useStellarAccountStatus({
49
+ params,
50
+ queryOptions,
51
+ }: UseStellarAccountStatusParams = {}): UseQueryResult<StellarAccountStatus, Error> {
52
+ const { sodax } = useSodaxContext();
53
+ return useQuery<StellarAccountStatus, Error>({
54
+ ...getStellarAccountStatusQueryOptions({ sodax, address: params?.address }),
55
+ ...queryOptions,
56
+ });
57
+ }
@@ -9,9 +9,7 @@ export type UseStakingConfigParams = ReadHookParams<StakingConfig>;
9
9
  * React hook to fetch the global staking config (unstaking period, min unstaking period, max
10
10
  * penalty). Hub-only read; no chain context required. Throws on `!ok`.
11
11
  */
12
- export function useStakingConfig({
13
- queryOptions,
14
- }: UseStakingConfigParams = {}): UseQueryResult<StakingConfig, Error> {
12
+ export function useStakingConfig({ queryOptions }: UseStakingConfigParams = {}): UseQueryResult<StakingConfig, Error> {
15
13
  const { sodax } = useSodaxContext();
16
14
 
17
15
  return useQuery<StakingConfig, Error>({
@@ -17,10 +17,10 @@ export type UseStakingInfoParams = ReadHookParams<
17
17
  * deriving the hub wallet from the spoke `srcAddress` + `srcChainKey`. Throws on `!ok` so React
18
18
  * Query lands in `error` state.
19
19
  */
20
- export function useStakingInfo({ params, queryOptions }: UseStakingInfoParams = {}): UseQueryResult<
21
- StakingInfo,
22
- Error
23
- > {
20
+ export function useStakingInfo({
21
+ params,
22
+ queryOptions,
23
+ }: UseStakingInfoParams = {}): UseQueryResult<StakingInfo, Error> {
24
24
  const { sodax } = useSodaxContext();
25
25
  const srcAddress = params?.srcAddress;
26
26
  const srcChainKey = params?.srcChainKey;
@@ -16,7 +16,11 @@ export type UseUnstakeVars<K extends SpokeChainKey = SpokeChainKey> = Omit<Unsta
16
16
  */
17
17
  export function useUnstake<K extends SpokeChainKey = SpokeChainKey>({
18
18
  mutationOptions,
19
- }: MutationHookParams<TxHashPair, UseUnstakeVars<K>> = {}): SafeUseMutationResult<TxHashPair, Error, UseUnstakeVars<K>> {
19
+ }: MutationHookParams<TxHashPair, UseUnstakeVars<K>> = {}): SafeUseMutationResult<
20
+ TxHashPair,
21
+ Error,
22
+ UseUnstakeVars<K>
23
+ > {
20
24
  const { sodax } = useSodaxContext();
21
25
  const queryClient = useQueryClient();
22
26
 
@@ -33,7 +33,6 @@ export function useCreateLimitOrder<K extends SpokeChainKey = SpokeChainKey>({
33
33
  return useSafeMutation<SwapResponse, Error, UseCreateLimitOrderVars<K>>({
34
34
  mutationKey: ['swap', 'limitOrder', 'create'],
35
35
  ...mutationOptions,
36
- mutationFn: async vars =>
37
- unwrapResult(await sodax.swaps.createLimitOrder({ ...vars, raw: false })),
36
+ mutationFn: async vars => unwrapResult(await sodax.swaps.createLimitOrder({ ...vars, raw: false })),
38
37
  });
39
38
  }
@@ -21,11 +21,7 @@ export type UseSwapVars<K extends SpokeChainKey = SpokeChainKey> = Omit<SwapActi
21
21
  */
22
22
  export function useSwap<K extends SpokeChainKey = SpokeChainKey>({
23
23
  mutationOptions,
24
- }: MutationHookParams<SwapResponse, UseSwapVars<K>> = {}): SafeUseMutationResult<
25
- SwapResponse,
26
- Error,
27
- UseSwapVars<K>
28
- > {
24
+ }: MutationHookParams<SwapResponse, UseSwapVars<K>> = {}): SafeUseMutationResult<SwapResponse, Error, UseSwapVars<K>> {
29
25
  const { sodax } = useSodaxContext();
30
26
  const queryClient = useQueryClient();
31
27
 
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Swaps API v2 hooks — typed React Query wrappers over `sodax.api.swaps.*` (the SwapsApiService
3
+ * HTTP client). One hook per endpoint of the backend Swaps API v2.
4
+ *
5
+ * Distinct from the on-chain `swap/` hooks (`useQuote`/`useStatus`/`useSwap`/…), which drive the
6
+ * `SwapService` path (wallet → hub chain). These hooks call the backend HTTP API instead.
7
+ */
8
+
9
+ // Tokens
10
+ export * from './useSwapsApiTokens.js';
11
+ export * from './useSwapsApiTokensByChain.js';
12
+
13
+ // Quote · deadline
14
+ export * from './useSwapsApiQuote.js';
15
+ export * from './useSwapsApiDeadline.js';
16
+
17
+ // Allowance · approve · create intent
18
+ export * from './useSwapsApiAllowance.js';
19
+ export * from './useSwapsApiApprove.js';
20
+ export * from './useSwapsApiCreateIntent.js';
21
+
22
+ // Intent lifecycle: submit · status · cancel · hash · packet · extra-data · lookup
23
+ export * from './useSwapsApiSubmitIntent.js';
24
+ export * from './useSwapsApiStatus.js';
25
+ export * from './isTerminalSwapIntentStatus.js'; // terminal-status predicate (drives the status hook's polling)
26
+ export * from './useSwapsApiCancelIntent.js';
27
+ export * from './useSwapsApiIntentHash.js';
28
+ export * from './useSwapsApiIntentPacket.js';
29
+ export * from './useSwapsApiIntentExtraData.js';
30
+ export * from './useSwapsApiFilledIntent.js';
31
+ export * from './useSwapsApiIntent.js';
32
+
33
+ // Limit orders · gas · fees
34
+ export * from './useSwapsApiCreateLimitOrder.js';
35
+ export * from './useSwapsApiEstimateGas.js';
36
+ export * from './useSwapsApiPartnerFee.js';
37
+ export * from './useSwapsApiSolverFee.js';
38
+
39
+ // Submit-tx state machine
40
+ export * from './useSwapsApiSubmitTx.js';
41
+ export * from './useSwapsApiSubmitTxStatus.js';
@@ -0,0 +1,24 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { isTerminalSwapIntentStatus } from './isTerminalSwapIntentStatus.js';
3
+
4
+ /**
5
+ * Guards the polling-stop invariant for `useSwapsApiStatus.refetchInterval`: the hook keeps polling
6
+ * (1s) until the solver reports a terminal status, then stops. Tested as a pure predicate because
7
+ * dapp-kit's vitest runs in the `node` environment (no hook rendering) — see nearStorageGate.test.ts.
8
+ */
9
+ describe('isTerminalSwapIntentStatus', () => {
10
+ it('is terminal for SOLVED (3) and FAILED (4)', () => {
11
+ expect(isTerminalSwapIntentStatus(3)).toBe(true);
12
+ expect(isTerminalSwapIntentStatus(4)).toBe(true);
13
+ });
14
+
15
+ it('is non-terminal for NOT_FOUND (-1), NOT_STARTED_YET (1), and STARTED_NOT_FINISHED (2)', () => {
16
+ expect(isTerminalSwapIntentStatus(-1)).toBe(false);
17
+ expect(isTerminalSwapIntentStatus(1)).toBe(false);
18
+ expect(isTerminalSwapIntentStatus(2)).toBe(false);
19
+ });
20
+
21
+ it('is non-terminal when no status has arrived yet (undefined) — keeps polling', () => {
22
+ expect(isTerminalSwapIntentStatus(undefined)).toBe(false);
23
+ });
24
+ });
@@ -0,0 +1,11 @@
1
+ import type { SwapIntentStatusCodeV2 } from '@sodax/sdk';
2
+
3
+ /**
4
+ * Terminal solver intent states: once reached, the intent is resolved and polling stops.
5
+ * `3` = SOLVED, `4` = FAILED (per `SwapIntentStatusCodeV2`); `-1` / `1` / `2` are non-terminal.
6
+ *
7
+ * Kept in its own pure module (no React/context imports) so it is unit-testable in dapp-kit's
8
+ * `node` test environment — importing the hook itself pulls in `useSodaxContext`.
9
+ */
10
+ export const isTerminalSwapIntentStatus = (status: SwapIntentStatusCodeV2 | undefined): boolean =>
11
+ status === 3 || status === 4;
@@ -0,0 +1,40 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { AllowanceCheckResponseV2, CreateIntentParamsV2, 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 UseSwapsApiAllowanceParams = ReadHookParams<
8
+ AllowanceCheckResponseV2 | undefined,
9
+ {
10
+ body: CreateIntentParamsV2 | undefined;
11
+ apiConfig?: RequestOverrideConfig;
12
+ }
13
+ >;
14
+
15
+ /**
16
+ * React hook to check whether the source-token allowance is already sufficient for an intent via
17
+ * the swaps API — `sodax.api.swaps.checkAllowance`. Returns `{ valid }`.
18
+ *
19
+ * @example
20
+ * const { data: allowance } = useSwapsApiAllowance({ params: { body: createIntentParams } });
21
+ */
22
+ export const useSwapsApiAllowance = ({
23
+ params,
24
+ queryOptions,
25
+ }: UseSwapsApiAllowanceParams = {}): UseQueryResult<AllowanceCheckResponseV2 | undefined, Error> => {
26
+ const { sodax } = useSodaxContext();
27
+ const body = params?.body;
28
+ const apiConfig = params?.apiConfig;
29
+
30
+ return useQuery({
31
+ queryKey: ['swapsApi', 'allowance', body?.srcChainKey, body?.inputToken, body?.inputAmount, body?.srcAddress],
32
+ queryFn: async (): Promise<AllowanceCheckResponseV2 | undefined> => {
33
+ if (!body) return undefined;
34
+ return unwrapResult(await sodax.api.swaps.checkAllowance(body, apiConfig));
35
+ },
36
+ enabled: !!body,
37
+ retry: 3,
38
+ ...queryOptions,
39
+ });
40
+ };
@@ -0,0 +1,42 @@
1
+ import type { ApproveResponseV2, CreateIntentParamsV2, RequestOverrideConfig } 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 useSwapsApiApprove}. The per-request `apiConfig` override belongs
9
+ * here rather than at the hook level — different calls in the same component can target different
10
+ * endpoints without re-rendering.
11
+ */
12
+ export type UseSwapsApiApproveVars = {
13
+ body: CreateIntentParamsV2;
14
+ apiConfig?: RequestOverrideConfig;
15
+ };
16
+
17
+ /**
18
+ * React hook to build an unsigned token-approval transaction for the source token via the swaps
19
+ * API — `sodax.api.swaps.approve`. Returns `{ tx }` (chain-specific unsigned tx) to sign and
20
+ * broadcast yourself; it does not change state, so no queries are invalidated.
21
+ *
22
+ * @example
23
+ * const { mutateAsync: approve } = useSwapsApiApprove();
24
+ * const { tx } = await approve({ body: createIntentParams });
25
+ */
26
+ export const useSwapsApiApprove = ({
27
+ mutationOptions,
28
+ }: MutationHookParams<ApproveResponseV2, UseSwapsApiApproveVars> = {}): SafeUseMutationResult<
29
+ ApproveResponseV2,
30
+ Error,
31
+ UseSwapsApiApproveVars
32
+ > => {
33
+ const { sodax } = useSodaxContext();
34
+
35
+ return useSafeMutation<ApproveResponseV2, Error, UseSwapsApiApproveVars>({
36
+ mutationKey: ['swapsApi', 'approve'],
37
+ retry: 3,
38
+ ...mutationOptions,
39
+ mutationFn: async ({ body, apiConfig }): Promise<ApproveResponseV2> =>
40
+ unwrapResult(await sodax.api.swaps.approve(body, apiConfig)),
41
+ });
42
+ };
@@ -0,0 +1,41 @@
1
+ import type { CancelIntentRequestV2, CancelIntentResponseV2, RequestOverrideConfig } 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 useSwapsApiCancelIntent}. The per-request `apiConfig` override
9
+ * belongs here rather than at the hook level.
10
+ */
11
+ export type UseSwapsApiCancelIntentVars = {
12
+ body: CancelIntentRequestV2;
13
+ apiConfig?: RequestOverrideConfig;
14
+ };
15
+
16
+ /**
17
+ * React hook to build an unsigned cancel-intent transaction via the swaps API —
18
+ * `sodax.api.swaps.cancelIntent`. The `intent` carries `bigint` numerics. Returns `{ tx }` to sign
19
+ * and broadcast yourself; it does not change state, so no queries are invalidated.
20
+ *
21
+ * @example
22
+ * const { mutateAsync: cancelIntent } = useSwapsApiCancelIntent();
23
+ * const { tx } = await cancelIntent({ body: { srcChainKey: 'sonic', intent } });
24
+ */
25
+ export const useSwapsApiCancelIntent = ({
26
+ mutationOptions,
27
+ }: MutationHookParams<CancelIntentResponseV2, UseSwapsApiCancelIntentVars> = {}): SafeUseMutationResult<
28
+ CancelIntentResponseV2,
29
+ Error,
30
+ UseSwapsApiCancelIntentVars
31
+ > => {
32
+ const { sodax } = useSodaxContext();
33
+
34
+ return useSafeMutation<CancelIntentResponseV2, Error, UseSwapsApiCancelIntentVars>({
35
+ mutationKey: ['swapsApi', 'cancelIntent'],
36
+ retry: 3,
37
+ ...mutationOptions,
38
+ mutationFn: async ({ body, apiConfig }): Promise<CancelIntentResponseV2> =>
39
+ unwrapResult(await sodax.api.swaps.cancelIntent(body, apiConfig)),
40
+ });
41
+ };
@@ -0,0 +1,41 @@
1
+ import type { CreateIntentParamsV2, CreateIntentResponseV2, RequestOverrideConfig } 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 useSwapsApiCreateIntent}. The per-request `apiConfig` override
9
+ * belongs here rather than at the hook level.
10
+ */
11
+ export type UseSwapsApiCreateIntentVars = {
12
+ body: CreateIntentParamsV2;
13
+ apiConfig?: RequestOverrideConfig;
14
+ };
15
+
16
+ /**
17
+ * React hook to build an unsigned create-intent transaction via the swaps API —
18
+ * `sodax.api.swaps.createIntent`. Returns `{ tx, intent, relayData }` to sign and broadcast
19
+ * yourself; it does not change state, so no queries are invalidated.
20
+ *
21
+ * @example
22
+ * const { mutateAsync: createIntent } = useSwapsApiCreateIntent();
23
+ * const { tx, intent, relayData } = await createIntent({ body: createIntentParams });
24
+ */
25
+ export const useSwapsApiCreateIntent = ({
26
+ mutationOptions,
27
+ }: MutationHookParams<CreateIntentResponseV2, UseSwapsApiCreateIntentVars> = {}): SafeUseMutationResult<
28
+ CreateIntentResponseV2,
29
+ Error,
30
+ UseSwapsApiCreateIntentVars
31
+ > => {
32
+ const { sodax } = useSodaxContext();
33
+
34
+ return useSafeMutation<CreateIntentResponseV2, Error, UseSwapsApiCreateIntentVars>({
35
+ mutationKey: ['swapsApi', 'createIntent'],
36
+ retry: 3,
37
+ ...mutationOptions,
38
+ mutationFn: async ({ body, apiConfig }): Promise<CreateIntentResponseV2> =>
39
+ unwrapResult(await sodax.api.swaps.createIntent(body, apiConfig)),
40
+ });
41
+ };
@@ -0,0 +1,41 @@
1
+ import type { CreateLimitOrderParamsV2, CreateLimitOrderResponseV2, RequestOverrideConfig } 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 useSwapsApiCreateLimitOrder}. The per-request `apiConfig` override
9
+ * belongs here rather than at the hook level.
10
+ */
11
+ export type UseSwapsApiCreateLimitOrderVars = {
12
+ body: CreateLimitOrderParamsV2;
13
+ apiConfig?: RequestOverrideConfig;
14
+ };
15
+
16
+ /**
17
+ * React hook to build an unsigned create-limit-order-intent transaction via the swaps API —
18
+ * `sodax.api.swaps.createLimitOrderIntent` (same as create-intent but `deadline` is optional).
19
+ * Returns `{ tx, intent, relayData }`; it does not change state, so no queries are invalidated.
20
+ *
21
+ * @example
22
+ * const { mutateAsync: createLimitOrder } = useSwapsApiCreateLimitOrder();
23
+ * const { tx, intent, relayData } = await createLimitOrder({ body: createLimitOrderParams });
24
+ */
25
+ export const useSwapsApiCreateLimitOrder = ({
26
+ mutationOptions,
27
+ }: MutationHookParams<CreateLimitOrderResponseV2, UseSwapsApiCreateLimitOrderVars> = {}): SafeUseMutationResult<
28
+ CreateLimitOrderResponseV2,
29
+ Error,
30
+ UseSwapsApiCreateLimitOrderVars
31
+ > => {
32
+ const { sodax } = useSodaxContext();
33
+
34
+ return useSafeMutation<CreateLimitOrderResponseV2, Error, UseSwapsApiCreateLimitOrderVars>({
35
+ mutationKey: ['swapsApi', 'createLimitOrder'],
36
+ retry: 3,
37
+ ...mutationOptions,
38
+ mutationFn: async ({ body, apiConfig }): Promise<CreateLimitOrderResponseV2> =>
39
+ unwrapResult(await sodax.api.swaps.createLimitOrderIntent(body, apiConfig)),
40
+ });
41
+ };
@@ -0,0 +1,36 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { DeadlineQueryV2, DeadlineResponseV2, 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 UseSwapsApiDeadlineParams = ReadHookParams<
8
+ DeadlineResponseV2,
9
+ {
10
+ query?: DeadlineQueryV2;
11
+ apiConfig?: RequestOverrideConfig;
12
+ }
13
+ >;
14
+
15
+ /**
16
+ * React hook to compute a swap deadline (hub timestamp + `offsetSeconds`, default 300s) via the
17
+ * swaps API — `sodax.api.swaps.getDeadline`.
18
+ *
19
+ * @example
20
+ * const { data: deadline } = useSwapsApiDeadline({ params: { query: { offsetSeconds: 600 } } });
21
+ */
22
+ export const useSwapsApiDeadline = ({
23
+ params,
24
+ queryOptions,
25
+ }: UseSwapsApiDeadlineParams = {}): UseQueryResult<DeadlineResponseV2, Error> => {
26
+ const { sodax } = useSodaxContext();
27
+ const query = params?.query;
28
+ const apiConfig = params?.apiConfig;
29
+
30
+ return useQuery<DeadlineResponseV2, Error>({
31
+ queryKey: ['swapsApi', 'deadline', query?.offsetSeconds ?? null],
32
+ queryFn: async (): Promise<DeadlineResponseV2> => unwrapResult(await sodax.api.swaps.getDeadline(query, apiConfig)),
33
+ retry: 3,
34
+ ...queryOptions,
35
+ });
36
+ };
@@ -0,0 +1,40 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { GasEstimateRequestV2, GasEstimateResponseV2, 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 UseSwapsApiEstimateGasParams = ReadHookParams<
8
+ GasEstimateResponseV2 | undefined,
9
+ {
10
+ body: GasEstimateRequestV2 | undefined;
11
+ apiConfig?: RequestOverrideConfig;
12
+ }
13
+ >;
14
+
15
+ /**
16
+ * React hook to estimate gas for a raw transaction on a spoke chain via the swaps API —
17
+ * `sodax.api.swaps.estimateGas`. Returns `{ gas }` (chain-specific shape).
18
+ *
19
+ * @example
20
+ * const { data } = useSwapsApiEstimateGas({ params: { body: { chainKey: 'sonic', tx } } });
21
+ */
22
+ export const useSwapsApiEstimateGas = ({
23
+ params,
24
+ queryOptions,
25
+ }: UseSwapsApiEstimateGasParams = {}): UseQueryResult<GasEstimateResponseV2 | undefined, Error> => {
26
+ const { sodax } = useSodaxContext();
27
+ const body = params?.body;
28
+ const apiConfig = params?.apiConfig;
29
+
30
+ return useQuery({
31
+ queryKey: ['swapsApi', 'estimateGas', body?.chainKey, body?.tx],
32
+ queryFn: async (): Promise<GasEstimateResponseV2 | undefined> => {
33
+ if (!body) return undefined;
34
+ return unwrapResult(await sodax.api.swaps.estimateGas(body, apiConfig));
35
+ },
36
+ enabled: !!body,
37
+ retry: 3,
38
+ ...queryOptions,
39
+ });
40
+ };
@@ -0,0 +1,41 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { IntentStateV2, 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 UseSwapsApiFilledIntentParams = ReadHookParams<
8
+ IntentStateV2 | undefined,
9
+ {
10
+ txHash: string | undefined;
11
+ apiConfig?: RequestOverrideConfig;
12
+ }
13
+ >;
14
+
15
+ /**
16
+ * React hook to get the on-chain fill state for an intent by its hub-chain tx hash via the swaps
17
+ * API — `sodax.api.swaps.getFilledIntent`. Returns
18
+ * `{ exists, remainingInput, receivedOutput, pendingPayment }`.
19
+ *
20
+ * @example
21
+ * const { data: fill } = useSwapsApiFilledIntent({ params: { txHash: '0x123...' } });
22
+ */
23
+ export const useSwapsApiFilledIntent = ({
24
+ params,
25
+ queryOptions,
26
+ }: UseSwapsApiFilledIntentParams = {}): UseQueryResult<IntentStateV2 | undefined, Error> => {
27
+ const { sodax } = useSodaxContext();
28
+ const txHash = params?.txHash;
29
+ const apiConfig = params?.apiConfig;
30
+
31
+ return useQuery({
32
+ queryKey: ['swapsApi', 'filledIntent', txHash],
33
+ queryFn: async (): Promise<IntentStateV2 | undefined> => {
34
+ if (!txHash) return undefined;
35
+ return unwrapResult(await sodax.api.swaps.getFilledIntent(txHash, apiConfig));
36
+ },
37
+ enabled: !!txHash && txHash.length > 0,
38
+ retry: 3,
39
+ ...queryOptions,
40
+ });
41
+ };
@@ -0,0 +1,40 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { GetIntentResponseV2, 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 UseSwapsApiIntentParams = ReadHookParams<
8
+ GetIntentResponseV2 | undefined,
9
+ {
10
+ txHash: string | undefined;
11
+ apiConfig?: RequestOverrideConfig;
12
+ }
13
+ >;
14
+
15
+ /**
16
+ * React hook to look up an Intent struct by its hub-chain creation tx hash via the swaps API —
17
+ * `sodax.api.swaps.getIntent`. The decoded intent's bigint fields are returned as decimal strings.
18
+ *
19
+ * @example
20
+ * const { data: intent } = useSwapsApiIntent({ params: { txHash: '0x123...' } });
21
+ */
22
+ export const useSwapsApiIntent = ({
23
+ params,
24
+ queryOptions,
25
+ }: UseSwapsApiIntentParams = {}): UseQueryResult<GetIntentResponseV2 | undefined, Error> => {
26
+ const { sodax } = useSodaxContext();
27
+ const txHash = params?.txHash;
28
+ const apiConfig = params?.apiConfig;
29
+
30
+ return useQuery({
31
+ queryKey: ['swapsApi', 'intent', txHash],
32
+ queryFn: async (): Promise<GetIntentResponseV2 | undefined> => {
33
+ if (!txHash) return undefined;
34
+ return unwrapResult(await sodax.api.swaps.getIntent(txHash, apiConfig));
35
+ },
36
+ enabled: !!txHash && txHash.length > 0,
37
+ retry: 3,
38
+ ...queryOptions,
39
+ });
40
+ };
@@ -0,0 +1,41 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { IntentExtraDataRequestV2, IntentExtraDataResponseV2, 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 UseSwapsApiIntentExtraDataParams = ReadHookParams<
8
+ IntentExtraDataResponseV2 | undefined,
9
+ {
10
+ body: IntentExtraDataRequestV2 | undefined;
11
+ apiConfig?: RequestOverrideConfig;
12
+ }
13
+ >;
14
+
15
+ /**
16
+ * React hook to recover the relay extra data needed by `/swaps/intents/submit` via the swaps API —
17
+ * `sodax.api.swaps.getIntentSubmitTxExtraData`. Provide EITHER `txHash` OR `intent` in the body
18
+ * (whose `bigint` numerics are serialized by the SDK). Returns `{ address, payload }`.
19
+ *
20
+ * @example
21
+ * const { data } = useSwapsApiIntentExtraData({ params: { body: { txHash: '0x123...' } } });
22
+ */
23
+ export const useSwapsApiIntentExtraData = ({
24
+ params,
25
+ queryOptions,
26
+ }: UseSwapsApiIntentExtraDataParams = {}): UseQueryResult<IntentExtraDataResponseV2 | undefined, Error> => {
27
+ const { sodax } = useSodaxContext();
28
+ const body = params?.body;
29
+ const apiConfig = params?.apiConfig;
30
+
31
+ return useQuery({
32
+ queryKey: ['swapsApi', 'intentExtraData', body?.txHash ?? body?.intent?.intentId?.toString()],
33
+ queryFn: async (): Promise<IntentExtraDataResponseV2 | undefined> => {
34
+ if (!body || (!body.txHash && !body.intent)) return undefined;
35
+ return unwrapResult(await sodax.api.swaps.getIntentSubmitTxExtraData(body, apiConfig));
36
+ },
37
+ enabled: !!body && (!!body.txHash || !!body.intent),
38
+ retry: 3,
39
+ ...queryOptions,
40
+ });
41
+ };
@@ -0,0 +1,40 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { IntentHashResponseV2, IntentRequestV2, 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 UseSwapsApiIntentHashParams = ReadHookParams<
8
+ IntentHashResponseV2 | undefined,
9
+ {
10
+ intent: IntentRequestV2 | undefined;
11
+ apiConfig?: RequestOverrideConfig;
12
+ }
13
+ >;
14
+
15
+ /**
16
+ * React hook to compute the keccak256 hash of an Intent struct via the swaps API —
17
+ * `sodax.api.swaps.getIntentHash`. The `intent` carries `bigint` numerics. Returns `{ hash }`.
18
+ *
19
+ * @example
20
+ * const { data } = useSwapsApiIntentHash({ params: { intent } });
21
+ */
22
+ export const useSwapsApiIntentHash = ({
23
+ params,
24
+ queryOptions,
25
+ }: UseSwapsApiIntentHashParams = {}): UseQueryResult<IntentHashResponseV2 | undefined, Error> => {
26
+ const { sodax } = useSodaxContext();
27
+ const intent = params?.intent;
28
+ const apiConfig = params?.apiConfig;
29
+
30
+ return useQuery({
31
+ queryKey: ['swapsApi', 'intentHash', intent?.intentId?.toString()],
32
+ queryFn: async (): Promise<IntentHashResponseV2 | undefined> => {
33
+ if (!intent) return undefined;
34
+ return unwrapResult(await sodax.api.swaps.getIntentHash({ intent }, apiConfig));
35
+ },
36
+ enabled: !!intent,
37
+ retry: 3,
38
+ ...queryOptions,
39
+ });
40
+ };