spl_token_2022/extension/pausable/
instruction.rs

1#[cfg(feature = "serde-traits")]
2use serde::{Deserialize, Serialize};
3use {
4    crate::{
5        check_program_account,
6        instruction::{encode_instruction, TokenInstruction},
7    },
8    bytemuck::{Pod, Zeroable},
9    num_enum::{IntoPrimitive, TryFromPrimitive},
10    solana_program::{
11        instruction::{AccountMeta, Instruction},
12        program_error::ProgramError,
13        pubkey::Pubkey,
14    },
15};
16
17/// Pausable extension instructions
18#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
19#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
20#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
21#[repr(u8)]
22pub enum PausableInstruction {
23    /// Initialize the pausable extension for the given mint account
24    ///
25    /// Fails if the account has already been initialized, so must be called
26    /// before `InitializeMint`.
27    ///
28    /// Accounts expected by this instruction:
29    ///
30    ///   0. `[writable]`  The mint account to initialize.
31    ///
32    /// Data expected by this instruction:
33    ///   `crate::extension::pausable::instruction::InitializeInstructionData`
34    Initialize,
35    /// Pause minting, burning, and transferring for the mint.
36    ///
37    /// Accounts expected by this instruction:
38    ///
39    ///   0. `[writable]` The mint to update.
40    ///   1. `[signer]` The mint's pause authority.
41    ///
42    ///   * Multisignature authority
43    ///   0. `[writable]` The mint to update.
44    ///   1. `[]` The mint's multisignature pause authority.
45    ///   2. `..2+M` `[signer]` M signer accounts.
46    Pause,
47    /// Resume minting, burning, and transferring for the mint.
48    ///
49    /// Accounts expected by this instruction:
50    ///
51    ///   0. `[writable]` The mint to update.
52    ///   1. `[signer]` The mint's pause authority.
53    ///
54    ///   * Multisignature authority
55    ///   0. `[writable]` The mint to update.
56    ///   1. `[]` The mint's multisignature pause authority.
57    ///   2. `..2+M` `[signer]` M signer accounts.
58    Resume,
59}
60
61/// Data expected by `PausableInstruction::Initialize`
62#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
63#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
64#[derive(Clone, Copy, Pod, Zeroable)]
65#[repr(C)]
66pub struct InitializeInstructionData {
67    /// The public key for the account that can pause the mint
68    pub authority: Pubkey,
69}
70
71/// Create an `Initialize` instruction
72pub fn initialize(
73    token_program_id: &Pubkey,
74    mint: &Pubkey,
75    authority: &Pubkey,
76) -> Result<Instruction, ProgramError> {
77    check_program_account(token_program_id)?;
78    let accounts = vec![AccountMeta::new(*mint, false)];
79    Ok(encode_instruction(
80        token_program_id,
81        accounts,
82        TokenInstruction::PausableExtension,
83        PausableInstruction::Initialize,
84        &InitializeInstructionData {
85            authority: *authority,
86        },
87    ))
88}
89
90/// Create a `Pause` instruction
91pub fn pause(
92    token_program_id: &Pubkey,
93    mint: &Pubkey,
94    authority: &Pubkey,
95    signers: &[&Pubkey],
96) -> Result<Instruction, ProgramError> {
97    check_program_account(token_program_id)?;
98    let mut accounts = vec![
99        AccountMeta::new(*mint, false),
100        AccountMeta::new_readonly(*authority, signers.is_empty()),
101    ];
102    for signer_pubkey in signers.iter() {
103        accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
104    }
105    Ok(encode_instruction(
106        token_program_id,
107        accounts,
108        TokenInstruction::PausableExtension,
109        PausableInstruction::Pause,
110        &(),
111    ))
112}
113
114/// Create a `Resume` instruction
115pub fn resume(
116    token_program_id: &Pubkey,
117    mint: &Pubkey,
118    authority: &Pubkey,
119    signers: &[&Pubkey],
120) -> Result<Instruction, ProgramError> {
121    check_program_account(token_program_id)?;
122    let mut accounts = vec![
123        AccountMeta::new(*mint, false),
124        AccountMeta::new_readonly(*authority, signers.is_empty()),
125    ];
126    for signer_pubkey in signers.iter() {
127        accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
128    }
129    Ok(encode_instruction(
130        token_program_id,
131        accounts,
132        TokenInstruction::PausableExtension,
133        PausableInstruction::Resume,
134        &(),
135    ))
136}