pinocchio_token/instructions/
approve_checked.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 ApproveChecked<'a> {
20 pub source: &'a AccountView,
22 pub mint: &'a AccountView,
24 pub delegate: &'a AccountView,
26 pub authority: &'a AccountView,
28 pub amount: u64,
30 pub decimals: u8,
32}
33
34impl ApproveChecked<'_> {
35 #[inline(always)]
36 pub fn invoke(&self) -> ProgramResult {
37 self.invoke_signed(&[])
38 }
39
40 #[inline(always)]
41 pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
42 let instruction_accounts: [InstructionAccount; 4] = [
44 InstructionAccount::writable(self.source.address()),
45 InstructionAccount::readonly(self.mint.address()),
46 InstructionAccount::readonly(self.delegate.address()),
47 InstructionAccount::readonly_signer(self.authority.address()),
48 ];
49
50 let mut instruction_data = [UNINIT_BYTE; 10];
55
56 write_bytes(&mut instruction_data, &[13]);
58 write_bytes(&mut instruction_data[1..9], &self.amount.to_le_bytes());
60 write_bytes(&mut instruction_data[9..], &[self.decimals]);
62
63 let instruction = InstructionView {
64 program_id: &crate::ID,
65 accounts: &instruction_accounts,
66 data: unsafe { from_raw_parts(instruction_data.as_ptr() as _, 10) },
67 };
68
69 invoke_signed(
70 &instruction,
71 &[self.source, self.mint, self.delegate, self.authority],
72 signers,
73 )
74 }
75}