Skip to main content

DriftGrpcClient

The DriftGrpcClient provides high-performance streaming of account updates, transaction updates, slot updates, and block metadata through Yellowstone gRPC. It supports advanced filtering and runs on a dedicated thread for optimal performance.

Constructor

pub fn new(endpoint: String, x_token: String) -> Self
Parameters:
  • endpoint - gRPC server endpoint URL
  • x_token - Authentication token for the gRPC service
Example:
let mut grpc_client = DriftGrpcClient::new(
    "https://grpc.example.com".to_string(),
    "your-auth-token".to_string()
);

Configuration

grpc_connection_opts

pub fn grpc_connection_opts(mut self, grpc_opts: GrpcConnectionOpts) -> Self
Set gRPC network connection options. Example:
let grpc_client = DriftGrpcClient::new(endpoint, token)
    .grpc_connection_opts(
        GrpcConnectionOpts::default().enable_compression()
    );

Callback Registration

on_account

pub fn on_account<T: Fn(&AccountUpdate) + Send + Sync + 'static>(
    &mut self,
    filter: AccountFilter,
    on_account: T,
)
Register a callback for account updates matching the specified filter. Can be called multiple times to register multiple callbacks. Important: on_account must prioritize fast handling to avoid blocking the gRPC thread. Parameters:
  • filter - Filter criteria for matching accounts
  • on_account - Callback function invoked on filter matches
Example:
use drift::grpc::AccountFilter;

let filter = AccountFilter::full()
    .with_discriminator(User::DISCRIMINATOR)
    .with_accounts([pubkey1, pubkey2].into_iter());

grpc_client.on_account(filter, |update| {
    println!("Account: {}, Slot: {}", update.pubkey, update.slot);
});

on_transaction

pub fn on_transaction<T: Fn(&TransactionUpdate) + Send + Sync + 'static>(
    &mut self,
    on_transaction: T,
)
Register a callback for transaction updates. Use with transactions_accounts_include in GeyserSubscribeOpts to filter by account. Example:
grpc_client.on_transaction(|tx_update| {
    println!("Transaction at slot: {}", tx_update.slot);
});

on_slot

pub fn on_slot<F: Fn(Slot) + Send + Sync + 'static>(&mut self, on_slot: F)
Register a callback for slot updates. Example:
grpc_client.on_slot(|slot| {
    println!("New slot: {}", slot);
});

on_block_meta

pub fn on_block_meta<F: Fn(SubscribeUpdateBlockMeta) + Send + Sync + 'static>(
    &mut self,
    on_block_meta: F,
)
Register a callback for block metadata updates.

Subscription

subscribe

pub async fn subscribe(
    self,
    commitment: CommitmentLevel,
    subscribe_opts: GeyserSubscribeOpts,
) -> Result<UnsubHandle, GrpcError>
Start the gRPC subscription for configured updates. Parameters:
  • commitment - Commitment level (Processed, Confirmed, or Finalized)
  • subscribe_opts - Subscription options and filters
Returns:
  • UnsubHandle - Handle to unsubscribe from the stream
Example:
use solana_sdk::commitment_config::CommitmentLevel;

let unsub_handle = grpc_client.subscribe(
    CommitmentLevel::Confirmed,
    GeyserSubscribeOpts {
        accounts_owners: vec![drift::constants::PROGRAM_ID.to_string()],
        slot_updates: true,
        ..Default::default()
    }
).await?;

AccountFilter

Provides flexible filtering criteria for account updates.

Filter Modes

Full Match - All criteria must be satisfied:
let filter = AccountFilter::full()
    .with_discriminator(User::DISCRIMINATOR)
    .with_memcmp(memcmp);
Partial Match - Any one criterion triggers a match:
let filter = AccountFilter::partial()
    .with_discriminator(User::DISCRIMINATOR)
    .with_accounts([acc1, acc2].into_iter());
Firehose - Matches ALL accounts:
let filter = AccountFilter::firehose();

Methods

pub fn with_accounts(mut self, pubkeys: impl Iterator<Item = Pubkey>) -> Self
pub fn with_discriminator(mut self, discriminator: &'static [u8]) -> Self
pub fn with_memcmp(mut self, memcmp: Memcmp) -> Self
pub fn matches(&self, pubkey: &Pubkey, account: &SubscribeUpdateAccountInfo) -> bool

GeyserSubscribeOpts

Configuration for gRPC subscription filters.
pub struct GeyserSubscribeOpts {
    pub accounts_memcmp: Vec<Memcmp>,
    pub accounts_datasize: Option<u64>,
    pub accounts_owners: Vec<String>,
    pub accounts_pubkeys: Vec<String>,
    pub from_slot: Option<u64>,
    pub ping: Option<i32>,
    pub interslot_updates: Option<bool>,
    pub transactions_accounts_include: Vec<String>,
    pub transactions_accounts_exclude: Vec<String>,
    pub transactions_accounts_required: Vec<String>,
    pub blocks_meta: bool,
    pub slot_updates: bool,
}
Example:
let opts = GeyserSubscribeOpts {
    accounts_owners: vec![drift_program_id.to_string()],
    accounts_datasize: Some(1024),
    slot_updates: true,
    transactions_accounts_include: vec![my_account.to_string()],
    ..Default::default()
};

GrpcConnectionOpts

Advanced gRPC connection settings for tuning performance and reliability.
pub struct GrpcConnectionOpts {
    pub connect_timeout_ms: Option<u64>,
    pub buffer_size: Option<usize>,
    pub http2_adaptive_window: Option<bool>,
    pub http2_keep_alive_interval_ms: Option<u64>,
    pub initial_connection_window_size: Option<u32>,
    pub initial_stream_window_size: Option<u32>,
    pub keep_alive_timeout_ms: Option<u64>,
    pub keep_alive_while_idle: Option<bool>,
    pub tcp_keepalive_ms: Option<u64>,
    pub tcp_nodelay: Option<bool>,
    pub timeout_ms: Option<u64>,
    pub max_decoding_message_size: usize,  // default: 1GiB
    pub compression: bool,
}

enable_compression

pub fn enable_compression(mut self) -> Self
Enable zstd compression for the gRPC connection.

Error Types

pub enum GrpcError {
    Geyser(GeyserGrpcBuilderError),
    Client(GeyserGrpcClientError),
    Stream(Status),
}

Build docs developers (and LLMs) love