Skip to main content
The Context type defines the network configuration for connecting to Drift Protocol on different Solana networks.

Overview

pub struct Context {
    name: &'static str,
    luts: &'static [Pubkey],
    pyth: Pubkey,
}
Context contains network-specific variables necessary for interacting with the Drift program:
  • Market lookup tables (LUTs) for transaction compression
  • Pyth oracle program ID
  • Network identifier

Constants

Context::MainNet

Use this for production MainNet connections.
pub const MainNet: Context
Example:
use drift_rs::{DriftClient, Context, Wallet};
use solana_rpc_client::nonblocking::rpc_client::RpcClient;

let client = DriftClient::new(
    Context::MainNet,
    RpcClient::new("https://api.mainnet-beta.solana.com".to_string()),
    wallet,
).await?;

Context::DevNet

Use this for DevNet testing.
pub const DevNet: Context
Example:
let client = DriftClient::new(
    Context::DevNet,
    RpcClient::new("https://api.devnet.solana.com".to_string()),
    wallet,
).await?;

Methods

luts()

Get the address lookup tables for this network.
pub fn luts(&self) -> &'static [Pubkey]
Returns the list of lookup table public keys that contain frequently used addresses for the Drift protocol on this network.

pyth_program_id()

Get the Pyth oracle program ID for this network.
pub fn pyth_program_id(&self) -> &Pubkey
Returns the address of the Pyth program used for oracle price feeds.

name()

Get the network name.
pub fn name(&self) -> &'static str
Returns "mainnet" or "devnet".

Lookup Tables

Lookup tables (LUTs) are used to compress transactions by storing frequently used addresses. The Drift program uses lookup tables to reduce transaction sizes. MainNet LUTs:
const LUTS_MAINNET: &[Pubkey] = &[
    // MainNet lookup tables
];
DevNet LUTs:
const LUTS_DEVNET: &[Pubkey] = &[
    // DevNet lookup tables  
];

Usage with TransactionBuilder

The Context is automatically used when creating transactions:
let client = DriftClient::new(
    Context::MainNet,
    rpc_client,
    wallet,
).await?;

// Context is used internally for lookup tables
let tx = client
    .init_tx(&subaccount, false)
    .await?
    .place_orders(vec![order])
    .build();

Oracle Program IDs

Each network uses a different Pyth program address: MainNet Pyth:
pub const ID: Pubkey = solana_program::pubkey!("FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH");
DevNet Pyth:
pub const ID_DEVNET: Pubkey = solana_program::pubkey!("gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s");

Network Selection Best Practices

Always use the correct Context for your network
  • Use Context::MainNet for production applications
  • Use Context::DevNet for testing and development
  • Never mix contexts (e.g., MainNet context with DevNet RPC)
RPC and Context Must MatchEnsure your RPC endpoint matches your Context:
// ✅ Correct
let client = DriftClient::new(
    Context::MainNet,
    RpcClient::new("https://api.mainnet-beta.solana.com".to_string()),
    wallet,
).await?;

// ❌ Incorrect - Context and RPC mismatch
let client = DriftClient::new(
    Context::MainNet,  // MainNet context
    RpcClient::new("https://api.devnet.solana.com".to_string()),  // DevNet RPC
    wallet,
).await?;

Complete Example

use drift_rs::{DriftClient, Context, Wallet};
use solana_rpc_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::signature::Keypair;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wallet = Wallet::new(Keypair::new());
    
    // Connect to MainNet
    let mainnet_client = DriftClient::new(
        Context::MainNet,
        RpcClient::new("https://api.mainnet-beta.solana.com".to_string()),
        wallet.clone(),
    ).await?;
    
    println!("Connected to: {}", mainnet_client.context.name());
    println!("Using {} lookup tables", mainnet_client.context.luts().len());
    
    // Connect to DevNet for testing
    let devnet_client = DriftClient::new(
        Context::DevNet,
        RpcClient::new("https://api.devnet.solana.com".to_string()),
        wallet,
    ).await?;
    
    println!("Connected to: {}", devnet_client.context.name());
    
    Ok(())
}

See Also

Build docs developers (and LLMs) love