// packages/dapp-kit/src/hooks/mm/useWithdraw.ts import type { MoneyMarketWithdrawActionParams, SpokeChainKey, TxHashPair } 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 useWithdraw}. 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 UseWithdrawVars = Omit< MoneyMarketWithdrawActionParams, 'raw' >; /** * React hook for withdrawing supplied tokens from the Sodax money market protocol. * * Throws on SDK failure so React Query's native error model engages (`isError`, `error`, * `onError`, `retry`). Returns the unwrapped `TxHashPair` on success. */ export function useWithdraw({ mutationOptions, }: MutationHookParams> = {}): SafeUseMutationResult> { const { sodax } = useSodaxContext(); const queryClient = useQueryClient(); return useSafeMutation>({ mutationKey: ['mm', 'withdraw'], ...mutationOptions, mutationFn: async vars => unwrapResult(await sodax.moneyMarket.withdraw({ ...vars, raw: false })), onSuccess: async (data, vars, ctx) => { const { params } = vars; queryClient.invalidateQueries({ queryKey: ['mm', 'userReservesData', params.srcChainKey, params.srcAddress] }); queryClient.invalidateQueries({ queryKey: ['mm', 'userFormattedSummary', params.srcChainKey, params.srcAddress], }); queryClient.invalidateQueries({ queryKey: ['mm', 'aTokensBalances'] }); const balanceChains = new Set([params.srcChainKey, params.dstChainKey ?? params.srcChainKey]); for (const chainKey of balanceChains) { queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', chainKey] }); } await mutationOptions?.onSuccess?.(data, vars, ctx); }, }); }