use borsh::BorshDeserialize;
use borsh::BorshSerialize;
#[derive(Debug)]
pub struct SwapBaseInput {
pub payer: solana_pubkey::Pubkey,
pub authority: solana_pubkey::Pubkey,
pub amm_config: solana_pubkey::Pubkey,
pub pool_state: solana_pubkey::Pubkey,
pub input_token_account: solana_pubkey::Pubkey,
pub output_token_account: solana_pubkey::Pubkey,
pub input_vault: solana_pubkey::Pubkey,
pub output_vault: solana_pubkey::Pubkey,
pub input_token_program: solana_pubkey::Pubkey,
pub output_token_program: solana_pubkey::Pubkey,
pub input_token_mint: solana_pubkey::Pubkey,
pub output_token_mint: solana_pubkey::Pubkey,
pub observation_state: solana_pubkey::Pubkey,
}
impl SwapBaseInput {
pub fn instruction(
&self,
args: SwapBaseInputInstructionArgs,
) -> 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: SwapBaseInputInstructionArgs,
remaining_accounts: &[solana_instruction::AccountMeta],
) -> solana_instruction::Instruction {
let mut accounts = Vec::with_capacity(13 + remaining_accounts.len());
accounts.push(solana_instruction::AccountMeta::new_readonly(
self.payer, true,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
self.authority,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
self.amm_config,
false,
));
accounts.push(solana_instruction::AccountMeta::new(self.pool_state, false));
accounts.push(solana_instruction::AccountMeta::new(
self.input_token_account,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
self.output_token_account,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
self.input_vault,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
self.output_vault,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
self.input_token_program,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
self.output_token_program,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
self.input_token_mint,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
self.output_token_mint,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
self.observation_state,
false,
));
accounts.extend_from_slice(remaining_accounts);
let mut data = borsh::to_vec(&SwapBaseInputInstructionData::new()).unwrap();
let mut args = borsh::to_vec(&args).unwrap();
data.append(&mut args);
solana_instruction::Instruction {
program_id: crate::RAYDIUM_CP_SWAP_ID,
accounts,
data,
}
}
}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SwapBaseInputInstructionData {
discriminator: [u8; 8],
}
impl SwapBaseInputInstructionData {
pub fn new() -> Self {
Self {
discriminator: [143, 190, 90, 218, 196, 30, 51, 222],
}
}
}
impl Default for SwapBaseInputInstructionData {
fn default() -> Self {
Self::new()
}
}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SwapBaseInputInstructionArgs {
pub amount_in: u64,
pub minimum_amount_out: u64,
}
#[derive(Clone, Debug, Default)]
pub struct SwapBaseInputBuilder {
payer: Option<solana_pubkey::Pubkey>,
authority: Option<solana_pubkey::Pubkey>,
amm_config: Option<solana_pubkey::Pubkey>,
pool_state: Option<solana_pubkey::Pubkey>,
input_token_account: Option<solana_pubkey::Pubkey>,
output_token_account: Option<solana_pubkey::Pubkey>,
input_vault: Option<solana_pubkey::Pubkey>,
output_vault: Option<solana_pubkey::Pubkey>,
input_token_program: Option<solana_pubkey::Pubkey>,
output_token_program: Option<solana_pubkey::Pubkey>,
input_token_mint: Option<solana_pubkey::Pubkey>,
output_token_mint: Option<solana_pubkey::Pubkey>,
observation_state: Option<solana_pubkey::Pubkey>,
amount_in: Option<u64>,
minimum_amount_out: Option<u64>,
__remaining_accounts: Vec<solana_instruction::AccountMeta>,
}
impl SwapBaseInputBuilder {
pub fn new() -> Self {
Self::default()
}
#[inline(always)]
pub fn payer(&mut self, payer: solana_pubkey::Pubkey) -> &mut Self {
self.payer = Some(payer);
self
}
#[inline(always)]
pub fn authority(&mut self, authority: solana_pubkey::Pubkey) -> &mut Self {
self.authority = Some(authority);
self
}
#[inline(always)]
pub fn amm_config(&mut self, amm_config: solana_pubkey::Pubkey) -> &mut Self {
self.amm_config = Some(amm_config);
self
}
#[inline(always)]
pub fn pool_state(&mut self, pool_state: solana_pubkey::Pubkey) -> &mut Self {
self.pool_state = Some(pool_state);
self
}
#[inline(always)]
pub fn input_token_account(&mut self, input_token_account: solana_pubkey::Pubkey) -> &mut Self {
self.input_token_account = Some(input_token_account);
self
}
#[inline(always)]
pub fn output_token_account(
&mut self,
output_token_account: solana_pubkey::Pubkey,
) -> &mut Self {
self.output_token_account = Some(output_token_account);
self
}
#[inline(always)]
pub fn input_vault(&mut self, input_vault: solana_pubkey::Pubkey) -> &mut Self {
self.input_vault = Some(input_vault);
self
}
#[inline(always)]
pub fn output_vault(&mut self, output_vault: solana_pubkey::Pubkey) -> &mut Self {
self.output_vault = Some(output_vault);
self
}
#[inline(always)]
pub fn input_token_program(&mut self, input_token_program: solana_pubkey::Pubkey) -> &mut Self {
self.input_token_program = Some(input_token_program);
self
}
#[inline(always)]
pub fn output_token_program(
&mut self,
output_token_program: solana_pubkey::Pubkey,
) -> &mut Self {
self.output_token_program = Some(output_token_program);
self
}
#[inline(always)]
pub fn input_token_mint(&mut self, input_token_mint: solana_pubkey::Pubkey) -> &mut Self {
self.input_token_mint = Some(input_token_mint);
self
}
#[inline(always)]
pub fn output_token_mint(&mut self, output_token_mint: solana_pubkey::Pubkey) -> &mut Self {
self.output_token_mint = Some(output_token_mint);
self
}
#[inline(always)]
pub fn observation_state(&mut self, observation_state: solana_pubkey::Pubkey) -> &mut Self {
self.observation_state = Some(observation_state);
self
}
#[inline(always)]
pub fn amount_in(&mut self, amount_in: u64) -> &mut Self {
self.amount_in = Some(amount_in);
self
}
#[inline(always)]
pub fn minimum_amount_out(&mut self, minimum_amount_out: u64) -> &mut Self {
self.minimum_amount_out = Some(minimum_amount_out);
self
}
#[inline(always)]
pub fn add_remaining_account(&mut self, account: solana_instruction::AccountMeta) -> &mut Self {
self.__remaining_accounts.push(account);
self
}
#[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 = SwapBaseInput {
payer: self.payer.expect("payer is not set"),
authority: self.authority.expect("authority is not set"),
amm_config: self.amm_config.expect("amm_config is not set"),
pool_state: self.pool_state.expect("pool_state is not set"),
input_token_account: self
.input_token_account
.expect("input_token_account is not set"),
output_token_account: self
.output_token_account
.expect("output_token_account is not set"),
input_vault: self.input_vault.expect("input_vault is not set"),
output_vault: self.output_vault.expect("output_vault is not set"),
input_token_program: self
.input_token_program
.expect("input_token_program is not set"),
output_token_program: self
.output_token_program
.expect("output_token_program is not set"),
input_token_mint: self.input_token_mint.expect("input_token_mint is not set"),
output_token_mint: self
.output_token_mint
.expect("output_token_mint is not set"),
observation_state: self
.observation_state
.expect("observation_state is not set"),
};
let args = SwapBaseInputInstructionArgs {
amount_in: self.amount_in.clone().expect("amount_in is not set"),
minimum_amount_out: self
.minimum_amount_out
.clone()
.expect("minimum_amount_out is not set"),
};
accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
}
}
pub struct SwapBaseInputCpiAccounts<'a, 'b> {
pub payer: &'b solana_account_info::AccountInfo<'a>,
pub authority: &'b solana_account_info::AccountInfo<'a>,
pub amm_config: &'b solana_account_info::AccountInfo<'a>,
pub pool_state: &'b solana_account_info::AccountInfo<'a>,
pub input_token_account: &'b solana_account_info::AccountInfo<'a>,
pub output_token_account: &'b solana_account_info::AccountInfo<'a>,
pub input_vault: &'b solana_account_info::AccountInfo<'a>,
pub output_vault: &'b solana_account_info::AccountInfo<'a>,
pub input_token_program: &'b solana_account_info::AccountInfo<'a>,
pub output_token_program: &'b solana_account_info::AccountInfo<'a>,
pub input_token_mint: &'b solana_account_info::AccountInfo<'a>,
pub output_token_mint: &'b solana_account_info::AccountInfo<'a>,
pub observation_state: &'b solana_account_info::AccountInfo<'a>,
}
pub struct SwapBaseInputCpi<'a, 'b> {
pub __program: &'b solana_account_info::AccountInfo<'a>,
pub payer: &'b solana_account_info::AccountInfo<'a>,
pub authority: &'b solana_account_info::AccountInfo<'a>,
pub amm_config: &'b solana_account_info::AccountInfo<'a>,
pub pool_state: &'b solana_account_info::AccountInfo<'a>,
pub input_token_account: &'b solana_account_info::AccountInfo<'a>,
pub output_token_account: &'b solana_account_info::AccountInfo<'a>,
pub input_vault: &'b solana_account_info::AccountInfo<'a>,
pub output_vault: &'b solana_account_info::AccountInfo<'a>,
pub input_token_program: &'b solana_account_info::AccountInfo<'a>,
pub output_token_program: &'b solana_account_info::AccountInfo<'a>,
pub input_token_mint: &'b solana_account_info::AccountInfo<'a>,
pub output_token_mint: &'b solana_account_info::AccountInfo<'a>,
pub observation_state: &'b solana_account_info::AccountInfo<'a>,
pub __args: SwapBaseInputInstructionArgs,
}
impl<'a, 'b> SwapBaseInputCpi<'a, 'b> {
pub fn new(
program: &'b solana_account_info::AccountInfo<'a>,
accounts: SwapBaseInputCpiAccounts<'a, 'b>,
args: SwapBaseInputInstructionArgs,
) -> Self {
Self {
__program: program,
payer: accounts.payer,
authority: accounts.authority,
amm_config: accounts.amm_config,
pool_state: accounts.pool_state,
input_token_account: accounts.input_token_account,
output_token_account: accounts.output_token_account,
input_vault: accounts.input_vault,
output_vault: accounts.output_vault,
input_token_program: accounts.input_token_program,
output_token_program: accounts.output_token_program,
input_token_mint: accounts.input_token_mint,
output_token_mint: accounts.output_token_mint,
observation_state: accounts.observation_state,
__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(13 + remaining_accounts.len());
accounts.push(solana_instruction::AccountMeta::new_readonly(
*self.payer.key,
true,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
*self.authority.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
*self.amm_config.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
*self.pool_state.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
*self.input_token_account.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
*self.output_token_account.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
*self.input_vault.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
*self.output_vault.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
*self.input_token_program.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
*self.output_token_program.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
*self.input_token_mint.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new_readonly(
*self.output_token_mint.key,
false,
));
accounts.push(solana_instruction::AccountMeta::new(
*self.observation_state.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(&SwapBaseInputInstructionData::new()).unwrap();
let mut args = borsh::to_vec(&self.__args).unwrap();
data.append(&mut args);
let instruction = solana_instruction::Instruction {
program_id: crate::RAYDIUM_CP_SWAP_ID,
accounts,
data,
};
let mut account_infos = Vec::with_capacity(14 + remaining_accounts.len());
account_infos.push(self.__program.clone());
account_infos.push(self.payer.clone());
account_infos.push(self.authority.clone());
account_infos.push(self.amm_config.clone());
account_infos.push(self.pool_state.clone());
account_infos.push(self.input_token_account.clone());
account_infos.push(self.output_token_account.clone());
account_infos.push(self.input_vault.clone());
account_infos.push(self.output_vault.clone());
account_infos.push(self.input_token_program.clone());
account_infos.push(self.output_token_program.clone());
account_infos.push(self.input_token_mint.clone());
account_infos.push(self.output_token_mint.clone());
account_infos.push(self.observation_state.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)
}
}
}
#[derive(Clone, Debug)]
pub struct SwapBaseInputCpiBuilder<'a, 'b> {
instruction: Box<SwapBaseInputCpiBuilderInstruction<'a, 'b>>,
}
impl<'a, 'b> SwapBaseInputCpiBuilder<'a, 'b> {
pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
let instruction = Box::new(SwapBaseInputCpiBuilderInstruction {
__program: program,
payer: None,
authority: None,
amm_config: None,
pool_state: None,
input_token_account: None,
output_token_account: None,
input_vault: None,
output_vault: None,
input_token_program: None,
output_token_program: None,
input_token_mint: None,
output_token_mint: None,
observation_state: None,
amount_in: None,
minimum_amount_out: None,
__remaining_accounts: Vec::new(),
});
Self { instruction }
}
#[inline(always)]
pub fn payer(&mut self, payer: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.payer = Some(payer);
self
}
#[inline(always)]
pub fn authority(&mut self, authority: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.authority = Some(authority);
self
}
#[inline(always)]
pub fn amm_config(
&mut self,
amm_config: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.amm_config = Some(amm_config);
self
}
#[inline(always)]
pub fn pool_state(
&mut self,
pool_state: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.pool_state = Some(pool_state);
self
}
#[inline(always)]
pub fn input_token_account(
&mut self,
input_token_account: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.input_token_account = Some(input_token_account);
self
}
#[inline(always)]
pub fn output_token_account(
&mut self,
output_token_account: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.output_token_account = Some(output_token_account);
self
}
#[inline(always)]
pub fn input_vault(
&mut self,
input_vault: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.input_vault = Some(input_vault);
self
}
#[inline(always)]
pub fn output_vault(
&mut self,
output_vault: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.output_vault = Some(output_vault);
self
}
#[inline(always)]
pub fn input_token_program(
&mut self,
input_token_program: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.input_token_program = Some(input_token_program);
self
}
#[inline(always)]
pub fn output_token_program(
&mut self,
output_token_program: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.output_token_program = Some(output_token_program);
self
}
#[inline(always)]
pub fn input_token_mint(
&mut self,
input_token_mint: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.input_token_mint = Some(input_token_mint);
self
}
#[inline(always)]
pub fn output_token_mint(
&mut self,
output_token_mint: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.output_token_mint = Some(output_token_mint);
self
}
#[inline(always)]
pub fn observation_state(
&mut self,
observation_state: &'b solana_account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.observation_state = Some(observation_state);
self
}
#[inline(always)]
pub fn amount_in(&mut self, amount_in: u64) -> &mut Self {
self.instruction.amount_in = Some(amount_in);
self
}
#[inline(always)]
pub fn minimum_amount_out(&mut self, minimum_amount_out: u64) -> &mut Self {
self.instruction.minimum_amount_out = Some(minimum_amount_out);
self
}
#[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
}
#[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 = SwapBaseInputInstructionArgs {
amount_in: self
.instruction
.amount_in
.clone()
.expect("amount_in is not set"),
minimum_amount_out: self
.instruction
.minimum_amount_out
.clone()
.expect("minimum_amount_out is not set"),
};
let instruction = SwapBaseInputCpi {
__program: self.instruction.__program,
payer: self.instruction.payer.expect("payer is not set"),
authority: self.instruction.authority.expect("authority is not set"),
amm_config: self.instruction.amm_config.expect("amm_config is not set"),
pool_state: self.instruction.pool_state.expect("pool_state is not set"),
input_token_account: self
.instruction
.input_token_account
.expect("input_token_account is not set"),
output_token_account: self
.instruction
.output_token_account
.expect("output_token_account is not set"),
input_vault: self
.instruction
.input_vault
.expect("input_vault is not set"),
output_vault: self
.instruction
.output_vault
.expect("output_vault is not set"),
input_token_program: self
.instruction
.input_token_program
.expect("input_token_program is not set"),
output_token_program: self
.instruction
.output_token_program
.expect("output_token_program is not set"),
input_token_mint: self
.instruction
.input_token_mint
.expect("input_token_mint is not set"),
output_token_mint: self
.instruction
.output_token_mint
.expect("output_token_mint is not set"),
observation_state: self
.instruction
.observation_state
.expect("observation_state is not set"),
__args: args,
};
instruction.invoke_signed_with_remaining_accounts(
signers_seeds,
&self.instruction.__remaining_accounts,
)
}
}
#[derive(Clone, Debug)]
struct SwapBaseInputCpiBuilderInstruction<'a, 'b> {
__program: &'b solana_account_info::AccountInfo<'a>,
payer: Option<&'b solana_account_info::AccountInfo<'a>>,
authority: Option<&'b solana_account_info::AccountInfo<'a>>,
amm_config: Option<&'b solana_account_info::AccountInfo<'a>>,
pool_state: Option<&'b solana_account_info::AccountInfo<'a>>,
input_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
output_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
input_vault: Option<&'b solana_account_info::AccountInfo<'a>>,
output_vault: Option<&'b solana_account_info::AccountInfo<'a>>,
input_token_program: Option<&'b solana_account_info::AccountInfo<'a>>,
output_token_program: Option<&'b solana_account_info::AccountInfo<'a>>,
input_token_mint: Option<&'b solana_account_info::AccountInfo<'a>>,
output_token_mint: Option<&'b solana_account_info::AccountInfo<'a>>,
observation_state: Option<&'b solana_account_info::AccountInfo<'a>>,
amount_in: Option<u64>,
minimum_amount_out: Option<u64>,
__remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
}