Prerequisites
Before this page: a connected or embedded wallet (see Connecting external wallets).What you’ll build
A swap flow has four steps:- Quote — ask for a route between two tokens.
- Show the estimated amount received and gas cost.
- Execute — sign and submit from the user’s wallet.
- Track — poll until the swap is done.
1000000 for 1 USDC with 6 decimals).
Choosing tokens
Build your token pickers fromgetSwapChains and getSwapTokens instead of hardcoding addresses. getSwapChains lists the networks that support swaps, and getSwapTokens lists the tokens on one network or searches them by name, symbol, or address. Each token includes the address and decimals you need for the quote.
For the token the user swaps from, show only the listed tokens the wallet holds, using its token balances. This keeps unsupported and spam tokens out of the picker. A listed token can be swapped on its network, but a specific pair is not guaranteed, so the quote remains the final check.
- TypeScript
- React
Quoting a swap
getSwapQuote takes a from and to side. Set from.amount to quote by input (or to.amount to quote by desired output — they’re mutually exclusive). The response includes the signingPayload you’ll execute and the estimated to.amount.
- TypeScript
- React
Executing the swap
Pass the quote’ssigningPayload to executeSwapTransaction. ERC-20 swaps may need a separate approval first — onStepChange tells you which step is running so you can update the UI.
- TypeScript
- React
Tracking status
A swap can take time to settle — especially across chains. PollgetSwapStatus with the transaction hash until the status is DONE or FAILED.
- TypeScript
- React
Re-check on an interval and resolve only once the swap reaches a terminal status.
Handling errors
See also
- Transaction confirmation & simulation — confirm the underlying transaction
- Token balances & display — reflect balances after the swap
- Funding flows — get tokens to swap