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
use std::str::FromStr;

use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{ensure, Addr, Api, Uint128};
use schemars::JsonSchema;
use semver::Version;
use serde::{Deserialize, Serialize};

use crate::error::ContractError;

#[cw_serde]
pub struct InstantiateMsg {
    pub kernel_address: String,
    pub owner: Option<String>,
}

#[cw_serde]
pub enum ExecuteMsg {
    Publish {
        code_id: u64,
        ado_type: String,
        action_fees: Option<Vec<ActionFee>>,
        version: String,
        publisher: Option<String>,
    },
    Unpublish {
        ado_type: String,
        version: String,
    },
    UpdateActionFees {
        ado_type: String,
        action_fees: Vec<ActionFee>,
    },
    RemoveActionFees {
        ado_type: String,
        actions: Vec<String>,
    },
    UpdatePublisher {
        ado_type: String,
        publisher: String,
    },
}

#[cw_serde]
pub struct ActionFee {
    pub action: String,
    pub asset: String,
    pub amount: Uint128,
    pub receiver: Option<Addr>,
}

impl ActionFee {
    pub fn new(action: String, asset: String, amount: Uint128) -> Self {
        Self {
            action,
            asset,
            amount,
            receiver: None,
        }
    }

    pub fn with_receive(&self, receiver: Addr) -> Self {
        Self {
            action: self.action.clone(),
            asset: self.asset.clone(),
            amount: self.amount,
            receiver: Some(receiver),
        }
    }

    /// Valiades the provided asset for an action fee
    /// An asset is valid if it fits the format "cw20:address" or "native:denom"
    /// If the asset type is cw20 the address is also validated
    /// TODO: Add denom validation in future cosmwasm version
    pub fn validate_asset(&self, api: &dyn Api) -> Result<(), ContractError> {
        let asset_split = self.asset.split(':').collect::<Vec<&str>>();
        // Ensure asset is in the format "cw20:address" or "native:denom"
        // This is double validated as the asset type in the ADODB contract for fees is validated as cw20:* or native:*
        ensure!(
            asset_split.len() == 2 && !asset_split.is_empty(),
            ContractError::InvalidAsset {
                asset: self.asset.clone()
            }
        );
        let asset_type = asset_split[0];
        ensure!(
            asset_type == "cw20" || asset_type == "native",
            ContractError::InvalidAsset {
                asset: self.asset.clone()
            }
        );

        if asset_type == "cw20" {
            api.addr_validate(asset_split[1])?;
        }

        Ok(())
    }

    /// Gets the asset string without the asset type
    ///
    /// i.e. **cw20:address** would return **"address"** or native:denom would return **"denom"**
    pub fn get_asset_string(&self) -> Result<&str, ContractError> {
        match self.asset.split(':').last() {
            Some(asset) => Ok(asset),
            None => Err(ContractError::InvalidAsset {
                asset: self.asset.clone(),
            }),
        }
    }
}

#[cw_serde]
pub struct MigrateMsg {}

#[cw_serde]
pub struct ADOMetadata {
    pub publisher: String,
    pub latest_version: String,
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
    #[returns(u64)]
    CodeId { key: String },
    // #[returns(Vec<u64>)]
    // UnpublishedCodeIds {},
    #[returns(IsUnpublishedCodeIdResponse)]
    IsUnpublishedCodeId { code_id: u64 },
    #[returns(Option<String>)]
    #[serde(rename = "ado_type")]
    ADOType { code_id: u64 },
    #[returns(Vec<String>)]
    #[serde(rename = "all_ado_types")]
    AllADOTypes {
        start_after: Option<String>,
        limit: Option<u32>,
    },
    #[returns(Vec<String>)]
    #[serde(rename = "ado_versions")]
    ADOVersions {
        ado_type: String,
        start_after: Option<String>,
        limit: Option<u32>,
    },
    // #[returns(Vec<String>)]
    // #[serde(rename = "unpublished_ado_versions")]
    // UnpublishedADOVersions { ado_type: String },
    #[returns(Option<ADOMetadata>)]
    #[serde(rename = "ado_metadata")]
    ADOMetadata { ado_type: String },
    #[returns(Option<ActionFee>)]
    ActionFee { ado_type: String, action: String },
    #[returns(Option<ActionFee>)]
    ActionFeeByCodeId { code_id: u64, action: String },
}

#[cw_serde]
pub struct IsUnpublishedCodeIdResponse {
    pub is_unpublished_code_id: bool,
}

#[derive(
    Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, JsonSchema,
)]
pub struct ADOVersion(String);

impl ADOVersion {
    #[inline]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        self.0.as_bytes()
    }

    #[inline]
    pub fn into_string(self) -> String {
        self.0
    }

    #[inline]
    pub fn from_string(string: impl Into<String>) -> ADOVersion {
        ADOVersion(string.into())
    }

    #[inline]
    pub fn from_type(ado_type: impl Into<String>) -> ADOVersion {
        ADOVersion(ado_type.into())
    }

    #[inline]
    pub fn with_version(&self, version: impl Into<String>) -> ADOVersion {
        let mut ado_version = self.clone();
        // Remove any previous version string if present
        ado_version.0 = ado_version.get_type();
        ado_version.0.push('@');
        ado_version.0.push_str(&version.into());
        ado_version
    }

    /// Validates a given ADOVersion
    ///
    /// A valid ADOVersion must:
    /// 1. Be non-empty
    /// 2. Have at most one `@` symbol
    ///
    /// ### Examples
    /// - `ado_type@0.1.0`
    /// - `ado_type`
    /// - `ado_type@latest`
    pub fn validate(&self) -> bool {
        !self.clone().into_string().is_empty()
            && self.clone().into_string().split('@').count() <= 2
            && (self.get_version() == "latest"
                || Version::from_str(self.get_version().as_str()).is_ok())
    }

    /// Gets the version for the given ADOVersion
    ///
    /// Returns `"latest"` if no version provided
    pub fn get_version(&self) -> String {
        match self
            .clone()
            .into_string()
            .split('@')
            .collect::<Vec<&str>>()
            .len()
        {
            1 => "latest".to_string(),
            _ => self.clone().into_string().split('@').collect::<Vec<&str>>()[1].to_string(),
        }
    }

    /// Gets the type for the given ADOVersion
    pub fn get_type(&self) -> String {
        self.clone().into_string().split('@').collect::<Vec<&str>>()[0].to_string()
    }

    /// Gets the type for the given ADOVersion
    pub fn get_tuple(&self) -> (String, String) {
        (self.get_type(), self.get_version())
    }
}

#[cfg(test)]
mod tests {
    use cosmwasm_std::testing::mock_dependencies;

    use super::*;

    #[test]
    fn test_validate() {
        let ado_version = ADOVersion::from_string("valid_version");
        assert!(ado_version.validate());

        let ado_version = ADOVersion::from_string("valid_version@0.1.0");
        assert!(ado_version.validate());

        let ado_version = ADOVersion::from_string("");
        assert!(!ado_version.validate());

        let ado_version = ADOVersion::from_string("not@valid@version");
        assert!(!ado_version.validate());
    }

    #[test]
    fn test_get_version() {
        let ado_version = ADOVersion::from_string("ado_type");
        assert_eq!(ado_version.get_version(), "latest");

        let ado_version = ADOVersion::from_string("ado_type@0.1.0");
        assert_eq!(ado_version.get_version(), "0.1.0");

        let ado_version = ADOVersion::from_string("ado_type@latest");
        assert_eq!(ado_version.get_version(), "latest");
    }

    #[test]
    fn test_get_type() {
        let ado_version = ADOVersion::from_string("ado_type");
        assert_eq!(ado_version.get_type(), "ado_type");

        let ado_version = ADOVersion::from_string("ado_type@0.1.0");
        assert_eq!(ado_version.get_type(), "ado_type");

        let ado_version = ADOVersion::from_string("ado_type@latest");
        assert_eq!(ado_version.get_type(), "ado_type");
    }

    #[test]
    fn test_action_fee_asset() {
        let deps = mock_dependencies();
        let action_fee = ActionFee::new(
            "action".to_string(),
            "cw20:address".to_string(),
            Uint128::zero(),
        );
        assert!(action_fee.validate_asset(deps.as_ref().api).is_ok());

        let action_fee = ActionFee::new(
            "action".to_string(),
            "native:denom".to_string(),
            Uint128::zero(),
        );
        assert!(action_fee.validate_asset(deps.as_ref().api).is_ok());

        let action_fee =
            ActionFee::new("action".to_string(), "cw20:aw".to_string(), Uint128::zero());
        assert!(action_fee.validate_asset(deps.as_ref().api).is_err());

        let action_fee =
            ActionFee::new("action".to_string(), "invalid".to_string(), Uint128::zero());
        assert!(action_fee.validate_asset(deps.as_ref().api).is_err());
    }
}