leo_lang/cli/commands/common/
query.rs1use super::*;
18use crate::cli::query::{LeoBlock, LeoProgram};
19
20use leo_ast::NetworkName;
21use leo_package::ProgramData;
22use leo_span::Symbol;
23use snarkvm::prelude::{Program, ProgramID};
24
25use indexmap::IndexSet;
26use std::collections::HashMap;
27
28pub fn get_public_balance<N: Network>(
30 private_key: &PrivateKey<N>,
31 endpoint: &str,
32 network: NetworkName,
33 context: &Context,
34) -> Result<u64> {
35 let address = Address::<N>::try_from(ViewKey::try_from(private_key)?)?;
37 let mut public_balance = LeoQuery {
39 env_override: EnvOptions { endpoint: Some(endpoint.to_string()), network: Some(network), ..Default::default() },
40 command: QueryCommands::Program {
41 command: LeoProgram {
42 name: "credits".to_string(),
43 edition: None,
44 mappings: false,
45 mapping_value: Some(vec!["account".to_string(), address.to_string()]),
46 },
47 },
48 }
49 .execute(Context::new(context.path.clone(), context.home.clone(), true)?)?;
50 public_balance.truncate(public_balance.len() - 3);
52 public_balance.parse::<u64>().map_err(|_| CliError::invalid_balance(address).into())
54}
55
56#[allow(dead_code)]
58pub fn get_latest_block_height(endpoint: &str, network: NetworkName, context: &Context) -> Result<u32> {
59 let height = LeoQuery {
61 env_override: EnvOptions { endpoint: Some(endpoint.to_string()), network: Some(network), ..Default::default() },
62 command: QueryCommands::Block {
63 command: LeoBlock {
64 id: None,
65 latest: false,
66 latest_hash: false,
67 latest_height: true,
68 range: None,
69 transactions: false,
70 to_height: false,
71 },
72 },
73 }
74 .execute(Context::new(context.path.clone(), context.home.clone(), true)?)?;
75 let height = height.parse::<u32>().map_err(CliError::string_parse_error)?;
77 Ok(height)
78}
79
80pub fn handle_broadcast<N: Network>(
84 endpoint: &str,
85 transaction: &Transaction<N>,
86 operation: &str,
87) -> Result<(String, u16)> {
88 let mut response = ureq::Agent::config_builder()
90 .max_redirects(0)
91 .build()
92 .new_agent()
93 .post(endpoint)
94 .header("X-Leo-Version", env!("CARGO_PKG_VERSION"))
95 .send_json(transaction)
96 .map_err(|err| CliError::broadcast_error(err.to_string()))?;
97 match response.status().as_u16() {
98 200..=299 => {
99 println!(
100 "✉️ Broadcasted transaction with:\n - transaction ID: '{}'",
101 transaction.id().to_string().bold().yellow(),
102 );
103 if let Some(fee) = transaction.fee_transition() {
104 println!(" - fee ID: '{}'", fee.id().to_string().bold().yellow());
106 println!(" - fee transaction ID: '{}'", Transaction::from_fee(fee)?.id().to_string().bold().yellow());
108 println!(" (use this to check for rejected transactions)\n");
109 }
110 Ok((response.body_mut().read_to_string().unwrap(), response.status().as_u16()))
111 }
112 301 => {
113 let msg = format!(
114 "⚠️ The endpoint `{endpoint}` has been permanently moved. Try using `https://api.explorer.provable.com/v1` in your `.env` file or via the `--endpoint` flag."
115 );
116 Err(CliError::broadcast_error(msg).into())
117 }
118 _ => {
119 let code = response.status();
120 let error_message = match response.body_mut().read_to_string() {
121 Ok(response) => format!("(status code {code}: {response:?})"),
122 Err(err) => format!("({err})"),
123 };
124
125 let msg = match transaction {
126 Transaction::Deploy(..) => {
127 format!("❌ Failed to deploy '{}' to {}: {}", operation.bold(), &endpoint, error_message)
128 }
129 Transaction::Execute(..) => {
130 format!(
131 "❌ Failed to broadcast execution '{}' to {}: {}",
132 operation.bold(),
133 &endpoint,
134 error_message
135 )
136 }
137 Transaction::Fee(..) => {
138 format!("❌ Failed to broadcast fee '{}' to {}: {}", operation.bold(), &endpoint, error_message)
139 }
140 };
141
142 Err(CliError::broadcast_error(msg).into())
143 }
144 }
145}
146
147pub fn load_latest_programs_from_network<N: Network>(
149 context: &Context,
150 program_id: ProgramID<N>,
151 network: NetworkName,
152 endpoint: &str,
153) -> Result<Vec<(Program<N>, Option<u16>)>> {
154 use snarkvm::prelude::Program;
155 use std::collections::HashSet;
156
157 let mut programs = HashMap::new();
159 let mut ordered_programs = IndexSet::new();
161 let mut stack = vec![(program_id, false)];
163
164 while let Some((current_id, seen)) = stack.pop() {
166 if seen {
169 ordered_programs.insert(current_id);
170 }
171 else {
173 if programs.contains_key(¤t_id) {
175 continue;
176 }
177 let program = leo_package::Program::fetch(
179 Symbol::intern(¤t_id.name().to_string()),
180 None,
181 &context.home()?,
182 network,
183 endpoint,
184 true,
185 )
186 .map_err(|_| CliError::custom(format!("Failed to fetch program source for ID: {current_id}")))?;
187 let ProgramData::Bytecode(program_src) = program.data else {
188 panic!("Expected bytecode when fetching a remote program");
189 };
190
191 let bytecode = Program::<N>::from_str(&program_src)
193 .map_err(|_| CliError::custom(format!("Failed to parse program source for ID: {current_id}")))?;
194
195 let imports = bytecode.imports().keys().cloned().collect::<HashSet<_>>();
197
198 programs.insert(current_id, (bytecode, program.edition));
200
201 stack.push((current_id, true));
203
204 for import_id in imports {
206 stack.push((import_id, false));
207 }
208 }
209 }
210
211 Ok(ordered_programs
213 .iter()
214 .map(|program_id| programs.remove(program_id).expect("Program not found in cache"))
215 .collect())
216}