Skip to content

Commit

Permalink
Extract alias logic into its own implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Jun 11, 2024
1 parent e189c71 commit 2b8035b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 75 deletions.
57 changes: 10 additions & 47 deletions cmd/soroban-cli/src/commands/contract/deploy/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::array::TryFromSliceError;
use std::fmt::Debug;
use std::fs::{self, create_dir_all};
use std::io::Write;
use std::num::ParseIntError;
use std::path::PathBuf;
use std::{array::TryFromSliceError, fs::OpenOptions};

use clap::{arg, command, Parser};
use rand::Rng;
Expand All @@ -18,7 +15,7 @@ use soroban_env_host::{
HostError,
};

use crate::commands::contract::AliasData;
use crate::commands::contract::{AliasData, AliasDataError};
use crate::commands::{
config::data,
contract::{self, id::wasm::get_contract_id},
Expand Down Expand Up @@ -108,16 +105,12 @@ pub enum Error {
Network(#[from] network::Error),
#[error(transparent)]
Wasm(#[from] wasm::Error),
#[error("cannot access config dir for alias file")]
CannotAccessConfigDir,
#[error(
"alias must be 1-30 chars long, and have only letters, numbers, underscores and dashes"
)]
InvalidAliasFormat { alias: String },
#[error(transparent)]
JsonSerialization(#[from] serde_json::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
AliasData(#[from] AliasDataError),
}

impl Cmd {
Expand All @@ -128,7 +121,13 @@ impl Cmd {
match res {
TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?),
TxnEnvelopeResult::Res(contract) => {
self.save_contract_id(&contract)?;
AliasData::save_contract_id(
&self.config.config_dir()?,
&contract,
self.alias.as_ref(),
&self.config.get_network()?.network_passphrase,
)?;

println!("{contract}");
}
}
Expand All @@ -149,42 +148,6 @@ impl Cmd {
None => Ok(()),
}
}

fn alias_path_for(&self, alias: &str) -> Result<PathBuf, Error> {
let config_dir = self.config.config_dir()?;
let file_name = format!("{alias}.json");

Ok(config_dir.join("contract-ids").join(file_name))
}

fn save_contract_id(&self, contract: &String) -> Result<(), Error> {
let Some(alias) = &self.alias else {
return Ok(());
};

let file_path = self.alias_path_for(alias)?;
let dir = file_path.parent().ok_or(Error::CannotAccessConfigDir)?;

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)?;

data.ids.insert(
self.config.get_network()?.network_passphrase,
contract.into(),
);

let content = serde_json::to_string(&data)?;

Ok(to_file.write_all(content.as_bytes())?)
}
}

#[async_trait::async_trait]
Expand Down
36 changes: 9 additions & 27 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use super::super::{
config::{self, locator},
events,
};
use super::AliasData;
use super::{AliasData, AliasDataError};
use crate::commands::txn_result::{TxnEnvelopeResult, TxnResult};
use crate::commands::NetworkRunnable;
use crate::get_spec::{self, get_remote_contract_spec};
Expand Down Expand Up @@ -153,12 +153,8 @@ pub enum Error {
Network(#[from] network::Error),
#[error(transparent)]
GetSpecError(#[from] get_spec::Error),
#[error("unable to read alias file")]
UnableToReadAliasFile,
#[error("alias file not found")]
NoAliasFileFound,
#[error(transparent)]
JsonDeserialization(#[from] serde_json::Error),
AliasData(#[from] AliasDataError),
}

impl From<Infallible> for Error {
Expand Down Expand Up @@ -321,28 +317,14 @@ impl Cmd {
.map_err(|e| Error::CannotParseContractId(contract_id.clone(), e))
}

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(file_name))
}

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);
}

let content = fs::read_to_string(file_path)?;
let data: AliasData = serde_json::from_str(&content)?;

match data.ids.get(network) {
Some(id) => Ok(Some(id.into())),
_ => Ok(None),
}
let config_dir = self.config.config_dir()?;
let network_passphrase = &self.config.get_network()?.network_passphrase;
Ok(AliasData::get_contract_id(
&self.contract_id,
&config_dir,
network_passphrase,
)?)
}
}

Expand Down
84 changes: 83 additions & 1 deletion cmd/soroban-cli/src/commands/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ pub mod optimize;
pub mod read;
pub mod restore;

use std::collections::HashMap;
use std::{
collections::HashMap,
fs::{self, create_dir_all, OpenOptions},
io::Write,
path::{Path, PathBuf},
};

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -174,3 +179,80 @@ pub enum SpecOutput {
pub struct AliasData {
ids: HashMap<String, String>,
}

#[derive(thiserror::Error, Debug)]
pub enum AliasDataError {
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("cannot access config dir for alias file")]
CannotAccessConfigDir,
}

impl AliasData {
pub fn load(config_dir: &Path, alias: Option<&str>) -> Result<Option<Self>, AliasDataError> {
let Some(alias) = alias else {
return Ok(None);
};

let path = Self::alias_path(config_dir, alias);
let content = fs::read_to_string(path)?;
let data: Self = serde_json::from_str(&content).unwrap_or_default();

Ok(Some(data))
}

pub fn alias_path(config_dir: &Path, alias: &str) -> PathBuf {
let file_name = format!("{alias}.json");
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<(), AliasDataError> {
let Some(alias) = alias else {
return Ok(());
};

let path = Self::alias_path(config_dir, alias);
let dir = path.parent().ok_or(AliasDataError::CannotAccessConfigDir)?;

create_dir_all(dir).map_err(|_| AliasDataError::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 to_file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(path)?;

data.ids
.insert(network_passphrase.into(), contract_id.into());

let content = serde_json::to_string(&data)?;

Ok(to_file.write_all(content.as_bytes())?)
}

pub fn get_contract_id(
alias: &str,
config_dir: &Path,
network_passphrase: &str,
) -> Result<Option<String>, AliasDataError> {
let alias_data = Self::load(config_dir, Some(alias))?;
let Some(alias_data) = alias_data else {
return Ok(None);
};

match alias_data.ids.get(network_passphrase) {
Some(id) => Ok(Some(id.into())),
_ => Ok(None),
}
}
}

0 comments on commit 2b8035b

Please sign in to comment.