sugar_cli/guard/
remove.rs

1use std::str::FromStr;
2
3use anchor_client::solana_sdk::pubkey::Pubkey;
4use anyhow::Result;
5use console::style;
6use mpl_candy_guard::{accounts::Unwrap as UnwrapAccount, instruction::Unwrap};
7
8use crate::{cache::load_cache, candy_machine::*, common::*, utils::*};
9
10pub struct GuardRemoveArgs {
11    pub keypair: Option<String>,
12    pub rpc_url: Option<String>,
13    pub cache: String,
14    pub candy_machine: Option<String>,
15    pub candy_guard: Option<String>,
16}
17
18pub fn process_guard_remove(args: GuardRemoveArgs) -> Result<()> {
19    println!("[1/1] {}Unwrapping", UNWRAP_EMOJI);
20
21    // the candy machine id specified takes precedence over the one from the cache
22
23    let candy_machine_id = if let Some(candy_machine) = args.candy_machine {
24        candy_machine
25    } else {
26        let cache = load_cache(&args.cache, false)?;
27        cache.program.candy_machine
28    };
29
30    let candy_machine_id = match Pubkey::from_str(&candy_machine_id) {
31        Ok(candy_machine_id) => candy_machine_id,
32        Err(_) => {
33            let error = anyhow!("Failed to parse candy machine id: {}", candy_machine_id);
34            error!("{:?}", error);
35            return Err(error);
36        }
37    };
38
39    // the candy guard id specified takes precedence over the one from the cache
40
41    let candy_guard_id = if let Some(candy_guard) = args.candy_guard {
42        candy_guard
43    } else {
44        let cache = load_cache(&args.cache, false)?;
45        cache.program.candy_guard
46    };
47
48    let candy_guard_id = match Pubkey::from_str(&candy_guard_id) {
49        Ok(candy_guard_id) => candy_guard_id,
50        Err(_) => {
51            let error = anyhow!("Failed to parse candy guard id: {}", candy_guard_id);
52            error!("{:?}", error);
53            return Err(error);
54        }
55    };
56
57    // remove the candy guard as mint authority
58
59    let sugar_config = sugar_setup(args.keypair, args.rpc_url)?;
60    let client = setup_client(&sugar_config)?;
61    let program = client.program(mpl_candy_guard::ID);
62    let payer = sugar_config.keypair;
63
64    let pb = spinner_with_style();
65    pb.set_message("Connecting...");
66
67    let tx = program
68        .request()
69        .accounts(UnwrapAccount {
70            candy_guard: candy_guard_id,
71            authority: payer.pubkey(),
72            candy_machine: candy_machine_id,
73            candy_machine_authority: payer.pubkey(),
74            candy_machine_program: CANDY_MACHINE_ID,
75        })
76        .args(Unwrap {});
77
78    let sig = tx.send()?;
79
80    pb.finish_and_clear();
81    println!("{} {}", style("Signature:").bold(), sig);
82
83    println!("\nThe candy guard is no longer the mint authority of the candy machine.");
84    println!(
85        "  -> New mint authority: {}",
86        style(format!("{}", payer.pubkey())).bold()
87    );
88
89    Ok(())
90}