#solana #trading #endpoint #orderly

orderly-connector-rs

A Rust client library for interacting with the Orderly Network API

34 releases

new 0.4.12 May 12, 2025
0.4.11 May 12, 2025
0.3.10 May 12, 2025
0.3.8 Apr 30, 2025
0.1.9 Apr 8, 2025

#1 in Magic Beans

Download history 92/week @ 2025-03-30 789/week @ 2025-04-06 1184/week @ 2025-04-13 2534/week @ 2025-04-20 2458/week @ 2025-04-27 1461/week @ 2025-05-04

7,658 downloads per month

MIT license

270KB
3.5K SLoC

Orderly Connector RS


Orderly Connector Rust

ranger.finance

Rust License: MIT Crates.io Documentation

Rust SDK for interacting with the Orderly Network API (v1/v2). For more information about Orderly Network, visit Orderly Documentation. This is based on the python connector orderly-evm-connector-python.

Features

  • REST Client: (rest::Client)
    • Access public endpoints (system status, exchange info, market data).
    • Access private, authenticated endpoints (account info, order management, positions).
    • Automatic request signing for private endpoints.
  • WebSocket Clients:
    • websocket::WebsocketPublicClient: Subscribe to public data streams (tickers, orderbooks, klines, etc.) for a specific account ID.
    • websocket::WebsocketPrivateClient: Subscribe to private data streams (execution reports, balance updates, position updates) using API key authentication.
    • Automatic WebSocket authentication for private streams.
    • Built-in reconnection logic with configurable retries.
  • Core Modules:
    • types: Request/response structures and enums.
    • auth: Authentication helpers (signing).
    • error: Custom error types.

Installation

Add the following to your Cargo.toml:

[dependencies]
orderly-connector-rs = "0.2.4" # or the latest version

Or install using cargo:

cargo add orderly-connector-rs

Quick Start

REST API Client

use orderly_connector_rs::rest::Client;
use orderly_connector_rs::types::OrderType;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a client with your API credentials
    let client = Client::new(
        "https://testnet-api-evm.orderly.org",  // API base URL
        Some("your_orderly_key"),               // Optional: API key
        Some("your_orderly_secret"),            // Optional: API secret
        Some("your_account_id"),                // Optional: Account ID
    )?;

    // Get system status
    let status = client.get_system_status().await?;
    println!("System status: {:?}", status);

    // Get exchange info
    let info = client.get_exchange_info(None).await?;
    println!("Exchange info: {:?}", info);

    Ok(())
}

WebSocket Client

use orderly_connector_rs::websocket::{WebsocketPublicClient, WebsocketClientConfig};
use orderly_connector_rs::types::OrderType;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create WebSocket configuration
    let config = WebsocketClientConfig {
        base_url: "wss://testnet-ws-evm.orderly.org".to_string(),
        orderly_key: Some("your_orderly_key".to_string()),
        orderly_secret: Some("your_orderly_secret".to_string()),
        orderly_account_id: Some("your_account_id".to_string()),
        wss_id: None,
    };

    // Create public WebSocket client
    let mut public_client = WebsocketPublicClient::new(config.clone())?;

    // Connect to WebSocket
    public_client.connect().await?;

    // Subscribe to orderbook updates
    public_client.subscribe_orderbook("PERP_ETH_USDC").await?;

    // Handle incoming messages
    while let Some(msg) = public_client.next().await {
        match msg {
            Ok(message) => println!("Received: {:?}", message),
            Err(e) => eprintln!("Error: {:?}", e),
        }
    }

    Ok(())
}

API Documentation

For detailed API documentation, visit docs.rs/orderly-connector-rs.

REST Client

The REST client provides methods for interacting with the Orderly Network API:

Public Endpoints

  • get_system_status(): Get the current system status
  • get_exchange_info(symbol: Option<String>): Get exchange information
  • get_market_trades(symbol: String, limit: Option<u32>): Get recent market trades
  • get_klines(symbol: String, interval: String, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>): Get kline/candlestick data

Private Endpoints

  • get_account_info(): Get account information
  • get_positions(symbol: Option<String>): Get current positions
  • create_order(request: CreateOrderRequest): Create a new order
  • get_orders(params: GetOrdersParams): Get order history
  • cancel_order(symbol: String, order_id: Option<String>, client_order_id: Option<String>): Cancel an order

Solana Integration

The SDK includes preliminary support for interacting with Orderly Network's Solana Vault program.

  • Solana Vault Program ID: ErBmAD61mGFKvrFNaTJuxoPwqrS8GgtwtqJTJVjFWx9Q

Functionality to prepare Solana transactions (like deposits) for signing is under development. See the implementation plan for details.

WebSocket Client

The WebSocket client provides real-time data streaming:

Public WebSocket

  • subscribe_ticker(symbol: String): Subscribe to ticker updates
  • subscribe_orderbook(symbol: String): Subscribe to orderbook updates
  • subscribe_trades(symbol: String): Subscribe to trade updates
  • subscribe_klines(symbol: String, interval: String): Subscribe to kline updates

Private WebSocket

  • subscribe_execution(): Subscribe to execution reports
  • subscribe_position(): Subscribe to position updates
  • subscribe_balance(): Subscribe to balance updates

Error Handling

The library uses a custom error type OrderlyError for error handling. All operations return a Result<T, OrderlyError> where T is the success type.

Example error handling:

match client.get_system_status().await {
    Ok(status) => println!("System status: {:?}", status),
    Err(OrderlyError::ClientError { status, code, message, .. }) => {
        eprintln!("Client error: {} (code: {})", message, code);
    },
    Err(e) => eprintln!("Error: {:?}", e),
}

Configuration

Environment Variables

The library supports configuration through environment variables:

ORDERLY_API_BASE_URL=https://testnet-api-evm.orderly.org
ORDERLY_KEY=your_orderly_key
ORDERLY_SECRET=your_orderly_secret
ORDERLY_ACCOUNT_ID=your_account_id

WebSocket Configuration

The WebsocketClientConfig struct allows you to configure:

  • base_url: WebSocket server URL
  • orderly_key: API key for authentication
  • orderly_secret: API secret for authentication
  • orderly_account_id: Account ID for private streams
  • wss_id: Optional WebSocket session ID

Examples

See the examples directory for more complete examples:

  • rest_client.rs: REST API usage examples
  • websocket_client.rs: WebSocket streaming examples
  • order_management.rs: Order creation and management examples

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

Dependencies

~82MB
~1.5M SLoC