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