Skip to main content

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

User picks the amount in the UI.

Props

Required

Source (optional)

The from 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): When paymentMethod is "CREDIT_DEBIT_CARD" (fiat source):

Destination (optional)

Payment method (optional)

paymentMethod controls the source funding method. Omit to default to the connected wallet.

Route options (optional)

Lifecycle callbacks

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 the useQuote 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 SDK Hooks.

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