Skip to main content
The MarketId type provides a type-safe way to identify and work with Drift markets.

Type Definition

pub struct MarketId {
    index: u16,
    kind: MarketType,
}
Fields:
  • index - The numeric index of the market (0-65535)
  • kind - The type of market (Perp or Spot)

Constructors

new

Create a new MarketId from parts.
pub fn new(index: u16, kind: MarketType) -> Self
Example:
use drift_rs::{MarketId, MarketType};

let market_id = MarketId::new(0, MarketType::Perp);

perp

Create a perp market ID.
pub const fn perp(index: u16) -> Self
Example:
let sol_perp = MarketId::perp(0);
let btc_perp = MarketId::perp(1);

spot

Create a spot market ID.
pub const fn spot(index: u16) -> Self
Example:
let usdc_spot = MarketId::spot(0);
let sol_spot = MarketId::spot(1);

Constants

QUOTE_SPOT

The USDC spot market (market index 0).
pub const QUOTE_SPOT: MarketId = MarketId {
    index: 0,
    kind: MarketType::Spot,
};
Example:
use drift_rs::MarketId;

let usdc_market = MarketId::QUOTE_SPOT;

Methods

index

Get the market index.
pub fn index(&self) -> u16
Example:
let market_id = MarketId::perp(5);
assert_eq!(market_id.index(), 5);

kind

Get the market type.
pub fn kind(&self) -> MarketType
Example:
let market_id = MarketId::perp(0);
assert_eq!(market_id.kind(), MarketType::Perp);

to_parts

Convert the MarketId into its component parts.
pub fn to_parts(self) -> (u16, MarketType)
Example:
let market_id = MarketId::perp(3);
let (index, kind) = market_id.to_parts();
assert_eq!(index, 3);
assert_eq!(kind, MarketType::Perp);

is_perp

Check if this is a perp market.
pub fn is_perp(self) -> bool
Example:
let market_id = MarketId::perp(0);
assert!(market_id.is_perp());

is_spot

Check if this is a spot market.
pub fn is_spot(self) -> bool
Example:
let market_id = MarketId::spot(1);
assert!(market_id.is_spot());

Traits

Debug

MarketId implements Debug with a human-readable format. Format:
  • Perp markets: perp/{index}
  • Spot markets: spot/{index}
Example:
let sol_perp = MarketId::perp(0);
println!("{:?}", sol_perp); // Outputs: perp/0

let usdc_spot = MarketId::spot(0);
println!("{:?}", usdc_spot); // Outputs: spot/0

From<(u16, MarketType)>

Convert a tuple into a MarketId.
impl From<(u16, MarketType)> for MarketId
Example:
let market_id: MarketId = (5, MarketType::Perp).into();
assert_eq!(market_id.index(), 5);
assert!(market_id.is_perp());

Other Traits

MarketId also implements:
  • Copy - Can be copied
  • Clone - Can be cloned
  • Default - Defaults to MarketId { index: 0, kind: MarketType::Perp }
  • PartialEq - Can be compared for equality
  • Eq - Full equality
  • Hash - Can be used as a hash map key

Usage Examples

Using with DriftClient

use drift_rs::{DriftClient, MarketId};

// Get SOL-PERP market data
let sol_perp = MarketId::perp(0);
let market_account = drift_client
    .get_perp_market_account(sol_perp.index())
    .await?;

// Get oracle price
let oracle_data = drift_client
    .get_oracle_price_data_and_slot(sol_perp)
    .await?;

Iterating Over Markets

// Get all perp markets
for index in 0..10 {
    let market_id = MarketId::perp(index);
    if let Ok(market) = drift_client.try_get_perp_market_account(index) {
        println!("Market {:?}: {}", market_id, market.name());
    }
}

Using in Collections

use std::collections::HashMap;
use drift_rs::MarketId;

// MarketId can be used as a HashMap key
let mut positions: HashMap<MarketId, i64> = HashMap::new();
positions.insert(MarketId::perp(0), 1_000_000);
positions.insert(MarketId::perp(1), -500_000);

for (market, size) in &positions {
    println!("Position in {:?}: {}", market, size);
}

MarketType

Enum representing the type of market.
pub enum MarketType {
    Perp,
    Spot,
}
See Market Type documentation for more details.

Build docs developers (and LLMs) love