spl_token_2022_interface/extension/cpi_guard/
instruction.rs1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3use {
4 crate::{
5 check_program_account,
6 instruction::{encode_instruction, TokenInstruction},
7 },
8 num_enum::{IntoPrimitive, TryFromPrimitive},
9 solana_instruction::{AccountMeta, Instruction},
10 solana_program_error::ProgramError,
11 solana_pubkey::Pubkey,
12};
13
14#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
17#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
18#[repr(u8)]
19pub enum CpiGuardInstruction {
20 Enable,
39 Disable,
54}
55
56pub fn enable_cpi_guard(
58 token_program_id: &Pubkey,
59 account: &Pubkey,
60 owner: &Pubkey,
61 signers: &[&Pubkey],
62) -> Result<Instruction, ProgramError> {
63 check_program_account(token_program_id)?;
64 let mut accounts = vec![
65 AccountMeta::new(*account, false),
66 AccountMeta::new_readonly(*owner, signers.is_empty()),
67 ];
68 for signer_pubkey in signers.iter() {
69 accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
70 }
71 Ok(encode_instruction(
72 token_program_id,
73 accounts,
74 TokenInstruction::CpiGuardExtension,
75 CpiGuardInstruction::Enable,
76 &(),
77 ))
78}
79
80pub fn disable_cpi_guard(
82 token_program_id: &Pubkey,
83 account: &Pubkey,
84 owner: &Pubkey,
85 signers: &[&Pubkey],
86) -> Result<Instruction, ProgramError> {
87 check_program_account(token_program_id)?;
88 let mut accounts = vec![
89 AccountMeta::new(*account, false),
90 AccountMeta::new_readonly(*owner, signers.is_empty()),
91 ];
92 for signer_pubkey in signers.iter() {
93 accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
94 }
95 Ok(encode_instruction(
96 token_program_id,
97 accounts,
98 TokenInstruction::CpiGuardExtension,
99 CpiGuardInstruction::Disable,
100 &(),
101 ))
102}