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
pub mod msgs {
    use std::collections::BTreeMap;

    use cosmwasm_schema::{cw_serde, QueryResponses};
    use cosmwasm_std::Addr;

    use super::definitions::Variable;

    #[cw_serde]
    pub struct InstantiateMsg {
        pub owners: Vec<String>,
    }

    #[cw_serde]
    pub enum ExecuteMsg {
        RegisterVariable(RegisterVariableMsg),
        RegisterVariables(Vec<RegisterVariableMsg>),
        RemoveVariable(RemoveVariableMsg),
        UpdateOwners(UpdateOwnerMsg),
    }

    #[cw_serde]
    pub struct RegisterVariableMsg {
        pub key: String,
        pub value: Variable,
    }

    impl RegisterVariableMsg {
        pub fn new(key: String, value: Variable) -> Self {
            Self { key, value }
        }
    }

    #[cw_serde]
    pub struct RemoveVariableMsg {
        pub key: String,
    }

    #[cw_serde]
    pub struct UpdateOwnerMsg {
        pub add: Option<Vec<String>>,
        pub remove: Option<Vec<String>>,
    }

    #[cw_serde]
    #[derive(QueryResponses)]
    pub enum QueryMsg {
        #[returns(Addr)]
        GetVariable { key: String },
        #[returns(BTreeMap<String, Variable>)]
        GetVariables { keys: Vec<String> },
        #[returns(Vec<(String, Variable)>)]
        AllVariables {
            start_after: Option<String>,
            limit: Option<u32>,
        },
    }

    #[cw_serde]
    pub struct MigrateMsg {}
}

pub mod definitions {

    use cosmwasm_schema::cw_serde;
    use cosmwasm_std::{from_json, Addr, Binary, Decimal, Deps, StdError, StdResult, Uint128};
    use serde::de::DeserializeOwned;

    #[cw_serde]
    pub struct Config {
        pub owners: Vec<Addr>,
    }

    impl Config {
        pub fn validate_owner(&self, address: &Addr) -> StdResult<()> {
            if !self.owners.contains(address) {
                return Err(StdError::generic_err(format!(
                    "{} is not an owner",
                    address
                )));
            }

            Ok(())
        }
        pub fn validate(&self) -> StdResult<()> {
            if self.owners.is_empty() {
                return Err(StdError::generic_err("Invalid 0 owners. Needed at least 1"));
            }

            Ok(())
        }
    }

    #[cw_serde]
    pub enum Variable {
        String(String),
        Addr(Addr),
        Uint128(Uint128),
        U64(u64),
        Decimal(Decimal),
        Binary(Binary),
    }

    impl Variable {
        pub fn unwrap_string(&self) -> StdResult<String> {
            if let Variable::String(val) = self {
                Ok(val.clone())
            } else {
                Err(StdError::generic_err(format!(
                    "Variable is not String, {:?}",
                    self
                )))
            }
        }

        pub fn unwrap_addr(&self) -> StdResult<Addr> {
            if let Variable::Addr(val) = self {
                Ok(val.clone())
            } else {
                Err(StdError::generic_err(format!(
                    "Variable is not Addr, {:?}",
                    self
                )))
            }
        }

        pub fn unwrap_uint128(&self) -> StdResult<Uint128> {
            if let Variable::Uint128(val) = self {
                Ok(*val)
            } else {
                Err(StdError::generic_err(format!(
                    "Variable is not Uint128, {:?}",
                    self
                )))
            }
        }

        pub fn unwrap_u64(&self) -> StdResult<u64> {
            if let Variable::U64(val) = self {
                Ok(*val)
            } else {
                Err(StdError::generic_err(format!(
                    "Variable is not u64, {:?}",
                    self
                )))
            }
        }

        pub fn unwrap_decimal(&self) -> StdResult<Decimal> {
            if let Variable::Decimal(val) = self {
                Ok(*val)
            } else {
                Err(StdError::generic_err(format!(
                    "Variable is not Decimal, {:?}",
                    self
                )))
            }
        }

        pub fn unwrap_binary<T: DeserializeOwned>(&self) -> StdResult<T> {
            if let Variable::Binary(val) = self {
                from_json::<T>(val)
            } else {
                Err(StdError::generic_err(format!(
                    "Variable is not Decimal, {:?}",
                    self
                )))
            }
        }

        pub fn validate(self, deps: Deps) -> StdResult<Variable> {
            if let Variable::Addr(val) = &self {
                deps.api.addr_validate(val.as_ref())?;
            }

            Ok(self)
        }
    }
}

pub mod helper {
    use std::collections::BTreeMap;

    use cosmwasm_std::{Deps, QuerierWrapper, StdResult};

    use crate::definitions::Variable;

    use super::msgs::QueryMsg;

    pub fn variable_manager_get_variable(
        querier: QuerierWrapper,
        key: impl Into<String>,
        variable_manager_addr: impl Into<String>,
    ) -> StdResult<Variable> {
        querier.query_wasm_smart(
            variable_manager_addr,
            &QueryMsg::GetVariable { key: key.into() },
        )
    }

    pub fn variable_manager_get_variables(
        deps: Deps,
        keys: Vec<impl Into<String>>,
        address_manager_addr: impl Into<String>,
    ) -> StdResult<BTreeMap<String, Variable>> {
        deps.querier.query_wasm_smart(
            address_manager_addr,
            &QueryMsg::GetVariables {
                keys: keys.into_iter().map(|val| val.into()).collect(),
            },
        )
    }
}