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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use anchor_lang::{prelude::*, solana_program::sysvar};
use mpl_token_metadata::{accounts::Metadata, types::TokenStandard};
use mpl_utils::resize_or_reallocate_account_raw;

use crate::{
    approve_metadata_delegate, assert_token_standard, cmp_pubkeys,
    constants::{AUTHORITY_SEED, MPL_TOKEN_AUTH_RULES_PROGRAM, RULE_SET_LENGTH, SET, UNSET},
    revoke_collection_authority_helper, AccountVersion, ApproveMetadataDelegateHelperAccounts,
    CandyError, CandyMachine, RevokeCollectionAuthorityHelperAccounts,
};

pub fn set_token_standard(ctx: Context<SetTokenStandard>, token_standard: u8) -> Result<()> {
    let accounts = ctx.accounts;
    let candy_machine = &mut accounts.candy_machine;

    let collection_metadata_info = &accounts.collection_metadata;
    let collection_metadata: Metadata =
        Metadata::try_from(&collection_metadata_info.to_account_info())?;
    // check that the update authority matches the collection update authority
    if !cmp_pubkeys(&collection_metadata.mint, accounts.collection_mint.key) {
        return err!(CandyError::MintMismatch);
    }

    assert_token_standard(token_standard)?;

    if matches!(candy_machine.version, AccountVersion::V1) {
        // revoking the existing collection authority
        let collection_authority_record = accounts
            .collection_authority_record
            .as_ref()
            .ok_or(CandyError::MissingCollectionAuthorityRecord)?;

        let revoke_accounts = RevokeCollectionAuthorityHelperAccounts {
            authority_pda: accounts.authority_pda.to_account_info(),
            collection_authority_record: collection_authority_record.to_account_info(),
            collection_metadata: accounts.collection_metadata.to_account_info(),
            collection_mint: accounts.collection_mint.to_account_info(),
            token_metadata_program: accounts.token_metadata_program.to_account_info(),
        };

        revoke_collection_authority_helper(
            revoke_accounts,
            candy_machine.key(),
            *ctx.bumps.get("authority_pda").unwrap(),
            collection_metadata.token_standard,
        )?;

        // approve a new metadata delegate

        let delegate_accounts = ApproveMetadataDelegateHelperAccounts {
            token_metadata_program: accounts.token_metadata_program.to_account_info(),
            authority_pda: accounts.authority_pda.to_account_info(),
            collection_metadata: accounts.collection_metadata.to_account_info(),
            collection_mint: accounts.collection_mint.to_account_info(),
            collection_update_authority: accounts.collection_update_authority.to_account_info(),
            delegate_record: accounts.collection_delegate_record.to_account_info(),
            payer: accounts.payer.to_account_info(),
            system_program: accounts.system_program.to_account_info(),
            sysvar_instructions: accounts.sysvar_instructions.to_account_info(),
            authorization_rules_program: accounts
                .authorization_rules_program
                .to_owned()
                .map(|authorization_rules_program| authorization_rules_program.to_account_info()),
            authorization_rules: accounts
                .authorization_rules
                .to_owned()
                .map(|authorization_rules| authorization_rules.to_account_info()),
        };

        approve_metadata_delegate(delegate_accounts)?;
        // bump the version of the account since we are setting a metadata delegate
        candy_machine.version = AccountVersion::V2;
    }

    msg!(
        "Changing token standard from {} to {}",
        candy_machine.token_standard,
        token_standard
    );

    candy_machine.token_standard = token_standard;

    let required_length = candy_machine.data.get_space_for_candy()?;
    let candy_machine_info = candy_machine.to_account_info();

    if token_standard == TokenStandard::ProgrammableNonFungible as u8 {
        // make sure we have space in the account to store the rule set
        if candy_machine_info.data_len() < (required_length + RULE_SET_LENGTH + 1) {
            msg!("Allocating space to store the rule set");

            resize_or_reallocate_account_raw(
                &candy_machine_info,
                &accounts.payer.to_account_info(),
                &accounts.system_program.to_account_info(),
                required_length + (1 + RULE_SET_LENGTH),
            )?;
        }

        let mut account_data = candy_machine_info.data.borrow_mut();

        if let Some(rule_set_info) = &accounts.rule_set {
            let rule_set = rule_set_info.key();
            account_data[required_length] = SET;

            msg!("Storing rule set pubkey");

            let index = required_length + 1;
            let mut storage = &mut account_data[index..index + RULE_SET_LENGTH];
            rule_set.serialize(&mut storage)?;
        } else {
            // clears the rule set
            account_data[required_length] = UNSET;
            let index = required_length + 1;
            account_data[index..index + RULE_SET_LENGTH].fill(0);

            msg!("Rule set cleared");
        }
    } else if required_length < candy_machine_info.data_len() {
        let end_index = candy_machine_info.data_len();
        let mut account_data = candy_machine_info.data.borrow_mut();
        account_data[required_length..end_index].fill(0);

        msg!("Remaining account bytes cleared");
    }

    Ok(())
}

/// Set the token standard to mint.
#[derive(Accounts)]
pub struct SetTokenStandard<'info> {
    /// Candy Machine account.
    #[account(mut, has_one = authority, has_one = collection_mint)]
    candy_machine: Account<'info, CandyMachine>,

    /// Candy Machine authority.
    authority: Signer<'info>,

    /// Authority PDA.
    ///
    /// CHECK: account checked in CPI
    #[account(
        mut,
        seeds = [AUTHORITY_SEED.as_bytes(), candy_machine.to_account_info().key.as_ref()],
        bump
    )]
    authority_pda: UncheckedAccount<'info>,

    /// Payer of the transaction.
    #[account(mut)]
    payer: Signer<'info>,

    /// Authorization rule set to be used by minted NFTs.
    ///
    /// CHECK: must be ownwed by mpl_token_auth_rules
    #[account(owner = MPL_TOKEN_AUTH_RULES_PROGRAM)]
    rule_set: Option<UncheckedAccount<'info>>,

    /// Collection metadata delegate record.
    ///
    /// CHECK: account checked in CPI
    #[account(mut)]
    collection_delegate_record: UncheckedAccount<'info>,

    /// Collection mint.
    ///
    /// CHECK: account checked in CPI
    collection_mint: UncheckedAccount<'info>,

    /// Collection metadata.
    ///
    /// CHECK: account checked in CPI
    #[account(mut)]
    collection_metadata: UncheckedAccount<'info>,

    /// Collection authority record.
    ///
    /// CHECK: account checked in CPI
    #[account(mut)]
    collection_authority_record: Option<UncheckedAccount<'info>>,

    /// Collection update authority.
    collection_update_authority: Signer<'info>,

    /// Token Metadata program.
    ///
    /// CHECK: account checked in CPI
    #[account(address = mpl_token_metadata::ID)]
    token_metadata_program: UncheckedAccount<'info>,

    /// System program.
    system_program: Program<'info, System>,

    /// Instructions sysvar account.
    ///
    /// CHECK: account constraints checked in account trait
    #[account(address = sysvar::instructions::id())]
    sysvar_instructions: UncheckedAccount<'info>,

    /// Token Authorization Rules program.
    ///
    /// CHECK: account checked in CPI
    #[account(address = MPL_TOKEN_AUTH_RULES_PROGRAM)]
    authorization_rules_program: Option<UncheckedAccount<'info>>,

    /// Token Authorization rules account for the collection metadata (if any).
    ///
    /// CHECK: account constraints checked in account trait
    #[account(owner = MPL_TOKEN_AUTH_RULES_PROGRAM)]
    authorization_rules: Option<UncheckedAccount<'info>>,
}