Skip to content

Commit

Permalink
Add alias support to contract asset deploy (#1829)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Jan 13, 2025
1 parent 6f13838 commit 6425231
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 43 deletions.
1 change: 1 addition & 0 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Deploy builtin Soroban Asset Contract
* `--instructions <INSTRUCTIONS>` — Number of instructions to simulate
* `--build-only` — Build the transaction and only write the base64 xdr to stdout
* `--sim-only` — (Deprecated) simulate the transaction and only write the base64 xdr to stdout
* `--alias <ALIAS>` — The alias that will be used to save the assets's id. Whenever used, `--alias` will always overwrite the existing contract id configuration without asking for confirmation



Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/commands/contract/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::commands::global;

pub mod asset;
pub mod utils;
pub mod wasm;

#[derive(Debug, clap::Subcommand)]
Expand Down
22 changes: 22 additions & 0 deletions cmd/soroban-cli/src/commands/contract/deploy/asset.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::locator;
use crate::xdr::{
Asset, ContractDataDurability, ContractExecutable, ContractIdPreimage, CreateContractArgs,
Error as XdrError, Hash, HostFunction, InvokeHostFunctionOp, LedgerKey::ContractData,
Expand All @@ -21,6 +22,8 @@ use crate::{
utils::contract_id_hash_from_asset,
};

use crate::commands::contract::deploy::utils::alias_validator;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("error parsing int: {0}")]
Expand All @@ -39,6 +42,8 @@ pub enum Error {
Network(#[from] network::Error),
#[error(transparent)]
Builder(#[from] builder::Error),
#[error(transparent)]
Locator(#[from] locator::Error),
}

impl From<Infallible> for Error {
Expand All @@ -56,8 +61,15 @@ pub struct Cmd {

#[command(flatten)]
pub config: config::Args,

#[command(flatten)]
pub fee: crate::fee::Args,

/// The alias that will be used to save the assets's id.
/// Whenever used, `--alias` will always overwrite the existing contract id
/// configuration without asking for confirmation.
#[arg(long, value_parser = clap::builder::ValueParser::new(alias_validator))]
pub alias: Option<String>,
}

impl Cmd {
Expand All @@ -66,6 +78,16 @@ impl Cmd {
match res {
TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?),
TxnEnvelopeResult::Res(contract) => {
let network = self.config.get_network()?;

if let Some(alias) = self.alias.clone() {
self.config.locator.save_contract_id(
&network.network_passphrase,
&contract,
&alias,
)?;
}

println!("{contract}");
}
}
Expand Down
56 changes: 56 additions & 0 deletions cmd/soroban-cli/src/commands/contract/deploy/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use regex::Regex;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(
"alias must be 1-30 chars long, and have only letters, numbers, underscores and dashes"
)]
InvalidAliasFormat { alias: String },
}

pub fn alias_validator(alias: &str) -> Result<String, Error> {
let regex = Regex::new(r"^[a-zA-Z0-9_-]{1,30}$").unwrap();

if regex.is_match(alias) {
Ok(alias.into())
} else {
Err(Error::InvalidAliasFormat {
alias: alias.into(),
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_alias_validator_with_valid_inputs() {
let valid_inputs = [
"hello",
"123",
"hello123",
"hello_123",
"123_hello",
"123-hello",
"hello-123",
"HeLlo-123",
];

for input in valid_inputs {
let result = alias_validator(input);
assert!(result.is_ok());
assert!(result.unwrap() == input);
}
}

#[test]
fn test_alias_validator_with_invalid_inputs() {
let invalid_inputs = ["", "invalid!", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"];

for input in invalid_inputs {
let result = alias_validator(input);
assert!(result.is_err());
}
}
}
44 changes: 1 addition & 43 deletions cmd/soroban-cli/src/commands/contract/deploy/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::commands::contract::deploy::utils::alias_validator;
use std::array::TryFromSliceError;
use std::ffi::OsString;
use std::fmt::Debug;
Expand All @@ -12,7 +13,6 @@ use crate::xdr::{
};
use clap::{arg, command, Parser};
use rand::Rng;
use regex::Regex;

use soroban_spec_tools::contract as contract_spec;

Expand Down Expand Up @@ -152,18 +152,6 @@ impl Cmd {
}
}

fn alias_validator(alias: &str) -> Result<String, Error> {
let regex = Regex::new(r"^[a-zA-Z0-9_-]{1,30}$").unwrap();

if regex.is_match(alias) {
Ok(alias.into())
} else {
Err(Error::InvalidAliasFormat {
alias: alias.into(),
})
}
}

#[async_trait::async_trait]
impl NetworkRunnable for Cmd {
type Error = Error;
Expand Down Expand Up @@ -390,34 +378,4 @@ mod tests {

assert!(result.is_ok());
}

#[test]
fn test_alias_validator_with_valid_inputs() {
let valid_inputs = [
"hello",
"123",
"hello123",
"hello_123",
"123_hello",
"123-hello",
"hello-123",
"HeLlo-123",
];

for input in valid_inputs {
let result = alias_validator(input);
assert!(result.is_ok());
assert!(result.unwrap() == input);
}
}

#[test]
fn test_alias_validator_with_invalid_inputs() {
let invalid_inputs = ["", "invalid!", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"];

for input in invalid_inputs {
let result = alias_validator(input);
assert!(result.is_err());
}
}
}

0 comments on commit 6425231

Please sign in to comment.