use anchor_lang::{prelude::*, solana_program::sysvar};
use crate::{
approve_asset_collection_delegate, cmp_pubkeys, constants::AUTHORITY_SEED,
revoke_asset_collection_delegate, ApproveAssetDelegateHelperAccounts, CandyError, CandyMachine,
RevokeAssetDelegateHelperAccounts,
};
pub fn set_collection(ctx: Context<SetCollection>) -> Result<()> {
let accounts = ctx.accounts;
let candy_machine = &mut accounts.candy_machine;
if !cmp_pubkeys(accounts.new_collection.key, &candy_machine.collection_mint) {
if candy_machine.items_redeemed > 0 {
return err!(CandyError::NoChangingCollectionDuringMint);
} else if !cmp_pubkeys(accounts.collection.key, &candy_machine.collection_mint) {
return err!(CandyError::MintMismatch);
}
candy_machine.collection_mint = accounts.new_collection.key();
}
let revoke_accounts = RevokeAssetDelegateHelperAccounts {
mpl_core_program: accounts.mpl_core_program.to_account_info(),
authority_pda: accounts.authority_pda.to_account_info(),
collection: accounts.collection.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(),
};
revoke_asset_collection_delegate(
revoke_accounts,
candy_machine.key(),
*ctx.bumps.get("authority_pda").unwrap(),
)?;
let delegate_accounts = ApproveAssetDelegateHelperAccounts {
payer: accounts.payer.to_account_info(),
authority_pda: accounts.authority_pda.to_account_info(),
collection: accounts.new_collection.to_account_info(),
collection_update_authority: accounts.new_collection_update_authority.to_account_info(),
system_program: accounts.system_program.to_account_info(),
sysvar_instructions: accounts.sysvar_instructions.to_account_info(),
mpl_core_program: accounts.mpl_core_program.to_account_info(),
};
approve_asset_collection_delegate(delegate_accounts)
}
#[derive(Accounts)]
pub struct SetCollection<'info> {
#[account(mut, has_one = authority)]
candy_machine: Box<Account<'info, CandyMachine>>,
authority: Signer<'info>,
#[account(
mut,
seeds = [AUTHORITY_SEED.as_bytes(), candy_machine.to_account_info().key.as_ref()],
bump
)]
authority_pda: UncheckedAccount<'info>,
#[account(mut)]
payer: Signer<'info>,
collection_update_authority: UncheckedAccount<'info>,
#[account(mut)]
collection: UncheckedAccount<'info>,
new_collection_update_authority: Signer<'info>,
#[account(mut)]
new_collection: UncheckedAccount<'info>,
#[account(address = mpl_core::ID)]
mpl_core_program: UncheckedAccount<'info>,
system_program: Program<'info, System>,
#[account(address = sysvar::instructions::id())]
sysvar_instructions: UncheckedAccount<'info>,
}