use crate::generated::types::RoutePlanStep;
use borsh::BorshSerialize;
use borsh::BorshDeserialize;
#[derive(Debug)]
pub struct RouteExactOut {
pub token_program: solana_program::pubkey::Pubkey,
pub token_authority: solana_program::pubkey::Pubkey,
pub source_token_account: solana_program::pubkey::Pubkey,
pub destination_token_account: solana_program::pubkey::Pubkey,
pub source_mint: solana_program::pubkey::Pubkey,
pub destination_mint: solana_program::pubkey::Pubkey,
pub platform_fee_account: Option<solana_program::pubkey::Pubkey>,
}
impl RouteExactOut {
pub fn instruction(&self, args: RouteExactOutInstructionArgs) -> solana_program::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: RouteExactOutInstructionArgs, remaining_accounts: &[solana_program::instruction::AccountMeta]) -> solana_program::instruction::Instruction {
let mut accounts = Vec::with_capacity(7+ remaining_accounts.len());
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.token_program,
false
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.token_authority,
true
));
accounts.push(solana_program::instruction::AccountMeta::new(
self.source_token_account,
false
));
accounts.push(solana_program::instruction::AccountMeta::new(
self.destination_token_account,
false
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.source_mint,
false
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.destination_mint,
false
));
if let Some(platform_fee_account) = self.platform_fee_account {
accounts.push(solana_program::instruction::AccountMeta::new(
platform_fee_account,
false,
));
} else {
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
crate::VORTEX_ID,
false,
));
}
accounts.extend_from_slice(remaining_accounts);
let mut data = borsh::to_vec(&RouteExactOutInstructionData::new()).unwrap();
let mut args = borsh::to_vec(&args).unwrap();
data.append(&mut args);
solana_program::instruction::Instruction {
program_id: crate::VORTEX_ID,
accounts,
data,
}
}
}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RouteExactOutInstructionData {
discriminator: [u8; 8],
}
impl RouteExactOutInstructionData {
pub fn new() -> Self {
Self {
discriminator: [43, 5, 26, 208, 142, 84, 128, 151],
}
}
}
impl Default for RouteExactOutInstructionData {
fn default() -> Self {
Self::new()
}
}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RouteExactOutInstructionArgs {
pub route_plan: Vec<RoutePlanStep>,
pub out_amount: u64,
pub quoted_in_amount: u64,
pub slippage_bps: u16,
pub platform_fee_bps: u8,
}
#[derive(Clone, Debug, Default)]
pub struct RouteExactOutBuilder {
token_program: Option<solana_program::pubkey::Pubkey>,
token_authority: Option<solana_program::pubkey::Pubkey>,
source_token_account: Option<solana_program::pubkey::Pubkey>,
destination_token_account: Option<solana_program::pubkey::Pubkey>,
source_mint: Option<solana_program::pubkey::Pubkey>,
destination_mint: Option<solana_program::pubkey::Pubkey>,
platform_fee_account: Option<solana_program::pubkey::Pubkey>,
route_plan: Option<Vec<RoutePlanStep>>,
out_amount: Option<u64>,
quoted_in_amount: Option<u64>,
slippage_bps: Option<u16>,
platform_fee_bps: Option<u8>,
__remaining_accounts: Vec<solana_program::instruction::AccountMeta>,
}
impl RouteExactOutBuilder {
pub fn new() -> Self {
Self::default()
}
#[inline(always)]
pub fn token_program(&mut self, token_program: solana_program::pubkey::Pubkey) -> &mut Self {
self.token_program = Some(token_program);
self
}
#[inline(always)]
pub fn token_authority(&mut self, token_authority: solana_program::pubkey::Pubkey) -> &mut Self {
self.token_authority = Some(token_authority);
self
}
#[inline(always)]
pub fn source_token_account(&mut self, source_token_account: solana_program::pubkey::Pubkey) -> &mut Self {
self.source_token_account = Some(source_token_account);
self
}
#[inline(always)]
pub fn destination_token_account(&mut self, destination_token_account: solana_program::pubkey::Pubkey) -> &mut Self {
self.destination_token_account = Some(destination_token_account);
self
}
#[inline(always)]
pub fn source_mint(&mut self, source_mint: solana_program::pubkey::Pubkey) -> &mut Self {
self.source_mint = Some(source_mint);
self
}
#[inline(always)]
pub fn destination_mint(&mut self, destination_mint: solana_program::pubkey::Pubkey) -> &mut Self {
self.destination_mint = Some(destination_mint);
self
}
#[inline(always)]
pub fn platform_fee_account(&mut self, platform_fee_account: Option<solana_program::pubkey::Pubkey>) -> &mut Self {
self.platform_fee_account = platform_fee_account;
self
}
#[inline(always)]
pub fn route_plan(&mut self, route_plan: Vec<RoutePlanStep>) -> &mut Self {
self.route_plan = Some(route_plan);
self
}
#[inline(always)]
pub fn out_amount(&mut self, out_amount: u64) -> &mut Self {
self.out_amount = Some(out_amount);
self
}
#[inline(always)]
pub fn quoted_in_amount(&mut self, quoted_in_amount: u64) -> &mut Self {
self.quoted_in_amount = Some(quoted_in_amount);
self
}
#[inline(always)]
pub fn slippage_bps(&mut self, slippage_bps: u16) -> &mut Self {
self.slippage_bps = Some(slippage_bps);
self
}
#[inline(always)]
pub fn platform_fee_bps(&mut self, platform_fee_bps: u8) -> &mut Self {
self.platform_fee_bps = Some(platform_fee_bps);
self
}
#[inline(always)]
pub fn add_remaining_account(&mut self, account: solana_program::instruction::AccountMeta) -> &mut Self {
self.__remaining_accounts.push(account);
self
}
#[inline(always)]
pub fn add_remaining_accounts(&mut self, accounts: &[solana_program::instruction::AccountMeta]) -> &mut Self {
self.__remaining_accounts.extend_from_slice(accounts);
self
}
#[allow(clippy::clone_on_copy)]
pub fn instruction(&self) -> solana_program::instruction::Instruction {
let accounts = RouteExactOut {
token_program: self.token_program.unwrap_or(solana_program::pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")),
token_authority: self.token_authority.expect("token_authority is not set"),
source_token_account: self.source_token_account.expect("source_token_account is not set"),
destination_token_account: self.destination_token_account.expect("destination_token_account is not set"),
source_mint: self.source_mint.expect("source_mint is not set"),
destination_mint: self.destination_mint.expect("destination_mint is not set"),
platform_fee_account: self.platform_fee_account,
};
let args = RouteExactOutInstructionArgs {
route_plan: self.route_plan.clone().expect("route_plan is not set"),
out_amount: self.out_amount.clone().expect("out_amount is not set"),
quoted_in_amount: self.quoted_in_amount.clone().expect("quoted_in_amount is not set"),
slippage_bps: self.slippage_bps.clone().expect("slippage_bps is not set"),
platform_fee_bps: self.platform_fee_bps.clone().expect("platform_fee_bps is not set"),
};
accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
}
}
pub struct RouteExactOutCpiAccounts<'a, 'b> {
pub token_program: &'b solana_program::account_info::AccountInfo<'a>,
pub token_authority: &'b solana_program::account_info::AccountInfo<'a>,
pub source_token_account: &'b solana_program::account_info::AccountInfo<'a>,
pub destination_token_account: &'b solana_program::account_info::AccountInfo<'a>,
pub source_mint: &'b solana_program::account_info::AccountInfo<'a>,
pub destination_mint: &'b solana_program::account_info::AccountInfo<'a>,
pub platform_fee_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
}
pub struct RouteExactOutCpi<'a, 'b> {
pub __program: &'b solana_program::account_info::AccountInfo<'a>,
pub token_program: &'b solana_program::account_info::AccountInfo<'a>,
pub token_authority: &'b solana_program::account_info::AccountInfo<'a>,
pub source_token_account: &'b solana_program::account_info::AccountInfo<'a>,
pub destination_token_account: &'b solana_program::account_info::AccountInfo<'a>,
pub source_mint: &'b solana_program::account_info::AccountInfo<'a>,
pub destination_mint: &'b solana_program::account_info::AccountInfo<'a>,
pub platform_fee_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
pub __args: RouteExactOutInstructionArgs,
}
impl<'a, 'b> RouteExactOutCpi<'a, 'b> {
pub fn new(
program: &'b solana_program::account_info::AccountInfo<'a>,
accounts: RouteExactOutCpiAccounts<'a, 'b>,
args: RouteExactOutInstructionArgs,
) -> Self {
Self {
__program: program,
token_program: accounts.token_program,
token_authority: accounts.token_authority,
source_token_account: accounts.source_token_account,
destination_token_account: accounts.destination_token_account,
source_mint: accounts.source_mint,
destination_mint: accounts.destination_mint,
platform_fee_account: accounts.platform_fee_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_program::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_program::account_info::AccountInfo<'a>, bool, bool)]
) -> solana_program::entrypoint::ProgramResult {
let mut accounts = Vec::with_capacity(7+ remaining_accounts.len());
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.token_program.key,
false
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.token_authority.key,
true
));
accounts.push(solana_program::instruction::AccountMeta::new(
*self.source_token_account.key,
false
));
accounts.push(solana_program::instruction::AccountMeta::new(
*self.destination_token_account.key,
false
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.source_mint.key,
false
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.destination_mint.key,
false
));
if let Some(platform_fee_account) = self.platform_fee_account {
accounts.push(solana_program::instruction::AccountMeta::new(
*platform_fee_account.key,
false,
));
} else {
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
crate::VORTEX_ID,
false,
));
}
remaining_accounts.iter().for_each(|remaining_account| {
accounts.push(solana_program::instruction::AccountMeta {
pubkey: *remaining_account.0.key,
is_signer: remaining_account.1,
is_writable: remaining_account.2,
})
});
let mut data = borsh::to_vec(&RouteExactOutInstructionData::new()).unwrap();
let mut args = borsh::to_vec(&self.__args).unwrap();
data.append(&mut args);
let instruction = solana_program::instruction::Instruction {
program_id: crate::VORTEX_ID,
accounts,
data,
};
let mut account_infos = Vec::with_capacity(8 + remaining_accounts.len());
account_infos.push(self.__program.clone());
account_infos.push(self.token_program.clone());
account_infos.push(self.token_authority.clone());
account_infos.push(self.source_token_account.clone());
account_infos.push(self.destination_token_account.clone());
account_infos.push(self.source_mint.clone());
account_infos.push(self.destination_mint.clone());
if let Some(platform_fee_account) = self.platform_fee_account {
account_infos.push(platform_fee_account.clone());
}
remaining_accounts.iter().for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));
if signers_seeds.is_empty() {
solana_program::program::invoke(&instruction, &account_infos)
} else {
solana_program::program::invoke_signed(&instruction, &account_infos, signers_seeds)
}
}
}
#[derive(Clone, Debug)]
pub struct RouteExactOutCpiBuilder<'a, 'b> {
instruction: Box<RouteExactOutCpiBuilderInstruction<'a, 'b>>,
}
impl<'a, 'b> RouteExactOutCpiBuilder<'a, 'b> {
pub fn new(program: &'b solana_program::account_info::AccountInfo<'a>) -> Self {
let instruction = Box::new(RouteExactOutCpiBuilderInstruction {
__program: program,
token_program: None,
token_authority: None,
source_token_account: None,
destination_token_account: None,
source_mint: None,
destination_mint: None,
platform_fee_account: None,
route_plan: None,
out_amount: None,
quoted_in_amount: None,
slippage_bps: None,
platform_fee_bps: None,
__remaining_accounts: Vec::new(),
});
Self { instruction }
}
#[inline(always)]
pub fn token_program(&mut self, token_program: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.token_program = Some(token_program);
self
}
#[inline(always)]
pub fn token_authority(&mut self, token_authority: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.token_authority = Some(token_authority);
self
}
#[inline(always)]
pub fn source_token_account(&mut self, source_token_account: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.source_token_account = Some(source_token_account);
self
}
#[inline(always)]
pub fn destination_token_account(&mut self, destination_token_account: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.destination_token_account = Some(destination_token_account);
self
}
#[inline(always)]
pub fn source_mint(&mut self, source_mint: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.source_mint = Some(source_mint);
self
}
#[inline(always)]
pub fn destination_mint(&mut self, destination_mint: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.destination_mint = Some(destination_mint);
self
}
#[inline(always)]
pub fn platform_fee_account(&mut self, platform_fee_account: Option<&'b solana_program::account_info::AccountInfo<'a>>) -> &mut Self {
self.instruction.platform_fee_account = platform_fee_account;
self
}
#[inline(always)]
pub fn route_plan(&mut self, route_plan: Vec<RoutePlanStep>) -> &mut Self {
self.instruction.route_plan = Some(route_plan);
self
}
#[inline(always)]
pub fn out_amount(&mut self, out_amount: u64) -> &mut Self {
self.instruction.out_amount = Some(out_amount);
self
}
#[inline(always)]
pub fn quoted_in_amount(&mut self, quoted_in_amount: u64) -> &mut Self {
self.instruction.quoted_in_amount = Some(quoted_in_amount);
self
}
#[inline(always)]
pub fn slippage_bps(&mut self, slippage_bps: u16) -> &mut Self {
self.instruction.slippage_bps = Some(slippage_bps);
self
}
#[inline(always)]
pub fn platform_fee_bps(&mut self, platform_fee_bps: u8) -> &mut Self {
self.instruction.platform_fee_bps = Some(platform_fee_bps);
self
}
#[inline(always)]
pub fn add_remaining_account(&mut self, account: &'b solana_program::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_program::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 = RouteExactOutInstructionArgs {
route_plan: self.instruction.route_plan.clone().expect("route_plan is not set"),
out_amount: self.instruction.out_amount.clone().expect("out_amount is not set"),
quoted_in_amount: self.instruction.quoted_in_amount.clone().expect("quoted_in_amount is not set"),
slippage_bps: self.instruction.slippage_bps.clone().expect("slippage_bps is not set"),
platform_fee_bps: self.instruction.platform_fee_bps.clone().expect("platform_fee_bps is not set"),
};
let instruction = RouteExactOutCpi {
__program: self.instruction.__program,
token_program: self.instruction.token_program.expect("token_program is not set"),
token_authority: self.instruction.token_authority.expect("token_authority is not set"),
source_token_account: self.instruction.source_token_account.expect("source_token_account is not set"),
destination_token_account: self.instruction.destination_token_account.expect("destination_token_account is not set"),
source_mint: self.instruction.source_mint.expect("source_mint is not set"),
destination_mint: self.instruction.destination_mint.expect("destination_mint is not set"),
platform_fee_account: self.instruction.platform_fee_account,
__args: args,
};
instruction.invoke_signed_with_remaining_accounts(signers_seeds, &self.instruction.__remaining_accounts)
}
}
#[derive(Clone, Debug)]
struct RouteExactOutCpiBuilderInstruction<'a, 'b> {
__program: &'b solana_program::account_info::AccountInfo<'a>,
token_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
token_authority: Option<&'b solana_program::account_info::AccountInfo<'a>>,
source_token_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
destination_token_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
source_mint: Option<&'b solana_program::account_info::AccountInfo<'a>>,
destination_mint: Option<&'b solana_program::account_info::AccountInfo<'a>>,
platform_fee_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
route_plan: Option<Vec<RoutePlanStep>>,
out_amount: Option<u64>,
quoted_in_amount: Option<u64>,
slippage_bps: Option<u16>,
platform_fee_bps: Option<u8>,
__remaining_accounts: Vec<(&'b solana_program::account_info::AccountInfo<'a>, bool, bool)>,
}