pinocchio_token/instructions/
approve.rs1use core::slice::from_raw_parts;
2
3use solana_account_view::AccountView;
4use solana_instruction_view::{
5 cpi::{invoke_signed, Signer},
6 InstructionAccount, InstructionView,
7};
8use solana_program_error::ProgramResult;
9
10use crate::{write_bytes, UNINIT_BYTE};
11
12pub struct Approve<'a> {
19 pub source: &'a AccountView,
21 pub delegate: &'a AccountView,
23 pub authority: &'a AccountView,
25 pub amount: u64,
27}
28
29impl Approve<'_> {
30 #[inline(always)]
31 pub fn invoke(&self) -> ProgramResult {
32 self.invoke_signed(&[])
33 }
34
35 #[inline(always)]
36 pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
37 let instruction_accounts: [InstructionAccount; 3] = [
39 InstructionAccount::writable(self.source.address()),
40 InstructionAccount::readonly(self.delegate.address()),
41 InstructionAccount::readonly_signer(self.authority.address()),
42 ];
43
44 let mut instruction_data = [UNINIT_BYTE; 9];
48
49 write_bytes(&mut instruction_data, &[4]);
51 write_bytes(&mut instruction_data[1..], &self.amount.to_le_bytes());
53
54 let instruction = InstructionView {
55 program_id: &crate::ID,
56 accounts: &instruction_accounts,
57 data: unsafe { from_raw_parts(instruction_data.as_ptr() as _, 9) },
58 };
59
60 invoke_signed(
61 &instruction,
62 &[self.source, self.delegate, self.authority],
63 signers,
64 )
65 }
66}