abstract_std/objects/entry/
contract_entry.rs

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
use std::{fmt::Display, str::FromStr};

use cosmwasm_std::{StdError, StdResult};
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::constants::ATTRIBUTE_DELIMITER;

/// Key to get the Address of a contract
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, JsonSchema, PartialOrd, Ord)]
// Need hash for ans scraper
#[cfg_attr(not(target_arch = "wasm32"), derive(Hash))]
pub struct UncheckedContractEntry {
    pub protocol: String,
    pub contract: String,
}

impl UncheckedContractEntry {
    pub fn new<T: ToString, R: ToString>(protocol: T, contract: R) -> Self {
        Self {
            protocol: protocol.to_string(),
            contract: contract.to_string(),
        }
    }
    pub fn check(self) -> ContractEntry {
        ContractEntry {
            contract: self.contract.to_ascii_lowercase(),
            protocol: self.protocol.to_ascii_lowercase(),
        }
    }
}

impl From<ContractEntry> for UncheckedContractEntry {
    fn from(contract_entry: ContractEntry) -> Self {
        Self {
            protocol: contract_entry.protocol,
            contract: contract_entry.contract,
        }
    }
}

impl TryFrom<&str> for UncheckedContractEntry {
    type Error = StdError;
    /// Try from a string slice like "protocol:contract_name"
    fn try_from(entry: &str) -> Result<Self, Self::Error> {
        let Some((protocol, contract_name)) = entry.split_once(ATTRIBUTE_DELIMITER) else {
            return Err(StdError::generic_err(
                "contract entry should be formatted as \"protocol:contract_name\".",
            ));
        };
        Ok(Self::new(protocol, contract_name))
    }
}

/// Key to get the Address of a contract
/// Use [`UncheckedContractEntry`] to construct this type.  
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, JsonSchema, Eq, PartialOrd, Ord)]
pub struct ContractEntry {
    pub protocol: String,
    pub contract: String,
}

impl FromStr for ContractEntry {
    type Err = StdError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        UncheckedContractEntry::try_from(s).map(Into::into)
    }
}

impl From<UncheckedContractEntry> for ContractEntry {
    fn from(entry: UncheckedContractEntry) -> Self {
        entry.check()
    }
}

impl Display for ContractEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}{ATTRIBUTE_DELIMITER}{}", self.protocol, self.contract)
    }
}

impl PrimaryKey<'_> for &ContractEntry {
    type Prefix = String;

    type SubPrefix = ();

    type Suffix = String;

    type SuperSuffix = Self;

    fn key(&self) -> Vec<cw_storage_plus::Key> {
        let mut keys = self.protocol.key();
        keys.extend(self.contract.key());
        keys
    }
}

impl Prefixer<'_> for &ContractEntry {
    fn prefix(&self) -> Vec<Key> {
        let mut res = self.protocol.prefix();
        res.extend(self.contract.prefix());
        res
    }
}

impl KeyDeserialize for &ContractEntry {
    type Output = ContractEntry;
    const KEY_ELEMS: u16 = 2;

    #[inline(always)]
    fn from_vec(mut value: Vec<u8>) -> StdResult<Self::Output> {
        let mut tu = value.split_off(2);
        let t_len = parse_length(&value)?;
        let u = tu.split_off(t_len);

        Ok(ContractEntry {
            protocol: String::from_vec(tu)?,
            contract: String::from_vec(u)?,
        })
    }
}

#[inline(always)]
fn parse_length(value: &[u8]) -> StdResult<usize> {
    Ok(u16::from_be_bytes(
        value
            .try_into()
            .map_err(|_| StdError::generic_err("Could not read 2 byte length"))?,
    )
    .into())
}

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

#[cfg(test)]
mod test {
    #![allow(clippy::needless_borrows_for_generic_args)]
    use cosmwasm_std::{testing::mock_dependencies, Addr, Order};
    use cw_storage_plus::Map;

    use super::*;

    mod key {
        use super::*;

        fn mock_key() -> ContractEntry {
            ContractEntry {
                protocol: "abstract".to_string(),
                contract: "rocket-ship".to_string(),
            }
        }

        fn mock_keys() -> (ContractEntry, ContractEntry, ContractEntry) {
            (
                ContractEntry {
                    protocol: "abstract".to_string(),
                    contract: "sailing-ship".to_string(),
                },
                ContractEntry {
                    protocol: "abstract".to_string(),
                    contract: "rocket-ship".to_string(),
                },
                ContractEntry {
                    protocol: "shitcoin".to_string(),
                    contract: "pump'n dump".to_string(),
                },
            )
        }

        #[coverage_helper::test]
        fn storage_key_works() {
            let mut deps = mock_dependencies();
            let key = mock_key();
            let map: Map<&ContractEntry, u64> = Map::new("map");

            map.save(deps.as_mut().storage, &key, &42069).unwrap();

            assert_eq!(map.load(deps.as_ref().storage, &key).unwrap(), 42069);

            let items = map
                .range(deps.as_ref().storage, None, None, Order::Ascending)
                .map(|item| item.unwrap())
                .collect::<Vec<_>>();

            assert_eq!(items.len(), 1);
            assert_eq!(items[0], (key, 42069));
        }

        #[coverage_helper::test]
        fn composite_key_works() {
            let mut deps = mock_dependencies();
            let key = mock_key();
            let map: Map<(&ContractEntry, Addr), u64> = Map::new("map");

            map.save(
                deps.as_mut().storage,
                (&key, Addr::unchecked("larry")),
                &42069,
            )
            .unwrap();

            map.save(
                deps.as_mut().storage,
                (&key, Addr::unchecked("jake")),
                &69420,
            )
            .unwrap();

            let items = map
                .prefix(&key)
                .range(deps.as_ref().storage, None, None, Order::Ascending)
                .map(|item| item.unwrap())
                .collect::<Vec<_>>();

            assert_eq!(items.len(), 2);
            assert_eq!(items[0], (Addr::unchecked("jake"), 69420));
            assert_eq!(items[1], (Addr::unchecked("larry"), 42069));
        }

        #[coverage_helper::test]
        fn partial_key_works() {
            let mut deps = mock_dependencies();
            let (key1, key2, key3) = mock_keys();
            let map: Map<&ContractEntry, u64> = Map::new("map");

            map.save(deps.as_mut().storage, &key1, &42069).unwrap();

            map.save(deps.as_mut().storage, &key2, &69420).unwrap();

            map.save(deps.as_mut().storage, &key3, &999).unwrap();

            let items = map
                .prefix("abstract".to_string())
                .range(deps.as_ref().storage, None, None, Order::Ascending)
                .map(|item| item.unwrap())
                .collect::<Vec<_>>();

            assert_eq!(items.len(), 2);
            assert_eq!(items[0], ("rocket-ship".to_string(), 69420));
            assert_eq!(items[1], ("sailing-ship".to_string(), 42069));
        }

        #[coverage_helper::test]
        fn test_contract_entry_from_str() {
            let contract_entry_str = "abstract:rocket-ship";
            let contract_entry = ContractEntry::from_str(contract_entry_str).unwrap();

            assert_eq!(contract_entry.protocol, "abstract");
            assert_eq!(contract_entry.contract, "rocket-ship");

            let contract_entry_str = "foo:>420/,:z/69";
            let contract_entry = ContractEntry::from_str(contract_entry_str).unwrap();

            assert_eq!(contract_entry.protocol, "foo");
            assert_eq!(contract_entry.contract, ">420/,:z/69");

            // Wrong formatting
            let contract_entry_str = "shitcoin/,>rocket-ship";
            let err = ContractEntry::from_str(contract_entry_str).unwrap_err();

            assert_eq!(
                err,
                StdError::generic_err(
                    "contract entry should be formatted as \"protocol:contract_name\".",
                )
            );
        }

        #[coverage_helper::test]
        fn test_contract_entry_to_string() {
            let contract_entry_str = "abstract:app";
            let contract_entry = ContractEntry::from_str(contract_entry_str).unwrap();

            assert_eq!(contract_entry.to_string(), contract_entry_str);
        }
    }
}