From 5d3da77b349d0dcf9e86cae07be872fb1a0ab718 Mon Sep 17 00:00:00 2001 From: Nando Vieira Date: Thu, 13 Jun 2024 11:46:12 -0700 Subject: [PATCH] Move functions to config instead. --- cmd/soroban-cli/src/commands/config/alias.rs | 50 ++++++++++--------- .../src/commands/contract/deploy/wasm.rs | 8 +-- .../src/commands/contract/invoke.rs | 8 ++- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/cmd/soroban-cli/src/commands/config/alias.rs b/cmd/soroban-cli/src/commands/config/alias.rs index b09677248..f2436bc72 100644 --- a/cmd/soroban-cli/src/commands/config/alias.rs +++ b/cmd/soroban-cli/src/commands/config/alias.rs @@ -2,12 +2,16 @@ use std::{ collections::HashMap, fs::{self, create_dir_all, OpenOptions}, io::Write, - path::{Path, PathBuf}, + path::PathBuf, }; use serde::{Deserialize, Serialize}; use stellar_strkey::DecodeError; +use crate::commands::config; + +use super::Args; + #[derive(Serialize, Deserialize, Default)] pub struct Data { ids: HashMap, @@ -23,44 +27,42 @@ pub enum Error { CannotAccessConfigDir, #[error("cannot parse contract ID {0}: {1}")] CannotParseContractId(String, DecodeError), + #[error(transparent)] + Config(#[from] config::Error), } -impl Data { - pub fn load(config_dir: &Path, alias: &str) -> Result, Error> { - let path = Self::alias_path(config_dir, alias); +impl Args { + pub fn load(&self, alias: &str) -> Result, Error> { + let path = self.alias_path(alias)?; if !path.exists() { return Ok(None); } let content = fs::read_to_string(path)?; - let data: Self = serde_json::from_str(&content).unwrap_or_default(); + let data: Data = serde_json::from_str(&content).unwrap_or_default(); Ok(Some(data)) } - pub fn alias_path(config_dir: &Path, alias: &str) -> PathBuf { + fn alias_path(&self, alias: &str) -> Result { let file_name = format!("{alias}.json"); - config_dir.join("contract-ids").join(file_name) + let config_dir = self.config_dir()?; + Ok(config_dir.join("contract-ids").join(file_name)) } - pub fn save_contract_id( - config_dir: &Path, - contract_id: &str, - alias: Option<&String>, - network_passphrase: &str, - ) -> Result<(), Error> { + pub fn save_contract_id(&self, contract_id: &str, alias: Option<&String>) -> Result<(), Error> { let Some(alias) = alias else { return Ok(()); }; - let path = Self::alias_path(config_dir, alias); + let path = self.alias_path(alias)?; let dir = path.parent().ok_or(Error::CannotAccessConfigDir)?; create_dir_all(dir).map_err(|_| Error::CannotAccessConfigDir)?; let content = fs::read_to_string(&path).unwrap_or_default(); - let mut data: Self = serde_json::from_str(&content).unwrap_or_default(); + let mut data: Data = serde_json::from_str(&content).unwrap_or_default(); let mut to_file = OpenOptions::new() .create(true) @@ -68,8 +70,10 @@ impl Data { .write(true) .open(path)?; + let network = self.get_network()?; + data.ids - .insert(network_passphrase.into(), contract_id.into()); + .insert(network.network_passphrase, contract_id.into()); let content = serde_json::to_string(&data)?; @@ -77,25 +81,25 @@ impl Data { } pub fn get_contract_id( + &self, alias: &str, - config_dir: &Path, network_passphrase: &str, ) -> Result, Error> { - let Some(alias_data) = Self::load(config_dir, alias)? else { + let Some(alias_data) = self.load(alias)? else { return Ok(None); }; Ok(alias_data.ids.get(network_passphrase).cloned()) } - pub fn load_contract_id_or_default( + pub fn resolve_contract_id( + &self, alias_or_contract_id: &str, - config_dir: &Path, network_passphrase: &str, ) -> Result<[u8; 32], Error> { - let contract_id = - Self::get_contract_id(alias_or_contract_id, config_dir, network_passphrase)? - .unwrap_or_else(|| alias_or_contract_id.to_string()); + let contract_id = self + .get_contract_id(alias_or_contract_id, network_passphrase)? + .unwrap_or_else(|| alias_or_contract_id.to_string()); 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/deploy/wasm.rs b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs index e3888c71c..989a594a6 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs @@ -120,12 +120,8 @@ impl Cmd { match res { TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?), TxnEnvelopeResult::Res(contract) => { - alias::Data::save_contract_id( - &self.config.config_dir()?, - &contract, - self.alias.as_ref(), - &self.config.get_network()?.network_passphrase, - )?; + self.config + .save_contract_id(&contract, self.alias.as_ref())?; println!("{contract}"); } diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 579c46129..99163f064 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -316,11 +316,9 @@ impl NetworkRunnable for Cmd { let unwrap_config = config.unwrap_or(&self.config); let network = unwrap_config.get_network()?; tracing::trace!(?network); - let contract_id = alias::Data::load_contract_id_or_default( - &self.contract_id, - &self.config.config_dir()?, - &network.network_passphrase, - )?; + let contract_id = self + .config + .resolve_contract_id(&self.contract_id, &network.network_passphrase)?; let spec_entries = self.spec_entries()?; if let Some(spec_entries) = &spec_entries { // For testing wasm arg parsing