Overview
TheSwapInterface component provides a complete trading interface for placing limit and market orders. It handles order creation, fee calculation, and position sizing with real-time price updates.
Props
The market identifier for the trading pair (e.g., “SOL_USDC”)
Component State
The component manages the following state:Trading Mode & Order Type
Trading Mode & Order Type
- isBuyMode (
boolean): Toggles between buy and sell mode - orderType (
string): Order type - “Limit” or “Market”
Price & Size
Price & Size
- limitPrice (
number): Price for limit orders (defaults to current market price) - size (
string): Trade size in base asset units - maxUSD (
number): USD value calculated from price × size
Calculations
Calculations
- fees (
number): Trading fees at 0.1% (calculated asmaxUSD * 0.001) - position (
number): Position size in base asset
Key Features
Order Creation Flow
The component follows this flow when creating orders:- Validation: Checks quantity > 0 and valid limit price
- Order Construction: Builds order object with market, side, quantity, price
- API Call: Sends order via
createOrder()utility - Feedback: Shows success/error toast notifications
User ID is retrieved from
localStorage.getItem("user_id") with fallback to "test_user"Real-time Calculations
The component recalculates values when size or price changes:Usage Example
Implementation Details
Context Integration
The component usesTradesContext to access current market price:
Order Object Structure
Source Code Reference
See implementation in:apps/web/src/components/SwapInterface.tsx:1-290
- Component Structure
- Styling States
- Buy/Sell mode toggle
- Order type selector (Limit/Market)
- Price input (conditional based on order type)
- Size and USD value inputs (bidirectional sync)
- Fee and position display
- Submit button with dynamic styling