mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-10-26 15:13:13 +03:00
clippy
This commit is contained in:
parent
275e837030
commit
96eeaf5aab
@ -160,7 +160,13 @@ fn handle_deploy<A: Aleo<Network = N, BaseField = N::Field>, N: Network>(
|
||||
}
|
||||
None => {
|
||||
// Make sure the user has enough public balance to pay for the deployment.
|
||||
check_balance(&private_key, &command.options.endpoint, &command.options.network, context.clone(), total_cost)?;
|
||||
check_balance(
|
||||
&private_key,
|
||||
&command.options.endpoint,
|
||||
&command.options.network,
|
||||
context.clone(),
|
||||
total_cost,
|
||||
)?;
|
||||
let fee_authorization = vm.authorize_fee_public(
|
||||
&private_key,
|
||||
total_cost,
|
||||
@ -197,20 +203,21 @@ fn handle_deploy<A: Aleo<Network = N, BaseField = N::Field>, N: Network>(
|
||||
}
|
||||
|
||||
// A helper function to display a cost breakdown of the deployment.
|
||||
fn deploy_cost_breakdown(name: &String, total_cost: f64, storage_cost: f64, synthesis_cost: f64, namespace_cost: f64, priority_fee: f64) {
|
||||
fn deploy_cost_breakdown(
|
||||
name: &String,
|
||||
total_cost: f64,
|
||||
storage_cost: f64,
|
||||
synthesis_cost: f64,
|
||||
namespace_cost: f64,
|
||||
priority_fee: f64,
|
||||
) {
|
||||
println!("Base deployment cost for '{}' is {} credits.", name.bold(), total_cost);
|
||||
// Display the cost breakdown in a table.
|
||||
let data = [
|
||||
[name, "Cost (credits)"],
|
||||
["Transaction Storage", &format!("{:.6}", storage_cost)],
|
||||
[
|
||||
"Program Synthesis",
|
||||
&format!("{:.6}", synthesis_cost),
|
||||
],
|
||||
[
|
||||
"Namespace",
|
||||
&format!("{:.6}", namespace_cost),
|
||||
],
|
||||
["Program Synthesis", &format!("{:.6}", synthesis_cost)],
|
||||
["Namespace", &format!("{:.6}", namespace_cost)],
|
||||
["Priority Fee", &format!("{:.6}", priority_fee)],
|
||||
["Total", &format!("{:.6}", total_cost)],
|
||||
];
|
||||
|
@ -14,19 +14,22 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use std::collections::HashMap;
|
||||
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},
|
||||
cli::helpers::dotenv_private_key,
|
||||
prelude::{Network, Parser as SnarkVMParser},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::cli::query::QueryCommands;
|
||||
use leo_retriever::NetworkName;
|
||||
use snarkvm::{
|
||||
circuit::{Aleo, AleoTestnetV0, AleoV0},
|
||||
cli::LOCALE,
|
||||
ledger::Transaction::Execute as ExecuteTransaction,
|
||||
package::Package as SnarkVMPackage,
|
||||
prelude::{
|
||||
execution_cost,
|
||||
query::Query as SnarkVMQuery,
|
||||
@ -34,17 +37,15 @@ use snarkvm::{
|
||||
helpers::memory::{BlockMemory, ConsensusMemory},
|
||||
ConsensusStore,
|
||||
},
|
||||
Address,
|
||||
Identifier,
|
||||
Locator,
|
||||
Process,
|
||||
Program as SnarkVMProgram,
|
||||
ProgramID,
|
||||
Value,
|
||||
VM,
|
||||
},
|
||||
package::Package as SnarkVMPackage,
|
||||
};
|
||||
use snarkvm::circuit::{Aleo, AleoTestnetV0, AleoV0};
|
||||
use snarkvm::cli::LOCALE;
|
||||
use snarkvm::prelude::{Identifier, Locator, Value};
|
||||
|
||||
/// Build, Prove and Run Leo program with inputs
|
||||
#[derive(Parser, Debug)]
|
||||
@ -100,8 +101,7 @@ fn handle_execute<A: Aleo>(command: Execute, context: Context) -> Result<<Execut
|
||||
let mut inputs = command.inputs.clone();
|
||||
|
||||
// Add the inputs to the arguments.
|
||||
match command.file.clone() {
|
||||
Some(file) => {
|
||||
if let Some(file) = command.file.clone() {
|
||||
// Get the contents from the file.
|
||||
let path = context.dir()?.join(file);
|
||||
let raw_content =
|
||||
@ -122,8 +122,6 @@ fn handle_execute<A: Aleo>(command: Execute, context: Context) -> Result<<Execut
|
||||
// Add the inputs from the file to the arguments.
|
||||
inputs.append(&mut inputs_from_file);
|
||||
}
|
||||
None => {},
|
||||
}
|
||||
|
||||
// Initialize an RNG.
|
||||
let rng = &mut rand::thread_rng();
|
||||
@ -155,7 +153,8 @@ fn handle_execute<A: Aleo>(command: Execute, context: Context) -> Result<<Execut
|
||||
};
|
||||
|
||||
// Specify the query
|
||||
let query = SnarkVMQuery::<A::Network, BlockMemory<A::Network>>::from(command.compiler_options.endpoint.clone());
|
||||
let query =
|
||||
SnarkVMQuery::<A::Network, BlockMemory<A::Network>>::from(command.compiler_options.endpoint.clone());
|
||||
|
||||
// Initialize the storage.
|
||||
let store = ConsensusStore::<A::Network, ConsensusMemory<A::Network>>::open(StorageMode::Production)?;
|
||||
@ -186,7 +185,8 @@ fn handle_execute<A: Aleo>(command: Execute, context: Context) -> Result<<Execut
|
||||
)?;
|
||||
|
||||
// Check the transaction cost.
|
||||
let (mut total_cost, (storage_cost, finalize_cost)) = if let ExecuteTransaction(_, execution, _) = &transaction {
|
||||
let (mut total_cost, (storage_cost, finalize_cost)) = if let ExecuteTransaction(_, execution, _) = &transaction
|
||||
{
|
||||
execution_cost(&vm.process().read(), execution)?
|
||||
} else {
|
||||
panic!("All transactions should be of type Execute.")
|
||||
@ -204,7 +204,13 @@ fn handle_execute<A: Aleo>(command: Execute, context: Context) -> Result<<Execut
|
||||
|
||||
// Check if the public balance is sufficient.
|
||||
if fee_record.is_none() {
|
||||
check_balance::<A::Network>(&private_key, &command.compiler_options.endpoint, &command.compiler_options.network, context, total_cost)?;
|
||||
check_balance::<A::Network>(
|
||||
&private_key,
|
||||
&command.compiler_options.endpoint,
|
||||
&command.compiler_options.network,
|
||||
context,
|
||||
total_cost,
|
||||
)?;
|
||||
}
|
||||
|
||||
println!("✅ Created execution transaction for '{}'", program_id.to_string().bold());
|
||||
@ -236,8 +242,15 @@ fn handle_execute<A: Aleo>(command: Execute, context: Context) -> Result<<Execut
|
||||
// Convert the inputs.
|
||||
let inputs = inputs.iter().map(|input| Value::from_str(input).unwrap()).collect::<Vec<Value<A::Network>>>();
|
||||
// Execute the request.
|
||||
let (response, execution, metrics) =
|
||||
package.execute::<A, _>(command.compiler_options.endpoint.clone(), &private_key, Identifier::try_from(command.name.clone())?, &inputs, rng).map_err(|err| PackageError::execution_error(err))?;
|
||||
let (response, execution, metrics) = package
|
||||
.execute::<A, _>(
|
||||
command.compiler_options.endpoint.clone(),
|
||||
&private_key,
|
||||
Identifier::try_from(command.name.clone())?,
|
||||
&inputs,
|
||||
rng,
|
||||
)
|
||||
.map_err(PackageError::execution_error)?;
|
||||
|
||||
let fee = None;
|
||||
|
||||
@ -286,7 +299,7 @@ fn handle_execute<A: Aleo>(command: Execute, context: Context) -> Result<<Execut
|
||||
_ => println!("\n➡️ Outputs\n"),
|
||||
};
|
||||
for output in response.outputs() {
|
||||
println!("{}", format!(" • {output}"));
|
||||
println!(" • {output}");
|
||||
}
|
||||
println!();
|
||||
|
||||
@ -352,18 +365,9 @@ fn execution_cost_breakdown(name: &String, total_cost: f64, storage_cost: f64, f
|
||||
// Display the cost breakdown in a table.
|
||||
let data = [
|
||||
[name, "Cost (credits)"],
|
||||
[
|
||||
"Transaction Storage",
|
||||
&format!("{:.6}", storage_cost),
|
||||
],
|
||||
[
|
||||
"On-chain Execution",
|
||||
&format!("{:.6}", finalize_cost),
|
||||
],
|
||||
[
|
||||
"Priority Fee",
|
||||
&format!("{:.6}", priority_fee),
|
||||
],
|
||||
["Transaction Storage", &format!("{:.6}", storage_cost)],
|
||||
["On-chain Execution", &format!("{:.6}", finalize_cost)],
|
||||
["Priority Fee", &format!("{:.6}", priority_fee)],
|
||||
["Total", &format!("{:.6}", total_cost)],
|
||||
];
|
||||
let mut out = Vec::new();
|
||||
|
@ -57,15 +57,15 @@ use super::*;
|
||||
use crate::cli::helpers::context::*;
|
||||
use leo_errors::{emitter::Handler, CliError, PackageError, Result};
|
||||
use leo_package::{build::*, outputs::OutputsDirectory, package::*};
|
||||
use snarkvm::prelude::{Address, block::Transaction, Ciphertext, Plaintext, PrivateKey, Record, ViewKey};
|
||||
use snarkvm::prelude::{block::Transaction, Address, Ciphertext, Plaintext, PrivateKey, Record, ViewKey};
|
||||
|
||||
use clap::Parser;
|
||||
use colored::Colorize;
|
||||
use std::str::FromStr;
|
||||
use tracing::span::Span;
|
||||
|
||||
use snarkvm::console::network::Network;
|
||||
use crate::cli::query::QueryCommands;
|
||||
use snarkvm::console::network::Network;
|
||||
|
||||
/// Base trait for the Leo CLI, see methods and their documentation for details.
|
||||
pub trait Command {
|
||||
@ -239,13 +239,19 @@ pub fn parse_record<N: Network>(private_key: &PrivateKey<N>, record: &str) -> Re
|
||||
}
|
||||
}
|
||||
|
||||
fn check_balance<N: Network>(private_key: &PrivateKey<N>, endpoint: &String, network: &String, context: Context, total_cost: u64) -> Result<()> {
|
||||
fn check_balance<N: Network>(
|
||||
private_key: &PrivateKey<N>,
|
||||
endpoint: &str,
|
||||
network: &str,
|
||||
context: Context,
|
||||
total_cost: u64,
|
||||
) -> Result<()> {
|
||||
// Derive the account address.
|
||||
let address = Address::<N>::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: endpoint.clone(),
|
||||
network: network.clone(),
|
||||
endpoint: endpoint.to_string(),
|
||||
network: network.to_string(),
|
||||
command: QueryCommands::Program {
|
||||
command: crate::cli::commands::query::Program {
|
||||
name: "credits".to_string(),
|
||||
@ -259,7 +265,7 @@ fn check_balance<N: Network>(private_key: &PrivateKey<N>, endpoint: &String, net
|
||||
public_balance.truncate(public_balance.len() - 3);
|
||||
// Compare balance.
|
||||
if public_balance.parse::<u64>().unwrap() < total_cost {
|
||||
return Err(PackageError::insufficient_balance(public_balance, total_cost).into());
|
||||
Err(PackageError::insufficient_balance(public_balance, total_cost).into())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user