use borsh::{BorshDeserialize, BorshSerialize};
pub const INIT_DISCRIMINATOR: [u8; 8] = [220, 59, 207, 236, 108, 250, 47, 100];
#[derive(Debug)]
pub struct Init {
pub upgrade_authority: solana_program::pubkey::Pubkey,
pub admin: solana_program::pubkey::Pubkey,
pub transfer_authority: solana_program::pubkey::Pubkey,
pub perpetuals: solana_program::pubkey::Pubkey,
pub perpetuals_program: solana_program::pubkey::Pubkey,
pub perpetuals_program_data: solana_program::pubkey::Pubkey,
pub system_program: solana_program::pubkey::Pubkey,
pub token_program: solana_program::pubkey::Pubkey,
}
impl Init {
pub fn instruction(
&self,
args: InitInstructionArgs,
) -> 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: InitInstructionArgs,
remaining_accounts: &[solana_program::instruction::AccountMeta],
) -> solana_program::instruction::Instruction {
let mut accounts = Vec::with_capacity(8 + remaining_accounts.len());
accounts.push(solana_program::instruction::AccountMeta::new(
self.upgrade_authority,
true,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.admin, false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
self.transfer_authority,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
self.perpetuals,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.perpetuals_program,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.perpetuals_program_data,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.system_program,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.token_program,
false,
));
accounts.extend_from_slice(remaining_accounts);
let mut data = borsh::to_vec(&InitInstructionData::new()).unwrap();
let mut args = borsh::to_vec(&args).unwrap();
data.append(&mut args);
solana_program::instruction::Instruction {
program_id: crate::PERPETUALS_ID,
accounts,
data,
}
}
}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct InitInstructionData {
discriminator: [u8; 8],
}
impl InitInstructionData {
pub fn new() -> Self {
Self {
discriminator: [220, 59, 207, 236, 108, 250, 47, 100],
}
}
}
impl Default for InitInstructionData {
fn default() -> Self {
Self::new()
}
}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct InitInstructionArgs {
pub allow_swap: bool,
pub allow_add_liquidity: bool,
pub allow_remove_liquidity: bool,
pub allow_increase_position: bool,
pub allow_decrease_position: bool,
pub allow_collateral_withdrawal: bool,
pub allow_liquidate_position: bool,
}
#[derive(Clone, Debug, Default)]
pub struct InitBuilder {
upgrade_authority: Option<solana_program::pubkey::Pubkey>,
admin: Option<solana_program::pubkey::Pubkey>,
transfer_authority: Option<solana_program::pubkey::Pubkey>,
perpetuals: Option<solana_program::pubkey::Pubkey>,
perpetuals_program: Option<solana_program::pubkey::Pubkey>,
perpetuals_program_data: Option<solana_program::pubkey::Pubkey>,
system_program: Option<solana_program::pubkey::Pubkey>,
token_program: Option<solana_program::pubkey::Pubkey>,
allow_swap: Option<bool>,
allow_add_liquidity: Option<bool>,
allow_remove_liquidity: Option<bool>,
allow_increase_position: Option<bool>,
allow_decrease_position: Option<bool>,
allow_collateral_withdrawal: Option<bool>,
allow_liquidate_position: Option<bool>,
__remaining_accounts: Vec<solana_program::instruction::AccountMeta>,
}
impl InitBuilder {
pub fn new() -> Self {
Self::default()
}
#[inline(always)]
pub fn upgrade_authority(
&mut self,
upgrade_authority: solana_program::pubkey::Pubkey,
) -> &mut Self {
self.upgrade_authority = Some(upgrade_authority);
self
}
#[inline(always)]
pub fn admin(&mut self, admin: solana_program::pubkey::Pubkey) -> &mut Self {
self.admin = Some(admin);
self
}
#[inline(always)]
pub fn transfer_authority(
&mut self,
transfer_authority: solana_program::pubkey::Pubkey,
) -> &mut Self {
self.transfer_authority = Some(transfer_authority);
self
}
#[inline(always)]
pub fn perpetuals(&mut self, perpetuals: solana_program::pubkey::Pubkey) -> &mut Self {
self.perpetuals = Some(perpetuals);
self
}
#[inline(always)]
pub fn perpetuals_program(
&mut self,
perpetuals_program: solana_program::pubkey::Pubkey,
) -> &mut Self {
self.perpetuals_program = Some(perpetuals_program);
self
}
#[inline(always)]
pub fn perpetuals_program_data(
&mut self,
perpetuals_program_data: solana_program::pubkey::Pubkey,
) -> &mut Self {
self.perpetuals_program_data = Some(perpetuals_program_data);
self
}
#[inline(always)]
pub fn system_program(&mut self, system_program: solana_program::pubkey::Pubkey) -> &mut Self {
self.system_program = Some(system_program);
self
}
#[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 allow_swap(&mut self, allow_swap: bool) -> &mut Self {
self.allow_swap = Some(allow_swap);
self
}
#[inline(always)]
pub fn allow_add_liquidity(&mut self, allow_add_liquidity: bool) -> &mut Self {
self.allow_add_liquidity = Some(allow_add_liquidity);
self
}
#[inline(always)]
pub fn allow_remove_liquidity(&mut self, allow_remove_liquidity: bool) -> &mut Self {
self.allow_remove_liquidity = Some(allow_remove_liquidity);
self
}
#[inline(always)]
pub fn allow_increase_position(&mut self, allow_increase_position: bool) -> &mut Self {
self.allow_increase_position = Some(allow_increase_position);
self
}
#[inline(always)]
pub fn allow_decrease_position(&mut self, allow_decrease_position: bool) -> &mut Self {
self.allow_decrease_position = Some(allow_decrease_position);
self
}
#[inline(always)]
pub fn allow_collateral_withdrawal(&mut self, allow_collateral_withdrawal: bool) -> &mut Self {
self.allow_collateral_withdrawal = Some(allow_collateral_withdrawal);
self
}
#[inline(always)]
pub fn allow_liquidate_position(&mut self, allow_liquidate_position: bool) -> &mut Self {
self.allow_liquidate_position = Some(allow_liquidate_position);
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 = Init {
upgrade_authority: self
.upgrade_authority
.expect("upgrade_authority is not set"),
admin: self.admin.expect("admin is not set"),
transfer_authority: self
.transfer_authority
.expect("transfer_authority is not set"),
perpetuals: self.perpetuals.expect("perpetuals is not set"),
perpetuals_program: self
.perpetuals_program
.expect("perpetuals_program is not set"),
perpetuals_program_data: self
.perpetuals_program_data
.expect("perpetuals_program_data is not set"),
system_program: self
.system_program
.unwrap_or(solana_program::pubkey!("11111111111111111111111111111111")),
token_program: self.token_program.unwrap_or(solana_program::pubkey!(
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
)),
};
let args = InitInstructionArgs {
allow_swap: self.allow_swap.clone().expect("allow_swap is not set"),
allow_add_liquidity: self
.allow_add_liquidity
.clone()
.expect("allow_add_liquidity is not set"),
allow_remove_liquidity: self
.allow_remove_liquidity
.clone()
.expect("allow_remove_liquidity is not set"),
allow_increase_position: self
.allow_increase_position
.clone()
.expect("allow_increase_position is not set"),
allow_decrease_position: self
.allow_decrease_position
.clone()
.expect("allow_decrease_position is not set"),
allow_collateral_withdrawal: self
.allow_collateral_withdrawal
.clone()
.expect("allow_collateral_withdrawal is not set"),
allow_liquidate_position: self
.allow_liquidate_position
.clone()
.expect("allow_liquidate_position is not set"),
};
accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
}
}
pub struct InitCpiAccounts<'a, 'b> {
pub upgrade_authority: &'b solana_program::account_info::AccountInfo<'a>,
pub admin: &'b solana_program::account_info::AccountInfo<'a>,
pub transfer_authority: &'b solana_program::account_info::AccountInfo<'a>,
pub perpetuals: &'b solana_program::account_info::AccountInfo<'a>,
pub perpetuals_program: &'b solana_program::account_info::AccountInfo<'a>,
pub perpetuals_program_data: &'b solana_program::account_info::AccountInfo<'a>,
pub system_program: &'b solana_program::account_info::AccountInfo<'a>,
pub token_program: &'b solana_program::account_info::AccountInfo<'a>,
}
pub struct InitCpi<'a, 'b> {
pub __program: &'b solana_program::account_info::AccountInfo<'a>,
pub upgrade_authority: &'b solana_program::account_info::AccountInfo<'a>,
pub admin: &'b solana_program::account_info::AccountInfo<'a>,
pub transfer_authority: &'b solana_program::account_info::AccountInfo<'a>,
pub perpetuals: &'b solana_program::account_info::AccountInfo<'a>,
pub perpetuals_program: &'b solana_program::account_info::AccountInfo<'a>,
pub perpetuals_program_data: &'b solana_program::account_info::AccountInfo<'a>,
pub system_program: &'b solana_program::account_info::AccountInfo<'a>,
pub token_program: &'b solana_program::account_info::AccountInfo<'a>,
pub __args: InitInstructionArgs,
}
impl<'a, 'b> InitCpi<'a, 'b> {
pub fn new(
program: &'b solana_program::account_info::AccountInfo<'a>,
accounts: InitCpiAccounts<'a, 'b>,
args: InitInstructionArgs,
) -> Self {
Self {
__program: program,
upgrade_authority: accounts.upgrade_authority,
admin: accounts.admin,
transfer_authority: accounts.transfer_authority,
perpetuals: accounts.perpetuals,
perpetuals_program: accounts.perpetuals_program,
perpetuals_program_data: accounts.perpetuals_program_data,
system_program: accounts.system_program,
token_program: accounts.token_program,
__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(8 + remaining_accounts.len());
accounts.push(solana_program::instruction::AccountMeta::new(
*self.upgrade_authority.key,
true,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.admin.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
*self.transfer_authority.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
*self.perpetuals.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.perpetuals_program.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.perpetuals_program_data.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.system_program.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.token_program.key,
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(&InitInstructionData::new()).unwrap();
let mut args = borsh::to_vec(&self.__args).unwrap();
data.append(&mut args);
let instruction = solana_program::instruction::Instruction {
program_id: crate::PERPETUALS_ID,
accounts,
data,
};
let mut account_infos = Vec::with_capacity(9 + remaining_accounts.len());
account_infos.push(self.__program.clone());
account_infos.push(self.upgrade_authority.clone());
account_infos.push(self.admin.clone());
account_infos.push(self.transfer_authority.clone());
account_infos.push(self.perpetuals.clone());
account_infos.push(self.perpetuals_program.clone());
account_infos.push(self.perpetuals_program_data.clone());
account_infos.push(self.system_program.clone());
account_infos.push(self.token_program.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 InitCpiBuilder<'a, 'b> {
instruction: Box<InitCpiBuilderInstruction<'a, 'b>>,
}
impl<'a, 'b> InitCpiBuilder<'a, 'b> {
pub fn new(program: &'b solana_program::account_info::AccountInfo<'a>) -> Self {
let instruction = Box::new(InitCpiBuilderInstruction {
__program: program,
upgrade_authority: None,
admin: None,
transfer_authority: None,
perpetuals: None,
perpetuals_program: None,
perpetuals_program_data: None,
system_program: None,
token_program: None,
allow_swap: None,
allow_add_liquidity: None,
allow_remove_liquidity: None,
allow_increase_position: None,
allow_decrease_position: None,
allow_collateral_withdrawal: None,
allow_liquidate_position: None,
__remaining_accounts: Vec::new(),
});
Self { instruction }
}
#[inline(always)]
pub fn upgrade_authority(
&mut self,
upgrade_authority: &'b solana_program::account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.upgrade_authority = Some(upgrade_authority);
self
}
#[inline(always)]
pub fn admin(&mut self, admin: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.admin = Some(admin);
self
}
#[inline(always)]
pub fn transfer_authority(
&mut self,
transfer_authority: &'b solana_program::account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.transfer_authority = Some(transfer_authority);
self
}
#[inline(always)]
pub fn perpetuals(
&mut self,
perpetuals: &'b solana_program::account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.perpetuals = Some(perpetuals);
self
}
#[inline(always)]
pub fn perpetuals_program(
&mut self,
perpetuals_program: &'b solana_program::account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.perpetuals_program = Some(perpetuals_program);
self
}
#[inline(always)]
pub fn perpetuals_program_data(
&mut self,
perpetuals_program_data: &'b solana_program::account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.perpetuals_program_data = Some(perpetuals_program_data);
self
}
#[inline(always)]
pub fn system_program(
&mut self,
system_program: &'b solana_program::account_info::AccountInfo<'a>,
) -> &mut Self {
self.instruction.system_program = Some(system_program);
self
}
#[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 allow_swap(&mut self, allow_swap: bool) -> &mut Self {
self.instruction.allow_swap = Some(allow_swap);
self
}
#[inline(always)]
pub fn allow_add_liquidity(&mut self, allow_add_liquidity: bool) -> &mut Self {
self.instruction.allow_add_liquidity = Some(allow_add_liquidity);
self
}
#[inline(always)]
pub fn allow_remove_liquidity(&mut self, allow_remove_liquidity: bool) -> &mut Self {
self.instruction.allow_remove_liquidity = Some(allow_remove_liquidity);
self
}
#[inline(always)]
pub fn allow_increase_position(&mut self, allow_increase_position: bool) -> &mut Self {
self.instruction.allow_increase_position = Some(allow_increase_position);
self
}
#[inline(always)]
pub fn allow_decrease_position(&mut self, allow_decrease_position: bool) -> &mut Self {
self.instruction.allow_decrease_position = Some(allow_decrease_position);
self
}
#[inline(always)]
pub fn allow_collateral_withdrawal(&mut self, allow_collateral_withdrawal: bool) -> &mut Self {
self.instruction.allow_collateral_withdrawal = Some(allow_collateral_withdrawal);
self
}
#[inline(always)]
pub fn allow_liquidate_position(&mut self, allow_liquidate_position: bool) -> &mut Self {
self.instruction.allow_liquidate_position = Some(allow_liquidate_position);
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 = InitInstructionArgs {
allow_swap: self
.instruction
.allow_swap
.clone()
.expect("allow_swap is not set"),
allow_add_liquidity: self
.instruction
.allow_add_liquidity
.clone()
.expect("allow_add_liquidity is not set"),
allow_remove_liquidity: self
.instruction
.allow_remove_liquidity
.clone()
.expect("allow_remove_liquidity is not set"),
allow_increase_position: self
.instruction
.allow_increase_position
.clone()
.expect("allow_increase_position is not set"),
allow_decrease_position: self
.instruction
.allow_decrease_position
.clone()
.expect("allow_decrease_position is not set"),
allow_collateral_withdrawal: self
.instruction
.allow_collateral_withdrawal
.clone()
.expect("allow_collateral_withdrawal is not set"),
allow_liquidate_position: self
.instruction
.allow_liquidate_position
.clone()
.expect("allow_liquidate_position is not set"),
};
let instruction = InitCpi {
__program: self.instruction.__program,
upgrade_authority: self
.instruction
.upgrade_authority
.expect("upgrade_authority is not set"),
admin: self.instruction.admin.expect("admin is not set"),
transfer_authority: self
.instruction
.transfer_authority
.expect("transfer_authority is not set"),
perpetuals: self.instruction.perpetuals.expect("perpetuals is not set"),
perpetuals_program: self
.instruction
.perpetuals_program
.expect("perpetuals_program is not set"),
perpetuals_program_data: self
.instruction
.perpetuals_program_data
.expect("perpetuals_program_data is not set"),
system_program: self
.instruction
.system_program
.expect("system_program is not set"),
token_program: self
.instruction
.token_program
.expect("token_program is not set"),
__args: args,
};
instruction.invoke_signed_with_remaining_accounts(
signers_seeds,
&self.instruction.__remaining_accounts,
)
}
}
#[derive(Clone, Debug)]
struct InitCpiBuilderInstruction<'a, 'b> {
__program: &'b solana_program::account_info::AccountInfo<'a>,
upgrade_authority: Option<&'b solana_program::account_info::AccountInfo<'a>>,
admin: Option<&'b solana_program::account_info::AccountInfo<'a>>,
transfer_authority: Option<&'b solana_program::account_info::AccountInfo<'a>>,
perpetuals: Option<&'b solana_program::account_info::AccountInfo<'a>>,
perpetuals_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
perpetuals_program_data: Option<&'b solana_program::account_info::AccountInfo<'a>>,
system_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
token_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
allow_swap: Option<bool>,
allow_add_liquidity: Option<bool>,
allow_remove_liquidity: Option<bool>,
allow_increase_position: Option<bool>,
allow_decrease_position: Option<bool>,
allow_collateral_withdrawal: Option<bool>,
allow_liquidate_position: Option<bool>,
__remaining_accounts: Vec<(
&'b solana_program::account_info::AccountInfo<'a>,
bool,
bool,
)>,
}