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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use anchor_lang::prelude::*;
use arrayref::array_ref;
use mpl_token_metadata::{
    accounts::Metadata,
    instructions::{
        ApproveCollectionAuthorityCpiBuilder, DelegateCollectionV1CpiBuilder,
        RevokeCollectionAuthorityCpiBuilder, RevokeCollectionV1CpiBuilder,
    },
    types::TokenStandard,
};
use solana_program::{
    account_info::AccountInfo,
    program_memory::sol_memcmp,
    program_pack::{IsInitialized, Pack},
    pubkey::{Pubkey, PUBKEY_BYTES},
};

use crate::{
    constants::{
        AUTHORITY_SEED, HIDDEN_SECTION, NULL_STRING, REPLACEMENT_INDEX, REPLACEMENT_INDEX_INCREMENT,
    },
    CandyError,
};

/// Anchor wrapper for Token program.
#[derive(Debug, Clone)]
pub struct Token;

impl anchor_lang::Id for Token {
    fn id() -> Pubkey {
        spl_token::id()
    }
}

/// Anchor wrapper for Associated Token program.
#[derive(Debug, Clone)]
pub struct AssociatedToken;

impl anchor_lang::Id for AssociatedToken {
    fn id() -> Pubkey {
        spl_associated_token_account::id()
    }
}

pub struct ApproveCollectionAuthorityHelperAccounts<'info> {
    /// CHECK: account checked in CPI
    pub payer: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub authority_pda: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_update_authority: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_mint: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_metadata: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_authority_record: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub token_metadata_program: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub system_program: AccountInfo<'info>,
}

pub struct RevokeCollectionAuthorityHelperAccounts<'info> {
    /// CHECK: account checked in CPI
    pub authority_pda: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_mint: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_metadata: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_authority_record: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub token_metadata_program: AccountInfo<'info>,
}

pub struct ApproveMetadataDelegateHelperAccounts<'info> {
    /// CHECK: account checked in CPI
    pub delegate_record: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub authority_pda: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_metadata: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_mint: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_update_authority: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub payer: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub system_program: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub sysvar_instructions: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub authorization_rules_program: Option<AccountInfo<'info>>,
    /// CHECK: account checked in CPI
    pub authorization_rules: Option<AccountInfo<'info>>,
    /// CHECK: account checked in CPI
    pub token_metadata_program: AccountInfo<'info>,
}

pub struct RevokeMetadataDelegateHelperAccounts<'info> {
    /// CHECK: account checked in CPI
    pub delegate_record: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub authority_pda: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_metadata: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_mint: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub collection_update_authority: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub payer: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub system_program: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub sysvar_instructions: AccountInfo<'info>,
    /// CHECK: account checked in CPI
    pub authorization_rules_program: Option<AccountInfo<'info>>,
    /// CHECK: account checked in CPI
    pub authorization_rules: Option<AccountInfo<'info>>,
    /// CHECK: account checked in CPI
    pub token_metadata_program: AccountInfo<'info>,
}

pub fn assert_initialized<T: Pack + IsInitialized>(account_info: &AccountInfo) -> Result<T> {
    let account: T = T::unpack_unchecked(&account_info.data.borrow())?;
    if !account.is_initialized() {
        Err(CandyError::Uninitialized.into())
    } else {
        Ok(account)
    }
}

/// Return the current number of lines written to the account.
pub fn get_config_count(data: &[u8]) -> Result<usize> {
    Ok(u32::from_le_bytes(*array_ref![data, HIDDEN_SECTION, 4]) as usize)
}

pub fn cmp_pubkeys(a: &Pubkey, b: &Pubkey) -> bool {
    sol_memcmp(a.as_ref(), b.as_ref(), PUBKEY_BYTES) == 0
}

/// Return a padded string up to the specified length. If the specified
/// string `value` is longer than the allowed `length`, return an error.
pub fn fixed_length_string(value: String, length: usize) -> Result<String> {
    if length < value.len() {
        // the value is larger than the allowed length
        return err!(CandyError::ExceededLengthError);
    }

    let padding = NULL_STRING.repeat(length - value.len());
    Ok(value + &padding)
}

/// Replace the index pattern variables on the specified string.
pub fn replace_patterns(value: String, index: usize) -> String {
    let mut mutable = value;
    // check for pattern $ID+1$
    if mutable.contains(REPLACEMENT_INDEX_INCREMENT) {
        mutable = mutable.replace(REPLACEMENT_INDEX_INCREMENT, &(index + 1).to_string());
    }
    // check for pattern $ID$
    if mutable.contains(REPLACEMENT_INDEX) {
        mutable = mutable.replace(REPLACEMENT_INDEX, &index.to_string());
    }

    mutable
}

pub fn approve_collection_authority_helper(
    accounts: ApproveCollectionAuthorityHelperAccounts,
) -> Result<()> {
    let ApproveCollectionAuthorityHelperAccounts {
        payer,
        authority_pda,
        collection_update_authority,
        collection_mint,
        collection_metadata,
        collection_authority_record,
        token_metadata_program,
        system_program,
    } = accounts;

    let collection_data: Metadata = Metadata::try_from(&collection_metadata)?;

    if !cmp_pubkeys(
        &collection_data.update_authority,
        &collection_update_authority.key(),
    ) {
        return err!(CandyError::IncorrectCollectionAuthority);
    }

    if !cmp_pubkeys(&collection_data.mint, &collection_mint.key()) {
        return err!(CandyError::MintMismatch);
    }

    // only approves a new delegate if the delegate account is empty
    // (i.e., it does not exist yet)
    if collection_authority_record.data_is_empty() {
        ApproveCollectionAuthorityCpiBuilder::new(&token_metadata_program)
            .collection_authority_record(&collection_authority_record)
            .new_collection_authority(&authority_pda)
            .metadata(&collection_metadata)
            .mint(&collection_mint)
            .update_authority(&collection_update_authority)
            .payer(&payer)
            .system_program(&system_program)
            .invoke()?;
    }

    Ok(())
}

pub fn revoke_collection_authority_helper(
    accounts: RevokeCollectionAuthorityHelperAccounts,
    candy_machine: Pubkey,
    signer_bump: u8,
    token_standard: Option<TokenStandard>,
) -> Result<()> {
    if matches!(token_standard, Some(TokenStandard::ProgrammableNonFungible)) {
        // pNFTs do not have a "legacy" collection authority, so we do not try to revoke
        // it. This would happen when the migration is completed and the candy machine
        // account version is still V1 - in any case, the "legacy" collection authority
        // is invalid since it does not apply to pNFTs and it will be replace by a
        // metadata delegate.
        Ok(())
    } else {
        RevokeCollectionAuthorityCpiBuilder::new(&accounts.token_metadata_program)
            .collection_authority_record(&accounts.collection_authority_record)
            .delegate_authority(&accounts.authority_pda)
            .revoke_authority(&accounts.authority_pda)
            .metadata(&accounts.collection_metadata)
            .mint(&accounts.collection_mint)
            .invoke_signed(&[&[
                AUTHORITY_SEED.as_bytes(),
                candy_machine.as_ref(),
                &[signer_bump],
            ]])
            .map_err(|error| error.into())
    }
}

pub fn approve_metadata_delegate(accounts: ApproveMetadataDelegateHelperAccounts) -> Result<()> {
    DelegateCollectionV1CpiBuilder::new(&accounts.token_metadata_program)
        .delegate_record(Some(&accounts.delegate_record))
        .delegate(&accounts.authority_pda)
        .mint(&accounts.collection_mint)
        .metadata(&accounts.collection_metadata)
        .payer(&accounts.payer)
        .authority(&accounts.collection_update_authority)
        .system_program(&accounts.system_program)
        .sysvar_instructions(&accounts.sysvar_instructions)
        .authorization_rules(accounts.authorization_rules.as_ref())
        .authorization_rules_program(accounts.authorization_rules_program.as_ref())
        .invoke()
        .map_err(|error| error.into())
}

pub fn revoke_metadata_delegate(
    accounts: RevokeMetadataDelegateHelperAccounts,
    candy_machine: Pubkey,
    signer_bump: u8,
) -> Result<()> {
    RevokeCollectionV1CpiBuilder::new(&accounts.token_metadata_program)
        .delegate_record(Some(&accounts.delegate_record))
        .delegate(&accounts.authority_pda)
        .mint(&accounts.collection_mint)
        .metadata(&accounts.collection_metadata)
        .payer(&accounts.payer)
        .authority(&accounts.authority_pda)
        .system_program(&accounts.system_program)
        .sysvar_instructions(&accounts.sysvar_instructions)
        .authorization_rules(accounts.authorization_rules.as_ref())
        .authorization_rules_program(accounts.authorization_rules_program.as_ref())
        .invoke_signed(&[&[
            AUTHORITY_SEED.as_bytes(),
            candy_machine.as_ref(),
            &[signer_bump],
        ]])
        .map_err(|error| error.into())
}

pub fn assert_token_standard(token_standard: u8) -> Result<()> {
    if token_standard == TokenStandard::NonFungible as u8
        || token_standard == TokenStandard::ProgrammableNonFungible as u8
    {
        Ok(())
    } else {
        err!(CandyError::InvalidTokenStandard)
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;

    #[test]
    fn check_keys_equal() {
        let key1 = Pubkey::new_unique();
        assert!(cmp_pubkeys(&key1, &key1));
    }

    #[test]
    fn check_keys_not_equal() {
        let key1 = Pubkey::new_unique();
        let key2 = Pubkey::new_unique();
        assert!(!cmp_pubkeys(&key1, &key2));
    }
}