carbon-dflow-aggregator-v4-decoder 2.0.0

Dflow Aggregator V4 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::Swap2Params,
    carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts},
};
/// Executes a token swap with a slippage fee. Outputs to the specified
/// destination account.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct Swap2WithDestination {
    pub params: Swap2Params,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Swap2WithDestinationInstructionAccounts {
    pub token_program: solana_pubkey::Pubkey,
    pub associated_token_program: solana_pubkey::Pubkey,
    pub system_program: solana_pubkey::Pubkey,
    pub user_token_authority: solana_pubkey::Pubkey,
    pub destination_token_account: solana_pubkey::Pubkey,
    pub destination_token_authority: solana_pubkey::Pubkey,
    pub destination_mint: solana_pubkey::Pubkey,
    pub event_authority: solana_pubkey::Pubkey,
    pub program: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl Swap2WithDestination {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [95, 123, 213, 246, 122, 1, 86, 231] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

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

impl ArrangeAccounts for Swap2WithDestination {
    type ArrangedAccounts = Swap2WithDestinationInstructionAccounts;

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

        let token_program = next_account(&mut iter)?;
        let associated_token_program = next_account(&mut iter)?;
        let system_program = next_account(&mut iter)?;
        let user_token_authority = next_account(&mut iter)?;
        let destination_token_account = next_account(&mut iter)?;
        let destination_token_authority = next_account(&mut iter)?;
        let destination_mint = next_account(&mut iter)?;
        let event_authority = next_account(&mut iter)?;
        let program = next_account(&mut iter)?;

        let remaining = iter.as_slice();

        Some(Swap2WithDestinationInstructionAccounts {
            token_program,
            associated_token_program,
            system_program,
            user_token_authority,
            destination_token_account,
            destination_token_authority,
            destination_mint,
            event_authority,
            program,
            remaining: remaining.to_vec(),
        })
    }
}