Bitrade is a high-performance, low-latency matching engine for spot trading built in Rust. It provides real-time order matching, trade execution, and market data services through gRPC APIs.
- ✅ Order Matching: Real-time limit and market order matching
- ✅ Trade Execution: Atomic trade execution with proper fee handling
- ✅ Market Depth: Real-time order book depth tracking
- ✅ Multiple Markets: Support for multiple trading pairs
- ✅ gRPC APIs: High-performance gRPC services for trading and querying
- ✅ PostgreSQL: Persistent storage with ACID transactions
- ✅ Wallet Management: Deposit, withdraw, and balance tracking
- ✅ Market Statistics: 24h high/low/volume tracking
- ✅ Fee Treasury: Automated fee collection and management
- ✅ Order History: Complete order and trade history
- ✅ Order Cancellation: Support for order cancellation and bulk operations
The project is organized as a Rust workspace with the following components:
engine/: Core matching engine with gRPC trading API (port 50020)query/: Read-only gRPC query service (port 50021)database/: PostgreSQL schema, models, and repository layercommon/: Shared utilities and types
- Rust 1.89+
- PostgreSQL 15+
- Docker & Docker Compose (optional)
- Clone the repository:
git clone <repository-url>
cd bitrade- Start the services:
./dev.sh prodThis will start:
- PostgreSQL database on port 5432
- Bitrade Engine on port 50020
- Bitrade Query Service on port 50021
- Clone the repository:
git clone <repository-url>
cd bitrade- Start development environment with hot reloading:
./dev.sh devDevelopment Mode Features:
- 🔥 Hot Reloading: Code changes automatically trigger rebuilds
- 📁 Volume Mounting: Live code changes without container rebuilds
- 🐛 Debug Logging: Detailed logs for development
- ⚡ Fast Rebuilds: Dependency caching for faster compilation
- 🔄 Auto-restart: Services restart automatically on code changes
- Set up PostgreSQL:
# Create database
createdb postgres
# Run migrations
cd database
diesel migration run- Set environment variables:
cp env.example .env
# Edit .env with your database configuration- Build and run:
# Build the project
cargo build --release
# Run the matching engine
cargo run --bin bitrade
# Run the query service (in another terminal)
cargo run --bin queryBitrade uses Diesel CLI for database migrations. The following commands are available:
# Run migrations in development environment
./migrate.sh dev
# Run migrations in production environment
./migrate.sh prod
# Check migration status
./migrate.sh status
# Reset database (WARNING: Deletes all data!)
./migrate.sh resetTo create a new migration:
cd database
diesel migration generate migration_nameThis will create a new migration file in database/migrations/.
The trading engine provides the following gRPC services:
CreateMarket: Create a new trading pairStartMarket: Start accepting orders for a marketStopMarket: Stop accepting orders for a market
AddOrder: Place a new order (limit or market)CancelOrder: Cancel a specific orderCancelAllOrders: Cancel all orders for a market
Deposit: Deposit funds to a user's walletWithdraw: Withdraw funds from a user's walletGetBalance: Get current balance for a user/asset
The query service provides read-only access to:
GetMarket: Get market informationListMarkets: List all available marketsGetMarketStats: Get 24h market statistics
GetOrder: Get specific order detailsListOrders: List orders with filtering and pagination
ListTrades: List trades with filtering and paginationGetUserTrades: Get trades for a specific user
GetWallet: Get wallet balance for a user/assetListWallets: List wallets with filtering and pagination
GetFeeTreasury: Get fee treasury information
The application can be configured through environment variables:
| Variable | Default | Description |
|---|---|---|
DATABASE_URL |
postgres://postgres:mysecretpassword@localhost/postgres |
PostgreSQL connection string |
SERVER_HOST |
[::] |
Server host address |
SERVER_PORT |
50020 |
Server port |
RUST_LOG |
info |
Logging level |
BITRADE_DATABASE_POOL_SIZE |
10 |
Database connection pool size |
cargo testcargo fmtcd database
diesel migration generate <migration_name>
diesel migration run
diesel migration revertcargo build --releasebitrade/
├── engine/ # Core matching engine
│ ├── src/
│ │ ├── main.rs # Application entry point
│ │ ├── grpc/ # gRPC service definitions
│ │ ├── market/ # Market management
│ │ ├── order_book/ # Order book and matching logic
│ │ ├── models/ # Data models
│ │ ├── validation/ # Input validation
│ │ └── wallet/ # Wallet operations
│ └── Cargo.toml
├── query/ # Query service
│ ├── src/
│ │ ├── main.rs # Query service entry point
│ │ ├── server.rs # gRPC server setup
│ │ ├── service.rs # Query service implementation
│ │ └── adapter.rs # Data adapters
│ └── Cargo.toml
├── database/ # Database layer
│ ├── src/
│ │ ├── models/ # Database models
│ │ ├── repository/ # Data access layer
│ │ ├── provider/ # Database provider traits
│ │ └── filters/ # Query filters
│ ├── migrations/ # Database migrations
│ └── Cargo.toml
├── common/ # Shared utilities
│ ├── src/
│ │ ├── utils/ # Common utilities
│ │ └── db/ # Database utilities
│ └── Cargo.toml
├── docker-compose.yml # Development environment
├── Dockerfile # Production container
└── README.md
Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
The matching engine is designed for high-performance trading:
- Latency: Sub-millisecond order matching
- Throughput: 100,000+ orders per second
- Concurrency: Multi-threaded order processing
- Persistence: ACID-compliant trade execution
- Input validation on all API endpoints
- SQL injection protection through Diesel ORM
- Proper error handling without information leakage
- Non-root container execution
The application provides comprehensive logging and can be monitored through:
- Structured logging with configurable levels
- gRPC health checks
- Database connection monitoring
- Order book depth tracking