use {
core::{mem::MaybeUninit, ptr::copy_nonoverlapping, slice::from_raw_parts},
pinocchio::{
cpi::{invoke_signed_unchecked, CpiAccount},
error::ProgramError,
instruction::{InstructionAccount, InstructionView},
AccountView, Address, ProgramResult,
},
solana_address::ADDRESS_BYTES,
};
pub struct InitializeNonceAccount<'account, 'address> {
pub account: &'account AccountView,
pub recent_blockhashes_sysvar: &'account AccountView,
pub rent_sysvar: &'account AccountView,
pub authority: &'address Address,
}
impl InitializeNonceAccount<'_, '_> {
pub const DISCRIMINATOR: u32 = 6;
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
let mut instruction_accounts = [const { MaybeUninit::<InstructionAccount>::uninit() }; 3];
instruction_accounts[0].write(InstructionAccount::writable(self.account.address()));
instruction_accounts[1].write(InstructionAccount::readonly(
self.recent_blockhashes_sysvar.address(),
));
instruction_accounts[2].write(InstructionAccount::readonly(self.rent_sysvar.address()));
let mut instruction_data = [const { MaybeUninit::<u8>::uninit() }; 36];
unsafe {
let dst = instruction_data.as_mut_ptr() as *mut u8;
copy_nonoverlapping(
Self::DISCRIMINATOR.to_le_bytes().as_ptr(),
dst,
size_of::<u32>(),
);
copy_nonoverlapping(self.authority.as_ref().as_ptr(), dst.add(4), ADDRESS_BYTES);
}
let instruction = InstructionView {
program_id: &crate::ID,
accounts: unsafe { from_raw_parts(instruction_accounts.as_ptr() as _, 3) },
data: unsafe { from_raw_parts(instruction_data.as_ptr() as _, 36) },
};
if self.account.is_borrowed() {
return Err(ProgramError::AccountBorrowFailed);
}
let mut accounts = [const { MaybeUninit::<CpiAccount>::uninit() }; 3];
CpiAccount::init_from_account_view(self.account, &mut accounts[0]);
CpiAccount::init_from_account_view(self.recent_blockhashes_sysvar, &mut accounts[1]);
CpiAccount::init_from_account_view(self.rent_sysvar, &mut accounts[2]);
unsafe {
invoke_signed_unchecked(&instruction, from_raw_parts(accounts.as_ptr() as _, 3), &[])
};
Ok(())
}
}