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
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 owner: String,
    }

    #[cw_serde]
    pub enum ExecuteMsg {
        RegisterVariable(RegisterVariableMsg),
        RemoveVariable(RemoveVariableMsg),
    }

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

    #[cw_serde]
    pub struct RemoveVariableMsg {
        pub key: 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 std::collections::BTreeMap;

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

    use super::msgs::QueryMsg;

    #[cw_serde]
    pub struct Config {
        pub owner: Addr,
    }

    impl AssertOwner for Config {
        fn get_admin(&self) -> Addr {
            self.owner.clone()
        }
    }

    #[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 fn variable_provider_get_variable(
        deps: Deps,
        key: impl Into<String>,
        variable_provider: &Addr,
    ) -> StdResult<Variable> {
        deps.querier
            .query_wasm_smart(variable_provider, &QueryMsg::GetVariable { key: key.into() })
    }

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