// packages/dapp-kit/src/hooks/swap/useSwapApprove.ts import { useSodaxContext } from '../shared/useSodaxContext.js'; import { useQueryClient } from '@tanstack/react-query'; import type { CreateIntentParams, CreateLimitOrderParams, GetWalletProviderType, SpokeChainKey, TxReturnType, } from '@sodax/sdk'; 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 useSwapApprove}. Generic over `K extends SpokeChainKey` (defaults * to the full union). Sophisticated callers can lock K at the call site to narrow the * `walletProvider` and `params.srcChainKey` types. */ export type UseSwapApproveVars = { params: CreateIntentParams | CreateLimitOrderParams; walletProvider: GetWalletProviderType; }; /** * React hook for approving ERC-20 token spending (or trustline establishment) for a swap or * limit-order intent. * * Throws on SDK failure so React Query's native error model engages (`isError`, `error`, * `onError`, `retry`). Returns the unwrapped tx return value on success. Invalidates all * `['swap', 'allowance', ...]` queries so any pending allowance check refreshes. */ export function useSwapApprove({ mutationOptions, }: MutationHookParams, UseSwapApproveVars> = {}): SafeUseMutationResult< TxReturnType, Error, UseSwapApproveVars > { const { sodax } = useSodaxContext(); const queryClient = useQueryClient(); return useSafeMutation, Error, UseSwapApproveVars>({ mutationKey: ['swap', 'approve'], ...mutationOptions, mutationFn: async ({ params, walletProvider }) => unwrapResult( await sodax.swaps.approve({ params: params as CreateIntentParams, raw: false, walletProvider, }), ), onSuccess: async (data, vars, ctx) => { queryClient.invalidateQueries({ queryKey: ['swap', 'allowance'] }); await mutationOptions?.onSuccess?.(data, vars, ctx); }, }); }