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