carbon-orca-whirlpool-decoder 1.0.0

Orca Whirlpool Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts};
/// Perform a swap in this Whirlpool
/// ### Authority
/// - "token_authority" - The authority to withdraw tokens from the input token
///   account.
///
/// ### Parameters
/// - `amount` - The amount of input or output token to swap from (depending on
///   amount_specified_is_input).
/// - `other_amount_threshold` - The maximum/minimum of input/output token to
///   swap into (depending on amount_specified_is_input).
/// - `sqrt_price_limit` - The maximum/minimum price the swap will swap to.
/// - `amount_specified_is_input` - Specifies the token the parameter
///   `amount`represents. If true, the amount represents the input token of the
///   swap.
/// - `a_to_b` - The direction of the swap. True if swapping from A to B. False
///   if swapping from B to A.
///
/// #### Special Errors
/// - `ZeroTradableAmount` - User provided parameter `amount` is 0.
/// - `InvalidSqrtPriceLimitDirection` - User provided parameter
///   `sqrt_price_limit` does not match the direction of the trade.
/// - `SqrtPriceOutOfBounds` - User provided parameter `sqrt_price_limit` is
///   over Whirlppool's max/min bounds for sqrt-price.
/// - `InvalidTickArraySequence` - User provided tick-arrays are not in
///   sequential order required to proceed in this trade direction.
/// - `TickArraySequenceInvalidIndex` - The swap loop attempted to access an
///   invalid array index during the query of the next initialized tick.
/// - `TickArrayIndexOutofBounds` - The swap loop attempted to access an invalid
///   array index during tick crossing.
/// - `LiquidityOverflow` - Liquidity value overflowed 128bits during tick
///   crossing.
/// - `InvalidTickSpacing` - The swap pool was initialized with tick-spacing of
///   0.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct Swap {
    pub amount: u64,
    pub other_amount_threshold: u64,
    pub sqrt_price_limit: u128,
    pub amount_specified_is_input: bool,
    pub a_to_b: bool,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SwapInstructionAccounts {
    pub token_program: solana_pubkey::Pubkey,
    pub token_authority: solana_pubkey::Pubkey,
    pub whirlpool: solana_pubkey::Pubkey,
    pub token_owner_account_a: solana_pubkey::Pubkey,
    pub token_vault_a: solana_pubkey::Pubkey,
    pub token_owner_account_b: solana_pubkey::Pubkey,
    pub token_vault_b: solana_pubkey::Pubkey,
    pub tick_array0: solana_pubkey::Pubkey,
    pub tick_array1: solana_pubkey::Pubkey,
    pub tick_array2: solana_pubkey::Pubkey,
    pub oracle: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl Swap {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [248, 198, 158, 145, 225, 117, 135, 200] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

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

impl ArrangeAccounts for Swap {
    type ArrangedAccounts = SwapInstructionAccounts;

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

        let token_program = next_account(&mut iter)?;
        let token_authority = next_account(&mut iter)?;
        let whirlpool = next_account(&mut iter)?;
        let token_owner_account_a = next_account(&mut iter)?;
        let token_vault_a = next_account(&mut iter)?;
        let token_owner_account_b = next_account(&mut iter)?;
        let token_vault_b = next_account(&mut iter)?;
        let tick_array0 = next_account(&mut iter)?;
        let tick_array1 = next_account(&mut iter)?;
        let tick_array2 = next_account(&mut iter)?;
        let oracle = next_account(&mut iter)?;

        let remaining = iter.as_slice();

        Some(SwapInstructionAccounts {
            token_program,
            token_authority,
            whirlpool,
            token_owner_account_a,
            token_vault_a,
            token_owner_account_b,
            token_vault_b,
            tick_array0,
            tick_array1,
            tick_array2,
            oracle,
            remaining: remaining.to_vec(),
        })
    }
}