pinocchio_token/instructions/
set_authority.rs1use core::slice::from_raw_parts;
2
3use solana_account_view::AccountView;
4use solana_address::Address;
5use solana_instruction_view::{
6 cpi::{invoke_signed, Signer},
7 InstructionAccount, InstructionView,
8};
9use solana_program_error::ProgramResult;
10
11use crate::{write_bytes, UNINIT_BYTE};
12
13#[repr(u8)]
14#[derive(Clone, Copy)]
15pub enum AuthorityType {
16 MintTokens = 0,
17 FreezeAccount = 1,
18 AccountOwner = 2,
19 CloseAccount = 3,
20}
21
22pub struct SetAuthority<'a> {
28 pub account: &'a AccountView,
30 pub authority: &'a AccountView,
32 pub authority_type: AuthorityType,
34 pub new_authority: Option<&'a Address>,
36}
37
38impl SetAuthority<'_> {
39 #[inline(always)]
40 pub fn invoke(&self) -> ProgramResult {
41 self.invoke_signed(&[])
42 }
43
44 #[inline(always)]
45 pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
46 let instruction_accounts: [InstructionAccount; 2] = [
48 InstructionAccount::writable(self.account.address()),
49 InstructionAccount::readonly_signer(self.authority.address()),
50 ];
51
52 let mut instruction_data = [UNINIT_BYTE; 35];
58 let mut length = instruction_data.len();
59
60 write_bytes(&mut instruction_data, &[6]);
62 write_bytes(&mut instruction_data[1..2], &[self.authority_type as u8]);
64
65 if let Some(new_authority) = self.new_authority {
66 write_bytes(&mut instruction_data[2..3], &[1]);
68 write_bytes(&mut instruction_data[3..], new_authority.as_array());
69 } else {
70 write_bytes(&mut instruction_data[2..3], &[0]);
71 length = 3;
73 }
74
75 let instruction = InstructionView {
76 program_id: &crate::ID,
77 accounts: &instruction_accounts,
78 data: unsafe { from_raw_parts(instruction_data.as_ptr() as _, length) },
79 };
80
81 invoke_signed(&instruction, &[self.account, self.authority], signers)
82 }
83}