Import

import { useGetSwapQuote } from '@0xsequence/hooks'

Usage

import { useGetSwapQuote } from '@0xsequence/hooks'

function SwapComponent() {
  const { data: swapQuote, isLoading } = useGetSwapQuote({
    params: {
      walletAddress: '0x123...',
      fromTokenAddress: '0x456...',
      toTokenAddress: '0x789...',
      toTokenAmount: '1000000000000000000', // amount to buy in wei
      chainId: 1,
      includeApprove: true,
      slippageBps: 100 // 1% slippage
    }
  })

  if (isLoading) return <div>Loading...</div>

  return (
    <div>
      {swapQuote && (
        <div>
          Price: {swapQuote.price}
          Max Price: {swapQuote.maxPrice}
          Transaction Value: {swapQuote.transactionValue}
          <button onClick={() => executeSwap(swapQuote)}>Swap</button>
        </div>
      )}
    </div>
  )
}

Return Type: UseQueryResult<LifiSwapQuote>

The hook returns all properties from React Query’s UseQueryResult with swap quote data. Here’s the detailed structure of the swapQuote.

interface LifiSwapQuote {
    currencyAddress: string;
    currencyBalance: string;
    price: string;
    maxPrice: string;
    to: string;
    transactionData: string;
    transactionValue: string;
    approveData: string;
    amount: string;
    amountMin: string;
}

Properties

data

SwapQuote | undefined

The swap quote object containing:

  • currencyAddress: Address of the currency to be swapped
  • currencyBalance: Balance of the currency in the user’s wallet
  • price: The current price for the swap
  • maxPrice: Maximum price allowed for the swap (includes slippage)
  • to: The target contract address that handles the swap
  • transactionData: Encoded transaction data for executing the swap
  • transactionValue: The value to be sent with the transaction (for native tokens)
  • approveData: Encoded approval transaction data (if includeApprove is true and needed)
  • amount: The amount of currency to be received
  • amountMin: The minimum amount to be received after slippage

isLoading

boolean

Loading state for the data fetch.

isError

boolean

Error state indicating if the query failed.

error

Error | null

Any error that occurred during data fetching.

Parameters

The hook accepts two parameters:

args: GetLifiSwapQuoteArgs

interface GetLifiSwapQuoteArgs {
  params: GetLifiSwapQuoteParams
}

interface GetLifiSwapQuoteParams {
    chainId: number;
    walletAddress: string;
    fromTokenAddress: string;
    toTokenAddress: string;
    fromTokenAmount?: string;
    toTokenAmount?: string;
    includeApprove: boolean;
    slippageBps: number;
}
ParameterTypeDescription
params.chainIdnumberThe chain ID where the swap will occur
params.walletAddressstringThe address of the user’s wallet
params.fromTokenAddressstringThe address of the token to sell
params.toTokenAddressstringThe address of the token to buy
params.fromTokenAmountstring(Optional) The amount of token to sell (in wei)
params.toTokenAmountstring(Optional) The amount of token to buy (in wei)
params.includeApprovebooleanWhether to include approval transaction data
params.slippageBpsnumberMaximum allowed slippage in basis points (100 = 1%)

Note: You must provide either fromTokenAmount or toTokenAmount, but not both.

options: HooksOptions

interface HooksOptions {
  disabled?: boolean
  retry?: boolean
}
ParameterTypeDescription
disabledboolean(Optional) Disable the query from automatically running
retryboolean(Optional) Whether to retry failed queries (defaults to true)