carbon-openbook-v2-decoder 2.0.0

Openbook V2 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts};
/// Process up to `limit` [events](crate::state::AnyEvent).
/// When a user places a 'take' order, they do not know beforehand which
/// market maker will have placed the 'make' order that they get executed
/// against. This prevents them from passing in a market maker's
/// [`OpenOrdersAccount`](crate::state::OpenOrdersAccount), which is needed
/// to credit/debit the relevant tokens to/from the maker. As such, Openbook
/// uses a 'crank' system, where `place_order` only emits events, and
/// `consume_events` handles token settlement.
/// Currently, there are two types of events:
/// [`FillEvent`](crate::state::FillEvent)s
/// and [`OutEvent`](crate::state::OutEvent)s.
/// A `FillEvent` is emitted when an order is filled, and it is handled by
/// debiting whatever the taker is selling from the taker and crediting
/// it to the maker, and debiting whatever the taker is buying from the
/// maker and crediting it to the taker. Note that *no tokens are moved*,
/// these are just debits and credits to each party's
/// [`Position`](crate::state::Position). An `OutEvent` is emitted when a limit
/// order needs to be removed from the book during a `place_order` invocation,
/// and it is handled by crediting whatever the maker would have sold (quote
/// token in a bid, base token in an ask) back to the maker.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct ConsumeEvents {
    pub limit: u64,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ConsumeEventsInstructionAccounts {
    pub consume_events_admin: Option<solana_pubkey::Pubkey>,
    pub market: solana_pubkey::Pubkey,
    pub event_heap: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl ConsumeEvents {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [221, 145, 177, 52, 31, 47, 63, 201] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

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

impl ArrangeAccounts for ConsumeEvents {
    type ArrangedAccounts = ConsumeEventsInstructionAccounts;

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

        let consume_events_admin = next_account(&mut iter);
        let market = next_account(&mut iter)?;
        let event_heap = next_account(&mut iter)?;

        let remaining = iter.as_slice();

        Some(ConsumeEventsInstructionAccounts {
            consume_events_admin,
            market,
            event_heap,
            remaining: remaining.to_vec(),
        })
    }
}