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
use std::sync::Arc;
use crate::{
error::DeployError,
file::{ContractInfo, CONFIG, WORKSPACE_SETTINGS},
settings::WorkspaceSettings,
};
use futures::executor::block_on;
use lazy_static::lazy_static;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;
lazy_static! {
pub static ref BIN_NAME: String = std::env::current_exe()
.unwrap()
.file_stem()
.unwrap()
.to_owned()
.into_string()
.unwrap();
}
pub fn replace_strings(value: &mut Value, contracts: &Vec<ContractInfo>) -> anyhow::Result<()> {
match value {
Value::String(string) => {
if let Some((_, new)) = string.split_once('&') {
if let Some(contract) = contracts.iter().find(|x| x.name == new) {
match &contract.addr {
Some(addr) => *string = addr.clone(),
None => {
return Err(DeployError::AddrNotFound {
name: contract.name.clone(),
}
.into())
}
}
}
}
}
Value::Array(array) => {
for value in array {
replace_strings(value, contracts)?;
}
}
Value::Object(map) => {
for (_, value) in map {
replace_strings(value, contracts)?;
}
}
_ => {}
}
Ok(())
}
pub fn replace_strings_any<T: Serialize + DeserializeOwned + Clone>(
object: &mut T,
contracts: &Vec<ContractInfo>,
) -> anyhow::Result<()> {
let mut value = serde_json::to_value(object.clone())?;
replace_strings(&mut value, contracts)?;
*object = serde_json::from_value(value)?;
Ok(())
}
pub async fn get_settings() -> anyhow::Result<Arc<WorkspaceSettings>> {
match WORKSPACE_SETTINGS.read().await.clone() {
Some(settings) => Ok(settings),
None => Err(DeployError::SettingsUninitialized.into()),
}
}
pub fn get_code_id(contract_name: &str) -> anyhow::Result<u64> {
let config = block_on(CONFIG.read());
Ok(config
.get_contract(contract_name)?
.code_id
.ok_or(DeployError::CodeIdNotFound)?)
}
pub fn get_addr(contract_name: &str) -> anyhow::Result<String> {
let config = block_on(CONFIG.read());
Ok(config
.get_contract(contract_name)?
.addr
.clone()
.ok_or(DeployError::AddrNotFound {
name: contract_name.to_string(),
})?)
}