diff --git a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs index 324898172..d50fa6e31 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs @@ -1,5 +1,5 @@ use std::fmt::Debug; -use std::fs::create_dir_all; +use std::fs::{self, create_dir_all}; use std::io::Write; use std::num::ParseIntError; use std::path::PathBuf; @@ -152,13 +152,9 @@ impl Cmd { fn alias_path_for(&self, alias: &str) -> Result { let config_dir = self.config.config_dir()?; - let network = self.config.network.network.clone().expect("must be set"); let file_name = format!("{alias}.json"); - Ok(config_dir - .join("contract-ids") - .join(network) - .join(file_name)) + Ok(config_dir.join("contract-ids").join(file_name)) } fn save_contract_id(&self, contract: &String) -> Result<(), Error> { @@ -171,17 +167,21 @@ impl Cmd { create_dir_all(dir).map_err(|_| Error::CannotAccessConfigDir)?; + let content = fs::read_to_string(&file_path).unwrap_or_default(); + let mut data: AliasData = serde_json::from_str(&content).unwrap_or_default(); + let mut to_file = OpenOptions::new() .create(true) .truncate(true) .write(true) .open(file_path)?; - let payload = AliasData { - id: contract.into(), - }; + data.ids.insert( + self.config.get_network()?.network_passphrase, + contract.into(), + ); - let content = serde_json::to_string(&payload)?; + let content = serde_json::to_string(&data)?; Ok(to_file.write_all(content.as_bytes())?) } diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index fa67f93d1..712c73c51 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -312,11 +312,8 @@ impl Cmd { impl Cmd { fn contract_id(&self) -> Result<[u8; 32], Error> { - let contract_id = match &self.config.network.network { - Some(network) => match self.load_contract_id(network) { - Ok(Some(id)) => id, - _ => self.contract_id.clone(), - }, + let contract_id: String = match self.load_contract_id() { + Ok(Some(id)) => id.to_string(), _ => self.contract_id.clone(), }; @@ -324,18 +321,16 @@ impl Cmd { .map_err(|e| Error::CannotParseContractId(contract_id.clone(), e)) } - fn alias_path_for(&self, network: &str) -> Result { + fn alias_path(&self) -> Result { let config_dir = self.config.config_dir()?; let file_name = format!("{}.json", self.contract_id); - Ok(config_dir - .join("contract-ids") - .join(network) - .join(file_name)) + Ok(config_dir.join("contract-ids").join(file_name)) } - fn load_contract_id(&self, network: &str) -> Result, Error> { - let file_path = self.alias_path_for(network)?; + fn load_contract_id(&self) -> Result, Error> { + let network = &self.config.get_network()?.network_passphrase; + let file_path = self.alias_path()?; if !file_path.exists() { return Ok(None); @@ -344,7 +339,10 @@ impl Cmd { let content = fs::read_to_string(file_path)?; let data: AliasData = serde_json::from_str(&content)?; - Ok(Some(data.id)) + match data.ids.get(network) { + Some(id) => Ok(Some(id.into())), + _ => Ok(None), + } } } diff --git a/cmd/soroban-cli/src/commands/contract/mod.rs b/cmd/soroban-cli/src/commands/contract/mod.rs index f423a9de6..9da55714c 100644 --- a/cmd/soroban-cli/src/commands/contract/mod.rs +++ b/cmd/soroban-cli/src/commands/contract/mod.rs @@ -13,6 +13,8 @@ pub mod optimize; pub mod read; pub mod restore; +use std::collections::HashMap; + use serde::{Deserialize, Serialize}; use crate::commands::global; @@ -168,7 +170,7 @@ pub enum SpecOutput { Docs, } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Default)] pub struct AliasData { - id: String, + ids: HashMap, }