carbon-drift-v2-decoder 2.0.0

Drift v2 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::{ContractTier, ContractType, InsuranceClaim, MarketStatus, PoolBalance, AMM},
    solana_pubkey::Pubkey,
};

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct PerpMarket {
    /// The perp market's address. It is a pda of the market index
    pub pubkey: Pubkey,
    /// The automated market maker
    pub amm: AMM,
    /// The market's pnl pool. When users settle negative pnl, the balance
    /// increases. When users settle positive pnl, the balance decreases.
    /// Can not go negative.
    pub pnl_pool: PoolBalance,
    /// Encoded display name for the perp market e.g. SOL-PERP
    pub name: [u8; 32],
    /// The perp market's claim on the insurance fund
    pub insurance_claim: InsuranceClaim,
    /// The max pnl imbalance before positive pnl asset weight is discounted
    /// pnl imbalance is the difference between long and short pnl. When it's
    /// greater than 0, the amm has negative pnl and the initial asset
    /// weight for positive pnl is discounted precision = QUOTE_PRECISION
    pub unrealized_pnl_max_imbalance: u64,
    /// The ts when the market will be expired. Only set if market is in reduce
    /// only mode
    pub expiry_ts: i64,
    /// The price at which positions will be settled. Only set if market is
    /// expired precision = PRICE_PRECISION
    pub expiry_price: i64,
    /// Every trade has a fill record id. This is the next id to be used
    pub next_fill_record_id: u64,
    /// Every funding rate update has a record id. This is the next id to be
    /// used
    pub next_funding_rate_record_id: u64,
    /// Every amm k updated has a record id. This is the next id to be used
    pub next_curve_record_id: u64,
    /// The initial margin fraction factor. Used to increase margin ratio for
    /// large positions precision: MARGIN_PRECISION
    pub imf_factor: u32,
    /// The imf factor for unrealized pnl. Used to discount asset weight for
    /// large positive pnl precision: MARGIN_PRECISION
    pub unrealized_pnl_imf_factor: u32,
    /// The fee the liquidator is paid for taking over perp position
    /// precision: LIQUIDATOR_FEE_PRECISION
    pub liquidator_fee: u32,
    /// The fee the insurance fund receives from liquidation
    /// precision: LIQUIDATOR_FEE_PRECISION
    pub if_liquidation_fee: u32,
    /// The margin ratio which determines how much collateral is required to
    /// open a position e.g. margin ratio of .1 means a user must have $100
    /// of total collateral to open a $1000 position precision:
    /// MARGIN_PRECISION
    pub margin_ratio_initial: u32,
    /// The margin ratio which determines when a user will be liquidated
    /// e.g. margin ratio of .05 means a user must have $50 of total collateral
    /// to maintain a $1000 position else they will be liquidated
    /// precision: MARGIN_PRECISION
    pub margin_ratio_maintenance: u32,
    /// The initial asset weight for positive pnl. Negative pnl always has an
    /// asset weight of 1 precision: SPOT_WEIGHT_PRECISION
    pub unrealized_pnl_initial_asset_weight: u32,
    /// The maintenance asset weight for positive pnl. Negative pnl always has
    /// an asset weight of 1 precision: SPOT_WEIGHT_PRECISION
    pub unrealized_pnl_maintenance_asset_weight: u32,
    /// number of users in a position (base)
    pub number_of_users_with_base: u32,
    /// number of users in a position (pnl) or pnl (quote)
    pub number_of_users: u32,
    pub market_index: u16,
    /// Whether a market is active, reduce only, expired, etc
    /// Affects whether users can open/close positions
    pub status: MarketStatus,
    /// Currently only Perpetual markets are supported
    pub contract_type: ContractType,
    /// The contract tier determines how much insurance a market can receive,
    /// with more speculative markets receiving less insurance
    /// It also influences the order perp markets can be liquidated, with less
    /// speculative markets being liquidated first
    pub contract_tier: ContractTier,
    pub paused_operations: u8,
    /// The spot market that pnl is settled in
    pub quote_spot_market_index: u16,
    /// Between -100 and 100, represents what % to increase/decrease the fee by
    /// E.g. if this is -50 and the fee is 5bps, the new fee will be 2.5bps
    /// if this is 50 and the fee is 5bps, the new fee will be 7.5bps
    pub fee_adjustment: i16,
    /// fuel multiplier for perp funding
    /// precision: 10
    pub fuel_boost_position: u8,
    /// fuel multiplier for perp taker
    /// precision: 10
    pub fuel_boost_taker: u8,
    /// fuel multiplier for perp maker
    /// precision: 10
    pub fuel_boost_maker: u8,
    pub pool_id: u8,
    pub high_leverage_margin_ratio_initial: u16,
    pub high_leverage_margin_ratio_maintenance: u16,
    pub protected_maker_limit_price_divisor: u8,
    pub protected_maker_dynamic_divisor: u8,
    pub lp_fee_transfer_scalar: u8,
    pub lp_status: u8,
    pub lp_paused_operations: u8,
    pub lp_exchange_fee_excluscion_scalar: u8,
    pub last_fill_price: u64,
    pub lp_pool_id: u8,
    pub market_config: u8,
    pub padding: [u8; 22],
}

impl PerpMarket {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [10, 223, 12, 44, 107, 245, 55, 247] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

        borsh::BorshDeserialize::deserialize(&mut data_slice).ok()
    }
}