Skip to main content

mpl_candy_machine_core/instructions/
set_collection.rs

1use anchor_lang::prelude::*;
2use mpl_token_metadata::state::{Metadata, TokenMetadataAccount};
3
4use crate::{
5    approve_collection_authority_helper, cmp_pubkeys, constants::AUTHORITY_SEED,
6    revoke_collection_authority_helper, AccountVersion, ApproveCollectionAuthorityHelperAccounts,
7    CandyError, CandyMachine, RevokeCollectionAuthorityHelperAccounts,
8};
9
10pub fn set_collection(ctx: Context<SetCollection>) -> Result<()> {
11    msg!("(Deprecated as of 1.0.0) Use SetCollectionV2 instead");
12
13    let accounts = ctx.accounts;
14    let candy_machine = &mut accounts.candy_machine;
15
16    if !matches!(candy_machine.version, AccountVersion::V1) {
17        return err!(CandyError::InvalidAccountVersion);
18    }
19
20    if candy_machine.items_redeemed > 0 {
21        return err!(CandyError::NoChangingCollectionDuringMint);
22    } else if !cmp_pubkeys(accounts.collection_mint.key, &candy_machine.collection_mint) {
23        return err!(CandyError::MintMismatch);
24    }
25
26    let collection_metadata_info = &accounts.collection_metadata;
27    let collection_metadata: Metadata =
28        Metadata::from_account_info(&collection_metadata_info.to_account_info())?;
29
30    // revoking the existing collection authority
31
32    let revoke_accounts = RevokeCollectionAuthorityHelperAccounts {
33        authority_pda: accounts.authority_pda.to_account_info(),
34        collection_authority_record: accounts.collection_authority_record.to_account_info(),
35        collection_metadata: accounts.collection_metadata.to_account_info(),
36        collection_mint: accounts.collection_mint.to_account_info(),
37        token_metadata_program: accounts.token_metadata_program.to_account_info(),
38    };
39
40    revoke_collection_authority_helper(
41        revoke_accounts,
42        candy_machine.key(),
43        *ctx.bumps.get("authority_pda").unwrap(),
44        collection_metadata.token_standard,
45    )?;
46
47    // approving the new collection authority
48
49    candy_machine.collection_mint = accounts.new_collection_mint.key();
50
51    let approve_collection_authority_helper_accounts = ApproveCollectionAuthorityHelperAccounts {
52        payer: accounts.payer.to_account_info(),
53        authority_pda: accounts.authority_pda.to_account_info(),
54        collection_update_authority: accounts.new_collection_update_authority.to_account_info(),
55        collection_mint: accounts.new_collection_mint.to_account_info(),
56        collection_metadata: accounts.new_collection_metadata.to_account_info(),
57        collection_authority_record: accounts.new_collection_authority_record.to_account_info(),
58        token_metadata_program: accounts.token_metadata_program.to_account_info(),
59        system_program: accounts.system_program.to_account_info(),
60    };
61
62    approve_collection_authority_helper(approve_collection_authority_helper_accounts)
63}
64
65/// Set the collection PDA for the candy machine
66#[derive(Accounts)]
67pub struct SetCollection<'info> {
68    /// Candy Machine account.
69    #[account(mut, has_one = authority)]
70    candy_machine: Account<'info, CandyMachine>,
71
72    /// Candy Machine authority.
73    authority: Signer<'info>,
74
75    /// Authority PDA.
76    ///
77    /// CHECK: account checked in seeds constraint
78    #[account(
79        mut, seeds = [AUTHORITY_SEED.as_bytes(), candy_machine.to_account_info().key.as_ref()],
80        bump
81    )]
82    authority_pda: UncheckedAccount<'info>,
83
84    /// Payer of the transaction.
85    payer: Signer<'info>,
86
87    /// Mint account of the collection.
88    ///
89    /// CHECK: account checked in CPI
90    collection_mint: UncheckedAccount<'info>,
91
92    /// Metadata account of the collection.
93    ///
94    /// CHECK: account checked in CPI
95    collection_metadata: UncheckedAccount<'info>,
96
97    /// Collection authority record.
98    ///
99    /// CHECK: account checked in CPI
100    #[account(mut)]
101    collection_authority_record: UncheckedAccount<'info>,
102
103    /// Update authority of the new collection NFT.
104    #[account(mut)]
105    new_collection_update_authority: Signer<'info>,
106
107    /// New collection metadata.
108    ///
109    /// CHECK: account checked in CPI
110    new_collection_metadata: UncheckedAccount<'info>,
111
112    /// New collection mint.
113    ///
114    /// CHECK: account checked in CPI
115    new_collection_mint: UncheckedAccount<'info>,
116
117    /// New collection master edition.
118    ///
119    /// CHECK: account checked in CPI
120    new_collection_master_edition: UncheckedAccount<'info>,
121
122    /// New collection authority record.
123    ///
124    /// CHECK: account checked in CPI
125    #[account(mut)]
126    new_collection_authority_record: UncheckedAccount<'info>,
127
128    /// Token Metadata program.
129    ///
130    /// CHECK: account checked in CPI
131    #[account(address = mpl_token_metadata::id())]
132    token_metadata_program: UncheckedAccount<'info>,
133
134    /// System program.
135    system_program: Program<'info, System>,
136}