stellar_registry_cli/commands/
install.rs1use clap::Parser;
2
3use stellar_cli::{
4 commands::contract::{fetch, invoke},
5 config,
6};
7use stellar_strkey::Contract;
8
9use crate::contract::NetworkContract;
10
11#[derive(Parser, Debug, Clone)]
12pub struct Cmd {
13 pub contract_name: String,
15
16 #[command(flatten)]
17 pub config: config::Args,
18}
19
20#[derive(thiserror::Error, Debug)]
21pub enum Error {
22 #[error(transparent)]
23 Fetch(#[from] fetch::Error),
24 #[error(transparent)]
25 Invoke(#[from] invoke::Error),
26 #[error(transparent)]
27 Io(#[from] std::io::Error),
28 #[error(transparent)]
29 Strkey(#[from] stellar_strkey::DecodeError),
30 #[error(transparent)]
31 LocatorConfig(#[from] stellar_cli::config::locator::Error),
32 #[error(transparent)]
33 Config(#[from] stellar_cli::config::Error),
34}
35
36impl Cmd {
37 pub async fn run(&self) -> Result<(), Error> {
38 let network = self.config.get_network()?;
40 let network_passphrase = network.network_passphrase;
41
42 let contract = self.get_contract_id().await?;
43
44 self.config.locator.save_contract_id(
46 &network_passphrase,
47 &contract,
48 &self.contract_name,
49 )?;
50
51 eprintln!(
52 "✅ Successfully registered contract alias '{}'",
53 self.contract_name
54 );
55 eprintln!("Contract ID: {:?}", contract.to_string());
56
57 Ok(())
58 }
59
60 pub async fn get_contract_id(&self) -> Result<Contract, Error> {
61 if self.contract_name == "registry" {
62 return Ok(self.config.contract_id()?);
63 }
64 let slop = vec!["fetch_contract_id", "--contract-name", &self.contract_name];
66 eprintln!("Fetching contract ID via registry...");
68 let raw = self.config.invoke_registry(&slop, None, true).await?;
69
70 let contract_id = raw.trim_matches('"').to_string();
71 Ok(contract_id.parse()?)
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 #[cfg(feature = "integration-tests")]
78 #[tokio::test]
79 async fn test_run() {
80 use super::*;
81 use std::env;
82 use stellar_cli::config::{locator, network};
83 use stellar_scaffold_test::RegistryTest;
84 let registry = RegistryTest::new().await;
86 let test_env = registry.clone().env;
87
88 env::set_var("STELLAR_RPC_URL", "http://localhost:8000/soroban/rpc");
90 env::set_var("STELLAR_ACCOUNT", "alice");
91 env::set_var(
92 "STELLAR_NETWORK_PASSPHRASE",
93 "Standalone Network ; February 2017",
94 );
95 env::set_var("STELLAR_REGISTRY_CONTRACT_ID", ®istry.registry_address);
96
97 let wasm_path = test_env
99 .cwd
100 .join("target/stellar/soroban_hello_world_contract.wasm");
101
102 registry
104 .clone()
105 .registry_cli("publish")
106 .arg("--wasm")
107 .arg(&wasm_path)
108 .arg("--binver")
109 .arg("0.0.2")
110 .arg("--wasm-name")
111 .arg("hello")
112 .assert()
113 .success();
114
115 registry
117 .registry_cli("deploy")
118 .arg("--contract-name")
119 .arg("hello")
120 .arg("--wasm-name")
121 .arg("hello")
122 .arg("version")
123 .arg("0.0.2")
124 .assert()
125 .success();
126
127 let cmd = Cmd {
129 contract_name: "hello".to_owned(),
130 config: config::Args {
131 locator: locator::Args {
132 global: false,
133 config_dir: Some(test_env.cwd.to_str().unwrap().into()),
134 },
135 network: network::Args {
136 rpc_url: Some("http://localhost:8000/soroban/rpc".to_string()),
137 network_passphrase: Some("Standalone Network ; February 2017".to_string()),
138 ..Default::default()
139 },
140 ..Default::default()
141 },
142 };
143
144 cmd.run().await.unwrap();
146 assert!(test_env.cwd.join(".stellar").exists());
147 }
148}