Skip to main content
function useGetSwapPrice(options: UseGetSwapPriceOptions): UseGetSwapPriceReturnType;
Reactive query hook that fetches a non-binding indicative price for a token swap. Automatically fetches when the user is signed in and all required parameters are provided. Includes preflight issues in the response (e.g. insufficient balance or allowance). When the token pair, network, account, or userId changes, previous data is cleared. When only the amount or slippage changes, previous data is preserved while the new price loads (stale-while-revalidate).

Parameters

ParameterTypeDescription
optionsUseGetSwapPriceOptionsConfiguration for the swap price query.

Returns

UseGetSwapPriceReturnType An object containing the swap price data, status, error, refetch, and reset functions.

Example

import { useGetSwapPrice } from "@coinbase/cdp-hooks";

function SwapPriceDisplay() {
  const [fromAmount, setFromAmount] = useState("");

  const { data, status, error, refetch, reset } = useGetSwapPrice({
    network: "base",
    fromToken: "0x4200000000000000000000000000000000000006",
    toToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
    fromAmount,
  });

  return (
    <div>
      <input
        value={fromAmount}
        onChange={(e) => setFromAmount(e.target.value)}
        placeholder="0"
      />
      {status === "pending" && <p>Loading price...</p>}
      {data?.liquidityAvailable && <p>Expected output: {data.toAmount}</p>}
      {error && <p>Error: {error.message}</p>}
      <button onClick={refetch} disabled={status === "pending"}>Refresh</button>
      <button onClick={reset}>Clear</button>
    </div>
  );
}