Skip to content

trevortrinh/exchange

Repository files navigation

banner

A full-stack crypto exchange. CLOB + frontend + market-making bots.

Deploy on Railway

Project Structure

exchange/
├── apps/
│   ├── backend/              # Rust Axum API + matching engine
│   │   └── db/               # PostgreSQL + ClickHouse
│   ├── frontend/             # Next.js trading interface
│   └── bots/                 # Market-making bots
├── packages/
│   ├── shared/               # Shared schemas (OpenAPI, WebSocket)
│   └── sdk/                  # Multi-language SDKs (TypeScript, Python, Rust)
└── docker-compose.yaml       # Infra

Demo

One-click Railway deployment with live trading: authentication via Turnkey embedded wallet, real-time candle data after a few hours of market activity, and executing both market and limit orders.

demo.mp4

Table of Contents

🚀 Getting Started

Prerequisites

Quick Start

# Clone the repository
git clone git@github.com:trevor-trinh/exchange.git
cd exchange

# Start everything with docker compose
docker compose up -d

Start Services Individually

# Install dependencies
just install

# Start databases
just db

# Start the backend
just backend

# Start the frontend
just frontend

# Start market-making bots
just bots

Access the app at:

🏗️ Architecture

Exchange Overview

Architecture

This exchange is built as a modern, full-featured trading system with the following components:

  • Backend
    • API: REST and WebSocket APIs with multi-language SDK support
    • Database: Dual architecture with PostgreSQL (OLTP) and ClickHouse (OLAP)
    • Matching Engine: In-memory orderbook with price-time priority matching
  • Frontend: Next.js application with TradingView integration and embedded wallet
  • Bots: Automated market makers with external market integration
  • Testing & Deployment: Containerized development and one-click deployment

Backend

Backend Architecture

Matching Engine

The matching engine is the core of the exchange, handling order placement, matching, and execution.

  • Orderbook: In-memory B-tree implementation with price-time priority, O(log n) complexity
  • Matcher: Price-time priority algorithm with continuous matching and partial fills
  • Executor: Atomic trade execution with balance locking and transaction rollback

Database

The exchange uses a dual-database architecture optimized for different workloads:

  • ClickHouse (OLAP): Materialized views for real-time candlestick aggregation and time-series analytics
  • PostgreSQL (OLTP): Primary transactional database storing users, tokens, markets, orders, balances, and trades

API

  • REST & WebSocket: OpenAPI-documented REST endpoints and real-time WebSocket subscriptions powered by Tokio
  • Multi-language SDKs: TypeScript, Python, and Rust clients auto-generated from OpenAPI and JSON Schema

Frontend

Frontend Architecture

Modern Next.js trading interface with professional features:

  • Zustand: Lightweight state management with real-time WebSocket synchronization
  • TypeScript SDK: Type-safe API client with WebSocket subscriptions
  • TradingView: Advanced charting with custom drawing tools and real-time updates
  • Turnkey: Embedded wallet with secure key management, no browser extension required
    • Note: When deploying, ensure NEXT_PUBLIC_ORGANIZATION_ID and NEXT_PUBLIC_AUTH_PROXY_CONFIG_ID are set as build-time environment variables

Bots

Bots Architecture

Automated market-making bots to provide liquidity:

  • BTC/USDC: Hyperliquid mirror bot that replicates order book depth with configurable risk parameters
  • BP/USDC: LMSR (Logarithmic Market Scoring Rule) market maker with dynamic spread adjustment (planned)

Testing & Deployment

  • Devcontainers: Easily start dev enviornment with tools pre-installed
  • Testcontainers: Integration testing with isolated database instances and automated cleanup
  • GitHub Actions: Automated CI/CD pipeline for testing and deployment
  • Railway: Super-duper easy deployment. Try it out!

Improvements

Features and improvements to consider for production:

  • perps
  • pnl
  • deposits / withdrawals
  • helllaaa latency
  • write ahead log lmao
  • design for concurrency across multiple markets
  • metrics & alerting
  • backups & disaster recovery
  • scaling / k8s
  • mm channel prioritization
  • cancel prioritization

License

MIT

About

A full-stack crypto exchange. CLOB + frontend + market-making bots.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published