sugar_cli/guard/
update.rs

1use std::str::FromStr;
2
3use anchor_client::solana_sdk::pubkey::Pubkey;
4use anyhow::Result;
5use console::style;
6use mpl_candy_guard::{accounts::Update as UpdateAccount, instruction::Update};
7
8use crate::{cache::load_cache, common::*, config::get_config_data, utils::*};
9
10pub struct GuardUpdateArgs {
11    pub keypair: Option<String>,
12    pub rpc_url: Option<String>,
13    pub cache: String,
14    pub config: String,
15    pub candy_guard: Option<String>,
16}
17
18pub fn process_guard_update(args: GuardUpdateArgs) -> Result<()> {
19    println!(
20        "{} {}Loading candy guard",
21        style("[1/2]").bold().dim(),
22        LOOKING_GLASS_EMOJI
23    );
24
25    // the candy guard id specified takes precedence over the one from the cache
26
27    let candy_guard_id = if let Some(candy_guard) = args.candy_guard {
28        candy_guard
29    } else {
30        let cache = load_cache(&args.cache, false)?;
31        cache.program.candy_guard
32    };
33
34    if candy_guard_id.is_empty() {
35        return Err(anyhow!("Missing candy guard id."));
36    }
37
38    let candy_guard_id = match Pubkey::from_str(&candy_guard_id) {
39        Ok(candy_guard_id) => candy_guard_id,
40        Err(_) => {
41            let error = anyhow!("Failed to parse candy guard id: {}", candy_guard_id);
42            error!("{:?}", error);
43            return Err(error);
44        }
45    };
46
47    let sugar_config = sugar_setup(args.keypair, args.rpc_url)?;
48    let client = setup_client(&sugar_config)?;
49    let program = client.program(mpl_candy_guard::ID);
50    let payer = sugar_config.keypair;
51
52    let pb = spinner_with_style();
53    pb.set_message("Connecting...");
54    // make sure the account exists on-chain
55    let _account = program.rpc().get_account(&candy_guard_id)?;
56    pb.finish_with_message("Done");
57
58    println!("{} {}", style("Candy guard ID:").bold(), candy_guard_id);
59
60    println!(
61        "\n{} {}Updating configuration",
62        style("[2/2]").bold().dim(),
63        COMPUTER_EMOJI
64    );
65
66    let config_data = get_config_data(&args.config)?;
67    let data = if let Some(guards) = &config_data.guards {
68        guards.to_guard_format()?
69    } else {
70        return Err(anyhow!("Missing guards configuration."));
71    };
72
73    let mut serialized_data = vec![0; data.size()];
74    data.save(&mut serialized_data)?;
75
76    let pb = spinner_with_style();
77    pb.set_message("Connecting...");
78
79    let tx = program
80        .request()
81        .accounts(UpdateAccount {
82            candy_guard: candy_guard_id,
83            authority: payer.pubkey(),
84            payer: payer.pubkey(),
85            system_program: system_program::ID,
86        })
87        .args(Update {
88            data: serialized_data,
89        });
90
91    let sig = tx.send()?;
92
93    pb.finish_and_clear();
94    println!("{} {}", style("Signature:").bold(), sig);
95
96    Ok(())
97}