//! 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;

/// Accounts.
#[derive(Debug)]
pub struct Initialize2 {
    pub token_program: solana_pubkey::Pubkey,

    pub spl_associated_token_account: solana_pubkey::Pubkey,

    pub system_program: solana_pubkey::Pubkey,

    pub rent: solana_pubkey::Pubkey,

    pub amm: solana_pubkey::Pubkey,

    pub amm_authority: solana_pubkey::Pubkey,

    pub amm_open_orders: solana_pubkey::Pubkey,

    pub lp_mint: solana_pubkey::Pubkey,

    pub coin_mint: solana_pubkey::Pubkey,

    pub pc_mint: solana_pubkey::Pubkey,

    pub pool_coin_token_account: solana_pubkey::Pubkey,

    pub pool_pc_token_account: solana_pubkey::Pubkey,

    pub pool_withdraw_queue: solana_pubkey::Pubkey,

    pub amm_target_orders: solana_pubkey::Pubkey,

    pub pool_temp_lp: solana_pubkey::Pubkey,

    pub serum_program: solana_pubkey::Pubkey,

    pub serum_market: solana_pubkey::Pubkey,

    pub user_wallet: solana_pubkey::Pubkey,

    pub user_token_coin: solana_pubkey::Pubkey,

    pub user_token_pc: solana_pubkey::Pubkey,

    pub user_lp_token_account: solana_pubkey::Pubkey,
}

impl Initialize2 {
    pub fn instruction(&self, args: Initialize2InstructionArgs) -> solana_instruction::Instruction {
        self.instruction_with_remaining_accounts(args, &[])
    }
    #[allow(clippy::arithmetic_side_effects)]
    #[allow(clippy::vec_init_then_push)]
    pub fn instruction_with_remaining_accounts(
        &self,
        args: Initialize2InstructionArgs,
        remaining_accounts: &[solana_instruction::AccountMeta],
    ) -> solana_instruction::Instruction {
        let mut accounts = Vec::with_capacity(21 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.token_program,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.spl_associated_token_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.system_program,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.rent, false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(self.amm, false));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.amm_authority,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.amm_open_orders,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(self.lp_mint, false));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.coin_mint,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.pc_mint,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.pool_coin_token_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.pool_pc_token_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.pool_withdraw_queue,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.amm_target_orders,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.pool_temp_lp,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.serum_program,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.serum_market,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(self.user_wallet, true));
        accounts.push(solana_instruction::AccountMeta::new(
            self.user_token_coin,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.user_token_pc,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.user_lp_token_account,
            false,
        ));
        accounts.extend_from_slice(remaining_accounts);
        let mut data = borsh::to_vec(&Initialize2InstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&args).unwrap();
        data.append(&mut args);

        solana_instruction::Instruction {
            program_id: crate::RAYDIUM_AMM_ID,
            accounts,
            data,
        }
    }
}

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Initialize2InstructionData {
    discriminator: [u8; 1],
}

impl Initialize2InstructionData {
    pub fn new() -> Self {
        Self { discriminator: [1] }
    }
}

impl Default for Initialize2InstructionData {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Initialize2InstructionArgs {
    pub nonce: u8,
    pub open_time: u64,
    pub init_pc_amount: u64,
    pub init_coin_amount: u64,
}

/// Instruction builder for `Initialize2`.
///
/// ### Accounts:
///
///   0. `[optional]` token_program (default to `TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA`)
///   1. `[]` spl_associated_token_account
///   2. `[optional]` system_program (default to `11111111111111111111111111111111`)
///   3. `[optional]` rent (default to `SysvarRent111111111111111111111111111111111`)
///   4. `[writable]` amm
///   5. `[]` amm_authority
///   6. `[writable]` amm_open_orders
///   7. `[writable]` lp_mint
///   8. `[]` coin_mint
///   9. `[]` pc_mint
///   10. `[writable]` pool_coin_token_account
///   11. `[writable]` pool_pc_token_account
///   12. `[writable]` pool_withdraw_queue
///   13. `[writable]` amm_target_orders
///   14. `[writable]` pool_temp_lp
///   15. `[]` serum_program
///   16. `[]` serum_market
///   17. `[writable, signer]` user_wallet
///   18. `[writable]` user_token_coin
///   19. `[writable]` user_token_pc
///   20. `[writable]` user_lp_token_account
#[derive(Clone, Debug, Default)]
pub struct Initialize2Builder {
    token_program: Option<solana_pubkey::Pubkey>,
    spl_associated_token_account: Option<solana_pubkey::Pubkey>,
    system_program: Option<solana_pubkey::Pubkey>,
    rent: Option<solana_pubkey::Pubkey>,
    amm: Option<solana_pubkey::Pubkey>,
    amm_authority: Option<solana_pubkey::Pubkey>,
    amm_open_orders: Option<solana_pubkey::Pubkey>,
    lp_mint: Option<solana_pubkey::Pubkey>,
    coin_mint: Option<solana_pubkey::Pubkey>,
    pc_mint: Option<solana_pubkey::Pubkey>,
    pool_coin_token_account: Option<solana_pubkey::Pubkey>,
    pool_pc_token_account: Option<solana_pubkey::Pubkey>,
    pool_withdraw_queue: Option<solana_pubkey::Pubkey>,
    amm_target_orders: Option<solana_pubkey::Pubkey>,
    pool_temp_lp: Option<solana_pubkey::Pubkey>,
    serum_program: Option<solana_pubkey::Pubkey>,
    serum_market: Option<solana_pubkey::Pubkey>,
    user_wallet: Option<solana_pubkey::Pubkey>,
    user_token_coin: Option<solana_pubkey::Pubkey>,
    user_token_pc: Option<solana_pubkey::Pubkey>,
    user_lp_token_account: Option<solana_pubkey::Pubkey>,
    nonce: Option<u8>,
    open_time: Option<u64>,
    init_pc_amount: Option<u64>,
    init_coin_amount: Option<u64>,
    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
}

impl Initialize2Builder {
    pub fn new() -> Self {
        Self::default()
    }
    /// `[optional account, default to 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA']`
    #[inline(always)]
    pub fn token_program(&mut self, token_program: solana_pubkey::Pubkey) -> &mut Self {
        self.token_program = Some(token_program);
        self
    }
    #[inline(always)]
    pub fn spl_associated_token_account(
        &mut self,
        spl_associated_token_account: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.spl_associated_token_account = Some(spl_associated_token_account);
        self
    }
    /// `[optional account, default to '11111111111111111111111111111111']`
    #[inline(always)]
    pub fn system_program(&mut self, system_program: solana_pubkey::Pubkey) -> &mut Self {
        self.system_program = Some(system_program);
        self
    }
    /// `[optional account, default to 'SysvarRent111111111111111111111111111111111']`
    #[inline(always)]
    pub fn rent(&mut self, rent: solana_pubkey::Pubkey) -> &mut Self {
        self.rent = Some(rent);
        self
    }
    #[inline(always)]
    pub fn amm(&mut self, amm: solana_pubkey::Pubkey) -> &mut Self {
        self.amm = Some(amm);
        self
    }
    #[inline(always)]
    pub fn amm_authority(&mut self, amm_authority: solana_pubkey::Pubkey) -> &mut Self {
        self.amm_authority = Some(amm_authority);
        self
    }
    #[inline(always)]
    pub fn amm_open_orders(&mut self, amm_open_orders: solana_pubkey::Pubkey) -> &mut Self {
        self.amm_open_orders = Some(amm_open_orders);
        self
    }
    #[inline(always)]
    pub fn lp_mint(&mut self, lp_mint: solana_pubkey::Pubkey) -> &mut Self {
        self.lp_mint = Some(lp_mint);
        self
    }
    #[inline(always)]
    pub fn coin_mint(&mut self, coin_mint: solana_pubkey::Pubkey) -> &mut Self {
        self.coin_mint = Some(coin_mint);
        self
    }
    #[inline(always)]
    pub fn pc_mint(&mut self, pc_mint: solana_pubkey::Pubkey) -> &mut Self {
        self.pc_mint = Some(pc_mint);
        self
    }
    #[inline(always)]
    pub fn pool_coin_token_account(
        &mut self,
        pool_coin_token_account: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.pool_coin_token_account = Some(pool_coin_token_account);
        self
    }
    #[inline(always)]
    pub fn pool_pc_token_account(
        &mut self,
        pool_pc_token_account: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.pool_pc_token_account = Some(pool_pc_token_account);
        self
    }
    #[inline(always)]
    pub fn pool_withdraw_queue(&mut self, pool_withdraw_queue: solana_pubkey::Pubkey) -> &mut Self {
        self.pool_withdraw_queue = Some(pool_withdraw_queue);
        self
    }
    #[inline(always)]
    pub fn amm_target_orders(&mut self, amm_target_orders: solana_pubkey::Pubkey) -> &mut Self {
        self.amm_target_orders = Some(amm_target_orders);
        self
    }
    #[inline(always)]
    pub fn pool_temp_lp(&mut self, pool_temp_lp: solana_pubkey::Pubkey) -> &mut Self {
        self.pool_temp_lp = Some(pool_temp_lp);
        self
    }
    #[inline(always)]
    pub fn serum_program(&mut self, serum_program: solana_pubkey::Pubkey) -> &mut Self {
        self.serum_program = Some(serum_program);
        self
    }
    #[inline(always)]
    pub fn serum_market(&mut self, serum_market: solana_pubkey::Pubkey) -> &mut Self {
        self.serum_market = Some(serum_market);
        self
    }
    #[inline(always)]
    pub fn user_wallet(&mut self, user_wallet: solana_pubkey::Pubkey) -> &mut Self {
        self.user_wallet = Some(user_wallet);
        self
    }
    #[inline(always)]
    pub fn user_token_coin(&mut self, user_token_coin: solana_pubkey::Pubkey) -> &mut Self {
        self.user_token_coin = Some(user_token_coin);
        self
    }
    #[inline(always)]
    pub fn user_token_pc(&mut self, user_token_pc: solana_pubkey::Pubkey) -> &mut Self {
        self.user_token_pc = Some(user_token_pc);
        self
    }
    #[inline(always)]
    pub fn user_lp_token_account(
        &mut self,
        user_lp_token_account: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.user_lp_token_account = Some(user_lp_token_account);
        self
    }
    #[inline(always)]
    pub fn nonce(&mut self, nonce: u8) -> &mut Self {
        self.nonce = Some(nonce);
        self
    }
    #[inline(always)]
    pub fn open_time(&mut self, open_time: u64) -> &mut Self {
        self.open_time = Some(open_time);
        self
    }
    #[inline(always)]
    pub fn init_pc_amount(&mut self, init_pc_amount: u64) -> &mut Self {
        self.init_pc_amount = Some(init_pc_amount);
        self
    }
    #[inline(always)]
    pub fn init_coin_amount(&mut self, init_coin_amount: u64) -> &mut Self {
        self.init_coin_amount = Some(init_coin_amount);
        self
    }
    /// Add an additional account to the instruction.
    #[inline(always)]
    pub fn add_remaining_account(&mut self, account: solana_instruction::AccountMeta) -> &mut Self {
        self.__remaining_accounts.push(account);
        self
    }
    /// Add additional accounts to the instruction.
    #[inline(always)]
    pub fn add_remaining_accounts(
        &mut self,
        accounts: &[solana_instruction::AccountMeta],
    ) -> &mut Self {
        self.__remaining_accounts.extend_from_slice(accounts);
        self
    }
    #[allow(clippy::clone_on_copy)]
    pub fn instruction(&self) -> solana_instruction::Instruction {
        let accounts = Initialize2 {
            token_program: self.token_program.unwrap_or(solana_pubkey::pubkey!(
                "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
            )),
            spl_associated_token_account: self
                .spl_associated_token_account
                .expect("spl_associated_token_account is not set"),
            system_program: self
                .system_program
                .unwrap_or(solana_pubkey::pubkey!("11111111111111111111111111111111")),
            rent: self.rent.unwrap_or(solana_pubkey::pubkey!(
                "SysvarRent111111111111111111111111111111111"
            )),
            amm: self.amm.expect("amm is not set"),
            amm_authority: self.amm_authority.expect("amm_authority is not set"),
            amm_open_orders: self.amm_open_orders.expect("amm_open_orders is not set"),
            lp_mint: self.lp_mint.expect("lp_mint is not set"),
            coin_mint: self.coin_mint.expect("coin_mint is not set"),
            pc_mint: self.pc_mint.expect("pc_mint is not set"),
            pool_coin_token_account: self
                .pool_coin_token_account
                .expect("pool_coin_token_account is not set"),
            pool_pc_token_account: self
                .pool_pc_token_account
                .expect("pool_pc_token_account is not set"),
            pool_withdraw_queue: self
                .pool_withdraw_queue
                .expect("pool_withdraw_queue is not set"),
            amm_target_orders: self
                .amm_target_orders
                .expect("amm_target_orders is not set"),
            pool_temp_lp: self.pool_temp_lp.expect("pool_temp_lp is not set"),
            serum_program: self.serum_program.expect("serum_program is not set"),
            serum_market: self.serum_market.expect("serum_market is not set"),
            user_wallet: self.user_wallet.expect("user_wallet is not set"),
            user_token_coin: self.user_token_coin.expect("user_token_coin is not set"),
            user_token_pc: self.user_token_pc.expect("user_token_pc is not set"),
            user_lp_token_account: self
                .user_lp_token_account
                .expect("user_lp_token_account is not set"),
        };
        let args = Initialize2InstructionArgs {
            nonce: self.nonce.clone().expect("nonce is not set"),
            open_time: self.open_time.clone().expect("open_time is not set"),
            init_pc_amount: self
                .init_pc_amount
                .clone()
                .expect("init_pc_amount is not set"),
            init_coin_amount: self
                .init_coin_amount
                .clone()
                .expect("init_coin_amount is not set"),
        };

        accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
    }
}

/// `initialize2` CPI accounts.
pub struct Initialize2CpiAccounts<'a, 'b> {
    pub token_program: &'b solana_account_info::AccountInfo<'a>,

    pub spl_associated_token_account: &'b solana_account_info::AccountInfo<'a>,

    pub system_program: &'b solana_account_info::AccountInfo<'a>,

    pub rent: &'b solana_account_info::AccountInfo<'a>,

    pub amm: &'b solana_account_info::AccountInfo<'a>,

    pub amm_authority: &'b solana_account_info::AccountInfo<'a>,

    pub amm_open_orders: &'b solana_account_info::AccountInfo<'a>,

    pub lp_mint: &'b solana_account_info::AccountInfo<'a>,

    pub coin_mint: &'b solana_account_info::AccountInfo<'a>,

    pub pc_mint: &'b solana_account_info::AccountInfo<'a>,

    pub pool_coin_token_account: &'b solana_account_info::AccountInfo<'a>,

    pub pool_pc_token_account: &'b solana_account_info::AccountInfo<'a>,

    pub pool_withdraw_queue: &'b solana_account_info::AccountInfo<'a>,

    pub amm_target_orders: &'b solana_account_info::AccountInfo<'a>,

    pub pool_temp_lp: &'b solana_account_info::AccountInfo<'a>,

    pub serum_program: &'b solana_account_info::AccountInfo<'a>,

    pub serum_market: &'b solana_account_info::AccountInfo<'a>,

    pub user_wallet: &'b solana_account_info::AccountInfo<'a>,

    pub user_token_coin: &'b solana_account_info::AccountInfo<'a>,

    pub user_token_pc: &'b solana_account_info::AccountInfo<'a>,

    pub user_lp_token_account: &'b solana_account_info::AccountInfo<'a>,
}

/// `initialize2` CPI instruction.
pub struct Initialize2Cpi<'a, 'b> {
    /// The program to invoke.
    pub __program: &'b solana_account_info::AccountInfo<'a>,

    pub token_program: &'b solana_account_info::AccountInfo<'a>,

    pub spl_associated_token_account: &'b solana_account_info::AccountInfo<'a>,

    pub system_program: &'b solana_account_info::AccountInfo<'a>,

    pub rent: &'b solana_account_info::AccountInfo<'a>,

    pub amm: &'b solana_account_info::AccountInfo<'a>,

    pub amm_authority: &'b solana_account_info::AccountInfo<'a>,

    pub amm_open_orders: &'b solana_account_info::AccountInfo<'a>,

    pub lp_mint: &'b solana_account_info::AccountInfo<'a>,

    pub coin_mint: &'b solana_account_info::AccountInfo<'a>,

    pub pc_mint: &'b solana_account_info::AccountInfo<'a>,

    pub pool_coin_token_account: &'b solana_account_info::AccountInfo<'a>,

    pub pool_pc_token_account: &'b solana_account_info::AccountInfo<'a>,

    pub pool_withdraw_queue: &'b solana_account_info::AccountInfo<'a>,

    pub amm_target_orders: &'b solana_account_info::AccountInfo<'a>,

    pub pool_temp_lp: &'b solana_account_info::AccountInfo<'a>,

    pub serum_program: &'b solana_account_info::AccountInfo<'a>,

    pub serum_market: &'b solana_account_info::AccountInfo<'a>,

    pub user_wallet: &'b solana_account_info::AccountInfo<'a>,

    pub user_token_coin: &'b solana_account_info::AccountInfo<'a>,

    pub user_token_pc: &'b solana_account_info::AccountInfo<'a>,

    pub user_lp_token_account: &'b solana_account_info::AccountInfo<'a>,
    /// The arguments for the instruction.
    pub __args: Initialize2InstructionArgs,
}

impl<'a, 'b> Initialize2Cpi<'a, 'b> {
    pub fn new(
        program: &'b solana_account_info::AccountInfo<'a>,
        accounts: Initialize2CpiAccounts<'a, 'b>,
        args: Initialize2InstructionArgs,
    ) -> Self {
        Self {
            __program: program,
            token_program: accounts.token_program,
            spl_associated_token_account: accounts.spl_associated_token_account,
            system_program: accounts.system_program,
            rent: accounts.rent,
            amm: accounts.amm,
            amm_authority: accounts.amm_authority,
            amm_open_orders: accounts.amm_open_orders,
            lp_mint: accounts.lp_mint,
            coin_mint: accounts.coin_mint,
            pc_mint: accounts.pc_mint,
            pool_coin_token_account: accounts.pool_coin_token_account,
            pool_pc_token_account: accounts.pool_pc_token_account,
            pool_withdraw_queue: accounts.pool_withdraw_queue,
            amm_target_orders: accounts.amm_target_orders,
            pool_temp_lp: accounts.pool_temp_lp,
            serum_program: accounts.serum_program,
            serum_market: accounts.serum_market,
            user_wallet: accounts.user_wallet,
            user_token_coin: accounts.user_token_coin,
            user_token_pc: accounts.user_token_pc,
            user_lp_token_account: accounts.user_lp_token_account,
            __args: args,
        }
    }
    #[inline(always)]
    pub fn invoke(&self) -> solana_program_entrypoint::ProgramResult {
        self.invoke_signed_with_remaining_accounts(&[], &[])
    }
    #[inline(always)]
    pub fn invoke_with_remaining_accounts(
        &self,
        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
    ) -> solana_program_entrypoint::ProgramResult {
        self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
    }
    #[inline(always)]
    pub fn invoke_signed(
        &self,
        signers_seeds: &[&[&[u8]]],
    ) -> solana_program_entrypoint::ProgramResult {
        self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
    }
    #[allow(clippy::arithmetic_side_effects)]
    #[allow(clippy::clone_on_copy)]
    #[allow(clippy::vec_init_then_push)]
    pub fn invoke_signed_with_remaining_accounts(
        &self,
        signers_seeds: &[&[&[u8]]],
        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
    ) -> solana_program_entrypoint::ProgramResult {
        let mut accounts = Vec::with_capacity(21 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.token_program.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.spl_associated_token_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.system_program.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.rent.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(*self.amm.key, false));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.amm_authority.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.amm_open_orders.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.lp_mint.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.coin_mint.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.pc_mint.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.pool_coin_token_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.pool_pc_token_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.pool_withdraw_queue.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.amm_target_orders.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.pool_temp_lp.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.serum_program.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.serum_market.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.user_wallet.key,
            true,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.user_token_coin.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.user_token_pc.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.user_lp_token_account.key,
            false,
        ));
        remaining_accounts.iter().for_each(|remaining_account| {
            accounts.push(solana_instruction::AccountMeta {
                pubkey: *remaining_account.0.key,
                is_signer: remaining_account.1,
                is_writable: remaining_account.2,
            })
        });
        let mut data = borsh::to_vec(&Initialize2InstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&self.__args).unwrap();
        data.append(&mut args);

        let instruction = solana_instruction::Instruction {
            program_id: crate::RAYDIUM_AMM_ID,
            accounts,
            data,
        };
        let mut account_infos = Vec::with_capacity(22 + remaining_accounts.len());
        account_infos.push(self.__program.clone());
        account_infos.push(self.token_program.clone());
        account_infos.push(self.spl_associated_token_account.clone());
        account_infos.push(self.system_program.clone());
        account_infos.push(self.rent.clone());
        account_infos.push(self.amm.clone());
        account_infos.push(self.amm_authority.clone());
        account_infos.push(self.amm_open_orders.clone());
        account_infos.push(self.lp_mint.clone());
        account_infos.push(self.coin_mint.clone());
        account_infos.push(self.pc_mint.clone());
        account_infos.push(self.pool_coin_token_account.clone());
        account_infos.push(self.pool_pc_token_account.clone());
        account_infos.push(self.pool_withdraw_queue.clone());
        account_infos.push(self.amm_target_orders.clone());
        account_infos.push(self.pool_temp_lp.clone());
        account_infos.push(self.serum_program.clone());
        account_infos.push(self.serum_market.clone());
        account_infos.push(self.user_wallet.clone());
        account_infos.push(self.user_token_coin.clone());
        account_infos.push(self.user_token_pc.clone());
        account_infos.push(self.user_lp_token_account.clone());
        remaining_accounts
            .iter()
            .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));

        if signers_seeds.is_empty() {
            solana_cpi::invoke(&instruction, &account_infos)
        } else {
            solana_cpi::invoke_signed(&instruction, &account_infos, signers_seeds)
        }
    }
}

/// Instruction builder for `Initialize2` via CPI.
///
/// ### Accounts:
///
///   0. `[]` token_program
///   1. `[]` spl_associated_token_account
///   2. `[]` system_program
///   3. `[]` rent
///   4. `[writable]` amm
///   5. `[]` amm_authority
///   6. `[writable]` amm_open_orders
///   7. `[writable]` lp_mint
///   8. `[]` coin_mint
///   9. `[]` pc_mint
///   10. `[writable]` pool_coin_token_account
///   11. `[writable]` pool_pc_token_account
///   12. `[writable]` pool_withdraw_queue
///   13. `[writable]` amm_target_orders
///   14. `[writable]` pool_temp_lp
///   15. `[]` serum_program
///   16. `[]` serum_market
///   17. `[writable, signer]` user_wallet
///   18. `[writable]` user_token_coin
///   19. `[writable]` user_token_pc
///   20. `[writable]` user_lp_token_account
#[derive(Clone, Debug)]
pub struct Initialize2CpiBuilder<'a, 'b> {
    instruction: Box<Initialize2CpiBuilderInstruction<'a, 'b>>,
}

impl<'a, 'b> Initialize2CpiBuilder<'a, 'b> {
    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
        let instruction = Box::new(Initialize2CpiBuilderInstruction {
            __program: program,
            token_program: None,
            spl_associated_token_account: None,
            system_program: None,
            rent: None,
            amm: None,
            amm_authority: None,
            amm_open_orders: None,
            lp_mint: None,
            coin_mint: None,
            pc_mint: None,
            pool_coin_token_account: None,
            pool_pc_token_account: None,
            pool_withdraw_queue: None,
            amm_target_orders: None,
            pool_temp_lp: None,
            serum_program: None,
            serum_market: None,
            user_wallet: None,
            user_token_coin: None,
            user_token_pc: None,
            user_lp_token_account: None,
            nonce: None,
            open_time: None,
            init_pc_amount: None,
            init_coin_amount: None,
            __remaining_accounts: Vec::new(),
        });
        Self { instruction }
    }
    #[inline(always)]
    pub fn token_program(
        &mut self,
        token_program: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.token_program = Some(token_program);
        self
    }
    #[inline(always)]
    pub fn spl_associated_token_account(
        &mut self,
        spl_associated_token_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.spl_associated_token_account = Some(spl_associated_token_account);
        self
    }
    #[inline(always)]
    pub fn system_program(
        &mut self,
        system_program: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.system_program = Some(system_program);
        self
    }
    #[inline(always)]
    pub fn rent(&mut self, rent: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.rent = Some(rent);
        self
    }
    #[inline(always)]
    pub fn amm(&mut self, amm: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.amm = Some(amm);
        self
    }
    #[inline(always)]
    pub fn amm_authority(
        &mut self,
        amm_authority: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.amm_authority = Some(amm_authority);
        self
    }
    #[inline(always)]
    pub fn amm_open_orders(
        &mut self,
        amm_open_orders: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.amm_open_orders = Some(amm_open_orders);
        self
    }
    #[inline(always)]
    pub fn lp_mint(&mut self, lp_mint: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.lp_mint = Some(lp_mint);
        self
    }
    #[inline(always)]
    pub fn coin_mint(&mut self, coin_mint: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.coin_mint = Some(coin_mint);
        self
    }
    #[inline(always)]
    pub fn pc_mint(&mut self, pc_mint: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.pc_mint = Some(pc_mint);
        self
    }
    #[inline(always)]
    pub fn pool_coin_token_account(
        &mut self,
        pool_coin_token_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.pool_coin_token_account = Some(pool_coin_token_account);
        self
    }
    #[inline(always)]
    pub fn pool_pc_token_account(
        &mut self,
        pool_pc_token_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.pool_pc_token_account = Some(pool_pc_token_account);
        self
    }
    #[inline(always)]
    pub fn pool_withdraw_queue(
        &mut self,
        pool_withdraw_queue: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.pool_withdraw_queue = Some(pool_withdraw_queue);
        self
    }
    #[inline(always)]
    pub fn amm_target_orders(
        &mut self,
        amm_target_orders: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.amm_target_orders = Some(amm_target_orders);
        self
    }
    #[inline(always)]
    pub fn pool_temp_lp(
        &mut self,
        pool_temp_lp: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.pool_temp_lp = Some(pool_temp_lp);
        self
    }
    #[inline(always)]
    pub fn serum_program(
        &mut self,
        serum_program: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.serum_program = Some(serum_program);
        self
    }
    #[inline(always)]
    pub fn serum_market(
        &mut self,
        serum_market: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.serum_market = Some(serum_market);
        self
    }
    #[inline(always)]
    pub fn user_wallet(
        &mut self,
        user_wallet: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.user_wallet = Some(user_wallet);
        self
    }
    #[inline(always)]
    pub fn user_token_coin(
        &mut self,
        user_token_coin: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.user_token_coin = Some(user_token_coin);
        self
    }
    #[inline(always)]
    pub fn user_token_pc(
        &mut self,
        user_token_pc: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.user_token_pc = Some(user_token_pc);
        self
    }
    #[inline(always)]
    pub fn user_lp_token_account(
        &mut self,
        user_lp_token_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.user_lp_token_account = Some(user_lp_token_account);
        self
    }
    #[inline(always)]
    pub fn nonce(&mut self, nonce: u8) -> &mut Self {
        self.instruction.nonce = Some(nonce);
        self
    }
    #[inline(always)]
    pub fn open_time(&mut self, open_time: u64) -> &mut Self {
        self.instruction.open_time = Some(open_time);
        self
    }
    #[inline(always)]
    pub fn init_pc_amount(&mut self, init_pc_amount: u64) -> &mut Self {
        self.instruction.init_pc_amount = Some(init_pc_amount);
        self
    }
    #[inline(always)]
    pub fn init_coin_amount(&mut self, init_coin_amount: u64) -> &mut Self {
        self.instruction.init_coin_amount = Some(init_coin_amount);
        self
    }
    /// Add an additional account to the instruction.
    #[inline(always)]
    pub fn add_remaining_account(
        &mut self,
        account: &'b solana_account_info::AccountInfo<'a>,
        is_writable: bool,
        is_signer: bool,
    ) -> &mut Self {
        self.instruction
            .__remaining_accounts
            .push((account, is_writable, is_signer));
        self
    }
    /// Add additional accounts to the instruction.
    ///
    /// Each account is represented by a tuple of the `AccountInfo`, a `bool` indicating whether the account is writable or not,
    /// and a `bool` indicating whether the account is a signer or not.
    #[inline(always)]
    pub fn add_remaining_accounts(
        &mut self,
        accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
    ) -> &mut Self {
        self.instruction
            .__remaining_accounts
            .extend_from_slice(accounts);
        self
    }
    #[inline(always)]
    pub fn invoke(&self) -> solana_program_entrypoint::ProgramResult {
        self.invoke_signed(&[])
    }
    #[allow(clippy::clone_on_copy)]
    #[allow(clippy::vec_init_then_push)]
    pub fn invoke_signed(
        &self,
        signers_seeds: &[&[&[u8]]],
    ) -> solana_program_entrypoint::ProgramResult {
        let args = Initialize2InstructionArgs {
            nonce: self.instruction.nonce.clone().expect("nonce is not set"),
            open_time: self
                .instruction
                .open_time
                .clone()
                .expect("open_time is not set"),
            init_pc_amount: self
                .instruction
                .init_pc_amount
                .clone()
                .expect("init_pc_amount is not set"),
            init_coin_amount: self
                .instruction
                .init_coin_amount
                .clone()
                .expect("init_coin_amount is not set"),
        };
        let instruction = Initialize2Cpi {
            __program: self.instruction.__program,

            token_program: self
                .instruction
                .token_program
                .expect("token_program is not set"),

            spl_associated_token_account: self
                .instruction
                .spl_associated_token_account
                .expect("spl_associated_token_account is not set"),

            system_program: self
                .instruction
                .system_program
                .expect("system_program is not set"),

            rent: self.instruction.rent.expect("rent is not set"),

            amm: self.instruction.amm.expect("amm is not set"),

            amm_authority: self
                .instruction
                .amm_authority
                .expect("amm_authority is not set"),

            amm_open_orders: self
                .instruction
                .amm_open_orders
                .expect("amm_open_orders is not set"),

            lp_mint: self.instruction.lp_mint.expect("lp_mint is not set"),

            coin_mint: self.instruction.coin_mint.expect("coin_mint is not set"),

            pc_mint: self.instruction.pc_mint.expect("pc_mint is not set"),

            pool_coin_token_account: self
                .instruction
                .pool_coin_token_account
                .expect("pool_coin_token_account is not set"),

            pool_pc_token_account: self
                .instruction
                .pool_pc_token_account
                .expect("pool_pc_token_account is not set"),

            pool_withdraw_queue: self
                .instruction
                .pool_withdraw_queue
                .expect("pool_withdraw_queue is not set"),

            amm_target_orders: self
                .instruction
                .amm_target_orders
                .expect("amm_target_orders is not set"),

            pool_temp_lp: self
                .instruction
                .pool_temp_lp
                .expect("pool_temp_lp is not set"),

            serum_program: self
                .instruction
                .serum_program
                .expect("serum_program is not set"),

            serum_market: self
                .instruction
                .serum_market
                .expect("serum_market is not set"),

            user_wallet: self
                .instruction
                .user_wallet
                .expect("user_wallet is not set"),

            user_token_coin: self
                .instruction
                .user_token_coin
                .expect("user_token_coin is not set"),

            user_token_pc: self
                .instruction
                .user_token_pc
                .expect("user_token_pc is not set"),

            user_lp_token_account: self
                .instruction
                .user_lp_token_account
                .expect("user_lp_token_account is not set"),
            __args: args,
        };
        instruction.invoke_signed_with_remaining_accounts(
            signers_seeds,
            &self.instruction.__remaining_accounts,
        )
    }
}

#[derive(Clone, Debug)]
struct Initialize2CpiBuilderInstruction<'a, 'b> {
    __program: &'b solana_account_info::AccountInfo<'a>,
    token_program: Option<&'b solana_account_info::AccountInfo<'a>>,
    spl_associated_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    system_program: Option<&'b solana_account_info::AccountInfo<'a>>,
    rent: Option<&'b solana_account_info::AccountInfo<'a>>,
    amm: Option<&'b solana_account_info::AccountInfo<'a>>,
    amm_authority: Option<&'b solana_account_info::AccountInfo<'a>>,
    amm_open_orders: Option<&'b solana_account_info::AccountInfo<'a>>,
    lp_mint: Option<&'b solana_account_info::AccountInfo<'a>>,
    coin_mint: Option<&'b solana_account_info::AccountInfo<'a>>,
    pc_mint: Option<&'b solana_account_info::AccountInfo<'a>>,
    pool_coin_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    pool_pc_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    pool_withdraw_queue: Option<&'b solana_account_info::AccountInfo<'a>>,
    amm_target_orders: Option<&'b solana_account_info::AccountInfo<'a>>,
    pool_temp_lp: Option<&'b solana_account_info::AccountInfo<'a>>,
    serum_program: Option<&'b solana_account_info::AccountInfo<'a>>,
    serum_market: Option<&'b solana_account_info::AccountInfo<'a>>,
    user_wallet: Option<&'b solana_account_info::AccountInfo<'a>>,
    user_token_coin: Option<&'b solana_account_info::AccountInfo<'a>>,
    user_token_pc: Option<&'b solana_account_info::AccountInfo<'a>>,
    user_lp_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    nonce: Option<u8>,
    open_time: Option<u64>,
    init_pc_amount: Option<u64>,
    init_coin_amount: Option<u64>,
    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
}
