enable on-chain execution

This commit is contained in:
evan-schott 2024-01-08 14:24:18 -08:00
parent 67f30038d3
commit 0d5cfb33fb
2 changed files with 63 additions and 15 deletions

View File

@ -15,28 +15,26 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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<String>,
#[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<String>,
#[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<Self::Output> {
// 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()?;

View File

@ -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.")]