A Turborepo monorepo implementing a Contract for Difference (CFD) trading platform for cryptocurrency with microservices architecture.
This project is a CFD trading platform that simulates cryptocurrency trading without actual asset ownership. Users trade on price movements using an internal ledger system with real-time market data from external exchanges.
- Monorepo Management: Turborepo with Bun package manager
- Workspaces:
apps/*andpackages/* - Languages: TypeScript throughout
- Infrastructure: Redis for data streaming and caching
- Port: 8080
- Framework: Express.js with TypeScript
- Features:
- Authentication system with JWT and email verification
- Order management endpoints
- Redis integration for user balance persistence
- CORS enabled for frontend communication
Key Components:
routes/auth/: Email-based authentication with token verificationroutes/order/: Trading order placement and user info retrievalredis/,mailthing/,tokenthing/: Supporting servicesvalidations/: Input validation with Zod
- Core Function: Real-time order processing and price management
- Data Sources:
- Price updates from Redis Pub/Sub
- Order stream from Redis Streams
- Features:
- In-memory price tracking for BTC, ETH, SOL
- Order validation (slippage, balance checks)
- User balance management with precision handling
- Order execution with risk management
Key Components:
order/: Order processing logic with slippage protectiontypes.ts: Type definitions for orders, assets, and user data
- Data Source: Backpack Exchange WebSocket
- Assets: SOL_USDC, ETH_USDC, BTC_USDC
- Function:
- Subscribes to real-time market data
- Calculates mid-prices from bid/ask spreads
- Publishes to Redis streams every 100ms
- Feeds both price updates and ticker streams
- Port: 3333
- Protocol: WebSocket
- Features:
- Subscription management per client
- Real-time ticker data streaming
- Asset-specific subscriptions (BTC, ETH, SOL)
- Framework: Next.js 16.0.0 with React 19
- Styling: Tailwind CSS v4
- Purpose: Trading interface (implementation pending)
eslint-config/: Shared ESLint configurationstypescript-config/: Shared TypeScript configurationsui/: Shared React component library (Button, Card, Code components)
tests/: Comprehensive engine testing with Bun test framework- Price update validation
- Order placement scenarios
- Slippage protection testing
- Balance management verification
flowchart TD
subgraph External["External Data Sources"]
BP[Backpack Exchange<br/>WebSocket API]
end
subgraph DataLayer["Data Layer"]
R[Redis<br/>• Streams<br/>• Pub/Sub<br/>• Key-Value]
end
subgraph Services["Backend Services"]
P[Poller Service<br/>Market Data Fetcher]
E[Trading Engine<br/>Order Processor]
WS[WebSocket Server<br/>Port: 3333]
API[Backend API<br/>Port: 8080]
end
subgraph Client["Client Layer"]
FE[Next.js Frontend<br/>Port: 3000]
end
%% Data Flow
BP -->|Real-time market data| P
P -->|price_updates channel| R
P -->|ticker_stream| R
R -->|Subscribe to price feeds| E
API -->|order_stream| R
R -->|Process trading orders| E
E -->|Update user balances| R
R -->|Stream ticker data| WS
WS <-->|Live price updates| FE
API <-->|Authentication & Orders| FE
%% Styling
classDef external fill:#ff6b6b,stroke:#c23616,color:#fff
classDef data fill:#4ecdc4,stroke:#00a085,color:#fff
classDef service fill:#45b7d1,stroke:#2c5aa0,color:#fff
classDef client fill:#54a0ff,stroke:#2f3542,color:#fff
class BP external
class R data
class P,E,WS,API service
class FE client
- Price Updates:
price_updateschannel (Pub/Sub) - Order Stream:
order_streamkey (Streams) - Ticker Stream:
ticker_streamkey (Streams) - User Balances:
balances:${userId}keys (Key-Value)
- Assets: BTC, ETH, SOL CFDs (no actual crypto ownership)
- Initial Balance: $5,000 virtual USD per user
- Settlement: Internal ledger system - no real crypto transactions
- Price Source: Live market data from Backpack Exchange
- Risk Management: Slippage protection, balance validation
- Position Types: Long positions (buy CFDs on price increase)
- User places CFD order through REST API
- Engine validates balance and slippage against real market prices
- Internal ledger updated (no actual crypto bought/sold)
- P&L calculated based on price movements
- User sees profit/loss without owning underlying assets
| Layer | Technology |
|---|---|
| Runtime | Bun |
| Backend | Express.js + TypeScript |
| Frontend | Next.js 16 + React 19 + Tailwind |
| Database | Redis (Streams, Pub/Sub, Key-Value) |
| Real-time | WebSocket (ws library) |
| Authentication | JWT + Email verification |
| Validation | Zod |
| Testing | Bun test |
| Build System | Turborepo |
- Node.js >= 18
- Bun package manager
- Redis server
# Install dependencies
bun install
# Start all services
bun dev- Backend API: http://localhost:8080
- WebSocket Server: ws://localhost:3333
- Frontend: http://localhost:3000
POST /api/v1/auth/in- Email registration/loginGET /api/v1/auth/verify- Email verification
POST /api/v1/trade- Place CFD order (updates internal ledger only)GET /api/v1/trade/info/:userId- Get virtual balance and positions
ws://localhost:3333- Real-time price feeds- Subscribe:
{"e": "subscribe", "params": ["BTC", "ETH", "SOL"]}
bun testTests cover CFD order validation, slippage protection, and virtual balance management.
This CFD platform simulates cryptocurrency trading without actual asset ownership, using real market prices for accurate profit/loss calculations.
flowchart TD
subgraph External["External Data Sources"]
BP[Backpack Exchange<br/>WebSocket API]
end
subgraph DataLayer["Data Layer"]
R[Redis<br/>• Streams<br/>• Pub/Sub<br/>• Cache]
DB[(PostgreSQL<br/>• User Balances<br/>• Orders<br/>• Trades)]
end
subgraph GatewayLayer["Gateway Layer"]
GW[API Gateway<br/>• Auth<br/>• Rate Limiting]
end
subgraph Services["Backend Services"]
P[Poller Service<br/>Market Data Fetcher]
E[Trading Engine<br/>Order Processor]
WS[WebSocket Server<br/>Port: 3333]
API[Backend API<br/>Port: 8080]
Auth[Auth Service]
end
subgraph Client["Client Layer"]
FE[Next.js Frontend<br/>Port: 3000]
end
%% Data Flow
BP -->|Real-time market data| P
P -->|price_updates channel| R
P -->|ticker_stream| R
R -->|Subscribe to price feeds| E
GW -->|order_stream| R
R -->|Process trading orders| E
E -->|Update user balances & orders| DB
E -->|Invalidate cache| R
R -->|Stream ticker data| WS
WS <-->|Live price updates| FE
GW <-->|Authenticated API Calls| FE
API --> GW
Auth --> GW
%% Styling
classDef external fill:#ff6b6b,stroke:#c23616,color:#fff
classDef data fill:#4ecdc4,stroke:#00a085,color:#fff
classDef service fill:#45b7d1,stroke:#2c5aa0,color:#fff
classDef client fill:#54a0ff,stroke:#2f3542,color:#fff
classDef gateway fill:#feca57,stroke:#b37400,color:#fff
class BP external
class R,DB data
class GW gateway
class P,E,WS,API,Auth service
class FE client