carbon-orca-whirlpool-decoder 1.0.0

Orca Whirlpool Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.

pub use super::super::events;
use carbon_core::{borsh, deserialize::ArrangeAccounts};

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(
    Debug,
    Clone,
    carbon_core::borsh::BorshSerialize,
    carbon_core::borsh::BorshDeserialize,
    PartialEq,
)]
pub enum CpiEvent {
    LiquidityDecreased(events::liquidity_decreased::LiquidityDecreasedEvent),
    LiquidityIncreased(events::liquidity_increased::LiquidityIncreasedEvent),
    LiquidityRepositioned(events::liquidity_repositioned::LiquidityRepositionedEvent),
    PoolInitialized(events::pool_initialized::PoolInitializedEvent),
    PositionOpened(events::position_opened::PositionOpenedEvent),
    Traded(events::traded::TradedEvent),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CpiEventInstructionAccounts {
    pub program: solana_pubkey::Pubkey,
    pub event_authority: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl CpiEvent {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [228, 69, 165, 46, 81, 203, 154, 29] {
            return None;
        }

        let event_data = &data[8..];

        if let Some(decoded) =
            events::liquidity_decreased::LiquidityDecreasedEvent::decode(event_data)
        {
            return Some(CpiEvent::LiquidityDecreased(decoded));
        }
        if let Some(decoded) =
            events::liquidity_increased::LiquidityIncreasedEvent::decode(event_data)
        {
            return Some(CpiEvent::LiquidityIncreased(decoded));
        }
        if let Some(decoded) =
            events::liquidity_repositioned::LiquidityRepositionedEvent::decode(event_data)
        {
            return Some(CpiEvent::LiquidityRepositioned(decoded));
        }
        if let Some(decoded) = events::pool_initialized::PoolInitializedEvent::decode(event_data) {
            return Some(CpiEvent::PoolInitialized(decoded));
        }
        if let Some(decoded) = events::position_opened::PositionOpenedEvent::decode(event_data) {
            return Some(CpiEvent::PositionOpened(decoded));
        }
        if let Some(decoded) = events::traded::TradedEvent::decode(event_data) {
            return Some(CpiEvent::Traded(decoded));
        }
        None
    }
}

impl ArrangeAccounts for CpiEvent {
    type ArrangedAccounts = CpiEventInstructionAccounts;

    fn arrange_accounts(
        accounts: &[solana_instruction::AccountMeta],
    ) -> Option<Self::ArrangedAccounts> {
        match accounts {
            [program, event_authority, remaining @ ..] => Some(CpiEventInstructionAccounts {
                program: program.pubkey,
                event_authority: event_authority.pubkey,
                remaining: remaining.to_vec(),
            }),
            [event_authority] => Some(CpiEventInstructionAccounts {
                program: crate::PROGRAM_ID,
                event_authority: event_authority.pubkey,
                remaining: Vec::new(),
            }),
            _ => None,
        }
    }
}