stellar_registry_cli/commands/
install.rs

1use std::path::PathBuf;
2
3use clap::Parser;
4
5use stellar_cli::{
6    commands::{
7        contract::{fetch, invoke},
8        global,
9    },
10    config::{self, network, UnresolvedContract},
11};
12
13use crate::testnet;
14
15#[derive(Parser, Debug, Clone)]
16pub struct Cmd {
17    /// Name of deployed contract
18    pub deployed_name: String,
19    /// Where to place the Wasm file. Default `<root>/target/stellar/<deployed_name>/index.wasm`
20    #[arg(long, short = 'o')]
21    pub out_dir: Option<PathBuf>,
22}
23
24#[derive(thiserror::Error, Debug)]
25pub enum Error {
26    #[error(transparent)]
27    Fetch(#[from] fetch::Error),
28    #[error(transparent)]
29    Invoke(#[from] invoke::Error),
30    #[error(transparent)]
31    StellarBuild(#[from] stellar_build::Error),
32    #[error(transparent)]
33    Io(#[from] std::io::Error),
34    #[error(transparent)]
35    Strkey(#[from] stellar_strkey::DecodeError),
36}
37
38impl Cmd {
39    pub async fn run(&self) -> Result<(), Error> {
40        let contract_id = testnet::contract_id();
41        let network = testnet::network();
42        let out_dir = self.out_dir.as_ref().unwrap();
43        let mut out_file = out_dir.join(&self.deployed_name);
44        out_file.set_extension("wasm");
45        let id_file = out_file.parent().unwrap().join("contract_id.txt");
46        let fetch_cmd = fetch::Cmd {
47            contract_id,
48            out_file: Some(out_file),
49            network,
50            ..Default::default()
51        };
52        fetch_cmd.run().await?;
53        std::fs::write(id_file, testnet::contract_id_strkey().to_string())?;
54        Ok(())
55    }
56
57    pub async fn get_contract_id(
58        &self,
59        contract_id: &UnresolvedContract,
60        network: &network::Args,
61    ) -> Result<String, Error> {
62        let mut cmd = invoke::Cmd {
63            contract_id: contract_id.clone(),
64            config: config::Args {
65                network: network.clone(),
66                ..Default::default()
67            },
68            is_view: true,
69            ..Default::default()
70        };
71        cmd.slop = vec!["fetch_contract_id", "--deployed_name", &self.deployed_name]
72            .into_iter()
73            .map(Into::into)
74            .collect::<Vec<_>>();
75        println!("Fetching contract ID...\n{cmd:#?}");
76        Ok(cmd
77            .invoke(&global::Args::default())
78            .await?
79            .into_result()
80            .unwrap())
81    }
82}
83
84#[cfg(test)]
85mod tests {
86
87    use super::*;
88
89    #[tokio::test]
90    #[ignore]
91    async fn test_run() {
92        std::env::set_var("SOROBAN_NETWORK", "local");
93        let cmd = Cmd {
94            deployed_name: "stellar_registry".to_owned(),
95            out_dir: None,
96        };
97        let contract_id = testnet::contract_id();
98        let network = testnet::network();
99        let res = cmd.get_contract_id(&contract_id, &network).await.unwrap();
100        println!("{res}");
101    }
102}