From 0d5cfb33fb2dfda8fe1f3fae463a72ec88e9706c Mon Sep 17 00:00:00 2001 From: evan-schott <53463459+evan-schott@users.noreply.github.com> Date: Mon, 8 Jan 2024 14:24:18 -0800 Subject: [PATCH] enable on-chain execution --- leo/cli/commands/execute.rs | 70 +++++++++++++++++++++++++++++-------- leo/cli/commands/mod.rs | 8 +++++ 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/leo/cli/commands/execute.rs b/leo/cli/commands/execute.rs index 2df5b01510..58bca065cf 100644 --- a/leo/cli/commands/execute.rs +++ b/leo/cli/commands/execute.rs @@ -15,28 +15,26 @@ // along with the Leo library. If not, see . use super::*; - -use snarkvm::cli::Execute as SnarkVMExecute; +use snarkos_cli::commands::{Developer, Execute as SnarkOSExecute}; +use snarkvm::cli::{dotenv_private_key, Execute as SnarkVMExecute}; /// Build, Prove and Run Leo program with inputs #[derive(Parser, Debug)] pub struct Execute { - #[clap(name = "NAME", help = "The name of the program to execute.", default_value = "main")] + #[clap(name = "NAME", help = "The name of the function to execute.", default_value = "main")] name: String, - #[clap(name = "INPUTS", help = "The inputs to the program. If none are provided, the input file is used.")] inputs: Vec, - - #[clap( - name = "ENDPOINT", - help = "The specified network endpoint.", - default_value = "https://api.explorer.aleo.org/v1", - long - )] - endpoint: String, - + #[clap(long, help = "Execute the transition on chain", default_value = "false")] + broadcast: bool, + #[clap(long, help = "Custom priority fee in microcredits", default_value = "1000000")] + priority_fee: String, + #[clap(long, help = "Custom network", default_value = "testnet3")] + network: String, + #[clap(long, help = "Custom private key")] + private_key: Option, #[clap(flatten)] - pub(crate) compiler_options: BuildOptions, + compiler_options: BuildOptions, } impl Command for Execute { @@ -52,6 +50,48 @@ impl Command for Execute { } fn apply(self, context: Context, input: Self::Input) -> Result { + // If the `broadcast` flag is set, then broadcast the transaction. + if self.broadcast { + // Get the program name + let project_name = context.open_manifest()?.program_id().to_string(); + + // Get the private key + let mut private_key = self.private_key; + if private_key.is_none() { + private_key = + Some(dotenv_private_key().map_err(CliError::failed_to_read_environment_private_key)?.to_string()); + } + + // Execute program + Developer::Execute( + SnarkOSExecute::try_parse_from( + [ + vec![ + "snarkos", + "--private-key", + private_key.as_ref().unwrap(), + "--query", + self.compiler_options.endpoint.as_str(), + "--priority-fee", + self.priority_fee.as_str(), + "--broadcast", + format!("{}/{}/transaction/broadcast", self.compiler_options.endpoint, self.network) + .as_str(), + project_name.as_str(), + &self.name, + ], + self.inputs.iter().map(|input| input.as_str()).collect(), + ] + .concat(), + ) + .unwrap(), + ) + .parse() + .map_err(CliError::failed_to_execute_deploy)?; + + return Ok(()); + } + // If input values are provided, then run the program with those inputs. // Otherwise, use the input file. let mut inputs = match self.inputs.is_empty() { @@ -75,7 +115,7 @@ impl Command for Execute { // Add the endpoint to the arguments. arguments.push(String::from("--endpoint")); - arguments.push(self.endpoint); + arguments.push(self.compiler_options.endpoint.clone()); // Open the Leo build/ directory let path = context.dir()?; diff --git a/leo/cli/commands/mod.rs b/leo/cli/commands/mod.rs index 230f4657cf..a42aaf8d08 100644 --- a/leo/cli/commands/mod.rs +++ b/leo/cli/commands/mod.rs @@ -122,6 +122,14 @@ pub trait Command { /// require Build command output as their input. #[derive(Parser, Clone, Debug, Default)] pub struct BuildOptions { + #[clap( + long, + help = "Endpoint to retrieve on-chain dependencies from.", + default_value = "http://api.explorer.aleo.org/v1" + )] + pub endpoint: String, + #[clap(long, help = "Does not recursively compile dependencies.")] + pub non_recursive: bool, #[clap(long, help = "Enables offline mode.")] pub offline: bool, #[clap(long, help = "Enable spans in AST snapshots.")]