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
use std::{thread, time::Duration};
use anchor_lang::AccountDeserialize;
use console::style;
use mpl_candy_machine::CandyMachine;
use crate::{
cache::*,
candy_machine::CANDY_MACHINE_ID,
common::*,
config::Cluster,
constants::{CANDY_EMOJI, PAPER_EMOJI},
pdas::get_collection_pda,
utils::*,
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.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())?;
let collection_info = get_collection_pda(&candy_machine_pubkey, &program).ok();
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 = candy_machine.data.items_available;
let cache_items = &mut cache.items;
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));
}
if !errors.is_empty() {
pb.abandon_with_message(format!("{}", style("Verification failed ").red().bold()));
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));
} else {
pb.finish_with_message(format!(
"{}",
style("Config line verification successful ").green().bold()
));
}
} else {
println!("\nHidden settings enabled. No config items to verify.");
}
if candy_machine.items_redeemed > 0 {
println!(
"\nAn item has already been minted. Skipping candy machine collection verification..."
);
} else {
let collection_mint_cache = cache.program.collection_mint.clone();
let collection_needs_deploy = if let Some(collection_item) = cache.items.get("-1") {
!collection_item.on_chain
} else {
false
};
let collection_item = cache.items.get_mut("-1");
if let Some((_, collection_pda_account)) = collection_info {
if collection_pda_account.mint.to_string() != collection_mint_cache {
println!("\nInvalid collection state found");
cache.program.collection_mint = collection_pda_account.mint.to_string();
if let Some(collection_item) = collection_item {
collection_item.on_chain = false;
}
cache.sync_file()?;
println!("Cache updated - re-run `deploy`.");
return Err(anyhow!(
"Collection mint in cache {} doesn't match on chain collection mint {}!",
collection_mint_cache,
collection_pda_account.mint.to_string()
));
} else if collection_needs_deploy {
println!("\nInvalid collection state found - re-run `deploy`.");
return Err(CacheError::InvalidState.into());
}
} else {
let mut error_found = false;
if collection_mint_cache != String::new() {
error_found = true;
cache.program.collection_mint = String::new();
}
if let Some(collection_item) = collection_item {
error_found = true;
collection_item.on_chain = false;
}
if error_found {
cache.sync_file()?;
println!("\nInvalid collection state found - re-run `deploy`.");
return Err(CacheError::InvalidState.into());
}
}
}
let cluster = match get_cluster(program.rpc())? {
Cluster::Devnet => "devnet",
Cluster::Mainnet => "mainnet",
Cluster::Unknown => "",
};
if cluster.is_empty() {
println!("\nVerification successful. You're good to go!");
} else {
println!(
"\nVerification successful. You're good to go!\n\nSee your candy machine at:\n -> https://www.solaneyes.com/address/{}{}",
cache.program.candy_machine,
cluster
);
}
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(())
}