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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::str::FromStr;

use anchor_client::solana_sdk::pubkey::Pubkey;
use anyhow::Result;
use console::style;
use mpl_candy_machine_core::{constants::NULL_STRING, AccountVersion};
use mpl_token_metadata::state::TokenStandard;

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

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

// number of indices per line
const PER_LINE: usize = 11;

pub fn process_show(args: ShowArgs) -> Result<()> {
    println!(
        "{} {}Looking up candy machine",
        if args.unminted {
            style("[1/2]").bold().dim()
        } else {
            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 (cndy_state, rule_set) = load_candy_machine(&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("", "mint authority", cndy_state.mint_authority.to_string());
    print_with_style(
        "",
        "collection mint",
        cndy_state.collection_mint.to_string(),
    );

    if matches!(cndy_state.version, AccountVersion::V1) {
        print_with_style("", "account version", "V1");
        print_with_style("", "token standard", "NonFungible (NFT)");
        print_with_style("", "rule set", "none");
    } else {
        print_with_style("", "account version", "V2");
        print_with_style(
            "",
            "token standard",
            if cndy_state.token_standard == TokenStandard::NonFungible as u8 {
                "NonFungible"
            } else {
                "ProgrammableNonFungible (pNFT)"
            },
        );

        if let Some(rule_set) = rule_set {
            print_with_style("", "rule set", rule_set.to_string());
        }
    }
    print_with_style("", "features", "none");

    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("", "symbol", cndy_data.symbol.trim_end_matches(NULL_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("", "creators", "".to_string());

    let creators = &cndy_data.creators;

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

    // 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());
    }

    // config line settings

    if let Some(config_line_settings) = &cndy_data.config_line_settings {
        print_with_style("", "config line settings", "");

        let prefix_name = if config_line_settings.prefix_name.is_empty() {
            style("<empty>").dim()
        } else {
            style(config_line_settings.prefix_name.as_str())
        };
        print_with_style("    ", "prefix_name", &prefix_name.to_string());
        print_with_style(
            "    ",
            "name_length",
            &config_line_settings.name_length.to_string(),
        );

        let prefix_uri = if config_line_settings.prefix_uri.is_empty() {
            style("<empty>").dim()
        } else {
            style(config_line_settings.prefix_uri.as_str())
        };
        print_with_style("    ", "prefix_uri", &prefix_uri.to_string());
        print_with_style(
            "    ",
            "uri_length",
            &config_line_settings.uri_length.to_string(),
        );
        print_with_style(
            "    ",
            "is_sequential",
            if config_line_settings.is_sequential {
                "true"
            } else {
                "false"
            },
        );
    } else {
        print_with_style("", "config line settings", "none");
    }

    // unminted indices

    if args.unminted {
        println!(
            "\n{} {}Retrieving unminted indices",
            style("[2/2]").bold().dim(),
            LOOKING_GLASS_EMOJI
        );

        let start = CONFIG_ARRAY_START
            + STRING_LEN_SIZE
            + (cndy_data.items_available as usize * cndy_data.get_config_line_size())
            + cndy_data
                .items_available
                .checked_div(8)
                .expect("Numerical overflow error") as usize
            + 1;

        let pb = spinner_with_style();
        pb.set_message("Connecting...");
        // retrieve the (raw) candy machine data
        let data = program.rpc().get_account_data(&candy_machine_id)?;

        pb.finish_and_clear();
        let mut indices = vec![];

        let remaining = cndy_data.items_available - cndy_state.items_redeemed;
        for i in 0..remaining {
            let slice = start + (i * 4) as usize;
            indices.push(u32::from_le_bytes(
                data[slice..slice + 4].try_into().unwrap(),
            ));
        }

        if indices.is_empty() {
            println!(
                "\n{}{}",
                PAPER_EMOJI,
                style("All items of the candy machine have been minted.").dim()
            );
        } else {
            // makes sure all items are in order
            indices.sort_unstable();
            // logs all indices
            info!("unminted list: {:?}", indices);

            println!(
                "\n{}{}",
                PAPER_EMOJI,
                style(format!("Unminted list ({} total):", indices.len())).dim()
            );
            let mut current = 0;

            for i in indices {
                if current == 0 {
                    println!("{}", style(" :").dim());
                    print!("{}", style(" :.. ").dim());
                }
                current += 1;

                print!(
                    "{:<5}{}",
                    i,
                    if current == PER_LINE {
                        current = 0;
                        "\n"
                    } else {
                        " "
                    }
                );
            }
            // just adds a new line break
            println!();
        }
    }

    Ok(())
}

pub fn print_with_style<S>(indent: &str, key: &str, value: S)
where
    S: core::fmt::Display,
{
    println!(
        " {} {}",
        style(format!("{}:.. {}:", indent, key)).dim(),
        value
    );
}