Hooks
useGetSwapRoutes
Hook for fetching available routes for a swap
Import
This hook replaces the useGetSwapPrices
hook which was removed in v5.2.3.
Usage
Return Type: UseQueryResult<LifiSwapRoute[]>
The hook returns all properties from React Query’s UseQueryResult
with swap routes data. Here’s the detailed structure of the LifiSwapRoute
object:
Properties
data
LifiSwapRoute[] | undefined
Array of swap route objects containing:
route
fromChainId
: The chain ID of the token to selltoChainId
: The chain ID of the token to buyfromTokens
: Array of tokens that can be used to pay for the swaptoTokens
: Array of tokens that can be received from the swap
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: UseGetSwapRoutesArgs
Parameter | Type | Description |
---|---|---|
walletAddress | string | The address of the user’s wallet |
toTokenAddress | string | The address of the token to buy |
chainId | number | The chain ID where the swap will occur |
toTokenAmount | string | The amount of token to buy (in wei) |
options: HooksOptions
Parameter | Type | Description |
---|---|---|
disabled | boolean | (Optional) Disable the query from automatically running |
retry | boolean | (Optional) Whether to retry failed queries (defaults to true) |
Additional Notes
- This hook uses React Query to fetch and cache swap routes from the Sequence API.
- Stale time is set to one hour by default to avoid refreshing quotes while the user is completing transactions.
- This hook will not return “fromTokens” that the user does not have in their wallet.