Skip to content

Commit

Permalink
Scope aliases by network passphrases.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Jun 10, 2024
1 parent e18e96e commit ea24a9a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
20 changes: 10 additions & 10 deletions cmd/soroban-cli/src/commands/contract/deploy/wasm.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -152,13 +152,9 @@ impl Cmd {

fn alias_path_for(&self, alias: &str) -> Result<PathBuf, Error> {
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> {
Expand All @@ -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())?)
}
Expand Down
24 changes: 11 additions & 13 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,30 +312,25 @@ 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(),
};

soroban_spec_tools::utils::contract_id_from_str(&contract_id)
.map_err(|e| Error::CannotParseContractId(contract_id.clone(), e))
}

fn alias_path_for(&self, network: &str) -> Result<PathBuf, Error> {
fn alias_path(&self) -> Result<PathBuf, Error> {
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<Option<String>, Error> {
let file_path = self.alias_path_for(network)?;
fn load_contract_id(&self) -> Result<Option<String>, Error> {
let network = &self.config.get_network()?.network_passphrase;
let file_path = self.alias_path()?;

if !file_path.exists() {
return Ok(None);
Expand All @@ -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),
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/soroban-cli/src/commands/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -168,7 +170,7 @@ pub enum SpecOutput {
Docs,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Default)]
pub struct AliasData {
id: String,
ids: HashMap<String, String>,
}

0 comments on commit ea24a9a

Please sign in to comment.