carbon-marginfi-v2-decoder 1.0.0

MarginfiV2 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::{OrderTriggerType, WrappedI80F48},
    solana_pubkey::Pubkey,
};

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct Order {
    pub marginfi_account: Pubkey,
    pub stop_loss: WrappedI80F48,
    pub take_profit: WrappedI80F48,
    /// Reserved for future use
    pub placeholder: u64,
    /// * a %, as u32, out of 100%, e.g. 50% = .5 * u32::MAX
    pub max_slippage: u32,
    pub pad0: [u8; 4],
    /// Active tags (currently 2). Remaining capacity is stored in padding for
    /// layout compatibility. Padding byte `ORDER_TAG_PADDING - 1` stores
    /// the tag count for forward compatibility. (u16 * 2 = 4 bytes)
    pub tags: [u16; 2],
    pub pad1: [u8; 4],
    pub tags_padding: [u8; 32],
    /// Stop Loss (0), Take Profit (1), or Both (2)
    pub trigger: OrderTriggerType,
    /// Bump to derive this pda
    pub bump: u8,
    pub pad2: [u8; 6],
    pub reserved1: [[u8; 32]; 4],
}

impl Order {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [134, 173, 223, 185, 77, 86, 28, 51] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

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