Real-time crypto leverage trading platform with live Binance price feeds, position management, and PnL tracking. Built with TypeScript and Bun.
Untitled.mp4
Binance WS ─> price_poller ─> Redis PUBLISH ─> ws server ─> frontend (live prices)
└> TimescaleDB (historical candles)
Redis SUB ─> server ─> monitors open positions (TP/SL/liquidation)
| Service | Port | Description |
|---|---|---|
| frontend/ | 5173 | React 19 + Vite + Tailwind CSS. Lightweight Charts for candlestick visualization |
| server/ | 5000 | Express 5 REST API. JWT auth, trade execution, candle queries. In-memory user/order state |
| ws/ | 8080 | WebSocket relay. Bridges Redis pub/sub to browser connections |
| price_poller/ | — | Binance aggTrade streams (BTC, ETH, SOL). Publishes to Redis, batch-inserts to TimescaleDB |
Infrastructure: TimescaleDB (hypertables + continuous aggregates for 1m/1d/1w candles), Redis 7.2 (pub/sub)
# 1. Start infrastructure
docker compose up -d redis db
# 2. Wait for DB to be healthy, then set up schema + candle views (one-time)
cd price_poller && bun install && bunx prisma db push && bun run seed && cd ..
# 3. Install dependencies and start all services
bun install && bun run install:all && bun run devdocker compose up --buildNote: The DB runs on
tmpfs— data is lost on restart. Re-run the seed step afterdocker compose down.
| Command | Description |
|---|---|
bun run dev |
Start all 4 services concurrently |
bun run install:all |
Install deps in all services |
bun run docker:up |
docker compose up -d --build |
bun run docker:down |
docker compose down |
| Command | Description |
|---|---|
bun run dev |
Dev server with HMR |
bun run build |
Production build |
bun run lint |
ESLint |
| Command | Description |
|---|---|
bun run seed |
Create TimescaleDB hypertable + continuous aggregates |
bunx prisma db push |
Apply Prisma schema |
All routes are prefixed with /api/v1.
| Method | Route | Auth | Description |
|---|---|---|---|
| POST | /user/signup |
No | Create account |
| POST | /user/signin |
No | Get JWT token |
| POST | /user/logout |
Yes | Logout |
| GET | /user/balance |
Yes | Get USD balance |
| POST | /trade |
Yes | Open a position |
| POST | /trade/close |
Yes | Close a position |
| GET | /trades/open |
Yes | List open trades |
| GET | /trades |
Yes | List closed trades |
| GET | /candles |
No | OHLCV candle data |
| GET | /asset |
No | Asset list with live prices |
- Assets: BTC, ETH, SOL (all paired with USDT)
- Leverage: 1x, 5x, 10x, 20x, 100x
- Order types: Market, Limit
- Risk management: Take Profit, Stop Loss, auto-liquidation
- Real-time: Live price streaming via WebSocket, auto-updating candlestick charts (1m, 1d, 1w)
All prices use integer math to avoid floating-point errors:
PRICE_SCALE = 10000— prices stored as integer x 10000USD_SCALE = 100— USD amounts stored as cents- PnL calculations use BigInt
- Runtime: Bun
- Frontend: React 19, Vite, Tailwind CSS, Lightweight Charts
- Backend: Express 5, Zod validation, JWT auth
- Database: TimescaleDB (PostgreSQL) with continuous aggregates
- Cache/Pub-Sub: Redis 7.2
- ORM: Prisma (price_poller)
- Containerization: Docker Compose