1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate::Abstract;
use abstract_os::objects::module::ModuleVersion;
use boot_core::{BootEnvironment, BootError::StdErr, Deploy, *};

use semver::Version;
use serde::Serialize;

/// Trait for deploying APIs
pub trait ApiDeployer<Chain: BootEnvironment, CustomInitMsg: Serialize>:
    ContractInstance<Chain>
    + BootInstantiate<Chain, InstantiateMsg = abstract_os::api::InstantiateMsg<CustomInitMsg>>
    + BootUpload<Chain>
{
    fn deploy(
        &mut self,
        version: Version,
        custom_init_msg: CustomInitMsg,
    ) -> Result<(), crate::AbstractBootError> {
        // retrieve the deployment
        let abstr = Abstract::load_from(self.get_chain().clone())?;

        // check for existing version
        let version_check = abstr
            .version_control
            .get_api_addr(&self.id(), ModuleVersion::from(version.to_string()));

        if version_check.is_ok() {
            return Err(StdErr(format!(
                "API {} already exists with version {}",
                self.id(),
                version
            ))
            .into());
        };

        self.upload()?;
        let init_msg = abstract_os::api::InstantiateMsg {
            app: custom_init_msg,
            base: abstract_os::api::BaseInstantiateMsg {
                ans_host_address: abstr.ans_host.address()?.into(),
                version_control_address: abstr.version_control.address()?.into(),
            },
        };
        self.instantiate(&init_msg, None, None)?;

        abstr
            .version_control
            .register_apis(vec![self.as_instance()], &version)?;
        Ok(())
    }
}

/// Trait for deploying APPs
pub trait AppDeployer<Chain: BootEnvironment>: ContractInstance<Chain> + BootUpload<Chain> {
    fn deploy(&mut self, version: Version) -> Result<(), crate::AbstractBootError> {
        // retrieve the deployment
        let abstr = Abstract::load_from(self.get_chain().clone())?;

        // check for existing version
        let version_check = abstr
            .version_control
            .get_app_code(&self.id(), ModuleVersion::from(version.to_string()));

        if version_check.is_ok() {
            return Err(StdErr(format!(
                "API {} already exists with version {}",
                self.id(),
                version
            ))
            .into());
        };

        self.upload()?;

        abstr
            .version_control
            .register_apps(vec![self.as_instance()], &version)?;
        Ok(())
    }
}

// /// An Abstract module deployer that can deploy modules to a chain.
// pub struct ModuleDeployer<Chain: BootEnvironment> {
//     pub chain: Chain,
//     pub version: Version,
//     pub ans_host: AnsHost<Chain>,
//     pub version_control: VersionControl<Chain>,
// }

// impl<Chain: BootEnvironment> ModuleDeployer<Chain> {
//     /// Create a new instance of the module deployer, loaded from the STATE_FILE.
//     pub fn new(chain: Chain, version: Version) -> Self {
//         let ans_host = AnsHost::new(ANS_HOST, chain.clone());
//         let version_control = VersionControl::new(VERSION_CONTROL, chain.clone());
//         Self {
//             chain,
//             ans_host,
//             version_control,
//             version,
//         }
//     }
//     /// Loads a deployment instance from a live chain given the **version_control_address**.
//     pub fn load_from_version_control(
//         chain: Chain,
//         abstract_version: &Version,
//         version_control_address: &Addr,
//     ) -> Result<Self, crate::AbstractBootError> {
//         let version_control = VersionControl::load(chain.clone(), version_control_address);

//         // TODO: get the version dynamically
//         // let info = &self.chain.runtime.block_on(DaemonQuerier::contract_info(
//         //     chain.sender.channel(),
//         //     self.address()?,
//         // ))?;

//         let result = version_control.get_api_addr(ANS_HOST, ModuleVersion::Latest);

//         let ans_host = AnsHost::load(chain.clone(), &result?);

//         Ok(Self {
//             chain,
//             ans_host,
//             version_control,
//             version: abstract_version.clone(),
//         })
//     }

//     /// Uploads, instantiates, and registers a new API module.
//     pub fn deploy_api<TInitMsg>(
//         &self,
//         api: &mut Contract<Chain>,
//         version: Version,
//         api_init_msg: TInitMsg,
//     ) -> Result<(), crate::AbstractBootError>
//     where
//         TInitMsg: Serialize + Debug,
//     {
//         // check for existing version
//         let version_check = self
//             .version_control
//             .get_api_addr(&api.id, ModuleVersion::from(version.to_string()));

//         if version_check.is_ok() {
//             return Err(StdErr(format!(
//                 "API {} already exists with version {}",
//                 api.id, version
//             ))
//             .into());
//         };

//         api.upload()?;
//         let init_msg = abstract_os::api::InstantiateMsg {
//             app: api_init_msg,
//             base: abstract_os::api::BaseInstantiateMsg {
//                 ans_host_address: self.ans_host.address()?.into(),
//                 version_control_address: self.version_control.address()?.into(),
//             },
//         };
//         api.instantiate(&init_msg, None, None)?;

//         self.version_control.register_apis(vec![api], &version)?;
//         Ok(())
//     }
// }