Skip to main content

stellar_registry_cli/commands/
fetch_contract_id.rs

1use clap::Parser;
2use stellar_cli::commands::contract::invoke;
3use stellar_registry_build::named_registry::PrefixedName;
4use stellar_strkey::Contract;
5
6use crate::commands::global;
7
8#[derive(Parser, Debug, Clone)]
9pub struct Cmd {
10    /// Name of deployed contract. Can use prefix if not using verified registry.
11    /// E.g. `unverified/<name>`
12    pub contract_name: PrefixedName,
13
14    #[command(flatten)]
15    pub config: global::Args,
16}
17
18#[derive(thiserror::Error, Debug)]
19pub enum Error {
20    #[error(transparent)]
21    Invoke(#[from] invoke::Error),
22    #[error(transparent)]
23    Config(#[from] stellar_cli::config::Error),
24    #[error(transparent)]
25    Registry(#[from] stellar_registry_build::Error),
26}
27
28impl Cmd {
29    pub async fn run(&self) -> Result<(), Error> {
30        let contract_id = self.fetch_contract_id().await?;
31        println!("{contract_id}");
32        Ok(())
33    }
34
35    pub async fn fetch_contract_id(&self) -> Result<Contract, Error> {
36        let registry = self.contract_name.registry(&self.config).await?;
37        Ok(registry.fetch_contract_id(&self.contract_name.name).await?)
38    }
39}
40
41#[cfg(feature = "integration-tests")]
42#[cfg(test)]
43mod tests {
44    use stellar_scaffold_test::RegistryTest;
45
46    #[tokio::test]
47    async fn simple() {
48        let registry = RegistryTest::new().await;
49        let v1 = registry.hello_wasm_v1();
50
51        // First publish the contract
52        registry
53            .registry_cli("publish")
54            .arg("--wasm")
55            .arg(v1.to_str().unwrap())
56            .arg("--binver")
57            .arg("0.0.1")
58            .arg("--wasm-name")
59            .arg("hello")
60            .assert()
61            .success();
62
63        // Then deploy it
64        registry
65            .registry_cli("deploy")
66            .arg("--contract-name")
67            .arg("hello")
68            .arg("--wasm-name")
69            .arg("hello")
70            .arg("--")
71            .arg("--admin=alice")
72            .assert()
73            .success();
74
75        let contract_id = registry
76            .parse_cmd::<super::Cmd>(&["hello"])
77            .unwrap()
78            .fetch_contract_id()
79            .await
80            .unwrap();
81        assert!(!contract_id.to_string().is_empty());
82    }
83
84    #[tokio::test]
85    async fn unverified() {
86        let registry = RegistryTest::new().await;
87        let v1 = registry.hello_wasm_v1();
88
89        // First publish the contract
90        registry
91            .registry_cli("publish")
92            .arg("--wasm")
93            .arg(v1.to_str().unwrap())
94            .arg("--binver")
95            .arg("0.0.1")
96            .arg("--wasm-name")
97            .arg("unverified/hello")
98            .assert()
99            .success();
100
101        // Then deploy it
102        registry
103            .registry_cli("deploy")
104            .arg("--contract-name")
105            .arg("unverified/hello")
106            .arg("--wasm-name")
107            .arg("unverified/hello")
108            .arg("--")
109            .arg("--admin=alice")
110            .assert()
111            .success();
112
113        let contract_id = registry
114            .parse_cmd::<super::Cmd>(&["unverified/hello"])
115            .unwrap()
116            .fetch_contract_id()
117            .await
118            .unwrap();
119        assert!(!contract_id.to_string().is_empty());
120    }
121}