Skip to content

Commit

Permalink
Add contract alias remove. (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Aug 27, 2024
1 parent c0e61aa commit 05e362b
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
33 changes: 33 additions & 0 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Tools for smart contract developers
###### **Subcommands:**

* `asset` — Utilities to deploy a Stellar Asset Contract or get its id
* `alias` — Utilities to manage contract aliases
* `bindings` — Generate code client bindings for a contract
* `build` — Build a contract from source
* `extend` — Extend the time to live ledger of a contract-data ledger entry
Expand Down Expand Up @@ -151,6 +152,38 @@ Deploy builtin Soroban Asset Contract



## `stellar contract alias`

Utilities to manage contract aliases

**Usage:** `stellar contract alias <COMMAND>`

###### **Subcommands:**

* `remove` — Remove contract alias



## `stellar contract alias remove`

Remove contract alias

**Usage:** `stellar contract alias remove [OPTIONS] <ALIAS>`

###### **Arguments:**

* `<ALIAS>` — The contract alias that will be removed

###### **Options:**

* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."
* `--rpc-url <RPC_URL>` — RPC server endpoint
* `--network-passphrase <NETWORK_PASSPHRASE>` — Network passphrase to sign the transaction sent to the rpc server
* `--network <NETWORK>` — Name of network to use from config



## `stellar contract bindings`

Generate code client bindings for a contract
Expand Down
24 changes: 24 additions & 0 deletions cmd/soroban-cli/src/commands/contract/alias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::commands::global;

pub mod remove;

#[derive(Debug, clap::Subcommand)]
pub enum Cmd {
/// Remove contract alias
Remove(remove::Cmd),
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Remove(#[from] remove::Error),
}

impl Cmd {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
match &self {
Cmd::Remove(remove) => remove.run(global_args).await?,
}
Ok(())
}
}
62 changes: 62 additions & 0 deletions cmd/soroban-cli/src/commands/contract/alias/remove.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::fmt::Debug;

use clap::{command, Parser};

use crate::commands::{config::network, global};
use crate::config::locator;
use crate::print::Print;

#[derive(Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
#[command(flatten)]
pub config_locator: locator::Args,

#[command(flatten)]
network: network::Args,

/// The contract alias that will be removed.
pub alias: String,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Locator(#[from] locator::Error),

#[error(transparent)]
Network(#[from] network::Error),

#[error("no contract found with alias `{alias}`")]
NoContract { alias: String },
}

impl Cmd {
#[allow(clippy::unused_async)]
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let print = Print::new(global_args.quiet);
let alias = &self.alias;
let network = self.network.get(&self.config_locator)?;
let network_passphrase = &network.network_passphrase;

let Some(contract) = self
.config_locator
.get_contract_id(&self.alias, network_passphrase)?
else {
return Err(Error::NoContract {
alias: alias.into(),
});
};

print.infoln(format!(
"Contract alias '{alias}' references {contract} on network '{network_passphrase}'"
));

self.config_locator
.remove_contract_id(&network.network_passphrase, alias)?;

print.checkln(format!("Contract alias '{alias}' has been removed"));

Ok(())
}
}
10 changes: 10 additions & 0 deletions cmd/soroban-cli/src/commands/contract/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod alias;
pub mod asset;
pub mod bindings;
pub mod build;
Expand All @@ -21,6 +22,11 @@ pub enum Cmd {
/// Utilities to deploy a Stellar Asset Contract or get its id
#[command(subcommand)]
Asset(asset::Cmd),

/// Utilities to manage contract aliases
#[command(subcommand)]
Alias(alias::Cmd),

/// Generate code client bindings for a contract
#[command(subcommand)]
Bindings(bindings::Cmd),
Expand Down Expand Up @@ -82,6 +88,9 @@ pub enum Error {
#[error(transparent)]
Asset(#[from] asset::Error),

#[error(transparent)]
Alias(#[from] alias::Error),

#[error(transparent)]
Bindings(#[from] bindings::Error),

Expand Down Expand Up @@ -132,6 +141,7 @@ impl Cmd {
Cmd::Bindings(bindings) => bindings.run().await?,
Cmd::Build(build) => build.run()?,
Cmd::Extend(extend) => extend.run().await?,
Cmd::Alias(alias) => alias.run(global_args).await?,
Cmd::Deploy(deploy) => deploy.run(global_args).await?,
Cmd::Id(id) => id.run()?,
Cmd::Info(info) => info.run().await?,
Expand Down
25 changes: 25 additions & 0 deletions cmd/soroban-cli/src/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub enum Error {
Json(#[from] serde_json::Error),
#[error("cannot access config dir for alias file")]
CannotAccessConfigDir,
#[error("cannot access alias config file (no permission or doesn't exist)")]
CannotAccessAliasConfigFile,
#[error("cannot parse contract ID {0}: {1}")]
CannotParseContractId(String, DecodeError),
}
Expand Down Expand Up @@ -277,6 +279,29 @@ impl Args {
Ok(to_file.write_all(content.as_bytes())?)
}

pub fn remove_contract_id(&self, network_passphrase: &str, alias: &str) -> Result<(), Error> {
let path = self.alias_path(alias)?;

if !path.is_file() {
return Err(Error::CannotAccessAliasConfigFile);
}

let content = fs::read_to_string(&path).unwrap_or_default();
let mut data: alias::Data = serde_json::from_str(&content).unwrap_or_default();

let mut to_file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(path)?;

data.ids.remove::<str>(network_passphrase);

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

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

pub fn get_contract_id(
&self,
alias: &str,
Expand Down

0 comments on commit 05e362b

Please sign in to comment.