Overview
Swap mode provides a flexible interface for cross-chain token exchanges. Users control both input and output, with the widget handling optimal routing across bridges and DEXs. Trade types:EXACT_INPUT and EXACT_OUTPUT.
Quick start
Props
Required
| Prop | Type | Description |
|---|---|---|
apiKey | string | Trails API key |
Source (optional)
Thefrom shape is a discriminated union on paymentMethod. For wallet, exchange, and crypto-transfer flows, source is crypto; for the fiat on-ramp arm, source is fiat.
When paymentMethod is "CONNECTED_WALLET", "EXCHANGE", "CRYPTO_TRANSFER", or omitted (crypto source):
| Prop | Type | Description |
|---|---|---|
from.token | string | Pre-select source ERC-20 token — symbol (e.g. "USDC") or contract address |
from.chain | ChainIdentifier | Pre-select source chain — name, ID, or viem Chain |
from.amount | string | number | Pre-fill source amount |
from.walletAddress | string | Source wallet address (defaults to connected wallet) |
from.exchange | string | Mesh exchange key (e.g. "coinbase"). EXCHANGE arm only. |
paymentMethod is "CREDIT_DEBIT_CARD" (fiat source):
| Prop | Type | Description |
|---|---|---|
from.currency | string | ISO 4217 fiat code, e.g. "USD", "EUR", "GBP" |
from.amount | string | number | Pre-fill fiat amount |
Destination (optional)
| Prop | Type | Description |
|---|---|---|
to.token | string | Pre-select or lock destination token — symbol or contract address |
to.chain | ChainIdentifier | Pre-select or lock destination chain |
to.amount | string | number | Pre-fill destination amount |
to.defaultToken | string | Default destination token — user can change |
to.defaultChain | ChainIdentifier | Default destination chain — user can change |
to.calldata | string | ABI-encoded calldata to execute after the swap lands |
to.supportedChains | ChainIdentifier[] | Restrict destination chains the user can pick |
to.supportedTokensByChain | SupportedTokensByChainConfig[] | Per-chain destination token allowlist |
Payment method (optional)
paymentMethod controls the source funding method. Omit to default to the connected wallet.
| Value | Method |
|---|---|
"CONNECTED_WALLET" | Connected wallet (default) |
"CRYPTO_TRANSFER" | QR code / address deposit |
"CREDIT_DEBIT_CARD" | Fiat on-ramp |
"EXCHANGE" | CEX transfer |
Route options (optional)
| Prop | Type | Description |
|---|---|---|
slippageTolerance | number | string | Slippage tolerance (e.g. 0.005 for 0.5%) |
swapProvider | RouteProvider | Preferred same-chain swap provider |
bridgeProvider | RouteProvider | Preferred bridge provider |
swapProviderFallback | boolean | Fall back if preferred swap provider is unavailable |
bridgeProviderFallback | boolean | Fall back if preferred bridge provider is unavailable |
Lifecycle callbacks
| Callback | Signature | When it fires |
|---|---|---|
onSwapStart | ({ sessionId }) => void | User begins the swap flow |
onSwapSuccess | ({ sessionId }) => void | Swap completes successfully |
onSwapError | ({ sessionId, error }) => void | Swap encounters an error |
Examples
Basic swap — user picks everything
Pre-configured cross-chain swap
Lock destination, let user pick source
Cross-chain swap with a fixed output amount
Custom bridge provider
Headless implementation
For custom UI, use theuseQuote hook directly:
This example reads
walletClient and address from wagmi hooks. Since 0xtrails@0.16.0, wagmi is opt-in — install @0xtrails/adapter-wagmi and configure wagmiAdapter to keep using wagmi alongside Trails. See Wallet Integration.Trade Types
EXACT_INPUT
User specifies exact input amount; output amount varies:EXACT_OUTPUT
User specifies exact output amount; input amount varies:Quote Types
Quote Refresh Strategy
Quotes can become stale. Implement refresh logic:Event Handling
Widget Events
useQuote Transaction Status
Error Handling
Use Cases
- Cross-chain Token Exchange: Swap any token to any other token across chains
- Portfolio Rebalancing: Shift holdings between chains and tokens
- Yield Optimization: Move assets to chains with better yields
- Gas Token Acquisition: Get native tokens for new chains
- Arbitrage: Take advantage of price differences across chains
Technical Notes
- Quotes automatically factor in gas costs and fees
- Multiple liquidity sources are queried for optimal routing
- Supports both same-chain and cross-chain swaps
- Native token wrapping/unwrapping is handled automatically
- Slippage protection is built-in
See Also
- Swap use cases — portfolio rebalancing, stablecoin conversion
- Hooks reference —
useQuotedetails - Quote providers — CCTP, Relay, SushiSwap, and more