sugar_cli/guard/
withdraw.rs1use std::str::FromStr;
2
3use anchor_client::solana_sdk::pubkey::Pubkey;
4use anyhow::Result;
5use console::style;
6use mpl_candy_guard::{accounts::Withdraw as WithdrawAccount, instruction::Withdraw};
7use solana_program::native_token::LAMPORTS_PER_MLN;
8
9use crate::{cache::load_cache, common::*, utils::*};
10
11pub struct GuardWithdrawArgs {
12 pub keypair: Option<String>,
13 pub rpc_url: Option<String>,
14 pub cache: String,
15 pub candy_guard: Option<String>,
16}
17
18pub fn process_guard_withdraw(args: GuardWithdrawArgs) -> Result<()> {
19 println!("[1/2] {}Loading candy guard", LOOKING_GLASS_EMOJI);
20
21 let (candy_guard_id, cache) = if let Some(candy_guard) = args.candy_guard {
24 (candy_guard, None)
25 } else {
26 let cache = load_cache(&args.cache, false)?;
27 (cache.program.candy_guard.clone(), Some(cache))
28 };
29
30 if candy_guard_id.is_empty() {
31 return Err(anyhow!("Missing candy guard id."));
32 }
33
34 let candy_guard_id = match Pubkey::from_str(&candy_guard_id) {
35 Ok(candy_guard_id) => candy_guard_id,
36 Err(_) => {
37 let error = anyhow!("Failed to parse candy guard id: {}", candy_guard_id);
38 error!("{:?}", error);
39 return Err(error);
40 }
41 };
42
43 let sugar_config = sugar_setup(args.keypair, args.rpc_url)?;
44 let client = setup_client(&sugar_config)?;
45 let program = client.program(mpl_candy_guard::ID);
46 let payer = sugar_config.keypair;
47
48 let pb = spinner_with_style();
49 pb.set_message("Connecting...");
50
51 let account = program.rpc().get_account(&candy_guard_id)?;
52
53 pb.finish_with_message("Done");
54
55 println!("\n[2/2] {}Retrieving funds", WITHDRAW_EMOJI);
56
57 let pb = spinner_with_style();
58 pb.set_message("Connecting...");
59
60 let tx = program
61 .request()
62 .accounts(WithdrawAccount {
63 candy_guard: candy_guard_id,
64 authority: payer.pubkey(),
65 })
66 .args(Withdraw {});
67
68 let sig = tx.send()?;
69
70 pb.finish_and_clear();
71 println!("{} {}", style("Signature:").bold(), sig);
72
73 println!(
74 "\nReceived ◎ {} from rent fee.",
75 (account.lamports as f64) / (LAMPORTS_PER_MLN as f64)
76 );
77
78 if cache.is_some() {
82 let mut cache = load_cache(&args.cache, false)?;
83 cache.program.candy_guard = String::new();
84 cache.sync_file()?;
85 }
86
87 Ok(())
88}