o2-sdk 0.0.1

Rust SDK for the O2 Exchange — a fully on-chain order book DEX on the Fuel Network
Documentation
o2-sdk-0.0.1 has been yanked.

Installation

Add to your Cargo.toml:

[dependencies]
o2-sdk = { git = "https://github.com/o2-exchange/sdks.git", path = "sdks/rust" }
tokio = { version = "1", features = ["full"] }

MSRV: Rust 1.75

Quick Start

use o2_sdk::{O2Client, Network, Side, OrderType};

#[tokio::main]
async fn main() -> Result<(), o2_sdk::O2Error> {
    let mut client = O2Client::new(Network::Testnet);
    let wallet = client.generate_wallet()?;
    let account = client.setup_account(&wallet).await?;
    let mut session = client.create_session(&wallet, &["fFUEL/fUSDC"], 30).await?;
    let order = client.create_order(
        &mut session, "fFUEL/fUSDC", Side::Buy, "0.05".parse()?, "100".parse()?,
        OrderType::Spot, true, true,
    ).await?;
    println!("tx: {}", order.tx_id.unwrap_or_default());
    Ok(())
}

Features

  • Trading — Place, cancel, and manage orders with automatic price/quantity scaling
  • Market Data — Fetch order book depth, recent trades, OHLCV candles, and ticker data
  • WebSocket Streams — Real-time depth, order, trade, balance, and nonce updates via Stream
  • Wallet Support — Fuel-native and EVM wallets with session-based signing
  • Batch Actions — Submit up to 5 actions per request (cancel + settle + create in one call)
  • Async Runtime — Built on tokio with reqwest for HTTP and tokio-tungstenite for WebSocket
  • Type Safety — Strongly typed responses with serde deserialization and thiserror errors

API Overview

Method Description
generate_wallet() / load_wallet(hex) Create or load a Fuel wallet
generate_evm_wallet() / load_evm_wallet(hex) Create or load an EVM wallet
setup_account(&wallet) Idempotent account setup
create_session(&wallet, markets, days) Create a trading session
create_order(&mut session, market, side, price, qty, ...) Place an order
cancel_order(&mut session, order_id, market) Cancel a specific order
cancel_all_orders(&mut session, market) Cancel all open orders
settle_balance(&mut session, market) Settle filled order proceeds
batch_actions(&mut session, actions, calls, collect) Submit raw action batch
get_markets() / get_market(name) Fetch market info
get_depth(market, precision) / get_trades(market, count) Order book and trade data
get_balances(trade_account_id) / get_orders(id, market, ...) Account data
stream_depth(market_id, precision) Real-time order book stream
stream_orders(identities) / stream_trades(market_id) Real-time updates

See AGENTS.md for the complete API reference with all parameters and types.

Guides

Guide Description
Trading Order types, batch actions, cancel/replace, and market maker patterns
Market Data Fetching depth, trades, candles, tickers, and balances
WebSocket Streams Real-time data with TypedStream and reconnection handling
Error Handling Error types, recovery patterns, and robust trading loops
External Signers Integrating KMS/HSM via the SignableWallet trait

Examples

Example Description
quickstart.rs Connect, create a wallet, place your first order
market_maker.rs Two-sided quoting loop with cancel/replace
taker_bot.rs Monitor depth and take liquidity
portfolio.rs Multi-market balance tracking and management

Run an example:

cargo run --example quickstart

Testing

Unit tests (no network required):

cargo test

Integration tests (requires O2_PRIVATE_KEY env var):

O2_PRIVATE_KEY=0x... cargo test -- --ignored --test-threads=1

The --test-threads=1 flag avoids nonce race conditions during integration tests.

AI Agent Integration

See AGENTS.md for an LLM-optimized reference covering all methods, types, error codes, and common patterns.