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
use anchor_lang::AccountDeserialize;
use console::style;
use std::{thread, time::Duration};
use mpl_candy_machine::CandyMachine;
use crate::cache::*;
use crate::candy_machine::CANDY_MACHINE_ID;
use crate::common::*;
use crate::config::Cluster;
use crate::constants::{CANDY_EMOJI, PAPER_EMOJI};
use crate::utils::*;
use crate::verify::VerifyError;
pub struct VerifyArgs {
pub keypair: Option<String>,
pub rpc_url: Option<String>,
pub cache: String,
}
#[derive(Debug)]
pub struct OnChainItem {
pub name: String,
pub uri: String,
}
pub fn process_verify(args: VerifyArgs) -> Result<()> {
let sugar_config = sugar_setup(args.keypair, args.rpc_url)?;
let mut cache = load_cache(&args.cache, false)?;
if cache.items.0.is_empty() {
println!(
"{}",
style("No cache items found - run 'upload' to create the cache file first.")
.red()
.bold()
);
return Err(CacheError::CacheFileNotFound(args.cache).into());
}
println!(
"{} {}Loading candy machine",
style("[1/2]").bold().dim(),
CANDY_EMOJI
);
let pb = spinner_with_style();
pb.set_message("Connecting...");
let candy_machine_pubkey = match Pubkey::from_str(&cache.program.candy_machine) {
Ok(pubkey) => pubkey,
Err(_) => {
pb.finish_and_clear();
return Err(CacheError::InvalidCandyMachineAddress(
cache.program.candy_machine.clone(),
)
.into());
}
};
let client = setup_client(&sugar_config)?;
let program = client.program(CANDY_MACHINE_ID);
let data = match program.rpc().get_account_data(&candy_machine_pubkey) {
Ok(account_data) => account_data,
Err(err) => {
return Err(VerifyError::FailedToGetAccountData(err.to_string()).into());
}
};
let candy_machine: CandyMachine = CandyMachine::try_deserialize(&mut data.as_slice())?;
pb.finish_with_message("Completed");
println!(
"\n{} {}Verification",
style("[2/2]").bold().dim(),
PAPER_EMOJI
);
if candy_machine.data.hidden_settings.is_none() {
let num_items = cache.items.0.len();
let cache_items = &mut cache.items.0;
let mut errors = Vec::new();
println!("Verifying {} config line(s): (Ctrl+C to abort)", num_items);
let pb = progress_bar_with_style(num_items as u64);
let step: u64 = 1_000_000 / num_items as u64;
for i in 0..num_items {
let name_start = CONFIG_ARRAY_START
+ STRING_LEN_SIZE
+ CONFIG_LINE_SIZE * (i as usize)
+ CONFIG_NAME_OFFSET;
let name_end = name_start + MAX_NAME_LENGTH;
let uri_start = CONFIG_ARRAY_START
+ STRING_LEN_SIZE
+ CONFIG_LINE_SIZE * (i as usize)
+ CONFIG_URI_OFFSET;
let uri_end = uri_start + MAX_URI_LENGTH;
let name_error = format!("Failed to decode name for item {}", i);
let name = String::from_utf8(data[name_start..name_end].to_vec())
.expect(&name_error)
.trim_matches(char::from(0))
.to_string();
let uri_error = format!("Failed to decode uri for item {}", i);
let uri = String::from_utf8(data[uri_start..uri_end].to_vec())
.expect(&uri_error)
.trim_matches(char::from(0))
.to_string();
let on_chain_item = OnChainItem { name, uri };
let cache_item = cache_items
.get_mut(&i.to_string())
.expect("Failed to get item from config.");
if let Err(err) = items_match(cache_item, &on_chain_item) {
cache_item.on_chain = false;
errors.push((i.to_string(), err.to_string()));
}
pb.inc(1);
thread::sleep(Duration::from_micros(step));
}
pb.finish();
if !errors.is_empty() {
cache.sync_file()?;
let total = errors.len();
println!("\nInvalid items found: ");
for e in errors {
println!("- Item {}: {}", e.0, e.1);
}
println!("\nCache updated - re-run `deploy`.");
return Err(anyhow!("{} invalid item(s) found.", total));
}
let cluster = match get_cluster(program.rpc())? {
Cluster::Devnet => "devnet",
Cluster::Mainnet => "mainnet",
};
println!(
"\nAll items checked out. You're good to go!\nSee your candy machine at: https://www.solaneyes.com/address/{}?cluster={}",
cache.program.candy_machine,
cluster
);
} else {
println!("\nHidden settings enabled. You're good to go!");
}
Ok(())
}
fn items_match(cache_item: &CacheItem, on_chain_item: &OnChainItem) -> Result<()> {
if cache_item.name != on_chain_item.name {
return Err(VerifyError::Mismatch(
"name".to_string(),
cache_item.name.clone(),
on_chain_item.name.clone(),
)
.into());
} else if cache_item.metadata_link != on_chain_item.uri {
return Err(VerifyError::Mismatch(
"uri".to_string(),
cache_item.metadata_link.clone(),
on_chain_item.uri.clone(),
)
.into());
}
Ok(())
}