use pinocchio::{
cpi::{invoke_signed, Signer},
instruction::{InstructionAccount, InstructionView},
AccountView, ProgramResult,
};
pub struct Transfer<'a> {
pub from: &'a AccountView,
pub to: &'a AccountView,
pub lamports: u64,
}
impl Transfer<'_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}
#[inline(always)]
pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
let instruction_accounts: [InstructionAccount; 2] = [
InstructionAccount::writable_signer(self.from.address()),
InstructionAccount::writable(self.to.address()),
];
let mut instruction_data = [0; 12];
instruction_data[0] = 2;
instruction_data[4..12].copy_from_slice(&self.lamports.to_le_bytes());
let instruction = InstructionView {
program_id: &crate::ID,
accounts: &instruction_accounts,
data: &instruction_data,
};
invoke_signed(&instruction, &[self.from, self.to], signers)
}
}