open_book 0.1.13

open book client
Documentation
//! This code was AUTOGENERATED using the codama library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun codama to update it.
//!
//! <https://github.com/codama-idl/codama>
//!

use borsh::BorshDeserialize;
use borsh::BorshSerialize;

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MarketState {
    pub discriminator: [u8; 8],
    /// 0
    /// Initialized, Market
    pub account_flags: u64,
    /// 1
    pub own_address: [u64; 4],
    /// 5
    pub vault_signer_nonce: u64,
    /// 6
    pub coin_mint: [u64; 4],
    /// 10
    pub pc_mint: [u64; 4],
    /// 14
    pub coin_vault: [u64; 4],
    /// 18
    pub coin_deposits_total: u64,
    /// 19
    pub coin_fees_accrued: u64,
    /// 20
    pub pc_vault: [u64; 4],
    /// 24
    pub pc_deposits_total: u64,
    /// 25
    pub pc_fees_accrued: u64,
    /// 26
    pub pc_dust_threshold: u64,
    /// 27
    pub req_q: [u64; 4],
    /// 31
    pub event_q: [u64; 4],
    /// 35
    pub bids: [u64; 4],
    /// 39
    pub asks: [u64; 4],
    /// 43
    pub coin_lot_size: u64,
    /// 44
    pub pc_lot_size: u64,
    /// 45
    pub fee_rate_bps: u64,
    /// 46
    pub referrer_rebates_accrued: u64,
}

impl MarketState {
    pub const LEN: usize = 384;

    #[inline(always)]
    pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
        let mut data = data;
        Self::deserialize(&mut data)
    }
}

impl<'a> TryFrom<&solana_account_info::AccountInfo<'a>> for MarketState {
    type Error = std::io::Error;

    fn try_from(account_info: &solana_account_info::AccountInfo<'a>) -> Result<Self, Self::Error> {
        let mut data: &[u8] = &(*account_info.data).borrow();
        Self::deserialize(&mut data)
    }
}

#[cfg(feature = "fetch")]
pub fn fetch_market_state(
    rpc: &solana_client::rpc_client::RpcClient,
    address: &solana_pubkey::Pubkey,
) -> Result<crate::shared::DecodedAccount<MarketState>, std::io::Error> {
    let accounts = fetch_all_market_state(rpc, &[*address])?;
    Ok(accounts[0].clone())
}

#[cfg(feature = "fetch")]
pub fn fetch_all_market_state(
    rpc: &solana_client::rpc_client::RpcClient,
    addresses: &[solana_pubkey::Pubkey],
) -> Result<Vec<crate::shared::DecodedAccount<MarketState>>, std::io::Error> {
    let accounts = rpc
        .get_multiple_accounts(addresses)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
    let mut decoded_accounts: Vec<crate::shared::DecodedAccount<MarketState>> = Vec::new();
    for i in 0..addresses.len() {
        let address = addresses[i];
        let account = accounts[i].as_ref().ok_or(std::io::Error::new(
            std::io::ErrorKind::Other,
            format!("Account not found: {}", address),
        ))?;
        let data = MarketState::from_bytes(&account.data)?;
        decoded_accounts.push(crate::shared::DecodedAccount {
            address,
            account: account.clone(),
            data,
        });
    }
    Ok(decoded_accounts)
}

#[cfg(feature = "fetch")]
pub fn fetch_maybe_market_state(
    rpc: &solana_client::rpc_client::RpcClient,
    address: &solana_pubkey::Pubkey,
) -> Result<crate::shared::MaybeAccount<MarketState>, std::io::Error> {
    let accounts = fetch_all_maybe_market_state(rpc, &[*address])?;
    Ok(accounts[0].clone())
}

#[cfg(feature = "fetch")]
pub fn fetch_all_maybe_market_state(
    rpc: &solana_client::rpc_client::RpcClient,
    addresses: &[solana_pubkey::Pubkey],
) -> Result<Vec<crate::shared::MaybeAccount<MarketState>>, std::io::Error> {
    let accounts = rpc
        .get_multiple_accounts(addresses)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
    let mut decoded_accounts: Vec<crate::shared::MaybeAccount<MarketState>> = Vec::new();
    for i in 0..addresses.len() {
        let address = addresses[i];
        if let Some(account) = accounts[i].as_ref() {
            let data = MarketState::from_bytes(&account.data)?;
            decoded_accounts.push(crate::shared::MaybeAccount::Exists(
                crate::shared::DecodedAccount {
                    address,
                    account: account.clone(),
                    data,
                },
            ));
        } else {
            decoded_accounts.push(crate::shared::MaybeAccount::NotFound(address));
        }
    }
    Ok(decoded_accounts)
}

#[cfg(feature = "anchor")]
impl anchor_lang::AccountDeserialize for MarketState {
    fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
        Ok(Self::deserialize(buf)?)
    }
}

#[cfg(feature = "anchor")]
impl anchor_lang::AccountSerialize for MarketState {}

#[cfg(feature = "anchor")]
impl anchor_lang::Owner for MarketState {
    fn owner() -> Pubkey {
        crate::OPEN_BOOK_DEX_ID
    }
}

#[cfg(feature = "anchor-idl-build")]
impl anchor_lang::IdlBuild for MarketState {}

#[cfg(feature = "anchor-idl-build")]
impl anchor_lang::Discriminator for MarketState {
    const DISCRIMINATOR: [u8; 8] = [0; 8];
}