A high-performance centralized cryptocurrency exchange built on Solana with MPC (Multi-Party Computation) wallet management. This exchange provides real-time order matching, depth management, and trade execution with low latency and high throughput.
This is a professional-grade centralized exchange (CEX) that provides:
- Real-time order matching using an in-memory matching engine
- MPC-based wallet management for enhanced security
- WebSocket real-time updates for trades, order depth, and market data
- High-performance architecture with async processing and Redis pub/sub
- TimescaleDB for time-series data storage (trades, orders, klines)
- Multiple market support with configurable precision and limits
The exchange follows a microservices architecture with four main services:
βββββββββββββββ βββββββββββββββ βββββββββββββββ βββββββββββββββ
β Client ββββββΆβ API Server ββββββΆβ Engine ββββββΆβ WS β
β (Browser) βββββββ (REST) βββββββ (Matching) ββββββΆβ (Real-time)β
βββββββββββββββ βββββββββββββββ βββββββββββββββ βββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β Redis β β Redis β β Redis β
β (Queue) β β (Pub/Sub) β β (DB Q) β
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β
βΌ
βββββββββββββββ
β Database β
β (Timescale) β
βββββββββββββββ
- User submits order β API validates and queues to Engine
- Engine processes β Matches order, updates balances, creates fills
- Real-time updates β Engine publishes to Redis pub/sub β WS broadcasts to clients
- Persistence β Engine queues to DB processor β TimescaleDB stores trades/orders
CEX/
βββ cex-be/ # Backend services (Rust)
β βββ api/ # REST API Server
β β βββ src/
β β βββ main.rs # Server entry point (port 3010)
β β βββ redismanager.rs # Redis client for API-Engine communication
β β βββ auth_service.rs # JWT authentication logic
β β βββ middleware.rs # Auth middleware, request validation
β β βββ validation.rs # Order validation, market checks
β β βββ types.rs # Request/response types
β β βββ routes/ # API endpoints
β β βββ auth.rs # /api/v1/auth - login, register
β β βββ order.rs # /api/v1/order - create, cancel, get open orders
β β βββ markets.rs # /api/v1/markets - list markets
β β βββ depth.rs # /api/v1/depth - order book snapshot
β β βββ trades.rs # /api/v1/trades - trade history
β β βββ ticker.rs # /api/v1/tickers - 24h stats
β β βββ klines.rs # /api/v1/klines - OHLCV candles
β β
β βββ engine/ # Matching Engine
β β βββ src/
β β βββ main.rs # Engine entry point - listens to Redis queue
β β βββ engine.rs # Core matching logic (705 lines)
β β βββ orderbook.rs # Order book data structure (bids/asks)
β β βββ redis_manager.rs # Redis clients (3 instances: queue, pubsub, db)
β β βββ types.rs # Internal message types
β β
β βββ ws/ # WebSocket Server
β β βββ src/
β β βββ main.rs # WS server entry point (port 8000)
β β βββ subscription_manager.rs # Manages user subscriptions to channels
β β βββ user_manager.rs # Maps user IDs to WebSocket connections
β β βββ user.rs # User connection state
β β βββ types.rs # WS message types
β β
β βββ db/ # Database Layer
β β βββ src/
β β βββ lib.rs # DB pool, message processing
β β βββ schema.rs # Diesel ORM schema definitions
β β βββ model.rs # Database models (User, Trade, Order, Market, UserAsset)
β β βββ start/
β β βββ db.rs # DB processor main - consumes db_processor queue
β β
β βββ docker/ # Docker configuration
β β βββ docker-compose.yml # TimescaleDB + Redis containers
β β βββ clear_data.sh # Script to truncate tables and flush Redis
β β
β βββ env.example # Environment variables template
β βββ Cargo.toml # Workspace configuration
β
βββ cex-fe/ # Frontend (Next.js + TypeScript)
β βββ app/
β β βββ page.tsx # Landing page
β β βββ login/ # Authentication pages
β β βββ trade/[market]/ # Dynamic trading page for each market
β β βββ components/
β β β βββ TradeView.tsx # Main trading interface
β β β βββ SwapUI.tsx # Swap interface
β β β βββ MarketBar.tsx # Market selector
β β β βββ depth/ # Order book depth components
β β β βββ home/Trades.tsx # Recent trades display
β β βββ context/
β β β βββ MarketContext.tsx # Global market state
β β β βββ UserContext.tsx # User authentication state
β β βββ utils/
β β βββ httpClient.ts # Axios wrapper for API calls
β β βββ wsClient.ts # WebSocket client wrapper
β β βββ ChartManager.ts # Trading chart integration
β β βββ types.ts # TypeScript type definitions
β βββ components/ui/ # Reusable UI components (shadcn)
β
βββ README.md # This file
- Purpose: HTTP REST API for all client requests
- Port: 3010
- Tech: Rust (Poem web framework)
- Key Responsibilities:
- User authentication (JWT)
- Order submission and validation
- Market data retrieval
- Communicates with Engine via Redis queue (
messages) - CORS enabled for frontend integration
- Purpose: Core order matching and trade execution
- Tech: Rust
- Key Responsibilities:
- Listens to Redis queue for orders
- Maintains in-memory order books per market
- Matches buy/sell orders (price-time priority)
- Manages user balances (available/locked)
- Publishes real-time updates to WS via Redis pub/sub
- Queues persistence events to DB processor
- Supports: CREATE_ORDER, CANCEL_ORDER, GET_DEPTH, GET_OPEN_ORDERS
- Purpose: Real-time data streaming to clients
- Port: 8000
- Tech: Rust (tokio-tungstenite)
- Key Responsibilities:
- Manages WebSocket connections per user
- Subscribes to Redis channels (e.g.,
trade@BTC-USD,depth@BTC-USD) - Broadcasts trades, depth updates to subscribed clients
- Handles subscription/unsubscription logic
- Purpose: Data persistence and time-series storage
- Tech: TimescaleDB (PostgreSQL extension), Diesel ORM
- Key Responsibilities:
- Consumes DB queue from Engine
- Stores trades, orders, market data
- Provides OHLCV data for charts (klines)
- Used by API for historical queries
- Purpose: User-facing trading interface
- Tech: Next.js 14, TypeScript, Tailwind CSS
- Features:
- Trading view with order book, charts, trade history
- User authentication
- Real-time market data via WebSocket
- Responsive design
1. Client β POST /api/v1/order β API Server
2. API validates order (price, quantity, market, user balance)
3. API β LPUSH "messages" β Redis Queue
4. Engine β BRPOP "messages" β Redis Queue
5. Engine matches order against orderbook
6. Engine β PUBLISH "trade@BTC-USD" β Redis Pub/Sub
7. Engine β RPUSH "db_events" β Redis DB Queue
8. WS β Receives pub/sub message β Redis
9. WS β Broadcasts to all subscribed clients
10. Client β Receives trade update via WebSocket
1. Client β GET /api/v1/depth?market=BTC-USD β API
2. API β LPUSH "messages" β Redis Queue
3. Engine pops message, queries in-memory orderbook
4. Engine β PUBLISH response to API's unique channel
5. API receives response, returns to client
- users: User accounts with encrypted JWT tokens
- markets: Supported trading pairs (e.g., BTC-USD) with precision/limits
- orders: Historical and active orders
- trades: Executed trades with fill details
- user_assets: User balances per asset (base/quote balances)
- JWT Authentication: All protected endpoints require valid JWT token
- Order Validation: Price/quantity validation before processing
- Balance Checks: Engine verifies sufficient funds before locking
- MPC Wallet Management: (Planned) Secure multi-party computation for private keys
- Rate Limiting: (Planned) Prevent abuse on public endpoints
- Install Rust toolchain
- Install Node.js and npm
- Set up TimescaleDB
- Configure Redis instances
- Set environment variables
- Run database migrations
- Start backend services
- Start frontend development server
- Latency: Sub-millisecond order matching
- Throughput: Handles thousands of orders per second
- Order Book: In-memory for fast matching, updates published in real-time
- Persistence: Async queue-based for non-blocking writes
Backend:
- Rust (async with Tokio)
- Redis (queues, pub/sub, caching)
- TimescaleDB (time-series data)
- Diesel ORM (type-safe SQL)
- Poem (REST API framework)
- JWT (authentication)
Frontend:
- Next.js 14 (App Router)
- TypeScript
- Tailwind CSS
- Recharts/TradingView (charts)
- WebSocket (real-time updates)
- β Core matching engine implemented
- β REST API endpoints functional
- β WebSocket real-time updates working
- β Frontend trading interface in progress
- β³ MPC wallet integration (planned)
- β³ Advanced order types (limit, stop-loss) (planned)
- β³ Admin dashboard (planned)
This is a developer-focused project. Contributions welcome!
[Your License Here]