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