Rust SDK

Rust SDK

High-performance Rust SDK for LX - zero-copy parsing, async/await with tokio, and memory-efficient trading

LX Rust SDK

Specification: LP-9002 DEX API & RPC | Crate

The Rust SDK provides high-performance, memory-safe access to LX with zero-copy deserialization and async/await support.

Features

  • Zero-copy parsing - Deserialize directly from network buffers
  • Async/await - Built on tokio for concurrent operations
  • Type safety - Compile-time guarantees with rich type system
  • Memory efficiency - Arena allocation and buffer pooling
  • TLS support - rustls with native-certs for secure connections

Installation

Add to your Cargo.toml:

[dependencies]
lux-dex-sdk = "0.1"
tokio = { version = "1", features = ["full"] }

# Optional: for custom TLS configuration
rustls = { version = "0.23", optional = true }

Feature Flags

[dependencies.lux-dex-sdk]
version = "0.1"
features = [
    "tls",           # TLS support (default)
    "grpc",          # gRPC client
    "websocket",     # WebSocket streaming
    "zero-copy",     # Zero-copy deserialization
    "simd",          # SIMD-accelerated parsing
]

Quick Start

use lux_dex_sdk::{Client, Order, OrderType, Side, Result};

#[tokio::main]
async fn main() -> Result<()> {
    // Create client with builder pattern
    let client = Client::builder()
        .json_rpc_url("http://localhost:8080/rpc")
        .websocket_url("ws://localhost:8081")
        .build()
        .await?;

    // Place a limit order
    let order = client.place_order(Order {
        symbol: "BTC-USD".into(),
        order_type: OrderType::Limit,
        side: Side::Buy,
        price: 50_000.0,
        size: 1.0,
        ..Default::default()
    }).await?;

    println!("Order placed: {}", order.id);

    // Get order book
    let book = client.get_order_book("BTC-USD", 10).await?;
    println!("Best bid: {:?}", book.bids.first());
    println!("Best ask: {:?}", book.asks.first());

    Ok(())
}

Project Structure

lux-dex-sdk/
├── Cargo.toml
├── src/
│   ├── lib.rs           # Public API exports
│   ├── client.rs        # Client implementation
│   ├── types.rs         # Core types (Order, Trade, etc.)
│   ├── error.rs         # Error types
│   ├── rpc/
│   │   ├── mod.rs
│   │   ├── json.rs      # JSON-RPC client
│   │   └── grpc.rs      # gRPC client
│   ├── ws/
│   │   ├── mod.rs
│   │   ├── stream.rs    # WebSocket streams
│   │   └── codec.rs     # Zero-copy codec
│   └── util/
│       ├── pool.rs      # Buffer pooling
│       └── arena.rs     # Arena allocator
├── benches/
│   └── throughput.rs    # Performance benchmarks
└── examples/
    ├── basic.rs
    ├── streaming.rs
    └── hft.rs

Performance Characteristics

OperationLatencyThroughput
Order placement< 100us50,000/sec
Order book query< 50us100,000/sec
WebSocket message< 10us1,000,000/sec
Zero-copy parse< 1us10,000,000/sec

Benchmark Results

Run benchmarks:

cargo bench --features simd

Typical results on M1 Mac:

order_placement         time:   [89.234 us 91.456 us 93.789 us]
orderbook_parse         time:   [823 ns 845 ns 867 ns]
websocket_decode        time:   [234 ns 241 ns 248 ns]
zero_copy_deserialize   time:   [45 ns 47 ns 49 ns]

Memory Usage

The SDK uses several strategies to minimize allocations:

  1. Buffer pooling - Reuse network buffers
  2. Arena allocation - Batch allocations for order books
  3. Zero-copy parsing - Borrow from network buffers
  4. Small string optimization - Inline short symbols
use lux_dex_sdk::pool::BufferPool;

// Create buffer pool for high-throughput scenarios
let pool = BufferPool::new(1024, 64 * 1024); // 1024 buffers, 64KB each

let client = Client::builder()
    .buffer_pool(pool)
    .build()
    .await?;

Error Handling

The SDK uses a custom Result type with detailed error variants:

use lux_dex_sdk::{Error, Result};

async fn place_order(client: &Client) -> Result<OrderId> {
    match client.place_order(order).await {
        Ok(response) => Ok(response.id),
        Err(Error::InsufficientBalance { required, available }) => {
            eprintln!("Need {} but only have {}", required, available);
            Err(Error::InsufficientBalance { required, available })
        }
        Err(Error::RateLimit { retry_after }) => {
            tokio::time::sleep(retry_after).await;
            client.place_order(order).await.map(|r| r.id)
        }
        Err(e) => Err(e),
    }
}

Thread Safety

All client types implement Send + Sync and can be safely shared across threads:

use std::sync::Arc;

let client = Arc::new(Client::builder().build().await?);

// Spawn multiple tasks sharing the client
for _ in 0..10 {
    let client = Arc::clone(&client);
    tokio::spawn(async move {
        client.place_order(order).await
    });
}

Comparison with Other SDKs

FeatureRustGoPythonTypeScript
Zero-copyYesNoNoNo
AsynctokiogoroutinesasyncioPromises
Memory safetyCompile-timeRuntimeRuntimeRuntime
Latency< 100us< 500us< 5ms< 10ms

Next Steps