sugar_cli/
candy_machine.rs1use anchor_client::{solana_sdk::pubkey::Pubkey, ClientError};
2use anchor_lang::AnchorDeserialize;
3use anyhow::{anyhow, Result};
4pub use mpl_candy_machine_core::ID as CANDY_MACHINE_ID;
5use mpl_candy_machine_core::{CandyMachine, CandyMachineData};
6
7use crate::{config::data::SugarConfig, pdas::get_metadata_pda, setup::setup_client};
8
9#[derive(Debug)]
17pub struct ConfigStatus {
18 pub index: u32,
19 pub on_chain: bool,
20}
21
22pub fn get_candy_machine_state(
23 sugar_config: &SugarConfig,
24 candy_machine_id: &Pubkey,
25) -> Result<CandyMachine> {
26 let client = setup_client(sugar_config)?;
27 let program = client.program(CANDY_MACHINE_ID);
28
29 program.account(*candy_machine_id).map_err(|e| match e {
30 ClientError::AccountNotFound => anyhow!("Candy Machine does not exist!"),
31 _ => anyhow!(
32 "Failed to deserialize Candy Machine account {}: {}",
33 candy_machine_id.to_string(),
34 e
35 ),
36 })
37}
38
39pub fn get_candy_machine_data(
40 sugar_config: &SugarConfig,
41 candy_machine_id: &Pubkey,
42) -> Result<CandyMachineData> {
43 let candy_machine = get_candy_machine_state(sugar_config, candy_machine_id)?;
44 Ok(candy_machine.data)
45}
46
47pub fn load_candy_machine(
48 sugar_config: &SugarConfig,
49 candy_machine_id: &Pubkey,
50) -> Result<(CandyMachine, Option<Pubkey>)> {
51 let client = setup_client(sugar_config)?;
52 let program = client.program(CANDY_MACHINE_ID);
53 let data = program.rpc().get_account_data(candy_machine_id)?;
55 let candy_machine = CandyMachine::deserialize(&mut &data[8..])?;
56
57 let (_, collection_metadata) = get_metadata_pda(&candy_machine.collection_mint, &program)?;
58
59 let rule_set = candy_machine.get_rule_set(&data, &collection_metadata)?;
60
61 Ok((candy_machine, rule_set))
62}