variable_provider_pkg/
lib.rs

1pub mod msgs {
2    use std::collections::BTreeMap;
3
4    use cosmwasm_schema::{cw_serde, QueryResponses};
5    use cosmwasm_std::Addr;
6
7    use super::definitions::Variable;
8
9    #[cw_serde]
10    pub struct InstantiateMsg {
11        pub owners: Vec<String>,
12    }
13
14    #[cw_serde]
15    pub enum ExecuteMsg {
16        RegisterVariable(RegisterVariableMsg),
17        RemoveVariable(RemoveVariableMsg),
18        UpdateOwners(UpdateOwnerMsg),
19    }
20
21    #[cw_serde]
22    pub struct RegisterVariableMsg {
23        pub key: String,
24        pub value: Variable,
25    }
26
27    #[cw_serde]
28    pub struct RemoveVariableMsg {
29        pub key: String,
30    }
31
32    #[cw_serde]
33    pub struct UpdateOwnerMsg {
34        pub add: Option<Vec<String>>,
35        pub remove: Option<Vec<String>>,
36    }
37
38    #[cw_serde]
39    #[derive(QueryResponses)]
40    pub enum QueryMsg {
41        #[returns(Addr)]
42        GetVariable { key: String },
43        #[returns(BTreeMap<String, Variable>)]
44        GetVariables { keys: Vec<String> },
45        #[returns(Vec<(String, Variable)>)]
46        AllVariables {
47            start_after: Option<String>,
48            limit: Option<u32>,
49        },
50    }
51
52    #[cw_serde]
53    pub struct MigrateMsg {}
54}
55
56pub mod definitions {
57
58    use cosmwasm_schema::cw_serde;
59    use cosmwasm_std::{from_json, Addr, Binary, Decimal, Deps, StdError, StdResult, Uint128};
60    use serde::de::DeserializeOwned;
61
62    #[cw_serde]
63    pub struct Config {
64        pub owners: Vec<Addr>,
65    }
66
67    impl Config {
68        pub fn validate_owner(&self, address: &Addr) -> StdResult<()> {
69            if !self.owners.contains(address) {
70                return Err(StdError::generic_err(format!(
71                    "{} is not an owner",
72                    address
73                )));
74            }
75
76            Ok(())
77        }
78        pub fn validate(&self) -> StdResult<()> {
79            if self.owners.is_empty() {
80                return Err(StdError::generic_err("Invalid 0 owners. Needed at least 1"));
81            }
82
83            Ok(())
84        }
85    }
86
87    #[cw_serde]
88    pub enum Variable {
89        String(String),
90        Addr(Addr),
91        Uint128(Uint128),
92        U64(u64),
93        Decimal(Decimal),
94        Binary(Binary),
95    }
96
97    impl Variable {
98        pub fn unwrap_string(&self) -> StdResult<String> {
99            if let Variable::String(val) = self {
100                Ok(val.clone())
101            } else {
102                Err(StdError::generic_err(format!(
103                    "Variable is not String, {:?}",
104                    self
105                )))
106            }
107        }
108
109        pub fn unwrap_addr(&self) -> StdResult<Addr> {
110            if let Variable::Addr(val) = self {
111                Ok(val.clone())
112            } else {
113                Err(StdError::generic_err(format!(
114                    "Variable is not Addr, {:?}",
115                    self
116                )))
117            }
118        }
119
120        pub fn unwrap_uint128(&self) -> StdResult<Uint128> {
121            if let Variable::Uint128(val) = self {
122                Ok(*val)
123            } else {
124                Err(StdError::generic_err(format!(
125                    "Variable is not Uint128, {:?}",
126                    self
127                )))
128            }
129        }
130
131        pub fn unwrap_u64(&self) -> StdResult<u64> {
132            if let Variable::U64(val) = self {
133                Ok(*val)
134            } else {
135                Err(StdError::generic_err(format!(
136                    "Variable is not u64, {:?}",
137                    self
138                )))
139            }
140        }
141
142        pub fn unwrap_decimal(&self) -> StdResult<Decimal> {
143            if let Variable::Decimal(val) = self {
144                Ok(*val)
145            } else {
146                Err(StdError::generic_err(format!(
147                    "Variable is not Decimal, {:?}",
148                    self
149                )))
150            }
151        }
152
153        pub fn unwrap_binary<T: DeserializeOwned>(&self) -> StdResult<T> {
154            if let Variable::Binary(val) = self {
155                from_json::<T>(val)
156            } else {
157                Err(StdError::generic_err(format!(
158                    "Variable is not Decimal, {:?}",
159                    self
160                )))
161            }
162        }
163
164        pub fn validate(self, deps: Deps) -> StdResult<Variable> {
165            if let Variable::Addr(val) = &self {
166                deps.api.addr_validate(val.as_ref())?;
167            }
168
169            Ok(self)
170        }
171    }
172}
173
174pub mod helper {
175    use std::collections::BTreeMap;
176
177    use cosmwasm_std::{Deps, QuerierWrapper, StdResult};
178
179    use crate::definitions::Variable;
180
181    use super::msgs::QueryMsg;
182
183    pub fn variable_provider_get_variable(
184        querier: QuerierWrapper,
185        key: impl Into<String>,
186        variable_provider_addr: impl Into<String>,
187    ) -> StdResult<Variable> {
188        querier.query_wasm_smart(
189            variable_provider_addr,
190            &QueryMsg::GetVariable { key: key.into() },
191        )
192    }
193
194    pub fn variable_provider_get_variables(
195        deps: Deps,
196        keys: Vec<impl Into<String>>,
197        address_provider_addr: impl Into<String>,
198    ) -> StdResult<BTreeMap<String, Variable>> {
199        deps.querier.query_wasm_smart(
200            address_provider_addr,
201            &QueryMsg::GetVariables {
202                keys: keys.into_iter().map(|val| val.into()).collect(),
203            },
204        )
205    }
206}