spl_token_2022/extension/pausable/
processor.rs

1use {
2    crate::{
3        check_program_account,
4        error::TokenError,
5        extension::{
6            pausable::{
7                instruction::{InitializeInstructionData, PausableInstruction},
8                PausableConfig,
9            },
10            BaseStateWithExtensionsMut, PodStateWithExtensionsMut,
11        },
12        instruction::{decode_instruction_data, decode_instruction_type},
13        pod::PodMint,
14        processor::Processor,
15    },
16    solana_program::{
17        account_info::{next_account_info, AccountInfo},
18        entrypoint::ProgramResult,
19        msg,
20        pubkey::Pubkey,
21    },
22};
23
24fn process_initialize(
25    _program_id: &Pubkey,
26    accounts: &[AccountInfo],
27    authority: &Pubkey,
28) -> ProgramResult {
29    let account_info_iter = &mut accounts.iter();
30    let mint_account_info = next_account_info(account_info_iter)?;
31    let mut mint_data = mint_account_info.data.borrow_mut();
32    let mut mint = PodStateWithExtensionsMut::<PodMint>::unpack_uninitialized(&mut mint_data)?;
33
34    let extension = mint.init_extension::<PausableConfig>(true)?;
35    extension.authority = Some(*authority).try_into()?;
36
37    Ok(())
38}
39
40/// Pause or resume minting / burning / transferring on the mint
41fn process_toggle_pause(
42    program_id: &Pubkey,
43    accounts: &[AccountInfo],
44    pause: bool,
45) -> ProgramResult {
46    let account_info_iter = &mut accounts.iter();
47    let mint_account_info = next_account_info(account_info_iter)?;
48    let authority_info = next_account_info(account_info_iter)?;
49    let authority_info_data_len = authority_info.data_len();
50
51    let mut mint_data = mint_account_info.data.borrow_mut();
52    let mut mint = PodStateWithExtensionsMut::<PodMint>::unpack(&mut mint_data)?;
53    let extension = mint.get_extension_mut::<PausableConfig>()?;
54    let maybe_authority: Option<Pubkey> = extension.authority.into();
55    let authority = maybe_authority.ok_or(TokenError::AuthorityTypeNotSupported)?;
56
57    Processor::validate_owner(
58        program_id,
59        &authority,
60        authority_info,
61        authority_info_data_len,
62        account_info_iter.as_slice(),
63    )?;
64
65    extension.paused = pause.into();
66    Ok(())
67}
68
69pub(crate) fn process_instruction(
70    program_id: &Pubkey,
71    accounts: &[AccountInfo],
72    input: &[u8],
73) -> ProgramResult {
74    check_program_account(program_id)?;
75
76    match decode_instruction_type(input)? {
77        PausableInstruction::Initialize => {
78            msg!("PausableInstruction::Initialize");
79            let InitializeInstructionData { authority } = decode_instruction_data(input)?;
80            process_initialize(program_id, accounts, authority)
81        }
82        PausableInstruction::Pause => {
83            msg!("PausableInstruction::Pause");
84            process_toggle_pause(program_id, accounts, true /* pause */)
85        }
86        PausableInstruction::Resume => {
87            msg!("PausableInstruction::Resume");
88            process_toggle_pause(program_id, accounts, false /* resume */)
89        }
90    }
91}