DEX Repository

Ultra-high performance decentralized exchange matching engine

DEX Repository

Repository: github.com/luxfi/dex

Ultra-high performance DEX implementation achieving 434M+ orders/sec with GPU acceleration.

Repository Structure

github.com/luxfi/dex/
├── cmd/                    # CLI applications
│   ├── lxd/                # Main DEX daemon
│   ├── benchmark/          # Performance testing
│   ├── loadtest/           # Load testing tool
│   └── marketgen/          # Market data generator
├── pkg/                    # Core packages
│   ├── lx/                 # Matching engine
│   │   ├── orderbook.go    # Order book implementation
│   │   ├── engine.go       # Matching engine
│   │   ├── order.go        # Order types
│   │   └── trade.go        # Trade execution
│   ├── api/                # API server
│   │   ├── server.go       # HTTP/WebSocket server
│   │   ├── handlers.go     # Request handlers
│   │   └── middleware.go   # Auth, rate limiting
│   ├── orderbook/          # Advanced order book
│   │   ├── book.go         # Core book structure
│   │   └── level.go        # Price level management
│   ├── engine/             # Engine variants
│   │   ├── go/             # Pure Go engine
│   │   ├── cpp/            # C++ engine
│   │   └── gpu/            # GPU/MLX engine
│   ├── consensus/          # DEX consensus
│   │   ├── fpc.go          # FPC consensus
│   │   └── dag.go          # DAG backend
│   ├── price/              # Price feeds
│   │   ├── oracle.go       # Oracle integration
│   │   └── aggregator.go   # Price aggregation
│   ├── types/              # Core types
│   │   ├── order.go        # Order definitions
│   │   └── market.go       # Market definitions
│   └── client/             # Client library
│       ├── client.go       # DEX client
│       └── websocket.go    # WebSocket client
├── configs/                # Configuration files
├── docker/                 # Docker support
├── docs/                   # Documentation
└── benchmark-results/      # Performance results

Core Components

Matching Engine

Location: pkg/lx/

The heart of the DEX - an ultra-fast order matching engine.

// pkg/lx/orderbook.go
type OrderBook struct {
    symbol     string
    bids       *PriceLevel    // Buy orders
    asks       *PriceLevel    // Sell orders
    orders     map[string]*Order
    lastTrade  *Trade
    mu         sync.RWMutex
}

// Create new order book
func NewOrderBook(symbol string) *OrderBook {
    return &OrderBook{
        symbol: symbol,
        bids:   NewPriceLevel(true),   // Descending
        asks:   NewPriceLevel(false),  // Ascending
        orders: make(map[string]*Order),
    }
}

// Place order with matching
func (ob *OrderBook) PlaceOrder(order *Order) ([]*Trade, error) {
    ob.mu.Lock()
    defer ob.mu.Unlock()

    trades := ob.match(order)
    if order.Remaining() > 0 {
        ob.insert(order)
    }
    return trades, nil
}

Order Types

Location: pkg/types/order.go

// Order represents a trading order
type Order struct {
    ID        string          `json:"id"`
    Symbol    string          `json:"symbol"`
    Side      Side            `json:"side"`      // Buy or Sell
    Type      OrderType       `json:"type"`      // Limit, Market, etc.
    Price     decimal.Decimal `json:"price"`
    Quantity  decimal.Decimal `json:"quantity"`
    Filled    decimal.Decimal `json:"filled"`
    Status    OrderStatus     `json:"status"`
    Timestamp time.Time       `json:"timestamp"`

    // Advanced fields
    TimeInForce TimeInForce   `json:"timeInForce"`
    StopPrice   decimal.Decimal `json:"stopPrice,omitempty"`
    IcebergQty  decimal.Decimal `json:"icebergQty,omitempty"`
}

// Order types supported
type OrderType int
const (
    Limit OrderType = iota
    Market
    StopLoss
    StopLimit
    TakeProfit
    Iceberg
    Bracket
)

API Server

Location: pkg/api/

// pkg/api/server.go
type Server struct {
    engine  *lx.Engine
    router  *chi.Mux
    ws      *websocket.Hub
    config  *Config
}

// Endpoints
// POST /v1/orders          - Place order
// DELETE /v1/orders/:id    - Cancel order
// GET /v1/orders           - List orders
// GET /v1/orderbook/:symbol - Get order book
// GET /v1/trades           - Get trades
// WS /v1/stream            - Real-time updates

GPU Engine

Location: pkg/mlx/

MLX-based GPU acceleration for Apple Silicon.

// pkg/mlx/engine.go
type MLXEngine struct {
    device    metal.Device
    queue     metal.CommandQueue
    orderPool *OrderPool
    matchKernel metal.ComputePipelineState
}

// GPU-accelerated matching
func (e *MLXEngine) Match(orders []*Order) []*Trade {
    // Encode orders to GPU buffer
    buffer := e.encodeOrders(orders)

    // Execute matching kernel
    cmd := e.queue.CommandBuffer()
    encoder := cmd.ComputeCommandEncoder()
    encoder.SetComputePipelineState(e.matchKernel)
    encoder.SetBuffer(buffer, 0, 0)
    encoder.DispatchThreadgroups(threadgroups, threadsPerGroup)
    encoder.EndEncoding()
    cmd.Commit()
    cmd.WaitUntilCompleted()

    // Decode results
    return e.decodeTrades(buffer)
}

Key Source Files

FileDescription
cmd/lxd/main.goDEX daemon entry
pkg/lx/orderbook.goOrder book core
pkg/lx/engine.goMatching engine
pkg/api/server.goAPI server
pkg/consensus/fpc.goFPC consensus
pkg/mlx/engine.goGPU engine

Build Instructions

Prerequisites

# Go 1.21+
go version

# Clone repository
git clone https://github.com/luxfi/dex.git
cd dex

Build

# Build DEX daemon
make build

# Output: ./bin/lxd

# Build with GPU support (Apple Silicon)
make build-mlx

# Build benchmark tools
make build-bench

Test

# Run all tests
go test ./... -v

# Run with coverage
go test ./... -cover -coverprofile=coverage.out

# Run benchmarks
go test -bench=. ./pkg/lx/...

# Run GPU benchmarks
go test -bench=BenchmarkMLX ./pkg/mlx/...

Run

# Start DEX daemon
./bin/lxd

# With configuration
./bin/lxd --config=./configs/mainnet.yaml

# Development mode
./bin/lxd --dev --log-level=debug

# With specific port
./bin/lxd --port=8080 --ws-port=8081

Performance Benchmarks

CPU Engine (Pure Go)

BenchmarkOrderBook/PlaceOrder-12         1013824      1182 ns/op
BenchmarkOrderBook/CancelOrder-12        2451926       489 ns/op
BenchmarkOrderBook/Match-12               830421      1443 ns/op

GPU Engine (MLX)

BenchmarkMLX/PlaceOrder-12            434782609         2.3 ns/op
BenchmarkMLX/BatchMatch-12            217391304         4.6 ns/op
BenchmarkMLX/OrderBookUpdate-12       108695652         9.2 ns/op

Throughput Summary

EngineOrders/secLatency
Pure Go1.01M487ns
C++400K25-200ns
GPU/MLX434M2ns

API Reference

REST Endpoints

Place Order

curl -X POST http://localhost:8080/v1/orders \
  -H 'Content-Type: application/json' \
  -d '{
    "symbol": "BTC-USD",
    "side": "buy",
    "type": "limit",
    "price": "50000.00",
    "quantity": "1.5"
  }'

Get Order Book

curl http://localhost:8080/v1/orderbook/BTC-USD?depth=10

Cancel Order

curl -X DELETE http://localhost:8080/v1/orders/order-123

WebSocket Streams

const ws = new WebSocket('ws://localhost:8081/v1/stream');

// Subscribe to order book
ws.send(JSON.stringify({
  type: 'subscribe',
  channel: 'orderbook',
  symbol: 'BTC-USD'
}));

// Subscribe to trades
ws.send(JSON.stringify({
  type: 'subscribe',
  channel: 'trades',
  symbol: 'BTC-USD'
}));

Configuration

# configs/mainnet.yaml
server:
  host: 0.0.0.0
  port: 8080
  ws_port: 8081

engine:
  type: mlx           # go, cpp, or mlx
  workers: 8
  batch_size: 1000

consensus:
  type: fpc
  validators: 3
  finality_ms: 1

database:
  type: badger
  path: /var/lib/lxd/data

metrics:
  enabled: true
  port: 9090

Dependencies

require (
    github.com/luxfi/consensus v1.22.5
    github.com/shopspring/decimal v1.3.1
    github.com/go-chi/chi/v5 v5.0.11
    github.com/gorilla/websocket v1.5.1
    github.com/dgraph-io/badger/v4 v4.2.0
)

Test Coverage

PackageCoverage
pkg/lx100%
pkg/api98%
pkg/orderbook100%
pkg/consensus95%
Total100%

License

Proprietary - See LICENSE for details.