ccxt-rust 0.1.2

Cryptocurrency exchange trading library in Rust
Documentation

CCXT-Rust


Rust License Rust CI Security Audit Documentation

A professional-grade Rust implementation of the CCXT (CryptoCurrency eXchange Trading) library, providing unified, type-safe access to Major cryptocurrency exchanges with high-performance async operations.

English | ็ฎ€ไฝ“ไธญๆ–‡

๐ŸŽฏ Supported Exchanges

Exchange Public API (Market Data) Private API (Trading) WebSocket
Binance โœ… โœ… โœ…
Bitget โœ… โœ… โœ…
Hyperliquid โœ… โœ… โœ…
OKX โœ… โœ… โœ…
Bybit โœ… โœ… โœ…

Legend: โœ… Supported, ๐Ÿšง In Progress, ๐Ÿ”„ Planned

๐ŸŒŸ Features

Core Capabilities

  • โœ… Type-Safe Trading Operations - Leverage Rust's strong type system for compile-time safety
  • โœ… Async/Await Architecture - Built on Tokio for efficient, non-blocking I/O operations
  • โœ… Precise Financial Calculations - Using rust_decimal for accurate monetary computations
  • โœ… Comprehensive Error Handling - Structured error types with full context propagation
  • โœ… REST API Support - Complete REST API implementation for exchange operations
  • โœ… WebSocket Real-Time Data - Live market data streaming with automatic reconnection
  • โœ… Multi-Exchange Support - Unified interface across multiple cryptocurrency exchanges

Advanced Features

  • Market Data Operations

    • Fetch tickers, order books, and OHLCV data
    • Real-time market data streaming via WebSocket
    • Advanced market data with depth and aggregation
  • Order Management

    • Create, cancel, and modify orders
    • Support for market, limit, and conditional orders
    • OCO (One-Cancels-Other) order support
    • Batch order operations
  • Account Management

    • Balance inquiries and account information
    • Deposit and withdrawal operations
    • Transaction history and ledger access
    • Fee management and calculation
  • Trading Features

    • Spot trading
    • Margin trading (cross and isolated)
    • Futures trading with position management
    • Leverage and margin management
  • WebSocket Features

    • Real-time order book updates
    • Live trade streaming
    • Account balance updates
    • Order status updates
    • Position updates for futures

๐Ÿ—๏ธ Architecture

The project follows a clean, modular workspace architecture with a unified Exchange trait:

ccxt-rust/
โ”œโ”€โ”€ ccxt-core/              # Core types, traits, and error handling
โ”‚   โ”œโ”€โ”€ types/              # Market, Order, Trade, Ticker, etc.
โ”‚   โ”œโ”€โ”€ exchange.rs         # Unified Exchange trait
โ”‚   โ”œโ”€โ”€ ws_exchange.rs      # WebSocket Exchange trait
โ”‚   โ”œโ”€โ”€ error.rs            # Comprehensive error types
โ”‚   โ””โ”€โ”€ base_exchange.rs    # Base exchange functionality
โ”œโ”€โ”€ ccxt-exchanges/         # Exchange-specific implementations
โ”‚   โ””โ”€โ”€ binance/            # Binance exchange implementation
โ”‚       โ”œโ”€โ”€ mod.rs          # Main Binance struct
โ”‚       โ”œโ”€โ”€ builder.rs      # BinanceBuilder
โ”‚       โ”œโ”€โ”€ exchange_impl.rs # Exchange trait implementation
โ”‚       โ”œโ”€โ”€ ws_exchange_impl.rs # WsExchange trait implementation
โ”‚       โ”œโ”€โ”€ rest/           # REST API client modules
โ”‚       โ”œโ”€โ”€ ws.rs           # WebSocket client
โ”‚       โ”œโ”€โ”€ parser.rs       # Response parsing
โ”‚       โ””โ”€โ”€ auth.rs         # Authentication
โ”œโ”€โ”€ examples/               # Comprehensive usage examples
โ”œโ”€โ”€ tests/                  # Integration tests
โ””โ”€โ”€ docs/                   # Detailed documentation

Unified Exchange Trait

The Exchange trait in ccxt-core provides a unified interface for all exchanges:

use ccxt_core::exchange::{Exchange, ExchangeCapabilities, BoxedExchange};

// Use any exchange through the unified interface
async fn fetch_price(exchange: &dyn Exchange, symbol: &str) -> Result<Decimal, Error> {
    // Check capability before calling
    if !exchange.capabilities().fetch_ticker() {
        return Err(Error::not_implemented("fetch_ticker"));
    }
    
    let ticker = exchange.fetch_ticker(symbol).await?;
    ticker.last.ok_or_else(|| Error::invalid_response("No last price"))
}

// Use multiple exchanges polymorphically
async fn compare_prices(exchanges: &[BoxedExchange], symbol: &str) {
    for exchange in exchanges {
        println!("{}: {:?}", exchange.name(), fetch_price(exchange.as_ref(), symbol).await);
    }
}

WebSocket Streaming

The WsExchange trait provides real-time data streaming:

use ccxt_core::ws_exchange::{WsExchange, FullExchange};
use futures::StreamExt;

async fn watch_market(exchange: &dyn WsExchange, symbol: &str) {
    exchange.ws_connect().await.unwrap();
    
    let mut stream = exchange.watch_ticker(symbol).await.unwrap();
    while let Some(Ok(ticker)) = stream.next().await {
        println!("Price: {:?}", ticker.last);
    }
}

๐Ÿš€ Quick Start

Prerequisites

  • Rust 1.91+ or later
  • Cargo (latest stable)

Installation

Add via command line:

cargo add ccxt-rust

Or add to your Cargo.toml:

[dependencies]
ccxt-core = { path = "ccxt-core" }
ccxt-exchanges = { path = "ccxt-exchanges" }
tokio = { version = "1.35", features = ["full"] }
rust_decimal = "1.39"
futures = "0.3"

Basic Usage with Builder Pattern

use ccxt_exchanges::binance::Binance;
use ccxt_core::exchange::Exchange;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize exchange using the builder pattern (recommended)
    let exchange = Binance::builder()
        .api_key("your_api_key")
        .secret("your_secret")
        .sandbox(false)  // Use production API
        .build()?;

    // Fetch ticker through the unified Exchange trait
    let ticker = exchange.fetch_ticker("BTC/USDT").await?;
    println!("BTC/USDT Price: {:?}", ticker.last);

    // Fetch order book
    let orderbook = exchange.fetch_order_book("BTC/USDT", Some(10)).await?;
    if let Some(best_bid) = orderbook.bids.first() {
        println!("Best bid: {}", best_bid.price);
    }
    if let Some(best_ask) = orderbook.asks.first() {
        println!("Best ask: {}", best_ask.price);
    }

    // Place an order (requires API credentials)
    use ccxt_core::types::{OrderType, OrderSide};
    use rust_decimal_macros::dec;
    
    let order = exchange.create_order(
        "BTC/USDT",
        OrderType::Limit,
        OrderSide::Buy,
        dec!(0.001),
        Some(dec!(40000)),
    ).await?;
    println!("Order placed: {}", order.id);

    Ok(())
}

Using Exchanges Polymorphically

use ccxt_core::exchange::{Exchange, BoxedExchange};
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create exchange as a trait object
    let exchange: BoxedExchange = Box::new(
        ccxt_exchanges::binance::Binance::builder().build()?
    );
    
    // Use through the unified interface
    println!("Exchange: {} ({})", exchange.name(), exchange.id());
    println!("Capabilities: {:?}", exchange.capabilities());
    
    // Check capabilities before calling methods
    if exchange.capabilities().fetch_ticker() {
        let ticker = exchange.fetch_ticker("BTC/USDT").await?;
        println!("Price: {:?}", ticker.last);
    }
    
    Ok(())
}

WebSocket Streaming

use ccxt_exchanges::binance::Binance;
use ccxt_core::ws_exchange::WsExchange;
use futures::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize exchange
    let exchange = Binance::builder().build()?;

    // Watch real-time ticker updates using the WsExchange trait
    let mut stream = exchange.watch_ticker("BTC/USDT").await?;
    
    while let Some(result) = stream.next().await {
        match result {
            Ok(ticker) => println!("Price: {:?}", ticker.last),
            Err(e) => eprintln!("Error: {}", e),
        }
    }

    Ok(())
}

๐Ÿ“š Examples

The project includes comprehensive examples covering all major features:

  • basic_usage.rs - Getting started with the library
  • binance_market_data_example.rs - Market data operations
  • binance_order_management_example.rs - Order creation and management
  • binance_account_example.rs - Account operations
  • binance_margin_example.rs - Margin trading
  • binance_futures_example.rs - Futures trading
  • binance_ws_example.rs - WebSocket streaming
  • binance_conditional_orders_example.rs - Conditional orders
  • binance_deposit_withdrawal_example.rs - Deposit/withdrawal operations

Run any example:

cargo run --example basic_usage
cargo run --example binance_ws_example

๐Ÿšฉ Feature Flags

Optimize your build by selecting only the features you need in Cargo.toml:

  • default: Enables rest, websocket, and rustls-tls.
  • rest: Enables REST API support.
  • websocket: Enables WebSocket support.
  • rustls-tls: Uses rustls for TLS (default, recommended).
  • native-tls: Uses platform-native TLS (OpenSSL/Schannel/Secure Transport).
  • compression: Enables GZIP compression for HTTP requests.
  • full: Enables all features.

๐Ÿ”ง Configuration

Environment Variables

Create a .env file from the template:

cp .env.example .env

Key configuration options:

# API Credentials
BINANCE_API_KEY=your_api_key_here
BINANCE_API_SECRET=your_secret_here

# Testing
ENABLE_PRIVATE_TESTS=false
ENABLE_INTEGRATION_TESTS=false
USE_MOCK_DATA=true
TEST_SYMBOL=BTC/USDT

# Logging
RUST_LOG=info

๐Ÿงช Testing

# Run all tests
cargo test

# Run tests with output
cargo test -- --nocapture

# Run specific test suite
cargo test -p ccxt-core
cargo test -p ccxt-exchanges

# Run integration tests
cargo test --test binance_integration_test

# Run with live API (requires credentials)
ENABLE_INTEGRATION_TESTS=true cargo test

๐Ÿ“– Documentation

Generate local documentation:

cargo doc --open

๐Ÿ› ๏ธ Development

Building

# Debug build
cargo build

# Release build (optimized)
cargo build --release

# Build specific package
cargo build -p ccxt-core
cargo build -p ccxt-exchanges

Code Quality

# Format code
cargo fmt

# Run linter
cargo clippy --all-targets --all-features

# Strict linting (no warnings)
cargo clippy --all-targets --all-features -- -D warnings

# Check compilation
cargo check --all-features

๐Ÿ” Security

  • Never commit API keys or secrets - Always use environment variables
  • Secure credential storage - Use system keychains or encrypted vaults
  • Rate limiting - Built-in rate limiting to prevent API bans
  • Input validation - All inputs are validated before API calls
  • HTTPS only - All communications use TLS encryption

๐Ÿค Contributing

Contributions are welcome! Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Follow Rust best practices and project conventions
  4. Add tests for new features
  5. Ensure all tests pass (cargo test)
  6. Run formatting and linting (cargo fmt && cargo clippy)
  7. Commit your changes (git commit -m 'Add amazing feature')
  8. Push to the branch (git push origin feature/amazing-feature)
  9. Open a Pull Request

Development Conventions

  • Code Style: Rust 2024 edition, 100-character line width
  • Testing: Minimum 80% test coverage
  • Documentation: All public APIs must have documentation
  • Error Handling: Use thiserror for custom errors, anyhow for context

๐Ÿ“Š Performance

Built for high performance:

  • Async I/O: Non-blocking operations using Tokio
  • Zero-copy parsing: Efficient JSON deserialization
  • Connection pooling: Reused HTTP connections
  • Optimized builds: LTO and single codegen unit for releases
  • Benchmarks: Criterion-based performance benchmarks

๐Ÿ› Troubleshooting

Common Issues

  1. Compilation errors

    • Ensure Rust 1.91+ is installed: rustc --version
    • Update dependencies: cargo update
    • Clean build: cargo clean && cargo build
  2. API authentication failures

    • Verify API keys in .env file
    • Check API key permissions on exchange
    • Ensure system clock is synchronized
  3. Rate limiting

    • Reduce request frequency
    • Use WebSocket for real-time data
    • Check exchange-specific rate limits

For more help, see documentation or open an issue.

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Inspired by the original CCXT library
  • Built with amazing Rust ecosystem libraries
  • Community contributors and testers

๐Ÿ“ž Contact & Support


Status: ๐Ÿšง Active Development | Version: 0.1.1 | Updated: 2025-12

โš ๏ธ Note: This library is under active development. APIs may change before v1.0. Not recommended for production use yet.