leo_lang/cli/commands/common/
query.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use 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
28/// A helper function to query the public balance of an address.
29pub fn get_public_balance<N: Network>(
30    private_key: &PrivateKey<N>,
31    endpoint: &str,
32    network: NetworkName,
33    context: &Context,
34) -> Result<u64> {
35    // Derive the account address.
36    let address = Address::<N>::try_from(ViewKey::try_from(private_key)?)?;
37    // Query the public balance of the address on the `account` mapping from `credits.aleo`.
38    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    // Remove the last 3 characters since they represent the `u64` suffix.
51    public_balance.truncate(public_balance.len() - 3);
52    // Make sure the balance is valid.
53    public_balance.parse::<u64>().map_err(|_| CliError::invalid_balance(address).into())
54}
55
56// A helper function to query for the latest block height.
57#[allow(dead_code)]
58pub fn get_latest_block_height(endpoint: &str, network: NetworkName, context: &Context) -> Result<u32> {
59    // Query the latest block height.
60    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    // Parse the height.
76    let height = height.parse::<u32>().map_err(CliError::string_parse_error)?;
77    Ok(height)
78}
79
80/// Determine if the transaction should be broadcast or displayed to user.
81///
82/// Returns (body, status code).
83pub fn handle_broadcast<N: Network>(
84    endpoint: &str,
85    transaction: &Transaction<N>,
86    operation: &str,
87) -> Result<(String, u16)> {
88    // Send the deployment request to the endpoint.
89    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                // Most transactions will have fees, but some, like credits.aleo/upgrade executions, may not.
105                println!("  - fee ID: '{}'", fee.id().to_string().bold().yellow());
106                // Print the fee as a transaction.
107                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
147/// Loads the latest edition of a program and all its imports from the network, using an iterative DFS.
148pub 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    // A cache for loaded programs, mapping a program ID to its bytecode and edition.
158    let mut programs = HashMap::new();
159    // The ordered set of programs.
160    let mut ordered_programs = IndexSet::new();
161    // Stack of program IDs to process (DFS traversal).
162    let mut stack = vec![(program_id, false)];
163
164    // Loop until all programs and their dependencies are visited.
165    while let Some((current_id, seen)) = stack.pop() {
166        // If the program has already been seen, then all its imports have been processed.
167        // Add it to the ordered set and continue.
168        if seen {
169            ordered_programs.insert(current_id);
170        }
171        // Otherwise, fetch it and schedule its imports for processing.
172        else {
173            // If the program is already in the cache, skip it.
174            if programs.contains_key(&current_id) {
175                continue;
176            }
177            // Fetch the program source from the network.
178            let program = leo_package::Program::fetch(
179                Symbol::intern(&current_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            // Parse the program source into a Program object.
192            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            // Get the imports of the program.
196            let imports = bytecode.imports().keys().cloned().collect::<HashSet<_>>();
197
198            // Add the program to the cache.
199            programs.insert(current_id, (bytecode, program.edition));
200
201            // Mark the program as seen.
202            stack.push((current_id, true));
203
204            // Queue all imported programs for processing.
205            for import_id in imports {
206                stack.push((import_id, false));
207            }
208        }
209    }
210
211    // Return all loaded programs in insertion order.
212    Ok(ordered_programs
213        .iter()
214        .map(|program_id| programs.remove(program_id).expect("Program not found in cache"))
215        .collect())
216}