diff --git a/errors/src/errors/package/package_errors.rs b/errors/src/errors/package/package_errors.rs index 530811a7e3..e8157b89d3 100644 --- a/errors/src/errors/package/package_errors.rs +++ b/errors/src/errors/package/package_errors.rs @@ -404,7 +404,7 @@ create_messages!( msg: format!("Failed to read manifest file from the provided file path {path} - {error}"), help: None, } - + @backtraced insufficient_balance { args: (balance: impl Display, fee: impl Display), diff --git a/leo/cli/commands/deploy.rs b/leo/cli/commands/deploy.rs index 0c7ec0282e..f618335ea3 100644 --- a/leo/cli/commands/deploy.rs +++ b/leo/cli/commands/deploy.rs @@ -133,16 +133,22 @@ fn handle_deploy, N: Network>( // Compute the minimum deployment cost. let (total_cost, (storage_cost, synthesis_cost, namespace_cost)) = deployment_cost(&deployment)?; - + if command.fee_options.estimate_fee { // Use `credit` denomination instead of `microcredit`. - deploy_cost_breakdown(name, total_cost as f64 / 1_000_000.0, storage_cost as f64 / 1_000_000.0, synthesis_cost as f64 / 1_000_000.0, namespace_cost as f64 / 1_000_000.0); + deploy_cost_breakdown( + name, + total_cost as f64 / 1_000_000.0, + storage_cost as f64 / 1_000_000.0, + synthesis_cost as f64 / 1_000_000.0, + namespace_cost as f64 / 1_000_000.0, + ); continue; } // Initialize an RNG. let rng = &mut rand::thread_rng(); - + // Prepare the fees. let fee = match &command.fee_options.record { Some(record) => { @@ -173,7 +179,7 @@ fn handle_deploy, N: Network>( // Generate the deployment transaction. let transaction = Transaction::from_deployment(owner, deployment, fee)?; - + println!("✅ Created deployment transaction for '{}'", name.bold()); // Determine if the transaction should be broadcast, stored, or displayed to the user. @@ -195,7 +201,21 @@ fn handle_deploy, N: Network>( fn deploy_cost_breakdown(name: &String, total_cost: f64, storage_cost: f64, synthesis_cost: f64, namespace_cost: f64) { println!("✅ Estimated deployment cost for '{}' is {} credits.", name.bold(), total_cost); // Display the cost breakdown in a table. - let data = [[name, "Cost (credits)", "Cost reduction tips"], ["Storage", &format!("{:.6}", storage_cost), "Use less instructions"], ["Synthesis", &format!("{:.6}", synthesis_cost), "Remove expensive operations (Ex: SHA3), or unnecessary imports"], ["Namespace", &format!("{:.6}", namespace_cost), "Lengthen the program name (each additional character makes it 10x cheaper)"], ["Total", &format!("{:.6}", total_cost), ""]]; + let data = [ + [name, "Cost (credits)", "Cost reduction tips"], + ["Storage", &format!("{:.6}", storage_cost), "Use less instructions"], + [ + "Synthesis", + &format!("{:.6}", synthesis_cost), + "Remove expensive operations (Ex: SHA3), or unnecessary imports", + ], + [ + "Namespace", + &format!("{:.6}", namespace_cost), + "Lengthen the program name (each additional character makes it 10x cheaper)", + ], + ["Total", &format!("{:.6}", total_cost), ""], + ]; let mut out = Vec::new(); text_tables::render(&mut out, data).unwrap(); println!("{}", ::std::str::from_utf8(&out).unwrap()); diff --git a/leo/cli/commands/execute.rs b/leo/cli/commands/execute.rs index 27955d4e6f..3994ce1671 100644 --- a/leo/cli/commands/execute.rs +++ b/leo/cli/commands/execute.rs @@ -14,22 +14,32 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use aleo_std::StorageMode; use super::*; +use aleo_std::StorageMode; use clap::Parser; use snarkvm::{ cli::{helpers::dotenv_private_key, Execute as SnarkVMExecute}, prelude::{MainnetV0, Network, Parser as SnarkVMParser, TestnetV0}, - }; -use snarkvm::file::AleoFile; -use snarkvm::ledger::Execution; -use snarkvm::ledger::Transaction::Execute as ExecuteTransaction; -use snarkvm::prelude::store::ConsensusStore; -use snarkvm::prelude::store::helpers::memory::{BlockMemory, ConsensusMemory}; -use snarkvm::prelude::{Address, execution_cost, Process, Program as SnarkVMProgram, ProgramID, VM}; -use snarkvm::prelude::query::Query as SnarkVMQuery; +}; + use crate::cli::query::QueryCommands; -use crate::cli::query::QueryCommands::Program; +use leo_retriever::NetworkName; +use snarkvm::{ + ledger::Transaction::Execute as ExecuteTransaction, + prelude::{ + execution_cost, + query::Query as SnarkVMQuery, + store::{ + helpers::memory::{BlockMemory, ConsensusMemory}, + ConsensusStore, + }, + Address, + Process, + Program as SnarkVMProgram, + ProgramID, + VM, + }, +}; /// Build, Prove and Run Leo program with inputs #[derive(Parser, Debug)] @@ -68,7 +78,7 @@ impl Command for Execute { (Build { options: self.compiler_options.clone() }).execute(context) } - fn apply(self, context: Context, input: Self::Input) -> Result { + fn apply(self, context: Context, _input: Self::Input) -> Result { // Parse the network. let network = NetworkName::try_from(self.compiler_options.network.as_str())?; match network { @@ -80,7 +90,6 @@ impl Command for Execute { // A helper function to handle the `execute` command. fn handle_execute(command: Execute, context: Context) -> Result<::Output> { - // If input values are provided, then run the program with those inputs. // Otherwise, use the input file. let mut inputs = command.inputs.clone(); @@ -116,9 +125,8 @@ fn handle_execute(command: Execute, context: Context) -> Result< { let local = context.open_manifest::()?.program_id().to_string(); // Throw error if local name doesn't match the specified name. @@ -143,18 +151,18 @@ fn handle_execute(command: Execute, context: Context) -> Result<>::from(command.compiler_options.endpoint.clone()); - + // Initialize an RNG. let rng = &mut rand::thread_rng(); - + // Initialize the storage. let store = ConsensusStore::>::open(StorageMode::Production)?; - + // Initialize the VM. let vm = VM::from(store)?; - + // Load the main program, and all of its imports. - let program_id = &ProgramID::::from_str(&format!("{}.aleo", program_name))?; + let program_id = &ProgramID::::from_str(&format!("{}.aleo", program_name))?; // TODO: create a local version too load_program_from_network(&command, context.clone(), &mut vm.process().write(), program_id)?; @@ -165,11 +173,19 @@ fn handle_execute(command: Execute, context: Context) -> Result<(command: Execute, context: Context) -> Result<::try_from(ViewKey::try_from(&private_key)?)?; // Query the public balance of the address on the `account` mapping from `credits.aleo`. - let mut public_balance = Query{ endpoint: command.compiler_options.endpoint.clone(), network: command.compiler_options.network.clone(), command: QueryCommands::Program { - command: crate::cli::commands::query::Program { - name: "credits".to_string(), - mappings: false, - mapping_value: Some(vec!["account".to_string(), address.to_string()]), + let mut public_balance = Query { + endpoint: command.compiler_options.endpoint.clone(), + network: command.compiler_options.network.clone(), + command: QueryCommands::Program { + command: crate::cli::commands::query::Program { + name: "credits".to_string(), + mappings: false, + mapping_value: Some(vec!["account".to_string(), address.to_string()]), + }, }, - } }.execute(context.clone())?; + } + .execute(context.clone())?; // Check balance. // Remove the last 3 characters since they represent the `u64` suffix. public_balance.truncate(public_balance.len() - 3); @@ -196,17 +217,29 @@ fn handle_execute(command: Execute, context: Context) -> Result<(command: Execute, context: Context) -> Result<(command: &Execute, context: Context, process: &mut Process, program_id: &ProgramID) -> Result<()> { +fn load_program_from_network( + command: &Execute, + context: Context, + process: &mut Process, + program_id: &ProgramID, +) -> Result<()> { // Fetch the program. - let program_src = Query{ endpoint: command.compiler_options.endpoint.clone(), network: command.compiler_options.network.clone(), command: QueryCommands::Program { - command: crate::cli::commands::query::Program { - name: program_id.to_string(), - mappings: false, - mapping_value: None, + let program_src = Query { + endpoint: command.compiler_options.endpoint.clone(), + network: command.compiler_options.network.clone(), + command: QueryCommands::Program { + command: crate::cli::commands::query::Program { + name: program_id.to_string(), + mappings: false, + mapping_value: None, + }, }, - } }.execute(context.clone())?; + } + .execute(context.clone())?; let program = SnarkVMProgram::::from_str(&program_src).unwrap(); // Return early if the program is already loaded. @@ -276,8 +319,21 @@ fn load_program_from_network(command: &Execute, context: Context, pr fn execution_cost_breakdown(name: &String, total_cost: f64, storage_cost: f64, finalize_cost: f64) { println!("✅ Estimated execution cost for '{}' is {} credits.", name.bold(), total_cost); // Display the cost breakdown in a table. - let data = [[name, "Cost (credits)", "Cost reduction tips"], ["Storage", &format!("{:.6}", storage_cost), "Use fewer nested transition functions and smaller input and output datatypes"], ["On-chain", &format!("{:.6}", finalize_cost), "Remove operations that are expensive computationally (Ex: hash functions) or storage-wise (Ex: Mapping insertions)"], ["Total", &format!("{:.6}", total_cost), ""]]; + let data = [ + [name, "Cost (credits)", "Cost reduction tips"], + [ + "Storage", + &format!("{:.6}", storage_cost), + "Use fewer nested transition functions and smaller input and output datatypes", + ], + [ + "On-chain", + &format!("{:.6}", finalize_cost), + "Remove operations that are expensive computationally (Ex: hash functions) or storage-wise (Ex: Mapping insertions)", + ], + ["Total", &format!("{:.6}", total_cost), ""], + ]; let mut out = Vec::new(); text_tables::render(&mut out, data).unwrap(); println!("{}", ::std::str::from_utf8(&out).unwrap()); -} \ No newline at end of file +} diff --git a/leo/cli/commands/mod.rs b/leo/cli/commands/mod.rs index 36395dfb1a..e7a6040f57 100644 --- a/leo/cli/commands/mod.rs +++ b/leo/cli/commands/mod.rs @@ -64,7 +64,7 @@ use colored::Colorize; use std::str::FromStr; use tracing::span::Span; -use snarkvm::{console::network::Network, prelude::ToBytes}; +use snarkvm::console::network::Network; /// Base trait for the Leo CLI, see methods and their documentation for details. pub trait Command { @@ -205,7 +205,11 @@ impl Default for BuildOptions { /// Used by Execute and Deploy commands. #[derive(Parser, Clone, Debug)] pub struct FeeOptions { - #[clap(long, help = "Estimate the deploy or execution fee without broadcasting to the network.", default_value = "false")] + #[clap( + long, + help = "Estimate the deploy or execution fee without broadcasting to the network.", + default_value = "false" + )] pub(crate) estimate_fee: bool, #[clap(long, help = "Priority fee in microcredits. Defaults to 0.", default_value = "0")] pub(crate) priority_fee: u64, diff --git a/leo/package/Cargo.toml b/leo/package/Cargo.toml index fc76c4212e..7369098ec7 100644 --- a/leo/package/Cargo.toml +++ b/leo/package/Cargo.toml @@ -48,7 +48,7 @@ features = [ "derive" ] version = "3.1.1" [dependencies.text-tables] -text-tables = "0.3.1" +version = "0.3.1" [dependencies.toml] version = "0.8"