Skip to main content
The Drift program emits various events that can be subscribed to for monitoring activity on-chain.

Event Subscription

Events can be monitored using the EventSubscriber:
use drift_rs::EventSubscriber;

let event_subscriber = EventSubscriber::new(
    drift_client.program_data(),
    rpc_client,
    commitment,
);

event_subscriber.subscribe().await?;

User Events

NewUserRecord

Emitted when a new user account is created.
pub struct NewUserRecord {
    pub ts: i64,
    pub user_authority: Pubkey,
    pub user: Pubkey,
    pub sub_account_id: u16,
    pub name: [u8; 32],
    pub referrer: Pubkey,
}
Fields:
  • ts - Unix timestamp
  • user_authority - The authority (owner) of the user account
  • user - The user account address
  • sub_account_id - Sub-account ID (0-65535)
  • name - User-assigned name for the account
  • referrer - Referrer’s authority address

Deposit/Withdrawal Events

DepositRecord

Emitted for deposits and withdrawals.
pub struct DepositRecord {
    pub ts: i64,
    pub user_authority: Pubkey,
    pub user: Pubkey,
    pub direction: DepositDirection,
    pub deposit_record_id: u64,
    pub amount: u64,
    pub market_index: u16,
    pub oracle_price: i64,
    pub market_deposit_balance: u128,
    pub market_withdraw_balance: u128,
    pub market_cumulative_deposit_interest: u128,
    pub market_cumulative_borrow_interest: u128,
    pub total_deposits_after: u64,
    pub total_withdraws_after: u64,
    pub explanation: DepositExplanation,
    pub transfer_user: Option<Pubkey>,
    pub signer: Option<Pubkey>,
    pub user_token_amount_after: i128,
}
Key Fields:
  • direction - Deposit or Withdraw
  • amount - Amount deposited/withdrawn (native token precision)
  • market_index - Spot market index
  • explanation - Reason for the deposit/withdrawal

Order Events

OrderRecord

Emitted when an order is placed.
pub struct OrderRecord {
    pub ts: i64,
    pub user: Pubkey,
    pub order: Order,
}

OrderActionRecord

Emitted when an order action occurs (fill, cancel, etc.).
pub struct OrderActionRecord {
    pub ts: i64,
    pub action: OrderAction,
    pub action_explanation: OrderActionExplanation,
    pub market_index: u16,
    pub market_type: MarketType,
    pub filler: Option<Pubkey>,
    pub filler_reward: Option<u64>,
    pub fill_record_id: Option<u64>,
    pub base_asset_amount_filled: Option<u64>,
    pub quote_asset_amount_filled: Option<u64>,
    pub taker_fee: Option<u64>,
    pub maker_fee: Option<i64>,
    pub referrer_reward: Option<u32>,
    pub quote_asset_amount_surplus: Option<i64>,
    pub taker: Option<Pubkey>,
    pub taker_order_id: Option<u32>,
    pub taker_order_direction: Option<PositionDirection>,
    pub maker: Option<Pubkey>,
    pub maker_order_id: Option<u32>,
    pub maker_order_direction: Option<PositionDirection>,
    pub oracle_price: i64,
    pub bit_flags: u8,
    pub builder_idx: Option<u8>,
    pub builder_fee: Option<u64>,
}
Key Fields:
  • action - Type of action (Fill, Cancel, etc.)
  • taker - User taking the order
  • maker - User making liquidity
  • taker_fee - Fee paid by taker
  • maker_fee - Fee paid by maker (can be negative = rebate)

Funding Events

FundingPaymentRecord

Emitted when a user pays or receives funding.
pub struct FundingPaymentRecord {
    pub ts: i64,
    pub user_authority: Pubkey,
    pub user: Pubkey,
    pub market_index: u16,
    pub funding_payment: i64,
    pub base_asset_amount: i64,
    pub user_last_cumulative_funding: i64,
    pub amm_cumulative_funding_long: i128,
    pub amm_cumulative_funding_short: i128,
}
Fields:
  • funding_payment - Amount paid (negative) or received (positive)
  • base_asset_amount - Position size

FundingRateRecord

Emitted when funding rate is updated for a market.
pub struct FundingRateRecord {
    pub ts: i64,
    pub record_id: u64,
    pub market_index: u16,
    pub funding_rate: i64,
    pub funding_rate_long: i128,
    pub funding_rate_short: i128,
    pub cumulative_funding_rate_long: i128,
    pub cumulative_funding_rate_short: i128,
    pub oracle_price_twap: i64,
    pub mark_price_twap: u64,
    pub period_revenue: i64,
}

Liquidation Events

LiquidationRecord

Emitted when a user is liquidated.
pub struct LiquidationRecord {
    pub ts: i64,
    pub liquidation_type: LiquidationType,
    pub user: Pubkey,
    pub liquidator: Pubkey,
    pub margin_requirement: u128,
    pub total_collateral: i128,
    pub margin_freed: u64,
    pub liquidation_id: u16,
    pub bankrupt: bool,
    pub canceled_order_ids: Vec<u32>,
    pub liquidate_perp: LiquidatePerpRecord,
    pub liquidate_spot: LiquidateSpotRecord,
    pub liquidate_borrow_for_perp_pnl: LiquidateBorrowForPerpPnlRecord,
    pub liquidate_perp_pnl_for_deposit: LiquidatePerpPnlForDepositRecord,
    pub perp_bankruptcy: PerpBankruptcyRecord,
    pub spot_bankruptcy: SpotBankruptcyRecord,
}
Key Fields:
  • liquidation_type - Type of liquidation
  • user - User being liquidated
  • liquidator - User performing liquidation
  • bankrupt - Whether user was bankrupted
  • margin_freed - Amount of margin freed

Settlement Events

SettlePnlRecord

Emitted when PnL is settled.
pub struct SettlePnlRecord {
    pub ts: i64,
    pub user: Pubkey,
    pub market_index: u16,
    pub pnl: i128,
    pub base_asset_amount: i64,
    pub quote_asset_amount_after: i64,
    pub quote_entry_amount: i64,
    pub settle_price: i64,
    pub explanation: SettlePnlExplanation,
}

LP Events

LPRecord

Emitted for LP actions (add/remove liquidity).
pub struct LPRecord {
    pub ts: i64,
    pub user: Pubkey,
    pub action: LPAction,
    pub n_shares: u64,
    pub market_index: u16,
    pub delta_base_asset_amount: i64,
    pub delta_quote_asset_amount: i64,
    pub pnl: i64,
}
Fields:
  • action - AddLiquidity or RemoveLiquidity
  • n_shares - Number of LP shares
  • pnl - PnL from LP position

Market Events

CurveRecord

Emitted when AMM curve is updated.
pub struct CurveRecord {
    pub ts: i64,
    pub record_id: u64,
    pub market_index: u16,
    pub peg_multiplier_before: u128,
    pub base_asset_reserve_before: u128,
    pub quote_asset_reserve_before: u128,
    pub sqrt_k_before: u128,
    pub peg_multiplier_after: u128,
    pub base_asset_reserve_after: u128,
    pub quote_asset_reserve_after: u128,
    pub sqrt_k_after: u128,
    pub base_asset_amount_long: u128,
    pub base_asset_amount_short: u128,
    pub total_fee: i128,
    pub oracle_price: i64,
    pub fill_record: u128,
}

SpotInterestRecord

Emitted when spot market interest is updated.
pub struct SpotInterestRecord {
    pub ts: i64,
    pub market_index: u16,
    pub deposit_balance: u128,
    pub cumulative_deposit_interest: u128,
    pub borrow_balance: u128,
    pub cumulative_borrow_interest: u128,
    pub optimal_utilization: u32,
    pub optimal_borrow_rate: u32,
    pub max_borrow_rate: u32,
}

Usage Examples

Subscribing to Events

use drift_rs::{EventSubscriber, events::OrderActionRecord};

let event_subscriber = EventSubscriber::new(
    drift_client.program_data(),
    rpc_client,
    commitment,
);

event_subscriber.subscribe().await?;

// Process events
loop {
    if let Some(event) = event_subscriber.next_event::<OrderActionRecord>().await {
        println!("Order action: {:?}", event);
    }
}

Filtering Events

use drift_rs::events::{OrderActionRecord, OrderAction};

loop {
    if let Some(event) = event_subscriber.next_event::<OrderActionRecord>().await {
        if event.action == OrderAction::Fill {
            println!(
                "Order filled: {} at price {}",
                event.base_asset_amount_filled.unwrap_or(0),
                event.oracle_price
            );
        }
    }
}

Monitoring User Activity

use drift_rs::events::DepositRecord;

let my_user_account = user_pubkey;

loop {
    if let Some(event) = event_subscriber.next_event::<DepositRecord>().await {
        if event.user == my_user_account {
            println!(
                "{} ${} in market {}",
                if event.direction == DepositDirection::Deposit { "Deposited" } else { "Withdrew" },
                event.amount,
                event.market_index
            );
        }
    }
}

Build docs developers (and LLMs) love