From 2016aeb7745fa22ff442684a3c53d6b6e3e5526a Mon Sep 17 00:00:00 2001 From: Nando Vieira <me@fnando.com> Date: Mon, 24 Jun 2024 16:49:11 -0700 Subject: [PATCH 1/5] Add support for alias on `stellar events`. --- .../src/commands/config/locator.rs | 15 ++++++++++-- cmd/soroban-cli/src/commands/events.rs | 24 +++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/cmd/soroban-cli/src/commands/config/locator.rs b/cmd/soroban-cli/src/commands/config/locator.rs index d32ff5e1d..ec5897af5 100644 --- a/cmd/soroban-cli/src/commands/config/locator.rs +++ b/cmd/soroban-cli/src/commands/config/locator.rs @@ -280,15 +280,26 @@ impl Args { Ok(alias_data.ids.get(network_passphrase).cloned()) } - pub fn resolve_contract_id( + pub fn resolve_contract_id_as_string( &self, alias_or_contract_id: &str, network_passphrase: &str, - ) -> Result<[u8; 32], Error> { + ) -> Result<String, Error> { let contract_id = self .get_contract_id(alias_or_contract_id, network_passphrase)? .unwrap_or_else(|| alias_or_contract_id.to_string()); + Ok(contract_id) + } + + pub fn resolve_contract_id( + &self, + alias_or_contract_id: &str, + network_passphrase: &str, + ) -> Result<[u8; 32], Error> { + let contract_id = + self.resolve_contract_id_as_string(alias_or_contract_id, network_passphrase)?; + soroban_spec_tools::utils::contract_id_from_str(&contract_id) .map_err(|e| Error::CannotParseContractId(contract_id.clone(), e)) } diff --git a/cmd/soroban-cli/src/commands/events.rs b/cmd/soroban-cli/src/commands/events.rs index 23cd07e9d..83b5b84b9 100644 --- a/cmd/soroban-cli/src/commands/events.rs +++ b/cmd/soroban-cli/src/commands/events.rs @@ -7,7 +7,7 @@ use super::{ config::{self, locator}, global, network, NetworkRunnable, }; -use crate::{rpc, utils}; +use crate::rpc; #[derive(Parser, Debug, Clone)] #[group(skip)] @@ -159,12 +159,12 @@ impl Cmd { } } + let network = self.network.get(&self.locator)?; + // Validate contract_ids - for id in &mut self.contract_ids { - utils::contract_id_from_str(id).map_err(|e| Error::InvalidContractId { - contract_id: id.clone(), - error: e, - })?; + for id in &self.contract_ids { + self.locator + .resolve_contract_id(id, &network.network_passphrase)?; } let response = self.run_against_rpc_server(None, None).await?; @@ -226,11 +226,21 @@ impl NetworkRunnable for Cmd { client .verify_network_passphrase(Some(&network.network_passphrase)) .await?; + + let contract_ids: Vec<String> = self + .contract_ids + .iter() + .map(|id| { + self.locator + .resolve_contract_id_as_string(id, &network.network_passphrase) + }) + .collect::<Result<Vec<_>, _>>()?; + Ok(client .get_events( start, Some(self.event_type), - &self.contract_ids, + &contract_ids, &self.topic_filters, Some(self.count), ) From 338ad4c0c17bcea7b84e2dc92db13f75c7adc8a4 Mon Sep 17 00:00:00 2001 From: Nando Vieira <me@fnando.com> Date: Wed, 26 Jun 2024 10:40:58 -0700 Subject: [PATCH 2/5] Address pr feedback. --- .../src/commands/config/locator.rs | 23 ++++++------------- .../commands/contract/bindings/typescript.rs | 3 ++- .../src/commands/contract/fetch.rs | 3 ++- .../src/commands/contract/invoke.rs | 3 ++- cmd/soroban-cli/src/commands/events.rs | 3 ++- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/cmd/soroban-cli/src/commands/config/locator.rs b/cmd/soroban-cli/src/commands/config/locator.rs index ec5897af5..a5f6b02f1 100644 --- a/cmd/soroban-cli/src/commands/config/locator.rs +++ b/cmd/soroban-cli/src/commands/config/locator.rs @@ -9,7 +9,7 @@ use std::{ path::{Path, PathBuf}, str::FromStr, }; -use stellar_strkey::DecodeError; +use stellar_strkey::{Contract, DecodeError}; use crate::{utils::find_config_dir, Pwd}; @@ -280,28 +280,19 @@ impl Args { Ok(alias_data.ids.get(network_passphrase).cloned()) } - pub fn resolve_contract_id_as_string( + pub fn resolve_contract_id( &self, alias_or_contract_id: &str, network_passphrase: &str, - ) -> Result<String, Error> { + ) -> Result<Contract, Error> { let contract_id = self .get_contract_id(alias_or_contract_id, network_passphrase)? .unwrap_or_else(|| alias_or_contract_id.to_string()); - Ok(contract_id) - } - - pub fn resolve_contract_id( - &self, - alias_or_contract_id: &str, - network_passphrase: &str, - ) -> Result<[u8; 32], Error> { - let contract_id = - self.resolve_contract_id_as_string(alias_or_contract_id, network_passphrase)?; - - soroban_spec_tools::utils::contract_id_from_str(&contract_id) - .map_err(|e| Error::CannotParseContractId(contract_id.clone(), e)) + Ok(Contract( + soroban_spec_tools::utils::contract_id_from_str(&contract_id) + .map_err(|e| Error::CannotParseContractId(contract_id.clone(), e))?, + )) } } diff --git a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs index 635446b85..f761fff85 100644 --- a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs +++ b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs @@ -96,7 +96,8 @@ impl NetworkRunnable for Cmd { let contract_id = self .locator - .resolve_contract_id(&self.contract_id, &network.network_passphrase)?; + .resolve_contract_id(&self.contract_id, &network.network_passphrase)? + .0; get_remote_contract_spec( &contract_id, diff --git a/cmd/soroban-cli/src/commands/contract/fetch.rs b/cmd/soroban-cli/src/commands/contract/fetch.rs index c2eb5d7f6..42f474558 100644 --- a/cmd/soroban-cli/src/commands/contract/fetch.rs +++ b/cmd/soroban-cli/src/commands/contract/fetch.rs @@ -127,7 +127,8 @@ impl Cmd { let network = self.network()?; Ok(self .locator - .resolve_contract_id(&self.contract_id, &network.network_passphrase)?) + .resolve_contract_id(&self.contract_id, &network.network_passphrase)? + .0) } } diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 537bcf9eb..f08f765cd 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -313,7 +313,8 @@ impl NetworkRunnable for Cmd { let contract_id = self .config .locator - .resolve_contract_id(&self.contract_id, &network.network_passphrase)?; + .resolve_contract_id(&self.contract_id, &network.network_passphrase)? + .0; let spec_entries = self.spec_entries()?; if let Some(spec_entries) = &spec_entries { // For testing wasm arg parsing diff --git a/cmd/soroban-cli/src/commands/events.rs b/cmd/soroban-cli/src/commands/events.rs index 83b5b84b9..e74d5cca6 100644 --- a/cmd/soroban-cli/src/commands/events.rs +++ b/cmd/soroban-cli/src/commands/events.rs @@ -232,7 +232,8 @@ impl NetworkRunnable for Cmd { .iter() .map(|id| { self.locator - .resolve_contract_id_as_string(id, &network.network_passphrase) + .resolve_contract_id(id, &network.network_passphrase) + .map(|contract| contract.to_string()) }) .collect::<Result<Vec<_>, _>>()?; From b4c93139fb180eac4538c28bed433b83251f650b Mon Sep 17 00:00:00 2001 From: Nando Vieira <me@fnando.com> Date: Wed, 26 Jun 2024 10:44:38 -0700 Subject: [PATCH 3/5] Remove unnecessary validation. --- cmd/soroban-cli/src/commands/events.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/cmd/soroban-cli/src/commands/events.rs b/cmd/soroban-cli/src/commands/events.rs index e74d5cca6..2eefb02f5 100644 --- a/cmd/soroban-cli/src/commands/events.rs +++ b/cmd/soroban-cli/src/commands/events.rs @@ -159,14 +159,6 @@ impl Cmd { } } - let network = self.network.get(&self.locator)?; - - // Validate contract_ids - for id in &self.contract_ids { - self.locator - .resolve_contract_id(id, &network.network_passphrase)?; - } - let response = self.run_against_rpc_server(None, None).await?; for event in &response.events { From 8af48ca640f320460c48a4d980e82650fcda0b56 Mon Sep 17 00:00:00 2001 From: Nando Vieira <me@fnando.com> Date: Wed, 26 Jun 2024 11:26:22 -0700 Subject: [PATCH 4/5] Do not map. --- cmd/soroban-cli/src/commands/events.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cmd/soroban-cli/src/commands/events.rs b/cmd/soroban-cli/src/commands/events.rs index 2eefb02f5..139fdc0a6 100644 --- a/cmd/soroban-cli/src/commands/events.rs +++ b/cmd/soroban-cli/src/commands/events.rs @@ -223,9 +223,11 @@ impl NetworkRunnable for Cmd { .contract_ids .iter() .map(|id| { - self.locator - .resolve_contract_id(id, &network.network_passphrase) - .map(|contract| contract.to_string()) + Ok::<String, Error>( + self.locator + .resolve_contract_id(id, &network.network_passphrase)? + .to_string(), + ) }) .collect::<Result<Vec<_>, _>>()?; From 6452341f572dafe25f7a61915156cdec14a00bed Mon Sep 17 00:00:00 2001 From: Nando Vieira <me@fnando.com> Date: Wed, 26 Jun 2024 11:35:56 -0700 Subject: [PATCH 5/5] Move type to collect. --- cmd/soroban-cli/src/commands/events.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cmd/soroban-cli/src/commands/events.rs b/cmd/soroban-cli/src/commands/events.rs index 139fdc0a6..a16be5734 100644 --- a/cmd/soroban-cli/src/commands/events.rs +++ b/cmd/soroban-cli/src/commands/events.rs @@ -223,13 +223,12 @@ impl NetworkRunnable for Cmd { .contract_ids .iter() .map(|id| { - Ok::<String, Error>( - self.locator - .resolve_contract_id(id, &network.network_passphrase)? - .to_string(), - ) + Ok(self + .locator + .resolve_contract_id(id, &network.network_passphrase)? + .to_string()) }) - .collect::<Result<Vec<_>, _>>()?; + .collect::<Result<Vec<_>, Error>>()?; Ok(client .get_events(