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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::str::FromStr;

use anchor_client::solana_sdk::pubkey::Pubkey;
use anyhow::Result;
use console::style;
use mpl_candy_guard::{accounts::Withdraw as WithdrawAccount, instruction::Withdraw};
use solana_program::native_token::LAMPORTS_PER_SOL;

use crate::{cache::load_cache, common::*, utils::*};

pub struct GuardWithdrawArgs {
    pub keypair: Option<String>,
    pub rpc_url: Option<String>,
    pub cache: String,
    pub candy_guard: Option<String>,
}

pub fn process_guard_withdraw(args: GuardWithdrawArgs) -> Result<()> {
    println!("[1/2] {}Loading candy guard", LOOKING_GLASS_EMOJI);

    // the candy guard id specified takes precedence over the one from the cache

    let (candy_guard_id, cache) = if let Some(candy_guard) = args.candy_guard {
        (candy_guard, None)
    } else {
        let cache = load_cache(&args.cache, false)?;
        (cache.program.candy_guard.clone(), Some(cache))
    };

    if candy_guard_id.is_empty() {
        return Err(anyhow!("Missing candy guard id."));
    }

    let candy_guard_id = match Pubkey::from_str(&candy_guard_id) {
        Ok(candy_guard_id) => candy_guard_id,
        Err(_) => {
            let error = anyhow!("Failed to parse candy guard id: {}", candy_guard_id);
            error!("{:?}", error);
            return Err(error);
        }
    };

    let sugar_config = sugar_setup(args.keypair, args.rpc_url)?;
    let client = setup_client(&sugar_config)?;
    let program = client.program(mpl_candy_guard::ID);
    let payer = sugar_config.keypair;

    let pb = spinner_with_style();
    pb.set_message("Connecting...");

    let account = program.rpc().get_account(&candy_guard_id)?;

    pb.finish_with_message("Done");

    println!("\n[2/2] {}Retrieving funds", WITHDRAW_EMOJI);

    let pb = spinner_with_style();
    pb.set_message("Connecting...");

    let tx = program
        .request()
        .accounts(WithdrawAccount {
            candy_guard: candy_guard_id,
            authority: payer.pubkey(),
        })
        .args(Withdraw {});

    let sig = tx.send()?;

    pb.finish_and_clear();
    println!("{} {}", style("Signature:").bold(), sig);

    println!(
        "\nReceived ◎ {} from rent fee.",
        (account.lamports as f64) / (LAMPORTS_PER_SOL as f64)
    );

    // if we closed the candy guard from the cache file, remove
    // its reference

    if cache.is_some() {
        let mut cache = load_cache(&args.cache, false)?;
        cache.program.candy_guard = String::new();
        cache.sync_file()?;
    }

    Ok(())
}