use {
core::{mem::MaybeUninit, slice::from_raw_parts},
pinocchio::{
cpi::{invoke_signed_unchecked, CpiAccount, Signer},
error::ProgramError,
instruction::{InstructionAccount, InstructionView},
AccountView, ProgramResult,
},
};
pub struct AdvanceNonceAccount<'account> {
pub account: &'account AccountView,
pub recent_blockhashes_sysvar: &'account AccountView,
pub authority: &'account AccountView,
}
impl AdvanceNonceAccount<'_> {
pub const DISCRIMINATOR: u32 = 4;
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}
#[inline(always)]
pub fn invoke_signed(&self, signers: &[Signer]) -> 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_signer(
self.authority.address(),
));
let instruction = InstructionView {
program_id: &crate::ID,
accounts: unsafe { from_raw_parts(instruction_accounts.as_ptr() as _, 3) },
data: &Self::DISCRIMINATOR.to_le_bytes(),
};
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.authority, &mut accounts[2]);
unsafe {
invoke_signed_unchecked(
&instruction,
from_raw_parts(accounts.as_ptr() as _, 3),
signers,
)
};
Ok(())
}
}