// packages/dapp-kit/src/hooks/swap/useSwap.ts import { useSodaxContext } from '../shared/useSodaxContext.js'; import type { SpokeChainKey, SwapActionParams, SwapResponse } from '@sodax/sdk'; import { useQueryClient } from '@tanstack/react-query'; import type { MutationHookParams } from '../shared/types.js'; import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js'; import { unwrapResult } from '../shared/unwrapResult.js'; /** * Mutation variables for {@link useSwap}. Generic over `K extends SpokeChainKey` (defaults to the * full union). Sophisticated callers can lock K at the hook call site to narrow the * `walletProvider` and `params.srcChainKey` types. */ export type UseSwapVars = Omit, 'raw'>; /** * React hook for executing an intent-based cross-chain swap. * * Throws on SDK failure so React Query's native error model engages (`isError`, `error`, * `onError`, `retry`). Returns the unwrapped `SwapResponse` on success. */ export function useSwap({ mutationOptions, }: MutationHookParams> = {}): SafeUseMutationResult< SwapResponse, Error, UseSwapVars > { const { sodax } = useSodaxContext(); const queryClient = useQueryClient(); return useSafeMutation>({ mutationKey: ['swap'], ...mutationOptions, mutationFn: async vars => unwrapResult(await sodax.swaps.swap({ ...vars, raw: false })), onSuccess: async (data, vars, ctx) => { queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', vars.params.srcChainKey] }); queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', vars.params.dstChainKey] }); await mutationOptions?.onSuccess?.(data, vars, ctx); }, }); }