stellar_registry_cli/commands/
create_alias.rs1use clap::Parser;
2
3use stellar_cli::{commands::contract::invoke, config};
4use stellar_strkey::Contract;
5
6use crate::contract::{NetworkContract, REGISTRY_NAME};
7
8#[derive(Parser, Debug, Clone)]
9pub struct Cmd {
10 pub contract_name: String,
12
13 #[command(flatten)]
14 pub config: config::Args,
15}
16
17#[derive(thiserror::Error, Debug)]
18pub enum Error {
19 #[error(transparent)]
20 Invoke(#[from] invoke::Error),
21 #[error(transparent)]
22 Strkey(#[from] stellar_strkey::DecodeError),
23 #[error(transparent)]
24 LocatorConfig(#[from] stellar_cli::config::locator::Error),
25 #[error(transparent)]
26 Config(#[from] stellar_cli::config::Error),
27}
28
29impl Cmd {
30 pub async fn run(&self) -> Result<(), Error> {
31 let network = self.config.get_network()?;
33 let network_passphrase = network.network_passphrase;
34
35 let contract = self.get_contract_id().await?;
36 let alias = &self.contract_name;
37
38 self.config
40 .locator
41 .save_contract_id(&network_passphrase, &contract, alias)?;
42
43 eprintln!("✅ Successfully registered contract alias '{alias}' for {contract}");
44
45 Ok(())
46 }
47
48 pub async fn get_contract_id(&self) -> Result<Contract, Error> {
49 if self.contract_name == REGISTRY_NAME {
50 return Ok(self.config.contract_id()?);
51 }
52 let slop = ["fetch_contract_id", "--contract-name", &self.contract_name];
54 eprintln!("Fetching contract ID via registry...");
56 let raw = self.config.view_registry(&slop).await?;
57 let contract_id = raw.trim_matches('"').to_string();
58 Ok(contract_id.parse()?)
59 }
60}
61
62#[cfg(feature = "integration-tests")]
63#[cfg(test)]
64mod tests {
65
66 use stellar_scaffold_test::RegistryTest;
67
68 #[tokio::test]
69 async fn test_run() {
70 let registry = RegistryTest::new().await;
72 let test_env = registry.clone().env;
73
74 let wasm_path = registry.hello_wasm_v1();
76
77 registry
79 .registry_cli("publish")
80 .arg("--wasm")
81 .arg(&wasm_path)
82 .arg("--binver")
83 .arg("0.0.2")
84 .arg("--wasm-name")
85 .arg("hello")
86 .assert()
87 .success();
88
89 registry
91 .registry_cli("deploy")
92 .arg("--contract-name")
93 .arg("hello")
94 .arg("--wasm-name")
95 .arg("hello")
96 .arg("--version")
97 .arg("0.0.2")
98 .arg("--")
99 .arg("--admin=alice")
100 .assert()
101 .success();
102
103 let cmd = registry.parse_cmd::<super::Cmd>(&["hello"]).unwrap();
105
106 cmd.run().await.unwrap();
108 assert!(test_env
109 .cwd
110 .join(".config/stellar/contract-ids/hello.json")
111 .exists());
112 }
113}