carbon-openbook-v2-decoder 2.0.0

Openbook V2 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::PlaceOrderArgs,
    carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts},
};
/// Place an order.
/// Different types of orders have different effects on the order book,
/// as described in [`PlaceOrderType`](crate::state::PlaceOrderType).
/// `price_lots` refers to the price in lots: the number of quote lots
/// per base lot. It is ignored for `PlaceOrderType::Market` orders.
/// `expiry_timestamp` is a unix timestamp for when this order should
/// expire. If 0 is passed in, the order will never expire. If the time
/// is in the past, the instruction is skipped. Timestamps in the future
/// are reduced to now + 65,535s.
/// `limit` determines the maximum number of orders from the book to fill,
/// and can be used to limit CU spent. When the limit is reached, processing
/// stops and the instruction succeeds.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct PlaceOrder {
    pub args: PlaceOrderArgs,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PlaceOrderInstructionAccounts {
    pub signer: solana_pubkey::Pubkey,
    pub open_orders_account: solana_pubkey::Pubkey,
    pub open_orders_admin: Option<solana_pubkey::Pubkey>,
    pub user_token_account: solana_pubkey::Pubkey,
    pub market: solana_pubkey::Pubkey,
    pub bids: solana_pubkey::Pubkey,
    pub asks: solana_pubkey::Pubkey,
    pub event_heap: solana_pubkey::Pubkey,
    pub market_vault: solana_pubkey::Pubkey,
    pub oracle_a: Option<solana_pubkey::Pubkey>,
    pub oracle_b: Option<solana_pubkey::Pubkey>,
    pub token_program: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl PlaceOrder {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [51, 194, 155, 175, 109, 130, 96, 106] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

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

impl ArrangeAccounts for PlaceOrder {
    type ArrangedAccounts = PlaceOrderInstructionAccounts;

    fn arrange_accounts(
        accounts: &[solana_instruction::AccountMeta],
    ) -> Option<Self::ArrangedAccounts> {
        let mut iter = accounts.iter();

        let signer = next_account(&mut iter)?;
        let open_orders_account = next_account(&mut iter)?;
        let open_orders_admin = next_account(&mut iter);
        let user_token_account = next_account(&mut iter)?;
        let market = next_account(&mut iter)?;
        let bids = next_account(&mut iter)?;
        let asks = next_account(&mut iter)?;
        let event_heap = next_account(&mut iter)?;
        let market_vault = next_account(&mut iter)?;
        let oracle_a = next_account(&mut iter);
        let oracle_b = next_account(&mut iter);
        let token_program = next_account(&mut iter)?;

        let remaining = iter.as_slice();

        Some(PlaceOrderInstructionAccounts {
            signer,
            open_orders_account,
            open_orders_admin,
            user_token_account,
            market,
            bids,
            asks,
            event_heap,
            market_vault,
            oracle_a,
            oracle_b,
            token_program,
            remaining: remaining.to_vec(),
        })
    }
}