use {
crate::hooked::ConfigKeys,
borsh::{BorshDeserialize, BorshSerialize},
kaigan::types::RemainderVec,
};
#[derive(Debug)]
pub struct Store {
pub config_account: (solana_program::pubkey::Pubkey, bool),
}
impl Store {
pub fn instruction(
&self,
args: StoreInstructionArgs,
) -> solana_program::instruction::Instruction {
self.instruction_with_remaining_accounts(args, &[])
}
#[allow(clippy::vec_init_then_push)]
pub fn instruction_with_remaining_accounts(
&self,
args: StoreInstructionArgs,
remaining_accounts: &[solana_program::instruction::AccountMeta],
) -> solana_program::instruction::Instruction {
let mut accounts = Vec::with_capacity(1 + remaining_accounts.len());
accounts.push(solana_program::instruction::AccountMeta::new(
self.config_account.0,
self.config_account.1,
));
accounts.extend_from_slice(remaining_accounts);
let mut data = borsh::to_vec(&StoreInstructionData::new()).unwrap();
let mut args = borsh::to_vec(&args).unwrap();
data.append(&mut args);
solana_program::instruction::Instruction {
program_id: crate::SOLANA_CONFIG_ID,
accounts,
data,
}
}
}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StoreInstructionData {}
impl StoreInstructionData {
pub fn new() -> Self {
Self {}
}
}
impl Default for StoreInstructionData {
fn default() -> Self {
Self::new()
}
}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StoreInstructionArgs {
pub keys: ConfigKeys,
pub data: RemainderVec<u8>,
}
#[derive(Clone, Debug, Default)]
pub struct StoreBuilder {
config_account: Option<(solana_program::pubkey::Pubkey, bool)>,
keys: Option<ConfigKeys>,
data: Option<RemainderVec<u8>>,
__remaining_accounts: Vec<solana_program::instruction::AccountMeta>,
}
impl StoreBuilder {
pub fn new() -> Self {
Self::default()
}
#[inline(always)]
pub fn config_account(
&mut self,
config_account: solana_program::pubkey::Pubkey,
as_signer: bool,
) -> &mut Self {
self.config_account = Some((config_account, as_signer));
self
}
#[inline(always)]
pub fn keys(&mut self, keys: ConfigKeys) -> &mut Self {
self.keys = Some(keys);
self
}
#[inline(always)]
pub fn data(&mut self, data: RemainderVec<u8>) -> &mut Self {
self.data = Some(data);
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 = Store {
config_account: self.config_account.expect("config_account is not set"),
};
let args = StoreInstructionArgs {
keys: self.keys.clone().expect("keys is not set"),
data: self.data.clone().expect("data is not set"),
};
accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
}
}
pub struct StoreCpiAccounts<'a, 'b> {
pub config_account: (&'b solana_program::account_info::AccountInfo<'a>, bool),
}
pub struct StoreCpi<'a, 'b> {
pub __program: &'b solana_program::account_info::AccountInfo<'a>,
pub config_account: (&'b solana_program::account_info::AccountInfo<'a>, bool),
pub __args: StoreInstructionArgs,
}
impl<'a, 'b> StoreCpi<'a, 'b> {
pub fn new(
program: &'b solana_program::account_info::AccountInfo<'a>,
accounts: StoreCpiAccounts<'a, 'b>,
args: StoreInstructionArgs,
) -> Self {
Self {
__program: program,
config_account: accounts.config_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::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(1 + remaining_accounts.len());
accounts.push(solana_program::instruction::AccountMeta::new(
*self.config_account.0.key,
self.config_account.1,
));
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(&StoreInstructionData::new()).unwrap();
let mut args = borsh::to_vec(&self.__args).unwrap();
data.append(&mut args);
let instruction = solana_program::instruction::Instruction {
program_id: crate::SOLANA_CONFIG_ID,
accounts,
data,
};
let mut account_infos = Vec::with_capacity(2 + remaining_accounts.len());
account_infos.push(self.__program.clone());
account_infos.push(self.config_account.0.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 StoreCpiBuilder<'a, 'b> {
instruction: Box<StoreCpiBuilderInstruction<'a, 'b>>,
}
impl<'a, 'b> StoreCpiBuilder<'a, 'b> {
pub fn new(program: &'b solana_program::account_info::AccountInfo<'a>) -> Self {
let instruction = Box::new(StoreCpiBuilderInstruction {
__program: program,
config_account: None,
keys: None,
data: None,
__remaining_accounts: Vec::new(),
});
Self { instruction }
}
#[inline(always)]
pub fn config_account(
&mut self,
config_account: &'b solana_program::account_info::AccountInfo<'a>,
as_signer: bool,
) -> &mut Self {
self.instruction.config_account = Some((config_account, as_signer));
self
}
#[inline(always)]
pub fn keys(&mut self, keys: ConfigKeys) -> &mut Self {
self.instruction.keys = Some(keys);
self
}
#[inline(always)]
pub fn data(&mut self, data: RemainderVec<u8>) -> &mut Self {
self.instruction.data = Some(data);
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 = StoreInstructionArgs {
keys: self.instruction.keys.clone().expect("keys is not set"),
data: self.instruction.data.clone().expect("data is not set"),
};
let instruction = StoreCpi {
__program: self.instruction.__program,
config_account: self
.instruction
.config_account
.expect("config_account is not set"),
__args: args,
};
instruction.invoke_signed_with_remaining_accounts(
signers_seeds,
&self.instruction.__remaining_accounts,
)
}
}
#[derive(Clone, Debug)]
struct StoreCpiBuilderInstruction<'a, 'b> {
__program: &'b solana_program::account_info::AccountInfo<'a>,
config_account: Option<(&'b solana_program::account_info::AccountInfo<'a>, bool)>,
keys: Option<ConfigKeys>,
data: Option<RemainderVec<u8>>,
__remaining_accounts: Vec<(
&'b solana_program::account_info::AccountInfo<'a>,
bool,
bool,
)>,
}