Overview
TheDepth component displays market depth information through two tabs: an order book view and a recent trades view. It maintains real-time data via WebSocket subscriptions and manages order book state through React context.
Props
The market identifier for the trading pair (e.g., “SOL_USDC”)
Component State
Controls which tab is displayed:
"orderbook": Shows the OrderBook component"recentTrades": Shows the RecentTrades component
WebSocket Integration
Depth Updates
Subscribes todepth.{market} stream for order book updates:
Depth Callback Logic
Depth Callback Logic
The component processes incremental depth updates:
- Update existing orders: Matches by price level and updates quantity
- Remove zero quantities: Filters out orders with quantity = 0
- Add new orders: Inserts new price levels not in current book
- Sort and trim: Maintains top 30 bids (descending) and asks (ascending)
Trade Updates
Subscribes totrade.{market} stream for recent trades:
Trade Callback Logic
Trade Callback Logic
Processes incoming trade data:
Context Dependencies
The component usesTradesContext to manage shared state:
Updates the trades array with new trade data
Updates the bids array (buy orders)
Updates the asks array (sell orders)
Updates the current market price
Sets total quantity of all bids
Sets total quantity of all asks
Reference to order book container for scroll control
Initial Data Loading
REST API Initialization
The component fetches initial data on mount:- Trades
- Order Book
Cleanup & Unsubscription
The component properly cleans up WebSocket subscriptions:Child Components
OrderBook
Displays bid/ask ladder with:- Price levels
- Quantities at each level
- Cumulative depth visualization
- Re-center button
apps/web/src/components/depth/OrderBook.tsx
RecentTrades
Shows trade history with:- Trade price (color-coded by side)
- Trade size
- Timestamp
apps/web/src/components/depth/RecentTrades.tsx
Usage Example
The component must be wrapped in a
TradesProvider to access the required context.Order Book Data Format
Bids and asks are arrays of tuples:Trade Data Format
Source Code Reference
See implementation in:apps/web/src/components/Depth.tsx:1-258