Import

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

Usage

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

function SwapComponent() {
  const { data: swapPrices, isLoading } = useGetSwapPrices({
    userAddress: '0x123...',
    buyCurrencyAddress: '0x456...',
    buyAmount: '1000000000000000000', 
    chainId: 1,
    withContractInfo: true
  })

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

  return (
    <div>
      {swapPrices?.map(swap => (
        <div key={swap.info?.address}>
          Token: {swap.info?.symbol}
          Price: {swap.price.price}
          Balance: {swap.balance.balance}
        </div>
      ))}
    </div>
  )
}

Return Type: UseQueryResult<SwapPricesWithCurrencyInfo[]>

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

interface SwapPrice {
    currencyAddress: string;
    currencyBalance: string;
    price: string;
    maxPrice: string;
    transactionValue: string;
}

interface Balance {
  balance: string
}

interface ContractInfo {
    chainId: number;
    address: string;
    source: string;
    name: string;
    type: string;
    symbol: string;
    decimals?: number;
    logoURI: string;
    deployed: boolean;
    bytecodeHash: string;
    extensions: ContractInfoExtensions;
    updatedAt: string;
    notFound: boolean;
    queuedAt?: string;
    status: ResourceStatus;
}

enum ResourceStatus {
    NOT_AVAILABLE = "NOT_AVAILABLE",
    STALE = "STALE",
    AVAILABLE = "AVAILABLE"
}

interface ContractInfoExtensions {
    link: string;
    description: string;
    categories: Array<string>;
    ogImage: string;
    ogName: string;
    originChainId: number;
    originAddress: string;
    blacklist: boolean;
    verified: boolean;
    verifiedBy: string;
    featured: boolean;
}

type SwapPricesWithCurrencyInfo = {
  price: SwapPrice
  info: ContractInfo | undefined
  balance: Balance
}

Properties

data

SwapPricesWithCurrencyInfo[] | undefined

Array of swap price objects containing:

price (SwapPrice)
  • currencyAddress: Address of the currency
  • currencyBalance: Balance of the currency
  • price: The swap price
  • maxPrice: The maximum price for the swap
  • transactionValue: The value of the transaction
info (ContractInfo)
  • chainId: Chain ID where the token exists
  • address: Token contract address
  • source: Source of the token information
  • name: Token name
  • type: Token type
  • symbol: Token symbol
  • decimals: Token decimals
  • logoURI: Token logo URL
  • deployed: Whether the token is deployed
  • bytecodeHash: Hash of the token’s bytecode
  • extensions: Additional token metadata
  • updatedAt: Last update timestamp
  • notFound: Whether token was not found
  • queuedAt: When token was queued for update
  • status: Token resource status
balance (Balance)
  • balance: User’s balance of the currency in base units

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

interface UseGetSwapPricesArgs {
  userAddress: string
  buyCurrencyAddress: string
  buyAmount: string
  chainId: number
  withContractInfo?: boolean
}
ParameterTypeDescription
userAddressstringThe address of the user’s wallet
buyCurrencyAddressstringThe address of the currency to buy
buyAmountstringThe amount of currency to buy (in base units)
chainIdnumberThe chain ID where the swap will occur
withContractInfoboolean(Optional) Whether to fetch additional contract info for each currency

options: HooksOptions

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