Skip to main content
Drift Protocol

drift-rs SDK

Experimental, high-performance Rust SDK for building offchain clients for Drift V2 protocol on Solana. Built for speed, reliability, and real-time trading applications.

What is drift-rs?

drift-rs is a powerful Rust SDK that provides a complete toolkit for interacting with the Drift Protocol. It’s designed around a subscription model where live account updates are transparently cached and made accessible via accessor methods, giving you real-time access to market data, oracle prices, and account states.

Real-time Subscriptions

Subscribe to markets, oracles, and accounts via WebSocket or gRPC for live updates with minimal latency

Spot & Perpetual Markets

Full support for both spot and perpetual futures markets with comprehensive trading operations

Oracle Integration

Direct integration with Pyth and other oracle price feeds for accurate market pricing

DLOB (Decentralized Limit Order Book)

Build and query L2/L3 orderbook snapshots for market making and trading strategies

Transaction Builder

Intuitive transaction builder for placing orders, managing positions, and executing trades

Position Management

Track and manage perp and spot positions with real-time P&L calculations

JIT (Just-In-Time) Trading

Place-and-take functionality for immediate order matching with top makers

Event Streaming

Subscribe to order fills, cancellations, funding payments, and other protocol events

Key Features

Subscription Model

The DriftClient is built on a subscription model that provides:
  • WebSocket subscriptions for low-latency market and oracle updates
  • gRPC subscriptions for advanced use cases with automatic market/oracle subscription
  • Transparent caching where subscribed data is automatically cached locally
  • Fallback to RPC when not subscribed, for flexible ad-hoc queries

High Performance

  • Written in Rust for maximum performance and memory efficiency
  • Zero-copy deserialization where possible
  • Efficient caching layer for subscribed accounts
  • Built on tokio async runtime for concurrent operations

Developer Experience

  • Type-safe API with comprehensive type definitions
  • Automatically generated IDL types from the Drift program
  • Clone-able client for easy sharing across async tasks
  • Rich error handling with descriptive error types

Architecture

use drift_rs::{DriftClient, Context, RpcClient, Wallet, MarketId};
use solana_sdk::signature::Keypair;

// Initialize client
let client = DriftClient::new(
    Context::MainNet,
    RpcClient::new("https://api.mainnet-beta.solana.com"),
    Keypair::new().into(),
)
.await
.expect("connects");

// Subscribe to live updates
let markets = [MarketId::perp(0), MarketId::spot(1)];
client.subscribe_markets(&markets).await.unwrap();
client.subscribe_oracles(&markets).await.unwrap();

// Access cached data instantly
let sol_price = client.try_get_oracle_price_data_and_slot(MarketId::perp(0));

Use Cases

drift-rs is perfect for:
  • Market Making Bots: Build sophisticated market making strategies with real-time order book data
  • Trading Bots: Execute automated trading strategies with low-latency market access
  • Analytics Tools: Monitor markets, positions, and protocol events for analysis
  • Arbitrage Systems: Detect and execute arbitrage opportunities across markets
  • Risk Management: Track positions and P&L in real-time for portfolio management
  • Liquidation Bots: Monitor underwater positions and execute liquidations

Get Started

Installation

Install drift-rs and set up your development environment

Quick Start

Build your first drift-rs application in minutes

API Reference

Explore the complete API documentation

Examples

Browse real-world examples and use cases

Community & Support

drift-rs is experimental and under active development. Always test thoroughly before using in production.

Build docs developers (and LLMs) love