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
use anchor_client::{solana_sdk::pubkey::Pubkey, ClientError};
use anchor_lang::AnchorDeserialize;
use anyhow::{anyhow, Result};
pub use mpl_candy_machine_core::ID as CANDY_MACHINE_ID;
use mpl_candy_machine_core::{CandyMachine, CandyMachineData};

use crate::{config::data::SugarConfig, pdas::get_metadata_pda, setup::setup_client};

// To test a custom candy machine program, comment the mpl_candy_machine::ID line
// above and use the following lines to declare the id to use:
//
//use solana_program::declare_id;
//declare_id!("<YOUR CANDY MACHINE ID>");
//pub use self::ID as CANDY_MACHINE_ID;

#[derive(Debug)]
pub struct ConfigStatus {
    pub index: u32,
    pub on_chain: bool,
}

pub fn get_candy_machine_state(
    sugar_config: &SugarConfig,
    candy_machine_id: &Pubkey,
) -> Result<CandyMachine> {
    let client = setup_client(sugar_config)?;
    let program = client.program(CANDY_MACHINE_ID);

    program.account(*candy_machine_id).map_err(|e| match e {
        ClientError::AccountNotFound => anyhow!("Candy Machine does not exist!"),
        _ => anyhow!(
            "Failed to deserialize Candy Machine account {}: {}",
            candy_machine_id.to_string(),
            e
        ),
    })
}

pub fn get_candy_machine_data(
    sugar_config: &SugarConfig,
    candy_machine_id: &Pubkey,
) -> Result<CandyMachineData> {
    let candy_machine = get_candy_machine_state(sugar_config, candy_machine_id)?;
    Ok(candy_machine.data)
}

pub fn load_candy_machine(
    sugar_config: &SugarConfig,
    candy_machine_id: &Pubkey,
) -> Result<(CandyMachine, Option<Pubkey>)> {
    let client = setup_client(sugar_config)?;
    let program = client.program(CANDY_MACHINE_ID);
    // retrieves the account data
    let data = program.rpc().get_account_data(candy_machine_id)?;
    let candy_machine = CandyMachine::deserialize(&mut &data[8..])?;

    let (_, collection_metadata) = get_metadata_pda(&candy_machine.collection_mint, &program)?;

    let rule_set = candy_machine.get_rule_set(&data, &collection_metadata)?;

    Ok((candy_machine, rule_set))
}