From 2c7a539d53f7233b47d8d645469f366c58730466 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Thu, 15 Feb 2024 13:29:47 -0500 Subject: [PATCH] chore: update dependencies and refactor test code The changes in this commit update the dependencies and refactor the test code to improve readability and maintainability. chore: remove unused code and options from test script --- .github/workflows/rpc-test.yml | 44 ++++ Cargo.lock | 65 ++++- cmd/crates/soroban-test/Cargo.toml | 6 +- cmd/crates/soroban-test/src/lib.rs | 182 +++++++++++--- cmd/crates/soroban-test/tests/it/config.rs | 44 ++-- .../soroban-test/tests/it/hello_world.rs | 22 -- .../tests/it/integration/custom_types.rs | 8 +- .../tests/it/integration/dotenv.rs | 96 ++++---- .../tests/it/integration/hello_world.rs | 229 ++++++++++-------- .../soroban-test/tests/it/integration/util.rs | 65 +---- .../soroban-test/tests/it/integration/wrap.rs | 178 +++++++------- cmd/crates/soroban-test/tests/it/main.rs | 2 +- cmd/crates/soroban-test/tests/it/util.rs | 19 +- cmd/soroban-cli/src/bin/main.rs | 1 + .../src/commands/contract/deploy/asset.rs | 25 +- .../src/commands/contract/deploy/wasm.rs | 34 ++- .../src/commands/contract/extend.rs | 23 +- .../src/commands/contract/fetch.rs | 26 +- cmd/soroban-cli/src/commands/contract/init.rs | 2 +- .../src/commands/contract/install.rs | 29 ++- .../src/commands/contract/invoke.rs | 72 +++--- cmd/soroban-cli/src/commands/contract/read.rs | 30 ++- .../src/commands/contract/restore.rs | 20 +- cmd/soroban-cli/src/commands/events.rs | 45 ++-- cmd/soroban-cli/src/commands/keys/generate.rs | 10 +- cmd/soroban-cli/src/commands/mod.rs | 11 + cmd/soroban-cli/src/commands/network/mod.rs | 30 ++- docs/soroban-cli-full-docs.md | 3 - 28 files changed, 795 insertions(+), 526 deletions(-) create mode 100644 .github/workflows/rpc-test.yml delete mode 100644 cmd/crates/soroban-test/tests/it/hello_world.rs diff --git a/.github/workflows/rpc-test.yml b/.github/workflows/rpc-test.yml new file mode 100644 index 000000000..cb5cc6fd8 --- /dev/null +++ b/.github/workflows/rpc-test.yml @@ -0,0 +1,44 @@ + +name: test-rpc + +on: + push: + branches: [main, release/**] + pull_request: + +jobs: + test: + name: Test against RPC + runs-on: ubuntu-22.04 + services: + rpc: + image: stellar/quickstart:latest@sha256:742a649d5d9be826dd4b1a378c95b0e1833e1bcb08c3f4b9b9a8cdd03da653e3 + ports: + - 9999:8000 + env: + ENABLE_LOGS: true + NETWORK: local + ENABLE_SOROBAN_RPC: true + LIMITS: unlimited + options: >- + --health-cmd "curl --no-progress-meter --fail-with-body -X POST \"http://localhost:9999/soroban/rpc\" -H 'Content-Type: application/json' -d '{\"jsonrpc\":\"2.0\",\"id\":8675309,\"method\":\"getNetwork\"}' && curl --no-progress-meter \"http://localhost:8000/friendbot\" | grep '\"invalid_field\": \"addr\"'" + --health-interval 10s + --health-timeout 5s + --health-retries 50 + steps: + # - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@v3 + - uses: actions/cache@v3 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - run: cargo build + # - run: cargo build + - run: make build-test-wasms + # - run: cargo test + - run: SOROBAN_PORT=9999 cargo test --features it --package soroban-test --test it -- integration diff --git a/Cargo.lock b/Cargo.lock index 28f8aaf13..3fabeee09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1193,7 +1193,7 @@ dependencies = [ "gix-date", "itoa", "thiserror", - "winnow", + "winnow 0.5.34", ] [[package]] @@ -1275,7 +1275,7 @@ dependencies = [ "smallvec", "thiserror", "unicode-bom", - "winnow", + "winnow 0.5.34", ] [[package]] @@ -1524,7 +1524,7 @@ dependencies = [ "itoa", "smallvec", "thiserror", - "winnow", + "winnow 0.5.34", ] [[package]] @@ -1647,7 +1647,7 @@ dependencies = [ "gix-transport", "maybe-async", "thiserror", - "winnow", + "winnow 0.5.34", ] [[package]] @@ -1680,7 +1680,7 @@ dependencies = [ "gix-validate", "memmap2", "thiserror", - "winnow", + "winnow 0.5.34", ] [[package]] @@ -3316,6 +3316,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -3561,8 +3570,8 @@ dependencies = [ "termcolor_output", "thiserror", "tokio", - "toml", - "toml_edit", + "toml 0.5.11", + "toml_edit 0.21.0", "tracing", "tracing-appender", "tracing-subscriber", @@ -3846,12 +3855,15 @@ dependencies = [ "soroban-cli", "soroban-env-host", "soroban-ledger-snapshot", + "soroban-rpc", "soroban-sdk", "soroban-spec", "soroban-spec-tools 20.3.0", "stellar-strkey 0.0.7", "thiserror", "tokio", + "toml 0.8.10", + "walkdir", "which", ] @@ -4285,11 +4297,26 @@ dependencies = [ "serde", ] +[[package]] +name = "toml" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a9aad4a3066010876e8dcf5a8a06e70a558751117a145c6ce2b82c2e2054290" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.22.5", +] + [[package]] name = "toml_datetime" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -4299,7 +4326,20 @@ checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" dependencies = [ "indexmap 2.1.0", "toml_datetime", - "winnow", + "winnow 0.5.34", +] + +[[package]] +name = "toml_edit" +version = "0.22.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99e68c159e8f5ba8a28c4eb7b0c0c190d77bb479047ca713270048145a9ad28a" +dependencies = [ + "indexmap 2.1.0", + "serde", + "serde_spanned", + "toml_datetime", + "winnow 0.6.0", ] [[package]] @@ -4908,6 +4948,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winnow" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1dbce9e90e5404c5a52ed82b1d13fc8cfbdad85033b6f57546ffd1265f8451" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.50.0" diff --git a/cmd/crates/soroban-test/Cargo.toml b/cmd/crates/soroban-test/Cargo.toml index 786be1b35..07e45fa02 100644 --- a/cmd/crates/soroban-test/Cargo.toml +++ b/cmd/crates/soroban-test/Cargo.toml @@ -25,6 +25,7 @@ stellar-strkey = { workspace = true } soroban-sdk = { workspace = true } sep5 = { workspace = true } soroban-cli = { workspace = true } +soroban-rpc = { workspace = true } thiserror = "1.0.31" sha2 = "0.10.6" @@ -32,11 +33,14 @@ assert_cmd = "2.0.4" assert_fs = "1.0.7" predicates = "2.1.5" fs_extra = "1.3.0" +toml = "0.8.10" + [dev-dependencies] serde_json = "1.0.93" which = { workspace = true } tokio = "1.28.1" +walkdir = "2.4.0" [features] -integration = [] +it = [] diff --git a/cmd/crates/soroban-test/src/lib.rs b/cmd/crates/soroban-test/src/lib.rs index bda6ec420..a0763977d 100644 --- a/cmd/crates/soroban-test/src/lib.rs +++ b/cmd/crates/soroban-test/src/lib.rs @@ -30,13 +30,17 @@ use assert_fs::{fixture::FixtureError, prelude::PathChild, TempDir}; use fs_extra::dir::CopyOptions; use soroban_cli::{ - commands::{config, contract, contract::invoke, global, keys}, - CommandParser, Pwd, + commands::{config, contract::invoke, global, keys, network, NetworkRunnable}, + CommandParser, }; mod wasm; pub use wasm::Wasm; +pub const TEST_ACCOUNT: &str = "test"; + +pub const LOCAL_NETWORK_PASSPHRASE: &str = "Standalone Network ; February 2017"; + #[derive(thiserror::Error, Debug)] pub enum Error { #[error(transparent)] @@ -53,11 +57,16 @@ pub enum Error { /// its own `TempDir` where it will save test-specific configuration. pub struct TestEnv { pub temp_dir: TempDir, + pub rpc_url: String, } impl Default for TestEnv { fn default() -> Self { - Self::new().unwrap() + let temp_dir = TempDir::new().unwrap(); + Self { + temp_dir, + rpc_url: "http://localhost:8889/soroban/rpc".to_string(), + } } } @@ -79,27 +88,68 @@ impl TestEnv { let test_env = TestEnv::default(); f(&test_env); } - pub fn new() -> Result { - let this = TempDir::new().map(|temp_dir| TestEnv { temp_dir })?; - std::env::set_var("XDG_CONFIG_HOME", this.temp_dir.as_os_str()); - this.new_assert_cmd("keys") - .arg("generate") - .arg("test") - .arg("-d") - .arg("--no-fund") - .assert(); - std::env::set_var("SOROBAN_ACCOUNT", "test"); - Ok(this) + + pub fn with_default_network(f: F) { + let test_env = TestEnv::new(); + f(&test_env); + } + + pub fn with_port(host_port: u16) -> TestEnv { + Self::with_rpc_url(&format!("http://localhost:{host_port}/soroban/rpc")) } + pub fn with_rpc_url(rpc_url: &str) -> TestEnv { + let env = TestEnv { + rpc_url: rpc_url.to_string(), + ..Default::default() + }; + env.generate_account("test", None).assert().success(); + env + } + + pub fn new() -> TestEnv { + if let Ok(rpc_url) = std::env::var("SOROBAN_RPC_URL") { + return Self::with_rpc_url(&rpc_url); + } + let host_port = std::env::var("SOROBAN_PORT") + .as_deref() + .ok() + .and_then(|n| n.parse().ok()) + .unwrap_or(8889); + Self::with_port(host_port) + } /// Create a new `assert_cmd::Command` for a given subcommand and set's the current directory /// to be the internal `temp_dir`. pub fn new_assert_cmd(&self, subcommand: &str) -> Command { - let mut this = Command::cargo_bin("soroban").unwrap_or_else(|_| Command::new("soroban")); - this.arg("-q"); - this.arg(subcommand); - this.current_dir(&self.temp_dir); - this + let mut cmd: Command = self.bin(); + cmd.arg(subcommand) + .env("SOROBAN_ACCOUNT", TEST_ACCOUNT) + .env("SOROBAN_RPC_URL", &self.rpc_url) + .env("SOROBAN_NETWORK_PASSPHRASE", LOCAL_NETWORK_PASSPHRASE) + .env("XDG_CONFIG_HOME", self.temp_dir.as_os_str()) + .env("SOROBAN_FEE", "1000000") + .current_dir(&self.temp_dir); + cmd + } + + pub fn bin(&self) -> Command { + Command::cargo_bin("soroban").unwrap_or_else(|_| Command::new("soroban")) + } + + pub fn generate_account(&self, account: &str, seed: Option) -> Command { + let mut cmd = self.new_assert_cmd("keys"); + cmd.arg("generate").arg(account); + if let Some(seed) = seed { + cmd.arg(format!("--seed={seed}")); + } + cmd + } + + pub fn fund_account(&self, account: &str) -> Assert { + self.new_assert_cmd("keys") + .arg("fund") + .arg(account) + .assert() } /// Parses a `&str` into a command and sets the pwd to be the same as the current `TestEnv`. @@ -129,32 +179,77 @@ impl TestEnv { } /// A convenience method for using the invoke command. - pub async fn invoke>(&self, command_str: &[I]) -> Result { - let cmd = contract::invoke::Cmd::parse_arg_vec( - &command_str - .iter() - .map(AsRef::as_ref) - .filter(|s| !s.is_empty()) - .collect::>(), - ) - .unwrap(); - self.invoke_cmd(cmd).await + pub async fn invoke_with_test>( + &self, + command_str: &[I], + ) -> Result { + self.invoke_with(command_str, "test").await + } + + /// A convenience method for using the invoke command. + pub async fn invoke_with>( + &self, + command_str: &[I], + source: &str, + ) -> Result { + let cmd = self.cmd_with_config::(command_str); + self.run_cmd_with(cmd, source).await + } + + /// A convenience method for using the invoke command. + pub fn cmd_with_config, T: CommandParser + NetworkRunnable>( + &self, + command_str: &[I], + ) -> T { + let mut arg = vec![ + "--network=local", + "--rpc-url=http", + "--network-passphrase=AA", + "--source-account=test", + ]; + let input = command_str + .iter() + .map(AsRef::as_ref) + .filter(|s| !s.is_empty()) + .collect::>(); + arg.extend(input); + T::parse_arg_vec(&arg).unwrap() } /// Invoke an already parsed invoke command - pub async fn invoke_cmd(&self, mut cmd: invoke::Cmd) -> Result { - cmd.set_pwd(self.dir()); - cmd.run_against_rpc_server(&global::Args { + pub async fn run_cmd_with( + &self, + cmd: T, + account: &str, + ) -> Result { + let config_dir = Some(self.dir().to_path_buf()); + let config = config::Args { + network: network::Args { + rpc_url: Some(self.rpc_url.clone()), + network_passphrase: Some(LOCAL_NETWORK_PASSPHRASE.to_string()), + network: None, + }, + source_account: account.to_string(), locator: config::locator::Args { global: false, - config_dir: None, + config_dir: config_dir.clone(), }, - filter_logs: Vec::default(), - quiet: false, - verbose: false, - very_verbose: false, - list: false, - }) + hd_path: None, + }; + cmd.run_against_rpc_server( + Some(&global::Args { + locator: config::locator::Args { + global: false, + config_dir, + }, + filter_logs: Vec::default(), + quiet: false, + verbose: false, + very_verbose: false, + list: false, + }), + Some(&config), + ) .await } @@ -181,7 +276,7 @@ impl TestEnv { /// Copy the contents of the current `TestEnv` to another `TestEnv` pub fn fork(&self) -> Result { - let this = TestEnv::new()?; + let this = TestEnv::new(); self.save(&this.temp_dir)?; Ok(this) } @@ -203,6 +298,7 @@ pub fn temp_ledger_file() -> OsString { pub trait AssertExt { fn stdout_as_str(&self) -> String; + fn stderr_as_str(&self) -> String; } impl AssertExt for Assert { @@ -212,6 +308,12 @@ impl AssertExt for Assert { .trim() .to_owned() } + fn stderr_as_str(&self) -> String { + String::from_utf8(self.get_output().stderr.clone()) + .expect("failed to make str") + .trim() + .to_owned() + } } pub trait CommandExt { fn json_arg(&mut self, j: A) -> &mut Self diff --git a/cmd/crates/soroban-test/tests/it/config.rs b/cmd/crates/soroban-test/tests/it/config.rs index 5912b2cf5..b6f1bff08 100644 --- a/cmd/crates/soroban-test/tests/it/config.rs +++ b/cmd/crates/soroban-test/tests/it/config.rs @@ -1,5 +1,5 @@ use assert_fs::TempDir; -use soroban_test::TestEnv; +use soroban_test::{AssertExt, TestEnv}; use std::{fs, path::Path}; use crate::util::{add_key, add_test_id, SecretKind, DEFAULT_SEED_PHRASE}; @@ -7,30 +7,35 @@ use soroban_cli::commands::network; const NETWORK_PASSPHRASE: &str = "Local Sandbox Stellar Network ; September 2022"; +fn ls(sandbox: &TestEnv) -> Vec { + sandbox + .new_assert_cmd("network") + .arg("ls") + .assert() + .stdout_as_str() + .split('\n') + .filter(|s| !s.is_empty()) + .map(ToString::to_string) + .collect::>() +} + #[test] fn set_and_remove_network() { TestEnv::with_default(|sandbox| { add_network(sandbox, "local"); let dir = sandbox.dir().join(".soroban").join("network"); - let read_dir = std::fs::read_dir(dir); - println!("{read_dir:#?}"); - let file = read_dir.unwrap().next().unwrap().unwrap(); + let mut read_dir = std::fs::read_dir(dir).unwrap(); + let file = read_dir.next().unwrap().unwrap(); assert_eq!(file.file_name().to_str().unwrap(), "local.toml"); + let res = ls(sandbox); + assert_eq!(res[0], "local"); + sandbox + .new_assert_cmd("network") + .arg("rm") + .arg("local") + .assert() + .success(); - let res = sandbox.cmd::(""); - let res = res.ls().unwrap(); - assert_eq!(res.len(), 1); - assert_eq!(&res[0], "local"); - - sandbox.cmd::("local").run().unwrap(); - - // sandbox - // .new_assert_cmd("config") - // .arg("network") - // .arg("rm") - // .arg("local") - // .assert() - // .stdout(""); sandbox .new_assert_cmd("network") .arg("ls") @@ -105,7 +110,7 @@ fn set_and_remove_global_network() { #[test] fn multiple_networks() { let sandbox = TestEnv::default(); - let ls = || -> Vec { sandbox.cmd::("").ls().unwrap() }; + let ls = || -> Vec { ls(&sandbox) }; add_network(&sandbox, "local"); println!("{:#?}", ls()); @@ -156,7 +161,6 @@ fn generate_key() { sandbox .new_assert_cmd("keys") .arg("generate") - .arg("--network=futurenet") .arg("--no-fund") .arg("--seed") .arg("0000000000000000") diff --git a/cmd/crates/soroban-test/tests/it/hello_world.rs b/cmd/crates/soroban-test/tests/it/hello_world.rs deleted file mode 100644 index 4c45403a1..000000000 --- a/cmd/crates/soroban-test/tests/it/hello_world.rs +++ /dev/null @@ -1,22 +0,0 @@ -use soroban_cli::commands::contract::{self, fetch}; -use soroban_test::TestEnv; -use std::path::PathBuf; - -use crate::util::{ - add_test_seed, is_rpc, network_passphrase, network_passphrase_arg, rpc_url, rpc_url_arg, - DEFAULT_PUB_KEY, DEFAULT_PUB_KEY_1, DEFAULT_SECRET_KEY, DEFAULT_SEED_PHRASE, HELLO_WORLD, - TEST_SALT, -}; - -#[tokio::test] -async fn fetch() { - if !is_rpc() { - return; - } - let e = TestEnv::default(); - let f = e.dir().join("contract.wasm"); - let id = deploy_hello(&e); - let cmd = e.cmd_arr::(&["--id", &id, "--out-file", f.to_str().unwrap()]); - cmd.run().await.unwrap(); - assert!(f.exists()); -} diff --git a/cmd/crates/soroban-test/tests/it/integration/custom_types.rs b/cmd/crates/soroban-test/tests/it/integration/custom_types.rs index fda2c1f61..28dd06dc0 100644 --- a/cmd/crates/soroban-test/tests/it/integration/custom_types.rs +++ b/cmd/crates/soroban-test/tests/it/integration/custom_types.rs @@ -3,7 +3,7 @@ use serde_json::json; use soroban_cli::commands; use soroban_test::TestEnv; -use crate::integration::util::{deploy_custom, extend_contract, CUSTOM_TYPES}; +use crate::integration::util::{deploy_custom, extend_contract}; use super::util::invoke_with_roundtrip; @@ -15,9 +15,9 @@ fn invoke_custom(e: &TestEnv, id: &str, func: &str) -> assert_cmd::Command { #[tokio::test] async fn parse() { - let sandbox = &TestEnv::default(); + let sandbox = &TestEnv::new(); let id = &deploy_custom(sandbox); - extend_contract(sandbox, id, CUSTOM_TYPES).await; + extend_contract(sandbox, id).await; symbol(sandbox, id); string_with_quotes(sandbox, id).await; symbol_with_quotes(sandbox, id).await; @@ -187,7 +187,7 @@ fn number_arg_return_ok(sandbox: &TestEnv, id: &str) { async fn number_arg_return_err(sandbox: &TestEnv, id: &str) { let res = sandbox - .invoke(&["--id", id, "--", "u32_fail_on_even", "--u32_=2"]) + .invoke_with_test(&["--id", id, "--", "u32_fail_on_even", "--u32_=2"]) .await .unwrap_err(); if let commands::contract::invoke::Error::ContractInvoke(name, doc) = &res { diff --git a/cmd/crates/soroban-test/tests/it/integration/dotenv.rs b/cmd/crates/soroban-test/tests/it/integration/dotenv.rs index 7c0f25b3f..d3ddd140d 100644 --- a/cmd/crates/soroban-test/tests/it/integration/dotenv.rs +++ b/cmd/crates/soroban-test/tests/it/integration/dotenv.rs @@ -1,67 +1,69 @@ use soroban_test::TestEnv; -use super::util::{deploy_hello, TEST_CONTRACT_ID}; +use super::util::deploy_hello; + +const SOROBAN_FEE: &str = "100"; fn write_env_file(e: &TestEnv, contents: &str) { let env_file = e.dir().join(".env"); - std::fs::write(&env_file, contents).unwrap(); + let contents = format!("SOROBAN_CONTRACT_ID={contents}"); + std::fs::write(&env_file, &contents).unwrap(); assert_eq!(contents, std::fs::read_to_string(env_file).unwrap()); } -fn contract_id() -> String { - format!("SOROBAN_CONTRACT_ID={TEST_CONTRACT_ID}") -} - #[test] fn can_read_file() { - TestEnv::with_default(|e| { - deploy_hello(e); - write_env_file(e, &contract_id()); - e.new_assert_cmd("contract") - .arg("invoke") - .arg("--") - .arg("hello") - .arg("--world=world") - .assert() - .stdout("[\"Hello\",\"world\"]\n") - .success(); - }); + let e = &TestEnv::new(); + let id = deploy_hello(e); + write_env_file(e, &id); + e.new_assert_cmd("contract") + .env("SOROBAN_FEE", SOROBAN_FEE) + .arg("invoke") + .arg("--") + .arg("hello") + .arg("--world=world") + .assert() + .stdout("[\"Hello\",\"world\"]\n") + .success(); } #[test] fn current_env_not_overwritten() { - TestEnv::with_default(|e| { - deploy_hello(e); - write_env_file(e, &contract_id()); + let e = TestEnv::new(); + write_env_file(&e, &deploy_hello(&e)); - e.new_assert_cmd("contract") - .env("SOROBAN_CONTRACT_ID", "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4") - .arg("invoke") - .arg("--") - .arg("hello") - .arg("--world=world") - .assert() - .stderr("error: Contract not found: CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4\n"); - }); + e.new_assert_cmd("contract") + .env( + "SOROBAN_CONTRACT_ID", + "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4", + ) + .env("SOROBAN_FEE", SOROBAN_FEE) + .arg("invoke") + .arg("--") + .arg("hello") + .arg("--world=world") + .assert() + .stderr( + "error: Contract not found: CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4\n", + ); } #[test] fn cli_args_have_priority() { - TestEnv::with_default(|e| { - deploy_hello(e); - write_env_file(e, &contract_id()); - e.new_assert_cmd("contract") - .env( - "SOROBAN_CONTRACT_ID", - "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4", - ) - .arg("invoke") - .arg("--id") - .arg(TEST_CONTRACT_ID) - .arg("--") - .arg("hello") - .arg("--world=world") - .assert() - .stdout("[\"Hello\",\"world\"]\n"); - }); + let e = &TestEnv::new(); + let id = deploy_hello(e); + write_env_file(e, &id); + e.new_assert_cmd("contract") + .env( + "SOROBAN_CONTRACT_ID", + "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4", + ) + .arg("invoke") + .arg("--id") + .arg(id) + .arg("--") + .arg("hello") + .arg("--world=world") + .assert() + .stdout("[\"Hello\",\"world\"]\n"); } diff --git a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs index 7bd8d596a..eec1bfa58 100644 --- a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs +++ b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs @@ -1,23 +1,67 @@ use predicates::boolean::PredicateBooleanExt; use soroban_cli::commands::{ + config::{locator, secret}, contract::{self, fetch}, - keys, }; -use soroban_test::TestEnv; +use soroban_test::{AssertExt, TestEnv, LOCAL_NETWORK_PASSPHRASE}; +use std::{thread::sleep, time::Duration}; -use crate::{integration::util::extend_contract, util::DEFAULT_SEED_PHRASE}; +use crate::integration::util::extend_contract; -use super::util::{ - add_test_seed, deploy_hello, extend, network_passphrase, network_passphrase_arg, rpc_url, - rpc_url_arg, DEFAULT_PUB_KEY, DEFAULT_PUB_KEY_1, DEFAULT_SECRET_KEY, HELLO_WORLD, -}; +use super::util::{deploy_hello, extend}; #[tokio::test] -#[ignore] async fn invoke() { - let sandbox = &TestEnv::default(); + let sandbox = &TestEnv::new(); + + sandbox + .new_assert_cmd("keys") + .arg("fund") + .arg("test") + .assert() + .stderr(predicates::str::contains("Account already exists")); + sandbox + .new_assert_cmd("keys") + .arg("fund") + .arg("test") + .arg("--hd-path=1") + .assert(); + let addr = sandbox + .new_assert_cmd("keys") + .arg("address") + .arg("test") + .assert() + .stdout_as_str(); + let addr_1 = sandbox + .new_assert_cmd("keys") + .arg("address") + .arg("test") + .arg("--hd-path=1") + .assert() + .stdout_as_str(); + println!("Addrs {addr}, {addr_1}"); + + let secret_key = sandbox + .new_assert_cmd("keys") + .arg("show") + .arg("test") + .assert() + .stdout_as_str(); + let secret_key_1 = sandbox + .new_assert_cmd("keys") + .arg("show") + .arg("test") + .arg("--hd-path=1") + .assert() + .stdout_as_str(); + let dir = sandbox.dir(); + let seed_phrase = std::fs::read_to_string(dir.join(".soroban/identity/test.toml")).unwrap(); + let s = toml::from_str::(&seed_phrase).unwrap(); + let secret::Secret::SeedPhrase { seed_phrase } = s else { + panic!("Expected seed phrase") + }; let id = &deploy_hello(sandbox); - extend_contract(sandbox, id, HELLO_WORLD).await; + extend_contract(sandbox, id).await; // Note that all functions tested here have no state invoke_hello_world(sandbox, id); sandbox @@ -29,22 +73,40 @@ async fn invoke() { .stdout(predicates::str::contains(id).not()) .success(); invoke_hello_world_with_lib(sandbox, id).await; - sandbox - .new_assert_cmd("events") - .arg("--start-ledger=20") - .arg("--id") - .arg(id) + let config_locator = locator::Args { + global: false, + config_dir: Some(dir.to_path_buf()), + }; + config_locator + .write_identity( + "testone", + &secret::Secret::SecretKey { + secret_key: secret_key_1.clone(), + }, + ) + .unwrap(); + let sk_from_file = std::fs::read_to_string(dir.join(".soroban/identity/testone.toml")).unwrap(); + + assert_eq!(sk_from_file, format!("secret_key = \"{secret_key_1}\"\n")); + let secret_key_1_readin = sandbox + .new_assert_cmd("keys") + .arg("show") + .arg("testone") .assert() - .stdout(predicates::str::contains(id)) - .success(); - invoke_hello_world_with_lib_two(sandbox, id).await; - invoke_auth(sandbox, id); - invoke_auth_with_identity(sandbox, id).await; - invoke_auth_with_different_test_account_fail(sandbox, id).await; + .stdout_as_str(); + assert_eq!(secret_key_1, secret_key_1_readin); + // list all files recursively from dir including in hidden folders + for entry in walkdir::WalkDir::new(dir) { + println!("{}", entry.unwrap().path().display()); + } + invoke_auth(sandbox, id, &addr); + invoke_auth_with_identity(sandbox, id, "test", &addr).await; + invoke_auth_with_identity(sandbox, id, "testone", &addr_1).await; + invoke_auth_with_different_test_account_fail(sandbox, id, &addr_1).await; // invoke_auth_with_different_test_account(sandbox, id); contract_data_read_failure(sandbox, id); - invoke_with_seed(sandbox, id).await; - invoke_with_sk(sandbox, id).await; + invoke_with_seed(sandbox, id, &seed_phrase).await; + invoke_with_sk(sandbox, id, &secret_key).await; // This does add an identity to local config invoke_with_id(sandbox, id).await; handles_kebab_case(sandbox, id).await; @@ -68,48 +130,27 @@ fn invoke_hello_world(sandbox: &TestEnv, id: &str) { } async fn invoke_hello_world_with_lib(e: &TestEnv, id: &str) { - let mut cmd = contract::invoke::Cmd { + let cmd = contract::invoke::Cmd { contract_id: id.to_string(), slop: vec!["hello".into(), "--world=world".into()], ..Default::default() }; - - cmd.config.network.rpc_url = rpc_url(); - cmd.config.network.network_passphrase = network_passphrase(); - - let res = e.invoke_cmd(cmd).await.unwrap(); + let res = e.run_cmd_with(cmd, "test").await.unwrap(); assert_eq!(res, r#"["Hello","world"]"#); } -async fn invoke_hello_world_with_lib_two(e: &TestEnv, id: &str) { - let hello_world = HELLO_WORLD.to_string(); - let mut invoke_args = vec!["--id", id, "--wasm", hello_world.as_str()]; - let args = vec!["--", "hello", "--world=world"]; - let res = - if let (Some(rpc), Some(network_passphrase)) = (rpc_url_arg(), network_passphrase_arg()) { - invoke_args.push(&rpc); - invoke_args.push(&network_passphrase); - e.invoke(&[invoke_args, args].concat()).await.unwrap() - } else { - e.invoke(&[invoke_args, args].concat()).await.unwrap() - }; - assert_eq!(res, r#"["Hello","world"]"#); -} - -fn invoke_auth(sandbox: &TestEnv, id: &str) { +fn invoke_auth(sandbox: &TestEnv, id: &str, addr: &str) { sandbox .new_assert_cmd("contract") .arg("invoke") .arg("--id") .arg(id) - .arg("--wasm") - .arg(HELLO_WORLD.path()) .arg("--") .arg("auth") - .arg(&format!("--addr={DEFAULT_PUB_KEY}")) + .arg("--addr=test") .arg("--world=world") .assert() - .stdout(format!("\"{DEFAULT_PUB_KEY}\"\n")) + .stdout(format!("\"{addr}\"\n")) .success(); // Invoke it again without providing the contract, to exercise the deployment @@ -120,65 +161,40 @@ fn invoke_auth(sandbox: &TestEnv, id: &str) { .arg(id) .arg("--") .arg("auth") - .arg(&format!("--addr={DEFAULT_PUB_KEY}")) + .arg("--addr=test") .arg("--world=world") .assert() - .stdout(format!("\"{DEFAULT_PUB_KEY}\"\n")) + .stdout(format!("\"{addr}\"\n")) .success(); } -async fn invoke_auth_with_identity(sandbox: &TestEnv, id: &str) { - sandbox - .cmd::("test -d ") - .run() - .await - .unwrap(); +async fn invoke_auth_with_identity(sandbox: &TestEnv, id: &str, key: &str, addr: &str) { sandbox .new_assert_cmd("contract") .arg("invoke") + .arg("--source") + .arg(key) .arg("--id") .arg(id) - .arg("--wasm") - .arg(HELLO_WORLD.path()) .arg("--") .arg("auth") .arg("--addr") - .arg(DEFAULT_PUB_KEY) + .arg(key) .arg("--world=world") .assert() - .stdout(format!("\"{DEFAULT_PUB_KEY}\"\n")) + .stdout(format!("\"{addr}\"\n")) .success(); } -// fn invoke_auth_with_different_test_account(sandbox: &TestEnv, id: &str) { -// sandbox -// .new_assert_cmd("contract") -// .arg("invoke") -// .arg("--hd-path=1") -// .arg("--id") -// .arg(id) -// .arg("--wasm") -// .arg(HELLO_WORLD.path()) -// .arg("--") -// .arg("auth") -// .arg(&format!("--addr={DEFAULT_PUB_KEY_1}")) -// .arg("--world=world") -// .assert() -// .stdout(format!("\"{DEFAULT_PUB_KEY_1}\"\n")) -// .success(); -// } - -async fn invoke_auth_with_different_test_account_fail(sandbox: &TestEnv, id: &str) { +async fn invoke_auth_with_different_test_account_fail(sandbox: &TestEnv, id: &str, addr: &str) { let res = sandbox - .invoke(&[ + .invoke_with_test(&[ "--hd-path=0", "--id", id, - &rpc_url_arg().unwrap_or_default(), - &network_passphrase_arg().unwrap_or_default(), "--", "auth", - &format!("--addr={DEFAULT_PUB_KEY_1}"), + &format!("--addr={addr}"), "--world=world", ]) .await; @@ -207,9 +223,12 @@ fn contract_data_read_failure(sandbox: &TestEnv, id: &str) { #[tokio::test] async fn contract_data_read() { const KEY: &str = "COUNTER"; - let sandbox = &TestEnv::default(); + let sandbox = &TestEnv::new(); let id = &deploy_hello(sandbox); - let res = sandbox.invoke(&["--id", id, "--", "inc"]).await.unwrap(); + let res = sandbox + .invoke_with_test(&["--id", id, "--", "inc"]) + .await + .unwrap(); assert_eq!(res.trim(), "1"); extend(sandbox, id, Some(KEY)).await; @@ -248,30 +267,21 @@ async fn contract_data_read() { .stdout(predicates::str::starts_with("COUNTER,2")); } -async fn invoke_with_seed(sandbox: &TestEnv, id: &str) { - invoke_with_source(sandbox, DEFAULT_SEED_PHRASE, id).await; +async fn invoke_with_seed(sandbox: &TestEnv, id: &str, seed_phrase: &str) { + invoke_with_source(sandbox, seed_phrase, id).await; } -async fn invoke_with_sk(sandbox: &TestEnv, id: &str) { - invoke_with_source(sandbox, DEFAULT_SECRET_KEY, id).await; +async fn invoke_with_sk(sandbox: &TestEnv, id: &str, sk: &str) { + invoke_with_source(sandbox, sk, id).await; } async fn invoke_with_id(sandbox: &TestEnv, id: &str) { - let identity = add_test_seed(sandbox.dir()); - invoke_with_source(sandbox, &identity, id).await; + invoke_with_source(sandbox, "test", id).await; } async fn invoke_with_source(sandbox: &TestEnv, source: &str, id: &str) { let cmd = sandbox - .invoke(&[ - "--source-account", - source, - "--id", - id, - "--", - "hello", - "--world=world", - ]) + .invoke_with(&["--id", id, "--", "hello", "--world=world"], source) .await .unwrap(); assert_eq!(cmd, "[\"Hello\",\"world\"]"); @@ -279,25 +289,32 @@ async fn invoke_with_source(sandbox: &TestEnv, source: &str, id: &str) { async fn handles_kebab_case(e: &TestEnv, id: &str) { assert!(e - .invoke(&["--id", id, "--", "multi-word-cmd", "--contract-owner=world",]) + .invoke_with_test(&["--id", id, "--", "multi-word-cmd", "--contract-owner=world",]) .await .is_ok()); } async fn fetch(sandbox: &TestEnv, id: &str) { let f = sandbox.dir().join("contract.wasm"); - let cmd = sandbox.cmd_arr::(&["--id", id, "--out-file", f.to_str().unwrap()]); + let cmd = sandbox.cmd_arr::(&[ + "--rpc-url", + &sandbox.rpc_url, + "--network-passphrase", + LOCAL_NETWORK_PASSPHRASE, + "--id", + id, + "--out-file", + f.to_str().unwrap(), + ]); cmd.run().await.unwrap(); assert!(f.exists()); } async fn invoke_prng_u64_in_range_test(sandbox: &TestEnv, id: &str) { assert!(sandbox - .invoke(&[ + .invoke_with_test(&[ "--id", id, - "--wasm", - HELLO_WORLD.path().to_str().unwrap(), "--", "prng_u64_in_range", "--low=0", diff --git a/cmd/crates/soroban-test/tests/it/integration/util.rs b/cmd/crates/soroban-test/tests/it/integration/util.rs index ea27680b7..996af270c 100644 --- a/cmd/crates/soroban-test/tests/it/integration/util.rs +++ b/cmd/crates/soroban-test/tests/it/integration/util.rs @@ -1,23 +1,9 @@ -use soroban_cli::commands::contract; -use soroban_test::{TestEnv, Wasm}; -use std::{fmt::Display, path::Path}; - -use crate::util::{add_key, SecretKind}; +use soroban_test::{AssertExt, TestEnv, Wasm}; +use std::fmt::Display; pub const HELLO_WORLD: &Wasm = &Wasm::Custom("test-wasms", "test_hello_world"); pub const CUSTOM_TYPES: &Wasm = &Wasm::Custom("test-wasms", "test_custom_types"); -pub fn add_test_seed(dir: &Path) -> String { - let name = "test_seed"; - add_key( - dir, - name, - SecretKind::Seed, - "coral light army gather adapt blossom school alcohol coral light army giggle", - ); - name.to_owned() -} - pub async fn invoke_with_roundtrip(e: &TestEnv, id: &str, func: &str, data: D) where D: Display, @@ -25,34 +11,13 @@ where let data = data.to_string(); println!("{data}"); let res = e - .invoke(&["--id", id, "--", func, &format!("--{func}"), &data]) + .invoke_with_test(&["--id", id, "--", func, &format!("--{func}"), &data]) .await .unwrap(); assert_eq!(res, data); } -pub const DEFAULT_PUB_KEY: &str = "GDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2DYQCHVCR4W4"; -pub const DEFAULT_SECRET_KEY: &str = "SC36BWNUOCZAO7DMEJNNKFV6BOTPJP7IG5PSHLUOLT6DZFRU3D3XGIXW"; - -pub const DEFAULT_PUB_KEY_1: &str = "GCKZUJVUNEFGD4HLFBUNVYM2QY2P5WQQZMGRA3DDL4HYVT5MW5KG3ODV"; pub const TEST_SALT: &str = "f55ff16f66f43360266b95db6f8fec01d76031054306ae4a4b380598f6cfd114"; -pub const TEST_CONTRACT_ID: &str = "CBVTIVBYWAO2HNPNGKDCZW4OZYYESTKNGD7IPRTDGQSFJS4QBDQQJX3T"; - -pub fn rpc_url() -> Option { - std::env::var("SOROBAN_RPC_URL").ok() -} - -pub fn rpc_url_arg() -> Option { - rpc_url().map(|url| format!("--rpc-url={url}")) -} - -pub fn network_passphrase() -> Option { - std::env::var("SOROBAN_NETWORK_PASSPHRASE").ok() -} - -pub fn network_passphrase_arg() -> Option { - network_passphrase().map(|p| format!("--network-passphrase={p}")) -} pub fn deploy_hello(sandbox: &TestEnv) -> String { deploy_contract(sandbox, HELLO_WORLD) @@ -66,6 +31,7 @@ pub fn deploy_contract(sandbox: &TestEnv, wasm: &Wasm) -> String { let hash = wasm.hash().unwrap(); sandbox .new_assert_cmd("contract") + .env("SOROBAN_FEE", "100000") .arg("install") .arg("--wasm") .arg(wasm.path()) @@ -83,22 +49,11 @@ pub fn deploy_contract(sandbox: &TestEnv, wasm: &Wasm) -> String { .arg(TEST_SALT) .arg("--ignore-checks") .assert() - .success() - .stdout(format!("{TEST_CONTRACT_ID}\n")); - TEST_CONTRACT_ID.to_string() + .stdout_as_str() } -pub async fn extend_contract(sandbox: &TestEnv, id: &str, wasm: &Wasm<'_>) { +pub async fn extend_contract(sandbox: &TestEnv, id: &str) { extend(sandbox, id, None).await; - let cmd: contract::extend::Cmd = sandbox.cmd_arr(&[ - "--wasm-hash", - wasm.hash().unwrap().to_string().as_str(), - "--durability", - "persistent", - "--ledgers-to-extend", - "100000", - ]); - cmd.run().await.unwrap(); } pub async fn extend(sandbox: &TestEnv, id: &str, value: Option<&str>) { @@ -114,6 +69,10 @@ pub async fn extend(sandbox: &TestEnv, id: &str, value: Option<&str>) { args.push("--key"); args.push(value); } - let cmd: contract::extend::Cmd = sandbox.cmd_arr(&args); - cmd.run().await.unwrap(); + sandbox + .new_assert_cmd("contract") + .arg("extend") + .args(args) + .assert() + .success(); } diff --git a/cmd/crates/soroban-test/tests/it/integration/wrap.rs b/cmd/crates/soroban-test/tests/it/integration/wrap.rs index a69e70c7c..aa356ce99 100644 --- a/cmd/crates/soroban-test/tests/it/integration/wrap.rs +++ b/cmd/crates/soroban-test/tests/it/integration/wrap.rs @@ -1,97 +1,101 @@ -use soroban_cli::CommandParser; -use soroban_cli::{ - commands::{contract::deploy::asset, keys}, - utils::contract_id_hash_from_asset, -}; -use soroban_test::TestEnv; - -use super::util::network_passphrase; +use soroban_cli::utils::contract_id_hash_from_asset; +use soroban_test::{AssertExt, TestEnv, LOCAL_NETWORK_PASSPHRASE}; #[tokio::test] #[ignore] async fn burn() { - let sandbox = &TestEnv::default(); - let network_passphrase = network_passphrase().unwrap(); - println!("NETWORK_PASSPHRASE: {network_passphrase:?}"); - let address = keys::address::Cmd::parse("test") - .unwrap() - .public_key() - .unwrap(); + let sandbox = &TestEnv::new(); + let network_passphrase = LOCAL_NETWORK_PASSPHRASE.to_string(); + let address = sandbox + .new_assert_cmd("keys") + .arg("address") + .arg("test") + .assert() + .stdout_as_str(); let asset = format!("native:{address}"); - wrap_cmd(&asset).run().await.unwrap(); + sandbox + .new_assert_cmd("contract") + .arg("asset") + .arg("deploy") + .arg("--source=test") + .arg("--asset") + .arg(&asset) + .assert() + .success(); + // wrap_cmd(&asset).run().await.unwrap(); let asset = soroban_cli::utils::parsing::parse_asset(&asset).unwrap(); let hash = contract_id_hash_from_asset(&asset, &network_passphrase).unwrap(); let id = stellar_strkey::Contract(hash.0).to_string(); - assert_eq!( - "CAMTHSPKXZJIRTUXQP5QWJIFH3XIDMKLFAWVQOFOXPTKAW5GKV37ZC4N", - id - ); - assert_eq!( - "true", - sandbox - .invoke(&[ - "--id", - &id, - "--source=test", - "--", - "authorized", - "--id", - &address.to_string() - ]) - .await - .unwrap() - ); - assert_eq!( - "\"9223372036854775807\"", - sandbox - .invoke(&[ - "--id", - &id, - "--source", - "test", - "--", - "balance", - "--id", - &address.to_string() - ]) - .await - .unwrap(), - ); - - println!( - "{}", - sandbox - .invoke(&[ - "--id", - &id, - "--source=test", - "--", - "burn", - "--id", - &address.to_string(), - "--amount=100" - ]) - .await - .unwrap() - ); - - assert_eq!( - "\"9223372036854775707\"", - sandbox - .invoke(&[ - "--id", - &id, - "--source=test", - "--", - "balance", - "--id", - &address.to_string() - ]) - .await - .unwrap(), - ); -} + println!("{id}, {address}"); + sandbox + .new_assert_cmd("contract") + .args([ + "invoke", + "--id", + &id, + "--", + "balance", + "--id", + &address.to_string(), + ]) + .assert() + .stdout("\"9223372036854775807\"\n"); + sandbox + .new_assert_cmd("contract") + .arg("invoke") + .args([ + "--id", + &id, + "--", + "authorized", + "--id", + &address.to_string(), + ]) + .assert() + .stdout("true\n"); + sandbox + .new_assert_cmd("contract") + .args([ + "invoke", + "--id", + &id, + "--", + "balance", + "--id", + &address.to_string(), + ]) + .assert() + .stdout("\"9223372036854775807\"\n"); + sandbox + .new_assert_cmd("contract") + .arg("invoke") + .env("RUST_LOGS", "trace") + .args([ + "--source=test", + "--id", + &id, + "--", + "burn", + "--from", + "test", + "--amount=100", + ]) + .assert() + .stdout("") + .stderr(""); -fn wrap_cmd(asset: &str) -> asset::Cmd { - asset::Cmd::parse_arg_vec(&["--source=test", &format!("--asset={asset}")]).unwrap() + println!("hi"); + sandbox + .new_assert_cmd("contract") + .args([ + "invoke", + "--id", + &id, + "--", + "balance", + "--id", + &address.to_string(), + ]) + .assert() + .stdout("\"9223372036854775707\"\n"); } diff --git a/cmd/crates/soroban-test/tests/it/main.rs b/cmd/crates/soroban-test/tests/it/main.rs index a6b18cb22..10aea449c 100644 --- a/cmd/crates/soroban-test/tests/it/main.rs +++ b/cmd/crates/soroban-test/tests/it/main.rs @@ -1,7 +1,7 @@ mod arg_parsing; mod config; mod help; -#[cfg(feature = "integration")] +#[cfg(feature = "it")] mod integration; mod plugin; mod util; diff --git a/cmd/crates/soroban-test/tests/it/util.rs b/cmd/crates/soroban-test/tests/it/util.rs index 112d5f841..c70091503 100644 --- a/cmd/crates/soroban-test/tests/it/util.rs +++ b/cmd/crates/soroban-test/tests/it/util.rs @@ -4,7 +4,7 @@ use soroban_cli::commands::{ config::{locator::KeyType, secret::Secret}, contract, }; -use soroban_test::{TestEnv, Wasm}; +use soroban_test::{TestEnv, Wasm, TEST_ACCOUNT}; pub const CUSTOM_TYPES: &Wasm = &Wasm::Custom("test-wasms", "test_custom_types"); @@ -44,7 +44,6 @@ pub fn add_test_id(dir: &Path) -> String { pub const DEFAULT_SEED_PHRASE: &str = "coral light army gather adapt blossom school alcohol coral light army giggle"; -#[allow(dead_code)] pub async fn invoke_custom( sandbox: &TestEnv, id: &str, @@ -52,21 +51,9 @@ pub async fn invoke_custom( arg: &str, wasm: &Path, ) -> Result { - let mut i: contract::invoke::Cmd = sandbox.cmd_arr(&[ - "--id", - id, - "--network", - "futurenet", - "--source", - "default", - "--", - func, - arg, - ]); + let mut i: contract::invoke::Cmd = sandbox.cmd_with_config(&["--id", id, "--", func, arg]); i.wasm = Some(wasm.to_path_buf()); - i.config.network.network = Some("futurenet".to_owned()); - i.invoke(&soroban_cli::commands::global::Args::default()) - .await + sandbox.run_cmd_with(i, TEST_ACCOUNT).await } pub const DEFAULT_CONTRACT_ID: &str = "CDR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OO5Z"; diff --git a/cmd/soroban-cli/src/bin/main.rs b/cmd/soroban-cli/src/bin/main.rs index 7a87099c0..70eeb6110 100644 --- a/cmd/soroban-cli/src/bin/main.rs +++ b/cmd/soroban-cli/src/bin/main.rs @@ -37,6 +37,7 @@ async fn main() { let builder = fmt::Subscriber::builder() .with_env_filter(e_filter) + .with_ansi(false) .with_writer(std::io::stderr); let subscriber = builder.finish(); diff --git a/cmd/soroban-cli/src/commands/contract/deploy/asset.rs b/cmd/soroban-cli/src/commands/contract/deploy/asset.rs index c10bf8164..170f90570 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/asset.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/asset.rs @@ -12,7 +12,7 @@ use std::convert::Infallible; use std::{array::TryFromSliceError, fmt::Debug, num::ParseIntError}; use crate::{ - commands::config, + commands::{config, global, NetworkRunnable}, rpc::{Client, Error as SorobanRpcError}, utils::{contract_id_hash_from_asset, parsing::parse_asset}, }; @@ -58,21 +58,30 @@ pub struct Cmd { impl Cmd { pub async fn run(&self) -> Result<(), Error> { - // Parse asset - let asset = parse_asset(&self.asset)?; - - let res_str = self.run_against_rpc_server(asset).await?; + let res_str = self.run_against_rpc_server(None, None).await?; println!("{res_str}"); Ok(()) } +} +impl NetworkRunnable for Cmd { + type Error = Error; + type Result = String; + + async fn run_against_rpc_server( + &self, + _: Option<&global::Args>, + config: Option<&config::Args>, + ) -> Result { + let config = config.unwrap_or(&self.config); + // Parse asset + let asset = parse_asset(&self.asset)?; - async fn run_against_rpc_server(&self, asset: Asset) -> Result { - let network = self.config.get_network()?; + let network = config.get_network()?; let client = Client::new(&network.rpc_url)?; client .verify_network_passphrase(Some(&network.network_passphrase)) .await?; - let key = self.config.key_pair()?; + let key = config.key_pair()?; // Get the account sequence number let public_strkey = diff --git a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs index 76c130173..7bc0e9986 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs @@ -14,7 +14,10 @@ use soroban_env_host::{ HostError, }; -use crate::commands::contract::{self, id::wasm::get_contract_id}; +use crate::commands::{ + contract::{self, id::wasm::get_contract_id}, + global, NetworkRunnable, +}; use crate::{ commands::{config, contract::install, HEADING_RPC}, rpc::{self, Client}, @@ -92,20 +95,30 @@ pub enum Error { impl Cmd { pub async fn run(&self) -> Result<(), Error> { - let res_str = self.run_and_get_contract_id().await?; + let res_str = self.run_against_rpc_server(None, None).await?; println!("{res_str}"); Ok(()) } +} + +impl NetworkRunnable for Cmd { + type Error = Error; + type Result = String; - pub async fn run_and_get_contract_id(&self) -> Result { + async fn run_against_rpc_server( + &self, + global_args: Option<&global::Args>, + config: Option<&config::Args>, + ) -> Result { + let config = config.unwrap_or(&self.config); let wasm_hash = if let Some(wasm) = &self.wasm { let hash = install::Cmd { wasm: wasm::Args { wasm: wasm.clone() }, - config: self.config.clone(), + config: config.clone(), fee: self.fee.clone(), ignore_checks: self.ignore_checks, } - .run_and_get_hash() + .run_against_rpc_server(global_args, Some(config)) .await?; hex::encode(hash) } else { @@ -115,18 +128,13 @@ impl Cmd { .to_string() }; - let hash = Hash(utils::contract_id_from_str(&wasm_hash).map_err(|e| { + let wasm_hash = Hash(utils::contract_id_from_str(&wasm_hash).map_err(|e| { Error::CannotParseWasmHash { wasm_hash: wasm_hash.clone(), error: e, } })?); - - self.run_against_rpc_server(hash).await - } - - async fn run_against_rpc_server(&self, wasm_hash: Hash) -> Result { - let network = self.config.get_network()?; + let network = config.get_network()?; let salt: [u8; 32] = match &self.salt { Some(h) => soroban_spec_tools::utils::padded_hex_from_str(h, 32) .map_err(|_| Error::CannotParseSalt { salt: h.clone() })? @@ -139,7 +147,7 @@ impl Cmd { client .verify_network_passphrase(Some(&network.network_passphrase)) .await?; - let key = self.config.key_pair()?; + let key = config.key_pair()?; // Get the account sequence number let public_strkey = diff --git a/cmd/soroban-cli/src/commands/contract/extend.rs b/cmd/soroban-cli/src/commands/contract/extend.rs index 86ea23b55..5a5cef4c6 100644 --- a/cmd/soroban-cli/src/commands/contract/extend.rs +++ b/cmd/soroban-cli/src/commands/contract/extend.rs @@ -9,7 +9,7 @@ use soroban_env_host::xdr::{ }; use crate::{ - commands::config, + commands::{config, global, NetworkRunnable}, key, rpc::{self, Client}, wasm, Pwd, @@ -80,7 +80,7 @@ pub enum Error { impl Cmd { #[allow(clippy::too_many_lines)] pub async fn run(&self) -> Result<(), Error> { - let ttl_ledger = self.run_against_rpc_server().await?; + let ttl_ledger = self.run_against_rpc_server(None, None).await?; if self.ttl_ledger_only { println!("{ttl_ledger}"); } else { @@ -99,14 +99,23 @@ impl Cmd { } res } - - async fn run_against_rpc_server(&self) -> Result { - let network = self.config.get_network()?; +} +impl NetworkRunnable for Cmd { + type Error = Error; + type Result = u32; + + async fn run_against_rpc_server( + &self, + _args: Option<&global::Args>, + config: Option<&config::Args>, + ) -> Result { + let config = config.unwrap_or(&self.config); + let network = config.get_network()?; tracing::trace!(?network); let keys = self.key.parse_keys()?; - let network = &self.config.get_network()?; + let network = &config.get_network()?; let client = Client::new(&network.rpc_url)?; - let key = self.config.key_pair()?; + let key = config.key_pair()?; let extend_to = self.ledgers_to_extend(); // Get the account sequence number diff --git a/cmd/soroban-cli/src/commands/contract/fetch.rs b/cmd/soroban-cli/src/commands/contract/fetch.rs index 61a82fc47..9d4a876c1 100644 --- a/cmd/soroban-cli/src/commands/contract/fetch.rs +++ b/cmd/soroban-cli/src/commands/contract/fetch.rs @@ -21,6 +21,7 @@ use stellar_strkey::DecodeError; use super::super::config::{self, locator}; use crate::commands::network::{self, Network}; +use crate::commands::{global, NetworkRunnable}; use crate::{ rpc::{self, Client}, utils, Pwd, @@ -115,15 +116,28 @@ impl Cmd { } pub async fn get_bytes(&self) -> Result, Error> { - self.run_against_rpc_server().await + self.run_against_rpc_server(None, None).await } pub fn network(&self) -> Result { Ok(self.network.get(&self.locator)?) } - pub async fn run_against_rpc_server(&self) -> Result, Error> { - let network = self.network()?; + fn contract_id(&self) -> Result<[u8; 32], Error> { + utils::contract_id_from_str(&self.contract_id) + .map_err(|e| Error::CannotParseContractId(self.contract_id.clone(), e)) + } +} + +impl NetworkRunnable for Cmd { + type Error = Error; + type Result = Vec; + async fn run_against_rpc_server( + &self, + _args: Option<&global::Args>, + config: Option<&config::Args>, + ) -> Result, Error> { + let network = config.map_or_else(|| self.network(), |c| Ok(c.get_network()?))?; tracing::trace!(?network); let contract_id = self.contract_id()?; let client = Client::new(&network.rpc_url)?; @@ -133,13 +147,7 @@ impl Cmd { // async closures are not yet stable Ok(client.get_remote_wasm(&contract_id).await?) } - - fn contract_id(&self) -> Result<[u8; 32], Error> { - utils::contract_id_from_str(&self.contract_id) - .map_err(|e| Error::CannotParseContractId(self.contract_id.clone(), e)) - } } - pub fn get_contract_wasm_from_storage( storage: &mut Storage, contract_id: [u8; 32], diff --git a/cmd/soroban-cli/src/commands/contract/init.rs b/cmd/soroban-cli/src/commands/contract/init.rs index fe88dbb68..8ce61c03a 100644 --- a/cmd/soroban-cli/src/commands/contract/init.rs +++ b/cmd/soroban-cli/src/commands/contract/init.rs @@ -82,7 +82,7 @@ fn get_valid_examples() -> Result, Error> { let body: ReqBody = get(GITHUB_API_URL) .call() .map_err(|e| { - eprintln!("Error fetching example contracts from soroban-examples repo"); + tracing::warn!("Error fetching example contracts from soroban-examples repo"); Box::new(e) })? .into_json()?; diff --git a/cmd/soroban-cli/src/commands/contract/install.rs b/cmd/soroban-cli/src/commands/contract/install.rs index c2fa6d889..8e15f201e 100644 --- a/cmd/soroban-cli/src/commands/contract/install.rs +++ b/cmd/soroban-cli/src/commands/contract/install.rs @@ -10,6 +10,7 @@ use soroban_env_host::xdr::{ }; use super::restore; +use crate::commands::{global, NetworkRunnable}; use crate::key; use crate::rpc::{self, Client}; use crate::{commands::config, utils, wasm}; @@ -65,17 +66,23 @@ pub enum Error { impl Cmd { pub async fn run(&self) -> Result<(), Error> { - let res_str = hex::encode(self.run_and_get_hash().await?); + let res_str = hex::encode(self.run_against_rpc_server(None, None).await?); println!("{res_str}"); Ok(()) } +} - pub async fn run_and_get_hash(&self) -> Result { - self.run_against_rpc_server(&self.wasm.read()?).await - } - - async fn run_against_rpc_server(&self, contract: &[u8]) -> Result { - let network = self.config.get_network()?; +impl NetworkRunnable for Cmd { + type Error = Error; + type Result = Hash; + async fn run_against_rpc_server( + &self, + args: Option<&global::Args>, + config: Option<&config::Args>, + ) -> Result { + let config = config.unwrap_or(&self.config); + let contract = self.wasm.read()?; + let network = config.get_network()?; let client = Client::new(&network.rpc_url)?; client .verify_network_passphrase(Some(&network.network_passphrase)) @@ -100,7 +107,7 @@ impl Cmd { tracing::warn!("the deployed smart contract {path} was built with Soroban Rust SDK v{rs_sdk_ver}, a release candidate version not intended for use with the Stellar Public Network", path = self.wasm.wasm.display()); } } - let key = self.config.key_pair()?; + let key = config.key_pair()?; // Get the account sequence number let public_strkey = @@ -109,7 +116,7 @@ impl Cmd { let sequence: i64 = account_details.seq_num.into(); let (tx_without_preflight, hash) = - build_install_contract_code_tx(contract, sequence + 1, self.fee.fee, &key)?; + build_install_contract_code_tx(&contract, sequence + 1, self.fee.fee, &key)?; // Currently internal errors are not returned if the contract code is expired if let Some(TransactionResult { @@ -138,12 +145,12 @@ impl Cmd { wasm_hash: None, durability: super::Durability::Persistent, }, - config: self.config.clone(), + config: config.clone(), fee: self.fee.clone(), ledgers_to_extend: None, ttl_ledger_only: true, } - .run_against_rpc_server() + .run_against_rpc_server(args, None) .await?; } diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 720c3c6f8..6e797b75f 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -28,6 +28,7 @@ use super::super::{ config::{self, locator}, events, }; +use crate::commands::NetworkRunnable; use crate::{commands::global, rpc, Pwd}; use soroban_spec_tools::{contract, Spec}; @@ -261,14 +262,44 @@ impl Cmd { } pub async fn invoke(&self, global_args: &global::Args) -> Result { - self.run_against_rpc_server(global_args).await + self.run_against_rpc_server(Some(global_args), None).await } - pub async fn run_against_rpc_server( + pub fn read_wasm(&self) -> Result>, Error> { + Ok(if let Some(wasm) = self.wasm.as_ref() { + Some(fs::read(wasm).map_err(|e| Error::CannotReadContractFile(wasm.clone(), e))?) + } else { + None + }) + } + + pub fn spec_entries(&self) -> Result>, Error> { + self.read_wasm()? + .map(|wasm| { + soroban_spec::read::from_wasm(&wasm).map_err(Error::CannotParseContractSpec) + }) + .transpose() + } +} + +impl Cmd { + fn contract_id(&self) -> Result<[u8; 32], Error> { + soroban_spec_tools::utils::contract_id_from_str(&self.contract_id) + .map_err(|e| Error::CannotParseContractId(self.contract_id.clone(), e)) + } +} + +impl NetworkRunnable for Cmd { + type Error = Error; + type Result = String; + + async fn run_against_rpc_server( &self, - global_args: &global::Args, + global_args: Option<&global::Args>, + config: Option<&config::Args>, ) -> Result { - let network = self.config.get_network()?; + let config = config.unwrap_or(&self.config); + let network = config.get_network()?; tracing::trace!(?network); let contract_id = self.contract_id()?; let spec_entries = self.spec_entries()?; @@ -280,7 +311,7 @@ impl Cmd { client .verify_network_passphrase(Some(&network.network_passphrase)) .await?; - let key = self.config.key_pair()?; + let key = config.key_pair()?; // Get the account sequence number let public_strkey = @@ -310,6 +341,11 @@ impl Cmd { txn.sim_response().events()?, ) } else { + let global::Args { + verbose, + very_verbose, + .. + } = global_args.map(Clone::clone).unwrap_or_default(); let res = client .send_assembled_transaction( txn, @@ -317,8 +353,7 @@ impl Cmd { &signers, &network.network_passphrase, Some(log_events), - (global_args.verbose || global_args.very_verbose || self.cost) - .then_some(log_resources), + (verbose || very_verbose || self.cost).then_some(log_resources), ) .await?; (res.return_value()?, res.contract_events()?) @@ -327,29 +362,6 @@ impl Cmd { crate::log::diagnostic_events(&events, tracing::Level::INFO); output_to_string(&spec, &return_value, &function) } - - pub fn read_wasm(&self) -> Result>, Error> { - Ok(if let Some(wasm) = self.wasm.as_ref() { - Some(fs::read(wasm).map_err(|e| Error::CannotReadContractFile(wasm.clone(), e))?) - } else { - None - }) - } - - pub fn spec_entries(&self) -> Result>, Error> { - self.read_wasm()? - .map(|wasm| { - soroban_spec::read::from_wasm(&wasm).map_err(Error::CannotParseContractSpec) - }) - .transpose() - } -} - -impl Cmd { - fn contract_id(&self) -> Result<[u8; 32], Error> { - soroban_spec_tools::utils::contract_id_from_str(&self.contract_id) - .map_err(|e| Error::CannotParseContractId(self.contract_id.clone(), e)) - } } fn log_events( diff --git a/cmd/soroban-cli/src/commands/contract/read.rs b/cmd/soroban-cli/src/commands/contract/read.rs index f25b6c2c0..72195fee3 100644 --- a/cmd/soroban-cli/src/commands/contract/read.rs +++ b/cmd/soroban-cli/src/commands/contract/read.rs @@ -14,7 +14,7 @@ use soroban_env_host::{ use soroban_sdk::xdr::Limits; use crate::{ - commands::config, + commands::{config, global, NetworkRunnable}, key, rpc::{self, Client, FullLedgerEntries, FullLedgerEntry}, }; @@ -91,19 +91,10 @@ pub enum Error { impl Cmd { pub async fn run(&self) -> Result<(), Error> { - let entries = self.run_against_rpc_server().await?; + let entries = self.run_against_rpc_server(None, None).await?; self.output_entries(&entries) } - async fn run_against_rpc_server(&self) -> Result { - let network = self.config.get_network()?; - tracing::trace!(?network); - let network = &self.config.get_network()?; - let client = Client::new(&network.rpc_url)?; - let keys = self.key.parse_keys()?; - Ok(client.get_full_ledger_entries(&keys).await?) - } - fn output_entries(&self, entries: &FullLedgerEntries) -> Result<(), Error> { if entries.entries.is_empty() { return Err(Error::NoContractDataEntryFoundForContractID); @@ -178,3 +169,20 @@ impl Cmd { Ok(()) } } + +impl NetworkRunnable for Cmd { + type Error = Error; + type Result = FullLedgerEntries; + async fn run_against_rpc_server( + &self, + _: Option<&global::Args>, + config: Option<&config::Args>, + ) -> Result { + let config = config.unwrap_or(&self.config); + let network = config.get_network()?; + tracing::trace!(?network); + let client = Client::new(&network.rpc_url)?; + let keys = self.key.parse_keys()?; + Ok(client.get_full_ledger_entries(&keys).await?) + } +} diff --git a/cmd/soroban-cli/src/commands/contract/restore.rs b/cmd/soroban-cli/src/commands/contract/restore.rs index 6ed39f892..720ee221d 100644 --- a/cmd/soroban-cli/src/commands/contract/restore.rs +++ b/cmd/soroban-cli/src/commands/contract/restore.rs @@ -13,6 +13,7 @@ use crate::{ commands::{ config::{self, locator}, contract::extend, + global, NetworkRunnable, }, key, rpc::{self, Client}, @@ -87,7 +88,7 @@ pub enum Error { impl Cmd { #[allow(clippy::too_many_lines)] pub async fn run(&self) -> Result<(), Error> { - let expiration_ledger_seq = self.run_against_rpc_server().await?; + let expiration_ledger_seq = self.run_against_rpc_server(None, None).await?; if let Some(ledgers_to_extend) = self.ledgers_to_extend { extend::Cmd { @@ -105,14 +106,23 @@ impl Cmd { Ok(()) } +} - pub async fn run_against_rpc_server(&self) -> Result { - let network = self.config.get_network()?; +impl NetworkRunnable for Cmd { + type Error = Error; + type Result = u32; + + async fn run_against_rpc_server( + &self, + _: Option<&global::Args>, + config: Option<&config::Args>, + ) -> Result { + let config = config.unwrap_or(&self.config); + let network = config.get_network()?; tracing::trace!(?network); let entry_keys = self.key.parse_keys()?; - let network = &self.config.get_network()?; let client = Client::new(&network.rpc_url)?; - let key = self.config.key_pair()?; + let key = config.key_pair()?; // Get the account sequence number let public_strkey = diff --git a/cmd/soroban-cli/src/commands/events.rs b/cmd/soroban-cli/src/commands/events.rs index aa46bbe23..12ac05010 100644 --- a/cmd/soroban-cli/src/commands/events.rs +++ b/cmd/soroban-cli/src/commands/events.rs @@ -3,7 +3,10 @@ use std::io; use soroban_env_host::xdr::{self, Limits, ReadXdr}; -use super::{config::locator, network}; +use super::{ + config::{self, locator}, + global, network, NetworkRunnable, +}; use crate::{rpc, utils}; #[derive(Parser, Debug, Clone)] @@ -119,6 +122,8 @@ pub enum Error { Network(#[from] network::Error), #[error(transparent)] Locator(#[from] locator::Error), + #[error(transparent)] + Config(#[from] config::Error), } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, clap::ValueEnum)] @@ -162,7 +167,7 @@ impl Cmd { })?; } - let response = self.run_against_rpc_server().await?; + let response = self.run_against_rpc_server(None, None).await?; for event in &response.events { match self.output { @@ -189,9 +194,31 @@ impl Cmd { Ok(()) } - async fn run_against_rpc_server(&self) -> Result { + fn start(&self) -> Result { + let start = match (self.start_ledger, self.cursor.clone()) { + (Some(start), _) => rpc::EventStart::Ledger(start), + (_, Some(c)) => rpc::EventStart::Cursor(c), + // should never happen because of required_unless_present flags + _ => return Err(Error::MissingStartLedgerAndCursor), + }; + Ok(start) + } +} +impl NetworkRunnable for Cmd { + type Error = Error; + type Result = rpc::GetEventsResponse; + + async fn run_against_rpc_server( + &self, + _args: Option<&global::Args>, + config: Option<&config::Args>, + ) -> Result { let start = self.start()?; - let network = self.network.get(&self.locator)?; + let network = if let Some(config) = config { + Ok(config.get_network()?) + } else { + self.network.get(&self.locator) + }?; let client = rpc::Client::new(&network.rpc_url)?; client @@ -208,14 +235,4 @@ impl Cmd { .await .map_err(Error::Rpc) } - - fn start(&self) -> Result { - let start = match (self.start_ledger, self.cursor.clone()) { - (Some(start), _) => rpc::EventStart::Ledger(start), - (_, Some(c)) => rpc::EventStart::Cursor(c), - // should never happen because of required_unless_present flags - _ => return Err(Error::MissingStartLedgerAndCursor), - }; - Ok(start) - } } diff --git a/cmd/soroban-cli/src/commands/keys/generate.rs b/cmd/soroban-cli/src/commands/keys/generate.rs index 07782b216..159191dc3 100644 --- a/cmd/soroban-cli/src/commands/keys/generate.rs +++ b/cmd/soroban-cli/src/commands/keys/generate.rs @@ -66,9 +66,13 @@ impl Cmd { if !self.no_fund { let addr = secret.public_key(self.hd_path)?; let network = self.network.get(&self.config_locator)?; - network.fund_address(&addr).await.unwrap_or_else(|_| { - tracing::warn!("Failed to fund address: {addr} on at {}", network.rpc_url); - }); + network + .fund_address(&addr) + .await + .map_err(|e| { + tracing::warn!("fund_address failed: {e}"); + }) + .unwrap_or_default(); } Ok(()) } diff --git a/cmd/soroban-cli/src/commands/mod.rs b/cmd/soroban-cli/src/commands/mod.rs index 952869af3..bb84dbf3b 100644 --- a/cmd/soroban-cli/src/commands/mod.rs +++ b/cmd/soroban-cli/src/commands/mod.rs @@ -158,3 +158,14 @@ pub enum Error { #[error(transparent)] Network(#[from] network::Error), } + +pub trait NetworkRunnable { + type Error; + type Result; + + fn run_against_rpc_server( + &self, + global_args: Option<&global::Args>, + config: Option<&config::Args>, + ) -> impl std::future::Future> + Send; +} diff --git a/cmd/soroban-cli/src/commands/network/mod.rs b/cmd/soroban-cli/src/commands/network/mod.rs index 22cba1904..a1c021722 100644 --- a/cmd/soroban-cli/src/commands/network/mod.rs +++ b/cmd/soroban-cli/src/commands/network/mod.rs @@ -12,6 +12,8 @@ use crate::{ use super::config::locator; +pub const LOCAL_NETWORK_PASSPHRASE: &str = "Standalone Network ; February 2017"; + pub mod add; pub mod ls; pub mod rm; @@ -43,6 +45,8 @@ pub enum Error { #[error("network arg or rpc url and network passphrase are required if using the network")] Network, #[error(transparent)] + Http(#[from] http::Error), + #[error(transparent)] Rpc(#[from] rpc::Error), #[error(transparent)] Hyper(#[from] hyper::Error), @@ -139,13 +143,27 @@ pub struct Network { impl Network { pub async fn helper_url(&self, addr: &str) -> Result { + use http::Uri; tracing::debug!("address {addr:?}"); - let client = Client::new(&self.rpc_url)?; - let helper_url_root = client.friendbot_url().await?; - let uri = http::Uri::from_str(&helper_url_root) - .map_err(|_| Error::InvalidUrl(helper_url_root.to_string()))?; - http::Uri::from_str(&format!("{uri:?}?addr={addr}")) - .map_err(|_| Error::InvalidUrl(helper_url_root.to_string())) + let rpc_uri = Uri::from_str(&self.rpc_url) + .map_err(|_| Error::InvalidUrl(self.rpc_url.to_string()))?; + if self.network_passphrase.as_str() == LOCAL_NETWORK_PASSPHRASE { + let auth = rpc_uri.authority().unwrap().clone(); + let scheme = rpc_uri.scheme_str().unwrap(); + // format!("{scheme}://{auth}/friendbot"); + Ok(Uri::builder() + .authority(auth) + .scheme(scheme) + .path_and_query(format!("/friendbot?addr={addr}")) + .build()?) + } else { + let client = Client::new(&self.rpc_url)?; + let uri = client.friendbot_url().await?; + Uri::from_str(&format!("{uri:?}?addr={addr}")).map_err(|e| { + tracing::error!("{e}"); + Error::InvalidUrl(uri.to_string()) + }) + } } #[allow(clippy::similar_names)] diff --git a/docs/soroban-cli-full-docs.md b/docs/soroban-cli-full-docs.md index 13700aed1..28a83fe32 100644 --- a/docs/soroban-cli-full-docs.md +++ b/docs/soroban-cli-full-docs.md @@ -683,9 +683,6 @@ Initialize a Soroban project with an example contract ###### **Options:** * `-w`, `--with-example ` - - Possible values: `account`, `alloc`, `atomic_multiswap`, `atomic_swap`, `auth`, `cross_contract`, `custom_types`, `deep_contract_auth`, `deployer`, `errors`, `events`, `fuzzing`, `increment`, `liquidity_pool`, `logging`, `mint-lock`, `simple_account`, `single_offer`, `timelock`, `token`, `upgradeable_contract`, `workspace` - * `-f`, `--frontend-template ` Default value: ``