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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::str::FromStr;

use anchor_client::solana_sdk::{native_token::LAMPORTS_PER_SOL, pubkey::Pubkey};
use anyhow::Result;
use chrono::NaiveDateTime;
use console::style;
use mpl_candy_machine::{EndSettingType, WhitelistMintMode};

use crate::cache::load_cache;
use crate::candy_machine::*;
use crate::common::*;
use crate::pdas::get_collection_pda;
use crate::utils::*;

pub struct ShowArgs {
    pub keypair: Option<String>,
    pub rpc_url: Option<String>,
    pub cache: String,
    pub candy_machine: Option<String>,
}

pub fn process_show(args: ShowArgs) -> Result<()> {
    println!(
        "{} {}Looking up candy machine",
        style("[1/1]").bold().dim(),
        LOOKING_GLASS_EMOJI
    );

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

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

    let candy_machine_id = if let Some(candy_machine) = args.candy_machine {
        candy_machine
    } else {
        let cache = load_cache(&args.cache, false)?;
        cache.program.candy_machine
    };

    let sugar_config = sugar_setup(args.keypair, args.rpc_url)?;
    let client = setup_client(&sugar_config)?;
    let program = client.program(CANDY_MACHINE_ID);

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

    let collection_mint =
        if let Ok((_, collection_pda)) = get_collection_pda(&candy_machine_id, &program) {
            Some(collection_pda.mint)
        } else {
            None
        };

    let cndy_state = get_candy_machine_state(&sugar_config, &candy_machine_id)?;
    let cndy_data = cndy_state.data;

    pb.finish_and_clear();

    println!(
        "\n{}{} {}",
        CANDY_EMOJI,
        style("Candy machine ID:").dim(),
        &candy_machine_id
    );

    // candy machine state and data

    println!(" {}", style(":").dim());
    print_with_style("", "authority", cndy_state.authority.to_string());
    print_with_style("", "wallet", cndy_state.wallet.to_string());
    match collection_mint {
        Some(collection_mint) => {
            print_with_style("", "collection mint", collection_mint.to_string())
        }
        None => print_with_style("", "collection mint", "none".to_string()),
    };

    if let Some(token_mint) = cndy_state.token_mint {
        print_with_style("", "spl token", token_mint.to_string());
    } else {
        print_with_style("", "spl token", "none".to_string());
    }

    print_with_style("", "max supply", cndy_data.max_supply.to_string());
    print_with_style("", "items redeemed", cndy_state.items_redeemed.to_string());
    print_with_style("", "items available", cndy_data.items_available.to_string());

    print_with_style("", "uuid", cndy_data.uuid.to_string());
    print_with_style(
        "",
        "price",
        format!(
            "◎ {} ({})",
            cndy_data.price as f64 / LAMPORTS_PER_SOL as f64,
            cndy_data.price
        ),
    );
    print_with_style("", "symbol", cndy_data.symbol.to_string());
    print_with_style(
        "",
        "seller fee basis points",
        format!(
            "{}% ({})",
            cndy_data.seller_fee_basis_points / 100,
            cndy_data.seller_fee_basis_points
        ),
    );
    print_with_style("", "is mutable", cndy_data.is_mutable.to_string());
    print_with_style(
        "",
        "retain authority",
        cndy_data.retain_authority.to_string(),
    );
    if let Some(date) = cndy_data.go_live_date {
        let date = NaiveDateTime::from_timestamp(date, 0);
        print_with_style(
            "",
            "go live date",
            date.format("%a %B %e %Y %H:%M:%S UTC").to_string(),
        );
    } else {
        print_with_style("", "go live date", "none".to_string());
    }
    print_with_style("", "creators", "".to_string());

    for (index, creator) in cndy_data.creators.into_iter().enumerate() {
        let info = format!(
            "{} ({}%{})",
            creator.address,
            creator.share,
            if creator.verified { ", verified" } else { "" },
        );
        print_with_style(":   ", &(index + 1).to_string(), info);
    }

    // end settings
    if let Some(end_settings) = cndy_data.end_settings {
        print_with_style("", "end settings", "".to_string());
        match end_settings.end_setting_type {
            EndSettingType::Date => {
                print_with_style(":   ", "end setting type", "date".to_string());
                let date = NaiveDateTime::from_timestamp(end_settings.number as i64, 0);
                print_with_style(
                    ":   ",
                    "number",
                    date.format("%a %B %e %Y %H:%M:%S UTC").to_string(),
                );
            }
            EndSettingType::Amount => {
                print_with_style(":   ", "end setting type", "amount".to_string());
                print_with_style(":   ", "number", end_settings.number.to_string());
            }
        }
    } else {
        print_with_style("", "end settings", "none".to_string());
    }

    // hidden settings
    if let Some(hidden_settings) = cndy_data.hidden_settings {
        print_with_style("", "hidden settings", "".to_string());
        print_with_style(":   ", "name", hidden_settings.name);
        print_with_style(":   ", "uri", hidden_settings.uri);
        print_with_style(
            ":   ",
            "hash",
            String::from_utf8(hidden_settings.hash.to_vec())?,
        );
    } else {
        print_with_style("", "hidden settings", "none".to_string());
    }

    // whitelist mint settings
    if let Some(whitelist_settings) = cndy_data.whitelist_mint_settings {
        print_with_style("", "whitelist mint settings", "".to_string());
        print_with_style(
            ":   ",
            "mode",
            if whitelist_settings.mode == WhitelistMintMode::BurnEveryTime {
                "burn every time".to_string()
            } else {
                "never burn".to_string()
            },
        );
        print_with_style(":   ", "mint", whitelist_settings.mint.to_string());
        print_with_style(":   ", "presale", whitelist_settings.presale.to_string());
        print_with_style(
            ":   ",
            "discount price",
            if let Some(value) = whitelist_settings.discount_price {
                format!("◎ {} ({})", value as f64 / LAMPORTS_PER_SOL as f64, value)
            } else {
                "none".to_string()
            },
        );
    } else {
        print_with_style("", "whitelist mint settings", "none".to_string());
    }

    // gatekeeper settings
    if let Some(gatekeeper) = cndy_data.gatekeeper {
        print_with_style("", "gatekeeper", "".to_string());
        print_with_style(
            "    ",
            "gatekeeper network",
            gatekeeper.gatekeeper_network.to_string(),
        );
        print_with_style(
            "    ",
            "expire on use",
            gatekeeper.expire_on_use.to_string(),
        );
    } else {
        print_with_style("", "gatekeeper", "none".to_string());
    }

    Ok(())
}

fn print_with_style(indent: &str, key: &str, value: String) {
    println!(
        " {} {}",
        style(format!("{}:.. {}:", indent, key)).dim(),
        value
    );
}