use anchor_lang::prelude::*;
use crate::{CandyError, CandyMachine, CandyMachineData};
pub fn update(ctx: Context<Update>, data: CandyMachineData) -> Result<()> {
let candy_machine = &mut ctx.accounts.candy_machine;
if (data.items_available != candy_machine.data.items_available)
&& data.hidden_settings.is_none()
{
return err!(CandyError::CannotChangeNumberOfLines);
}
if candy_machine.data.items_available > 0
&& candy_machine.data.hidden_settings.is_none()
&& data.hidden_settings.is_some()
{
return err!(CandyError::CannotSwitchToHiddenSettings);
}
data.validate()?;
if let Some(config_lines) = &candy_machine.data.config_line_settings {
if let Some(new_config_lines) = &data.config_line_settings {
if config_lines.name_length < new_config_lines.name_length
|| config_lines.uri_length < new_config_lines.uri_length
{
return err!(CandyError::CannotIncreaseLength);
}
if config_lines.is_sequential != new_config_lines.is_sequential
&& candy_machine.items_redeemed > 0
{
return err!(CandyError::CannotChangeSequentialIndexGeneration);
}
}
} else if data.config_line_settings.is_some() {
return err!(CandyError::CannotSwitchFromHiddenSettings);
}
candy_machine.data = data;
Ok(())
}
#[derive(Accounts)]
pub struct Update<'info> {
#[account(mut, has_one = authority)]
candy_machine: Account<'info, CandyMachine>,
authority: Signer<'info>,
}