SwapForm component provides a full-featured swap interface for exchanging tokens across chains. It handles quote fetching, balance checking, and swap execution with real-time price updates.
Import
Overview
The SwapForm is a complex, self-contained component that orchestrates the entire swap workflow:- Asset Selection: Choose source and target tokens from available assets
- Amount Input: Enter swap amounts with balance validation
- Quote Fetching: Automatically fetches and displays real-time quotes
- Balance Management: Shows balances and provides percentage shortcuts (25%, 50%, 75%, MAX)
- Transaction Execution: Handles the complete swap flow with status tracking
- Error Handling: Displays meaningful errors and validation messages
Component Structure
This component does not accept props - it’s a standalone form that manages its own state.Key Features
Asset Management
The component maintains state for source and destination assets:Balance Validation
Built-in balance checking prevents insufficient balance errors:Debounced Quote Fetching
Quotes are fetched with a 1-second debounce to reduce API calls:Usage Example
State Management
The component uses several hooks for state management:Fetches available assets from the API
Loads supported blockchain networks
Manages quote fetching and execution
Retrieves user balances for the predicted address
Provides access to the embedded wallet instance
User Interactions
Percentage Buttons
The source token input includes quick-select buttons for common percentages:- 25%: Swap 25% of available balance
- 50%: Swap 50% of available balance
- 75%: Swap 75% of available balance
- MAX: Swap entire balance
Swap Direction Toggle
Users can reverse the swap direction by clicking the swap icon button between inputs:Quote Lifecycle
1. Quote Request
Triggered when amount, source asset, or target asset changes:2. Quote Display
Shows quote details including:- Exchange rate
- Token prices
- Quote expiration countdown
- Destination amount
3. Quote Execution
Executes the swap when the user clicks the swap button:Button States
The swap button dynamically updates based on the application state:| State | Button Text | Disabled |
|---|---|---|
| Not authenticated | ”Login to Swap” | Yes |
| Loading quote | ”Getting Quote…” | Yes |
| Executing | ”Executing Swap…” | Yes |
| Insufficient balance | ”Insufficient Balance” | Yes |
| Ready | ”Swap” | No |
Error Handling
API Errors
Displays API errors in an alert component:Quote Errors
Shows specific quote-related errors:Loading States
The component shows appropriate loading indicators:Transaction Completion
After a successful swap, the component:- Clears the form inputs
- Resets the quote state
- Refreshes user balances
Onboarding Support
The component includes data attributes for onboarding tooltips:data-onboarding="main-card": Main card containerdata-onboarding="from-token": Source token inputdata-onboarding="to-token": Destination token inputdata-onboarding="swap-button": Execute swap buttondata-onboarding="quote-details": Quote information display
Dependencies
Related Components
- TokenInput - Asset selection and amount input
- QuoteDetails - Quote information display
- TransferForm - Cross-chain token transfers