mpl_candy_machine_core/instructions/
update.rs

1use anchor_lang::prelude::*;
2use mpl_token_metadata::state::MAX_SYMBOL_LENGTH;
3
4use crate::{utils::fixed_length_string, CandyError, CandyMachine, CandyMachineData};
5
6pub fn update(ctx: Context<Update>, data: CandyMachineData) -> Result<()> {
7    let candy_machine = &mut ctx.accounts.candy_machine;
8
9    if (data.items_available != candy_machine.data.items_available)
10        && data.hidden_settings.is_none()
11    {
12        return err!(CandyError::CannotChangeNumberOfLines);
13    }
14
15    if candy_machine.data.items_available > 0
16        && candy_machine.data.hidden_settings.is_none()
17        && data.hidden_settings.is_some()
18    {
19        return err!(CandyError::CannotSwitchToHiddenSettings);
20    }
21
22    let symbol = fixed_length_string(data.symbol.clone(), MAX_SYMBOL_LENGTH)?;
23    // validates the config data settings
24    data.validate()?;
25
26    if let Some(config_lines) = &candy_machine.data.config_line_settings {
27        if let Some(new_config_lines) = &data.config_line_settings {
28            // it is only possible to update the config lines settings if the
29            // new values are equal or smaller than the current ones
30            if config_lines.name_length < new_config_lines.name_length
31                || config_lines.uri_length < new_config_lines.uri_length
32            {
33                return err!(CandyError::CannotIncreaseLength);
34            }
35
36            if config_lines.is_sequential != new_config_lines.is_sequential
37                && candy_machine.items_redeemed > 0
38            {
39                return err!(CandyError::CannotChangeSequentialIndexGeneration);
40            }
41        }
42    } else if data.config_line_settings.is_some() {
43        return err!(CandyError::CannotSwitchFromHiddenSettings);
44    }
45
46    candy_machine.data = data;
47    candy_machine.data.symbol = symbol;
48
49    Ok(())
50}
51
52/// Update the candy machine state.
53#[derive(Accounts)]
54pub struct Update<'info> {
55    /// Candy Machine account.
56    #[account(mut, has_one = authority)]
57    candy_machine: Account<'info, CandyMachine>,
58
59    /// Authority of the candy machine.
60    authority: Signer<'info>,
61}