Import

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

Usage

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

function SwapComponent() {
  const { data: swapQuote, isLoading } = useGetSwapQuote({
    userAddress: '0x123...',
    buyCurrencyAddress: '0x456...',
    sellCurrencyAddress: '0x789...',
    buyAmount: '1000000000000000000', 
    chainId: 1,
    includeApprove: true
  })

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

  return (
    <div>
      {swapQuote && (
        <div>
          Currency: {swapQuote.currencyAddress}
          Price: {swapQuote.price}
          Max Price: {swapQuote.maxPrice}
          Transaction Value: {swapQuote.transactionValue}
        </div>
      )}
    </div>
  )
}

Return Type: UseQueryResult<SwapQuote>

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

interface SwapQuote {
  currencyAddress: string
  currencyBalance: string
  price: string
  maxPrice: string
  to: string
  transactionData: string
  transactionValue: string
  approveData: 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 for the swap
  • transactionData: Encoded transaction data for executing the swap
  • transactionValue: The value to be sent with the transaction
  • approveData: Encoded approval transaction data (if includeApprove is true)

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: UseGetSwapQuoteArgs

interface UseGetSwapQuoteArgs {
  userAddress: string
  buyCurrencyAddress: string
  sellCurrencyAddress: string
  buyAmount: string
  chainId: number
  includeApprove?: boolean
  slippagePercentage?: number
}
ParameterTypeDescription
userAddressstringThe address of the user’s wallet
buyCurrencyAddressstringThe address of the currency to buy
sellCurrencyAddressstringThe address of the currency to sell
buyAmountstringThe amount of currency to buy (in base units)
chainIdnumberThe chain ID where the swap will occur
includeApproveboolean(Optional) Whether to include approval transaction data
slippagePercentagenumber(Optional) Maximum allowed slippage percentage

options: HooksOptions

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