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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#[cfg(feature = "ledger")]
use std::rc::Rc;
use std::{
    fmt::Display,
    fs::{create_dir_all, OpenOptions},
    io::prelude::*,
    path::PathBuf,
};

use cosm_tome::{
    clients::{client::CosmTome, cosmos_grpc::CosmosgRPC},
    config::cfg::ChainConfig,
    signing_key::key::{Key, KeyringParams, SigningKey},
};
// use cosmrs::tendermint::chain::Id;
use inquire::{Confirm, CustomType, Select, Text};
use interactive_parse::traits::InteractiveParseObj;
use lazy_static::lazy_static;
#[cfg(feature = "ledger")]
use ledger_utility::Connection;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::error::{DeployError, DeployResult};
#[cfg(feature = "ledger")]
use crate::ledger::get_ledger_info;

lazy_static! {
    pub static ref CONFIG_PATH: PathBuf = PathBuf::from("deployment/.wasm-deploy/config.json");
    pub static ref BUILD_DIR: PathBuf = PathBuf::from("target/debug/");
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Env {
    pub is_active: bool,
    pub env_id: String,
    pub chain_id: String,
    pub contracts: Vec<ContractInfo>,
    pub key_name: String,
}

impl Display for Env {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.env_id.fmt(f)
    }
}

#[derive(Clone, Debug, JsonSchema, PartialEq, Serialize, Deserialize)]
pub struct ContractInfo {
    pub name: String,
    pub addr: Option<String>,
    pub code_id: Option<u64>,
}

impl Display for ContractInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.name.fmt(f)
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Settings {
    pub store_code_chunk_size: usize,
}

impl Default for Settings {
    fn default() -> Self {
        Settings {
            store_code_chunk_size: 2,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct Config {
    #[serde(default)]
    pub settings: Settings,
    pub shell_completion_dir: Option<PathBuf>,
    pub chains: Vec<ChainConfig>,
    pub envs: Vec<Env>,
    pub keys: Vec<SigningKey>,
}

impl Config {
    pub fn init() -> Result<Config, DeployError> {
        create_dir_all(CONFIG_PATH.parent().expect("Invalid CONFIG_PATH")).unwrap();
        let config = Config::default();
        Ok(config)
    }

    pub fn load() -> Result<Config, DeployError> {
        let config = match std::fs::read(CONFIG_PATH.as_path()) {
            Ok(serialized) => serde_json::from_slice(&serialized)?,
            Err(_) => return Err(DeployError::ConfigNotFound {}),
        };

        Ok(config)
    }

    pub fn save(&self) -> Result<(), DeployError> {
        let mut file = OpenOptions::new()
            .truncate(true)
            .write(true)
            .create(true)
            .open(CONFIG_PATH.as_path())?;
        let serialized = serde_json::to_vec_pretty(self)?;
        file.write_all(&serialized)?;
        Ok(())
    }

    pub fn get_active_env_mut(&mut self) -> Result<&mut Env, DeployError> {
        match self.envs.iter().position(|x| x.is_active) {
            Some(index) => Ok(self.envs.get_mut(index).unwrap()),
            None => {
                println!("No env found, creating one");
                Ok(self.add_env()?)
            }
        }
    }

    pub fn get_active_env(&self) -> Result<&Env, DeployError> {
        match self.envs.iter().position(|x| x.is_active) {
            Some(index) => Ok(self.envs.get(index).unwrap()),
            None => Err(DeployError::EnvNotFound),
        }
    }

    pub fn get_active_chain_info(&mut self) -> Result<ChainConfig, DeployError> {
        let chains = self.chains.clone();
        let env = self.get_active_env_mut()?;
        match chains.iter().find(|x| x.chain_id == env.chain_id) {
            Some(chain_info) => Ok(chain_info.clone()),
            None => self.add_chain(),
        }
    }

    #[allow(unused_mut)]
    pub async fn get_active_key(&mut self) -> Result<SigningKey, DeployError> {
        let active_key_name = self.get_active_env()?.key_name.clone();
        let key = self
            .keys
            .iter_mut()
            .find(|x| x.name == active_key_name)
            .ok_or(DeployError::KeyNotFound {
                key_name: active_key_name,
            })?;
        let mut key = key.clone();
        #[cfg(feature = "ledger")]
        if let Key::Ledger { connection, .. } = &mut key.key {
            if connection.is_none() {
                *connection = Some(Rc::new(Connection::new().await));
            }
        }
        Ok(key)
    }

    pub fn _get_active_chain_id(&mut self) -> Result<String, DeployError> {
        Ok(self.get_active_chain_info()?.chain_id)
    }

    // pub fn _get_client(&mut self) -> Result<impl Client, DeployError> {
    //     let url = self.get_active_chain_info()?.rpc_endpoint;
    //     Ok(HttpClient::new(url.as_str()).unwrap())
    // }

    pub fn add_chain_from(&mut self, chain_info: ChainConfig) -> Result<ChainConfig, DeployError> {
        match self
            .chains
            .iter()
            .any(|x| x.chain_id == chain_info.chain_id)
        {
            true => Err(DeployError::ChainAlreadyExists),
            false => {
                self.chains.push(chain_info.clone());
                Ok(chain_info)
            }
        }
    }

    pub fn add_chain(&mut self) -> Result<ChainConfig, DeployError> {
        let chain_info = ChainConfig::parse_to_obj()?;
        self.add_chain_from(chain_info.clone())?;
        Ok(chain_info)
    }

    /// Adds or replaces a contract
    pub fn add_contract_from(
        &mut self,
        new_contract: ContractInfo,
    ) -> Result<ContractInfo, DeployError> {
        let env = self.get_active_env_mut()?;
        match env
            .contracts
            .iter_mut()
            .find(|x| x.name == new_contract.name)
        {
            Some(contract) => *contract = new_contract.clone(),
            None => env.contracts.push(new_contract.clone()),
        }
        Ok(new_contract)
    }

    pub fn add_contract(&mut self) -> Result<ContractInfo, DeployError> {
        let contract = ContractInfo::parse_to_obj()?;
        self.add_contract_from(contract.clone())?;
        Ok(contract)
    }

    pub fn get_contract_addr_mut(&mut self, name: &String) -> Result<&String, DeployError> {
        let contract = self.get_contract(name)?;
        match &contract.addr {
            Some(addr) => Ok(addr),
            None => Err(DeployError::NoAddr),
        }
    }

    pub fn _get_code_id(&mut self, name: &String) -> Result<&mut u64, DeployError> {
        let contract = self.get_contract(name)?;
        match &mut contract.code_id {
            Some(code_id) => Ok(code_id),
            None => Err(DeployError::CodeIdNotFound),
        }
    }

    pub fn get_contract(&mut self, name: &String) -> Result<&mut ContractInfo, DeployError> {
        let env = self.get_active_env_mut()?;
        env.contracts
            .iter_mut()
            .find(|x| &x.name == name)
            .ok_or(DeployError::ContractNotFound)
    }

    pub fn add_key_from(&mut self, key: SigningKey) -> Result<SigningKey, DeployError> {
        if self.keys.iter().any(|x| x.name == key.name) {
            return Err(DeployError::KeyAlreadyExists);
        }
        self.keys.push(key.clone());
        Ok(key)
    }

    pub async fn add_key(&mut self) -> Result<SigningKey, DeployError> {
        let key_type = Select::new("Select Key Type", vec!["Keyring", "Mnemonic"]).prompt()?;
        let key = match key_type {
            "Keyring" => {
                let params = KeyringParams::parse_to_obj()?;
                let entry = keyring::Entry::new(&params.service, &params.key_name);
                let password = inquire::Text::new("Mnemonic?").prompt()?;
                entry.set_password(password.as_str())?;
                Key::Keyring(params)
            }
            "Mnemonic" => Key::Mnemonic(Text::new("Enter Mnemonic").prompt()?),
            #[cfg(feature = "ledger")]
            "Ledger" => {
                let chain_info = self.get_active_chain_info()?;
                let connection = Connection::new().await;
                let info = get_ledger_info(&connection, chain_info).await?;
                Key::Ledger {
                    info,
                    connection: None,
                }
            }
            _ => panic!("should not happen"),
        };
        let name = Text::new("Key Name?").prompt()?;
        let derivation_path = Text::new("Derivation Path?")
            .with_help_message("\"m/44'/118'/0'/0/0\"")
            .prompt()?;
        self.add_key_from(SigningKey {
            name,
            key,
            derivation_path,
        })
    }

    pub fn add_env(&mut self) -> Result<&mut Env, DeployError> {
        println!("Creating new deployment environment");
        let env_id = inquire::Text::new("Environment label?")
            .with_help_message("\"dev\", \"prod\", \"other\"")
            .prompt()?;
        if self.envs.iter().any(|x| x.env_id == env_id) {
            return Err(DeployError::EnvAlreadyExists);
        }
        let chain_id = inquire::Select::new(
            "Select which chain to activate",
            self.chains
                .iter()
                .map(|x| x.chain_id.clone())
                .collect::<Vec<_>>(),
        )
        .with_help_message("\"dev\", \"prod\", \"other\"")
        .prompt()?;
        let key_name = inquire::Select::new(
            "Select key",
            self.keys.iter().map(|x| x.name.clone()).collect::<Vec<_>>(),
        )
        .with_help_message("\"my_key\"")
        .prompt()?;
        let env = Env {
            is_active: true,
            key_name,
            env_id,
            chain_id,
            contracts: vec![],
        };
        self.envs.push(env);
        if self.envs.len() > 1 {
            self.change_env()?
        }
        Ok(self.envs.last_mut().unwrap())
    }

    pub fn change_env(&mut self) -> Result<(), DeployError> {
        let env = Select::new("Select env to activate", self.envs.clone()).prompt()?;
        self.envs.iter_mut().for_each(|x| x.is_active = *x == env);
        Ok(())
    }

    pub fn get_grpc_client(&mut self) -> DeployResult<CosmTome<CosmosgRPC>> {
        let chain_info = self.get_active_chain_info()?;
        let client = CosmosgRPC::new(
            chain_info
                .grpc_endpoint
                .clone()
                .ok_or(DeployError::MissingGRpc)?,
        );
        Ok(CosmTome::new(chain_info, client))
    }
}

// TODO: move this into impl block
pub fn get_shell_completion_dir() -> Result<Option<PathBuf>, DeployError> {
    let mut config = Config::load()?;
    match config.shell_completion_dir {
        Some(shell_completion_path) => Ok(Some(shell_completion_path)),
        None => {
            let ans =
                Confirm::new("Shell completion directory not found.\nWould you like to add one?")
                    .with_default(true)
                    .prompt()?;
            match ans {
                true => {
                    let string =
                        CustomType::<String>::new("Enter you shell completion script directory.")
                            .prompt()?;
                    let path = PathBuf::from(string);
                    match path.is_dir() {
                        true => {
                            config.shell_completion_dir = Some(path.clone());
                            config.save()?;
                            Ok(Some(path))
                        }
                        false => Err(DeployError::InvalidDir),
                    }
                }
                false => Ok(None),
            }
        }
    }
}