Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ chrono = "0.4"
ed25519-dalek = "2.0"
base64 = "0.21"
rust_decimal = { version = "1.35", features = ["serde-with-str"] }
num-traits = "0.2"

# Optional dependencies
dotenv = { version = "0.15", optional = true }
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ tokio = { version = "1.0", features = ["full"] }
### Basic Usage

```rust
use lotusx::{BinanceConnector, BybitConnector};
use lotusx::exchanges::binance::BinanceBuilder;
use lotusx::core::config::ExchangeConfig;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load configuration from environment
let config = ExchangeConfig::from_env("BINANCE")?;
let binance = BinanceConnector::new(config);
let binance = BinanceBuilder::new().build(config).await?;

// Get markets
let markets = binance.get_markets().await?;
Expand Down Expand Up @@ -93,13 +93,18 @@ PARADEX_TESTNET=true
- **🧪 Testnet**: Full testnet support for safe development
- **📊 Performance Testing**: Built-in latency analysis and HFT metrics
- **🛡️ Type Safe**: Strong typing for all API responses
- **🎯 Kernel Architecture**: Unified transport layer with modular design

## 📖 **Examples**

### Trading

```rust
use lotusx::core::types::*;
use lotusx::exchanges::binance::BinanceBuilder;

// Create exchange connector
let binance = BinanceBuilder::new().build(config).await?;

let order = OrderRequest {
symbol: "BTCUSDT".to_string(),
Expand All @@ -117,6 +122,11 @@ let response = binance.place_order(order).await?;
### WebSocket Streaming

```rust
use lotusx::exchanges::binance::BinanceBuilder;

// Create exchange connector
let binance = BinanceBuilder::new().build(config).await?;

let symbols = vec!["BTCUSDT".to_string()];
let subscription_types = vec![SubscriptionType::Ticker];

Expand Down Expand Up @@ -182,6 +192,7 @@ Paradex 134589 3.42 5.2
```rust
use lotusx::utils::exchange_factory::*;
use lotusx::utils::latency_testing::*;
use lotusx::exchanges::binance::BinanceBuilder;

// Build custom test configuration
let configs = ExchangeTestConfigBuilder::new()
Expand Down
156 changes: 85 additions & 71 deletions docs/ADDING_NEW_EXCHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,90 +37,90 @@ src/

## 🏗️ Exchange Module Structure

Each exchange follows a modular structure, but with flexibility based on requirements. Here are the patterns used by existing exchanges:
Each exchange follows the new **Kernel Architecture** with unified transport layer and modular design. The structure is consistent across all exchanges:

### Standard Structure (Most Exchanges)
### Standard Kernel Structure (All Exchanges)
```
src/exchanges/exchange_name/
├── mod.rs # Module exports and re-exports
├── client.rs # Main connector struct (lightweight)
├── types.rs # Exchange-specific data structures
├── converters.rs # Type conversions between exchange and core types
├── market_data.rs # Market data implementation
├── trading.rs # Order placement and management
└── account.rs # Account information queries
├── mod.rs # Module exports and builder
├── builder.rs # Exchange builder pattern implementation
├── codec.rs # Message encoding/decoding
├── conversions.rs # Type conversions between exchange and core types
├── connector/ # Modular connector implementations
│ ├── mod.rs # Connector composition
│ ├── account.rs # Account information queries
│ ├── market_data.rs # Market data implementation
│ └── trading.rs # Order placement and management
├── rest.rs # REST API client implementation
├── signer.rs # Authentication and request signing
└── types.rs # Exchange-specific data structures
```

### With Authentication Module
Some exchanges require their own authentication logic:
```
src/exchanges/exchange_name/
├── ... (standard files)
└── auth.rs # Authentication and request signing
```

### With Custom WebSocket Implementation
Exchanges with complex WebSocket requirements may have:
```
src/exchanges/exchange_name/
├── ... (standard files)
└── websocket.rs # Exchange-specific WebSocket handling
```
### Kernel Integration Benefits
- **Unified Transport**: Leverages `src/core/kernel/` for HTTP and WebSocket communication
- **Consistent Authentication**: Uses `Signer` trait for secure credential handling
- **Modular Design**: Clean separation of concerns with focused modules
- **Builder Pattern**: Consistent instantiation across all exchanges

## 🔄 Current Exchange Examples

### Binance Pattern (Standard with Auth)
- `client.rs` - Lightweight connector
- `auth.rs` - HMAC-SHA256 authentication
- All standard modules present
### Binance Pattern (Standard Kernel)
- `builder.rs` - Exchange builder implementing `BinanceBuilder`
- `signer.rs` - HMAC-SHA256 authentication via `Signer` trait
- `connector/` - Modular trait implementations
- All standard kernel modules present

### Binance Perpetual Pattern (Auth Reuse)
- `client.rs` - Lightweight connector
- No `auth.rs` - reuses authentication from binance
- All other standard modules present

### Hyperliquid Pattern (Custom WebSocket)
- `client.rs` - More complex due to EIP-712 authentication
- `auth.rs` - EIP-712 cryptographic signing
- `websocket.rs` - Custom WebSocket message handling
- All standard modules present

### Bybit Perpetual Pattern (Minimal)
- `client.rs` - Lightweight connector
- No `auth.rs` - reuses authentication from bybit spot
- All other standard modules present
- `builder.rs` - Exchange builder implementing `BinancePerpBuilder`
- `signer.rs` - Reuses binance authentication module
- `connector/` - Modular trait implementations
- All other standard kernel modules present

### Hyperliquid Pattern (Custom Codec)
- `builder.rs` - Exchange builder implementing `HyperliquidBuilder`
- `signer.rs` - EIP-712 cryptographic signing
- `codec.rs` - Custom WebSocket message handling
- `connector/` - Modular trait implementations
- All standard kernel modules present

### Bybit Perpetual Pattern (Minimal Auth)
- `builder.rs` - Exchange builder implementing `BybitPerpBuilder`
- `signer.rs` - Reuses bybit spot authentication
- `connector/` - Modular trait implementations
- All other standard kernel modules present

## 🚀 Step-by-Step Implementation Approach

### Step 1: Plan Your Exchange Structure
Before writing code, determine:
- Does the exchange need custom authentication? (create `auth.rs`)
- Does it have complex WebSocket requirements? (create `websocket.rs`)
- Can you reuse authentication from another exchange?
- Does the exchange need custom WebSocket message handling? (enhance `codec.rs`)
- Can you reuse authentication from another exchange? (reuse `signer.rs`)
- What are the exchange's specific API endpoints and authentication requirements?

### Step 2: Create the Exchange Directory
```bash
mkdir src/exchanges/exchange_name
mkdir src/exchanges/exchange_name/connector
```

### Step 3: Implement Core Modules (In Order)

#### Start with Foundation
1. **`types.rs`** - Define all exchange-specific data structures
2. **`client.rs`** - Create the main connector struct (keep it lightweight)
3. **`mod.rs`** - Set up module exports
2. **`builder.rs`** - Create the exchange builder implementing build pattern
3. **`mod.rs`** - Set up module exports and builder

#### Add Authentication (If Needed)
4. **`auth.rs`** - Implement authentication logic if exchange requires unique auth
#### Add Transport Layer
4. **`rest.rs`** - Implement `RestClient` trait for HTTP communication
5. **`signer.rs`** - Implement `Signer` trait for authentication
6. **`codec.rs`** - Implement `Codec` trait for message encoding/decoding

#### Implement Core Functionality
5. **`converters.rs`** - Convert between exchange types and core types
6. **`market_data.rs`** - Implement market data retrieval and WebSocket subscriptions
7. **`trading.rs`** - Implement order placement and cancellation
8. **`account.rs`** - Implement account balance and position retrieval

#### Add Advanced Features (If Needed)
9. **`websocket.rs`** - Custom WebSocket handling for complex exchanges
7. **`conversions.rs`** - Convert between exchange types and core types
8. **`connector/mod.rs`** - Set up connector composition
9. **`connector/market_data.rs`** - Implement `MarketDataSource` trait
10. **`connector/trading.rs`** - Implement `OrderPlacer` trait
11. **`connector/account.rs`** - Implement `AccountInfo` trait

### Step 4: Register Your Exchange
Add your exchange to `src/exchanges/mod.rs`:
Expand All @@ -135,42 +135,56 @@ Consider adding your exchange to:

## 📋 Core Traits to Implement

Every exchange must implement these core traits (defined in `src/core/traits.rs`):
Every exchange must implement these core traits using the kernel architecture:

### Kernel Layer Traits (in `src/core/kernel/`)
1. **`RestClient`** - HTTP client abstraction for API communication
2. **`Signer`** - Authentication and request signing
3. **`Codec`** - Message encoding/decoding for WebSocket communication

### Exchange Layer Traits (in `src/core/traits.rs`)
1. **`ExchangeConnector`** - Base connector trait
2. **`MarketDataSource`** - Market data retrieval and WebSocket subscriptions
3. **`OrderPlacer`** - Order placement and cancellation
4. **`AccountInfo`** - Account balance and position information

Optional traits (for specific exchange types):
### Optional traits (for specific exchange types)
- **`FundingRateSource`** - For perpetual exchanges with funding rates

### Builder Pattern
- **`ExchangeBuilder`** - Consistent builder pattern for exchange instantiation

## 🎨 Design Patterns

### Lightweight Client Pattern
The `client.rs` file should be minimal, containing only:
- The main connector struct
- Basic configuration and setup
- Constructor methods
### Builder Pattern
The `builder.rs` file implements the builder pattern:
- Exchange builder struct (e.g., `BinanceBuilder`)
- `build()` method returning configured connector
- Configuration validation and setup

All functionality is implemented in separate modules.
### Kernel Integration Pattern
Each exchange leverages the kernel layer:
- `rest.rs` implements `RestClient` for HTTP communication
- `signer.rs` implements `Signer` for authentication
- `codec.rs` implements `Codec` for WebSocket message handling

### Trait-Based Implementation
Each module implements specific traits:
### Connector Composition Pattern
The `connector/` directory contains focused implementations:
- `market_data.rs` implements `MarketDataSource`
- `trading.rs` implements `OrderPlacer`
- `account.rs` implements `AccountInfo`
- `mod.rs` composes all connectors into final exchange connector

### Converter Pattern
The `converters.rs` module handles all data transformations:
The `conversions.rs` module handles all data transformations:
- Exchange format → Core format
- Core format → Exchange format
- Type safety and validation

### Authentication Reuse
Exchanges from the same provider can share authentication:
- `binance_perp` reuses `binance` auth
- `bybit_perp` reuses `bybit` auth
- `binance_perp` reuses `binance` signer
- `bybit_perp` reuses `bybit` signer

## 🔧 Development Tips

Expand Down Expand Up @@ -198,13 +212,13 @@ Create a basic example file in `examples/exchange_name_example.rs`:
// Basic example showing your exchange in action
use lotusx::{
core::{config::ExchangeConfig, traits::MarketDataSource},
exchanges::exchange_name::ExchangeNameConnector,
exchanges::exchange_name::ExchangeNameBuilder,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = ExchangeConfig::from_env("EXCHANGE_NAME")?;
let connector = ExchangeNameConnector::new(config);
let connector = ExchangeNameBuilder::new().build(config).await?;

// Test basic functionality
let markets = connector.get_markets().await?;
Expand Down
52 changes: 52 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,58 @@

All notable changes to the LotusX project will be documented in this file.

## PR-13

### Added
- **Kernel Architecture**: Complete architectural refactoring with unified transport layer
- **Core Kernel Module**: New `src/core/kernel/` with codec, REST client, WebSocket session, and signer abstractions
- **Unified Transport**: Standardized HTTP and WebSocket communication patterns across all exchanges
- **Builder Pattern**: New builder-based exchange instantiation replacing direct constructors
- **Modular Exchange Structure**: Consistent file organization with separate concerns per module
- **Transport Abstraction**: Generic `RestClient` and `WebSocketSession` traits for protocol-agnostic communication

### Technical Implementation
- **Kernel Components** (`src/core/kernel/`)
- **Codec**: Unified message encoding/decoding with `Codec` trait
- **REST Client**: Generic HTTP client abstraction with `RestClient` trait
- **WebSocket Session**: Unified WebSocket handling with `WebSocketSession` trait
- **Signer**: Authentication abstraction with `Signer` trait
- **Transport Layer**: Protocol-agnostic communication infrastructure

- **Exchange Refactoring**: All exchanges updated to use kernel architecture
- **Binance Spot & Perp**: Complete migration to builder pattern with modular structure
- **Bybit Spot & Perp**: Full kernel integration with unified transport
- **Hyperliquid**: Kernel-based architecture with EIP-712 authentication
- **Backpack**: Unified transport with builder pattern
- **Paradex**: Complete kernel integration with modular design

### Enhanced Architecture
- **Connector Pattern**: New `ExchangeConnector` structs composing sub-trait implementations
- **Modular Design**: Separate files for account, market data, trading, codec, conversions, REST, and signer
- **Builder Interface**: Consistent `ExchangeBuilder` pattern across all exchanges
- **Trait Composition**: Clean separation of concerns with focused trait implementations

### Performance Improvements
- **Unified Transport**: Optimized HTTP and WebSocket connection management
- **Memory Efficiency**: Reduced overhead through shared transport abstractions
- **Connection Pooling**: Efficient resource management in kernel layer
- **HFT Optimizations**: Low-latency patterns maintained with new architecture

### Breaking Changes
- **Exchange Instantiation**: All exchanges now use builder pattern instead of direct constructors
- **Client Removal**: Legacy client structs replaced with connector pattern
- **Import Changes**: Updated import paths for new modular structure
- **Configuration**: Builder-based configuration replacing direct config passing

### Code Quality
- **Consistency**: Unified patterns across all exchange implementations
- **Maintainability**: Clear separation of concerns with focused modules
- **Extensibility**: Easy addition of new exchanges with established patterns
- **Type Safety**: Enhanced compile-time validation through trait system

### Dependencies
- **num-traits**: Added for numeric trait abstractions in kernel layer

## PR-12

### Added
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading