1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use {
    super::*,
    spl_token_2022::extension::default_account_state::instruction::{
        decode_instruction, DefaultAccountStateInstruction,
    },
};

pub(in crate::parse_token) fn parse_default_account_state_instruction(
    instruction_data: &[u8],
    account_indexes: &[u8],
    account_keys: &AccountKeys,
) -> Result<ParsedInstructionEnum, ParseInstructionError> {
    let (default_account_state_instruction, account_state) = decode_instruction(instruction_data)
        .map_err(|_| {
        ParseInstructionError::InstructionNotParsable(ParsableProgram::SplToken)
    })?;
    let instruction_type = "DefaultAccountState";
    match default_account_state_instruction {
        DefaultAccountStateInstruction::Initialize => {
            check_num_token_accounts(account_indexes, 1)?;
            Ok(ParsedInstructionEnum {
                instruction_type: format!("initialize{instruction_type}"),
                info: json!({
                    "mint": account_keys[account_indexes[0] as usize].to_string(),
                    "accountState": UiAccountState::from(account_state),
                }),
            })
        }
        DefaultAccountStateInstruction::Update => {
            check_num_token_accounts(account_indexes, 2)?;
            let mut value = json!({
                "mint": account_keys[account_indexes[0] as usize].to_string(),
                "accountState": UiAccountState::from(account_state),
            });
            let map = value.as_object_mut().unwrap();
            parse_signers(
                map,
                1,
                account_keys,
                account_indexes,
                "freezeAuthority",
                "multisigFreezeAuthority",
            );
            Ok(ParsedInstructionEnum {
                instruction_type: format!("update{instruction_type}"),
                info: value,
            })
        }
    }
}

#[cfg(test)]
mod test {
    use {
        super::*,
        crate::parse_token::test::*,
        solana_sdk::pubkey::Pubkey,
        spl_token_2022::{
            extension::default_account_state::instruction::{
                initialize_default_account_state, update_default_account_state,
            },
            solana_program::message::Message,
            state::AccountState,
        },
    };

    #[test]
    fn test_parse_default_account_state_instruction() {
        let mint_pubkey = Pubkey::new_unique();
        let init_default_account_state_ix = initialize_default_account_state(
            &spl_token_2022::id(),
            &convert_pubkey(mint_pubkey),
            &AccountState::Frozen,
        )
        .unwrap();
        let message = Message::new(&[init_default_account_state_ix], None);
        let compiled_instruction = convert_compiled_instruction(&message.instructions[0]);
        assert_eq!(
            parse_token(
                &compiled_instruction,
                &AccountKeys::new(&convert_account_keys(&message), None)
            )
            .unwrap(),
            ParsedInstructionEnum {
                instruction_type: "initializeDefaultAccountState".to_string(),
                info: json!({
                    "mint": mint_pubkey.to_string(),
                    "accountState": "frozen",
                })
            }
        );

        // Single mint freeze_authority
        let mint_freeze_authority = Pubkey::new_unique();
        let update_default_account_state_ix = update_default_account_state(
            &spl_token_2022::id(),
            &convert_pubkey(mint_pubkey),
            &convert_pubkey(mint_freeze_authority),
            &[],
            &AccountState::Initialized,
        )
        .unwrap();
        let message = Message::new(&[update_default_account_state_ix], None);
        let compiled_instruction = convert_compiled_instruction(&message.instructions[0]);
        assert_eq!(
            parse_token(
                &compiled_instruction,
                &AccountKeys::new(&convert_account_keys(&message), None)
            )
            .unwrap(),
            ParsedInstructionEnum {
                instruction_type: "updateDefaultAccountState".to_string(),
                info: json!({
                    "mint": mint_pubkey.to_string(),
                    "accountState": "initialized",
                    "freezeAuthority": mint_freeze_authority.to_string(),
                })
            }
        );

        // Multisig mint freeze_authority
        let multisig_pubkey = Pubkey::new_unique();
        let multisig_signer0 = Pubkey::new_unique();
        let multisig_signer1 = Pubkey::new_unique();
        let update_default_account_state_ix = update_default_account_state(
            &spl_token_2022::id(),
            &convert_pubkey(mint_pubkey),
            &convert_pubkey(multisig_pubkey),
            &[
                &convert_pubkey(multisig_signer0),
                &convert_pubkey(multisig_signer1),
            ],
            &AccountState::Initialized,
        )
        .unwrap();
        let message = Message::new(&[update_default_account_state_ix], None);
        let compiled_instruction = convert_compiled_instruction(&message.instructions[0]);
        assert_eq!(
            parse_token(
                &compiled_instruction,
                &AccountKeys::new(&convert_account_keys(&message), None)
            )
            .unwrap(),
            ParsedInstructionEnum {
                instruction_type: "updateDefaultAccountState".to_string(),
                info: json!({
                    "mint": mint_pubkey.to_string(),
                    "accountState": "initialized",
                    "multisigFreezeAuthority": multisig_pubkey.to_string(),
                    "signers": vec![
                        multisig_signer0.to_string(),
                        multisig_signer1.to_string(),
                    ],
                })
            }
        );
    }
}