Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifropc committed Feb 5, 2025
1 parent 722a07f commit 6e48749
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 57 deletions.
4 changes: 2 additions & 2 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ fn ls(sandbox: &TestEnv) -> Vec<String> {
.collect::<Vec<_>>()
}

pub const NETWORKS: &str = r#"local
pub const NETWORKS: &str = r"local
futurenet
mainnet
testnet
"#;
";

#[test]
fn set_and_remove_network() {
Expand Down
17 changes: 17 additions & 0 deletions cmd/soroban-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ use clap::CommandFactory;
use dotenvy::dotenv;
use tracing_subscriber::{fmt, EnvFilter};

use crate::commands::contract::arg_parsing::Error::HelpMessage;
use crate::commands::contract::deploy::wasm::Error::ArgParse;
use crate::commands::contract::invoke::Error::ArgParsing;
use crate::commands::contract::Error::{Deploy, Invoke};
use crate::commands::Error::Contract;
use crate::config::Config;
use crate::print::Print;
use crate::upgrade_check::upgrade_check;
use crate::{commands, Root};
use std::error::Error;

#[tokio::main]
pub async fn main() {
Expand Down Expand Up @@ -86,6 +92,17 @@ pub async fn main() {

let printer = Print::new(root.global_args.quiet);
if let Err(e) = root.run().await {
// TODO: source is None (should be HelpMessage)
let _source = commands::Error::source(&e);
// TODO use source instead
if let Contract(Invoke(ArgParsing(HelpMessage(help)))) = e {
println!("{help}");
std::process::exit(1);
}
if let Contract(Deploy(ArgParse(HelpMessage(help)))) = e {
println!("{help}");
std::process::exit(1);
}
printer.errorln(format!("error: {e}"));
std::process::exit(1);
}
Expand Down
78 changes: 37 additions & 41 deletions cmd/soroban-cli/src/commands/contract/deploy/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ use crate::xdr::{
};
use clap::{arg, command, Parser};
use rand::Rng;

use soroban_spec_tools::contract as contract_spec;

use crate::commands::contract::arg_parsing::Error::HelpMessage;
use crate::commands::contract::deploy::wasm::Error::ArgParse;
use crate::{
assembled::simulate_and_assemble_transaction,
commands::{
Expand Down Expand Up @@ -129,42 +128,37 @@ pub enum Error {

impl Cmd {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let res = self.run_against_rpc_server(Some(global_args), None).await;
let res = self
.run_against_rpc_server(Some(global_args), None)
.await?
.to_envelope();
match res {
Ok(res) => match res.to_envelope() {
TxnEnvelopeResult::TxnEnvelope(tx) => {
println!("{}", tx.to_xdr_base64(Limits::none())?);
}
TxnEnvelopeResult::Res(contract) => {
let network = self.config.get_network()?;

if let Some(alias) = self.alias.clone() {
if let Some(existing_contract) = self
.config
.locator
.get_contract_id(&alias, &network.network_passphrase)?
{
let print = Print::new(global_args.quiet);
print.warnln(format!(
"Overwriting existing contract id: {existing_contract}"
));
};
self.config.locator.save_contract_id(
&network.network_passphrase,
&contract,
&alias,
)?;
}

println!("{contract}");
TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?),
TxnEnvelopeResult::Res(contract) => {
let network = self.config.get_network()?;

if let Some(alias) = self.alias.clone() {
if let Some(existing_contract) = self
.config
.locator
.get_contract_id(&alias, &network.network_passphrase)?
{
let print = Print::new(global_args.quiet);
print.warnln(format!(
"Overwriting existing contract id: {existing_contract}"
));
};

self.config.locator.save_contract_id(
&network.network_passphrase,
&contract,
&alias,
)?;
}
},
Err(ArgParse(HelpMessage(help))) => {
println!("{help}");

println!("{contract}");
}
Err(e) => return Err(e),
}

Ok(())
}
}
Expand Down Expand Up @@ -253,13 +247,15 @@ impl NetworkRunnable for Cmd {
} else {
let mut slop = vec![OsString::from(CONSTRUCTOR_FUNCTION_NAME)];
slop.extend_from_slice(&self.slop);
let params = arg_parsing::build_host_function_parameters(
&stellar_strkey::Contract(contract_id.0),
&slop,
&entries,
config,
)?;
Some(params.2)
Some(
arg_parsing::build_host_function_parameters(
&stellar_strkey::Contract(contract_id.0),
&slop,
&entries,
config,
)?
.2,
)
}
} else {
None
Expand Down
18 changes: 4 additions & 14 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use soroban_spec::read::FromWasmError;
use super::super::events;
use super::arg_parsing;
use crate::assembled::Assembled;
use crate::commands::contract::arg_parsing::Error::HelpMessage;
use crate::commands::contract::invoke::Error::ArgParsing;
use crate::{
assembled::simulate_and_assemble_transaction,
commands::{
Expand Down Expand Up @@ -133,20 +131,12 @@ impl From<Infallible> for Error {

impl Cmd {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let res = self.invoke(global_args).await;
let res = self.invoke(global_args).await?.to_envelope();
match res {
Ok(res) => match res.to_envelope() {
TxnEnvelopeResult::TxnEnvelope(tx) => {
println!("{}", tx.to_xdr_base64(Limits::none())?);
}
TxnEnvelopeResult::Res(output) => {
println!("{output}");
}
},
Err(ArgParsing(HelpMessage(help))) => {
println!("{help}");
TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?),
TxnEnvelopeResult::Res(output) => {
println!("{output}");
}
Err(e) => return Err(e),
}
Ok(())
}
Expand Down

0 comments on commit 6e48749

Please sign in to comment.