// packages/dapp-kit/src/hooks/partner/useSetSwapPreference.ts import type { HubChainKey, SetSwapPreferenceAction, SpokeChainKey, TxReturnType } from '@sodax/sdk'; import { useQueryClient } from '@tanstack/react-query'; import { useSodaxContext } from '../shared/useSodaxContext.js'; 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 useSetSwapPreference}. Generic over `K extends SpokeChainKey` * for compatibility with the SDK signature; at runtime the SDK enforces hub-only * (`isHubChainKeyType(srcChainKey)`). */ export type UseSetSwapPreferenceVars = Omit< SetSwapPreferenceAction, 'raw' >; /** * React hook to set the partner's auto-swap preferences (output token + destination chain + * destination address) on the protocol-intents contract. * * Throws on SDK failure so React Query's native error model engages (`isError`, `error`, * `onError`, `retry`). Returns the unwrapped tx return value on success. */ export function useSetSwapPreference({ mutationOptions, }: MutationHookParams, UseSetSwapPreferenceVars> = {}): SafeUseMutationResult< TxReturnType, Error, UseSetSwapPreferenceVars > { const { sodax } = useSodaxContext(); const queryClient = useQueryClient(); return useSafeMutation, Error, UseSetSwapPreferenceVars>({ mutationKey: ['partner', 'setSwapPreference'], ...mutationOptions, mutationFn: async vars => unwrapResult(await sodax.partners.feeClaim.setSwapPreference({ ...vars, raw: false })), onSuccess: async (data, vars, ctx) => { queryClient.invalidateQueries({ queryKey: ['partner', 'feeClaim', 'autoSwapPreferences', (vars.params as { srcAddress: string }).srcAddress], }); await mutationOptions?.onSuccess?.(data, vars, ctx); }, }); } // Helper alias for the common Sonic-only call shape. export type UseSetSwapPreferenceVarsHub = UseSetSwapPreferenceVars;