13 releases

new 1.0.0-alpha.15 Apr 17, 2025
1.0.0-alpha.14 Mar 29, 2025
1.0.0-alpha.11 Feb 20, 2025
1.0.0-alpha.6 Jan 23, 2025
1.0.0-alpha.3 Oct 24, 2024

#446 in Magic Beans

Download history 106/week @ 2025-01-18 18/week @ 2025-01-25 124/week @ 2025-02-01 291/week @ 2025-02-08 218/week @ 2025-02-15 39/week @ 2025-02-22 24/week @ 2025-03-01 5/week @ 2025-03-08 76/week @ 2025-03-15 177/week @ 2025-03-22 157/week @ 2025-03-29 11/week @ 2025-04-05 74/week @ 2025-04-12

495 downloads per month

Apache-2.0

1.5MB
34K SLoC

drift-rs

Crates.io Docs Discord Chat License

drift-rs

Experimental, high performance Rust SDK for building offchain clients for Drift V2 protocol.

See the offical docs

Install

# point to git (recommended)
drift-rs = { git = "https://github.com/drift-labs/drift-rs", tag = "v1.0.0-alpha.14" }

# use crates.io*
drift-rs = "1.0.0-alpha.14"

* crates.io requires libdrift is installed and linked locally

Use

The DriftClient struct provides methods for reading drift program accounts and crafting transactions.
It is built on a subscription model where live account updates are transparently cached and made accessible via accessor methods.
The client may be subscribed either via Ws or gRPC.

use drift_rs::{AccountFilter, DriftClient, Wallet, grpc::GrpcSubscribeOpts};
use solana_sdk::signature::Keypair;

async fn main() {
    let client = DriftClient::new(
        Context::MainNet,
        RpcClient::new("https://rpc-provider.com"),
        Keypair::new().into(),
    )
    .await
    .expect("connects");

    // Subscribe via WebSocket
    //
    // 1) Ws-based live market and price changes
    let markets = [MarketId::spot(1), MarketId::perp(0)];
    client.subscribe_markets(&markets).await.unwrap();
    client.subscribe_oracles(&markets).await.unwrap();
    client.subscribe_account("SUBACCOUNT_1");

    // OR 2) subscribe via gRPC (advanced)
    // gRPC automatically subscribes to all markets and oracles
    client.grpc_subscribe(
      "https://grpc.example.com".into(),
      "API-X-TOKEN".into(),
      GrpcSubscribeOpts::default()
        .user_accounts("SUBACCOUNT_1", "SUB_ACCOUNT_2")
        .on_slot(move |new_slot| {
          // do something on slot
        })
        .on_account(
          AccountFilter::partial().with_discriminator(User::DISCRIMINATOR),
          move |account| {
              // do something on user account updates
          })
    ).await;

    //
    // Fetch latest values
    ///
    let sol_perp_price = client.oracle_price(MarketId::perp(0));
    let subaccount_1: User = client.try_get_account("SUBACCOUNT_1"));

Setup

Mac

Install rosetta (m-series only) and configure Rust toolchain for x86_64
⚠️ 1.76.0-x86_64 must also be installed alongside latest stable rust

softwareupdate --install-rosetta

# replace '1.85.0' with preferred latest stable version
rustup install 1.85.0-x86_64-apple-darwin 1.76.0-x86_64-apple-darwin --force-non-host

rustup override set 1.85.0-x86_64-apple-darwin

Linux

# replace '1.85.0' with preferred latest stable version
rustup install 1.85.0-x86_64-unknown-linux-gnu 1.76.0-x86_64-unknown-linux-gnu --force-non-host

rustup override set 1.85.0-x86_64-unknown-linux-gnu

⚠️ the non-x86_64 toolchains are incompatible due to memory layout differences between solana program (BPF) and aarch64 and will fail at runtime with deserialization errors like: InvalidSize.

Local Development

drift-rs links to the drift program crate via FFI, build from source (default) by cloning git submodule or dynamically link with a version from drift-ffi-sys

clone repo and submodules

git clone https://github.com/drift-labs/drift-rs &&\
cd drift-rs &&\
git submodule update --init --recursive

build

# Build from source (default)
CARGO_DRIFT_FFI_STATIC=1

# Provide a prebuilt drift_ffi_sys lib 
CARGO_DRIFT_FFI_PATH="/path/to/libdrift_ffi_sys"

Development

Release

git tag v<MAJOR.MINOR.PATCH> && git push

Updating IDL types

from repo root dir:

./scripts/idl-update.sh
cargo check # build new IDL types
# commit changes...

Dependencies

~54–77MB
~1.5M SLoC