use pinocchio::{
account_info::AccountInfo,
entrypoint::ProgramResult,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
};
pub struct CreateAccountWithSeed<'a, 'b, 'c> {
pub from: &'a AccountInfo,
pub to: &'a AccountInfo,
pub base: Option<&'a AccountInfo>,
pub seed: &'b str,
pub lamports: u64,
pub space: u64,
pub owner: &'c Pubkey,
}
impl<'a, 'b, 'c> CreateAccountWithSeed<'a, 'b, 'c> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}
pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
let account_metas: [AccountMeta; 3] = [
AccountMeta::writable_signer(self.from.key()),
AccountMeta::writable(self.to.key()),
AccountMeta::readonly_signer(self.base.unwrap_or(self.from).key()),
];
let mut instruction_data = [0; 120];
instruction_data[0] = 3;
instruction_data[4..36].copy_from_slice(self.base.unwrap_or(self.from).key());
instruction_data[36..40].copy_from_slice(&u32::to_le_bytes(self.seed.len() as u32));
let offset = 40 + self.seed.len();
instruction_data[40..offset].copy_from_slice(self.seed.as_bytes());
instruction_data[offset..offset + 8].copy_from_slice(&self.lamports.to_le_bytes());
instruction_data[offset + 8..offset + 16].copy_from_slice(&self.space.to_le_bytes());
instruction_data[offset + 16..offset + 48].copy_from_slice(self.owner.as_ref());
let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data[..offset + 48],
};
invoke_signed(
&instruction,
&[self.from, self.to, self.base.unwrap_or(self.from)],
signers,
)
}
}