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
use std::{
convert::{TryFrom, TryInto},
fmt::Display,
};
use cosmwasm_std::{StdError, StdResult};
use crate::constants::{ASSET_DELIMITER, ATTRIBUTE_DELIMITER, TYPE_DELIMITER};
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::AssetEntry;
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, JsonSchema, PartialOrd, Ord)]
pub struct UncheckedContractEntry {
pub protocol: String,
pub contract: String,
}
impl UncheckedContractEntry {
pub fn new<T: ToString>(protocol: T, contract: T) -> 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<String> for UncheckedContractEntry {
type Error = StdError;
fn try_from(entry: String) -> Result<Self, Self::Error> {
let composite: Vec<&str> = entry.split(ATTRIBUTE_DELIMITER).collect();
if composite.len() != 2 {
return Err(StdError::generic_err(
"contract entry should be formatted as \"protocol:contract_name\".",
));
}
Ok(Self::new(composite[0], composite[1]))
}
}
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, JsonSchema, Eq, PartialOrd, Ord)]
pub struct ContractEntry {
pub protocol: String,
pub contract: String,
}
const STAKING_CONTRACT_PREFIX: &str = "staking";
impl ContractEntry {
pub fn construct_staking_entry(dex_name: &str, assets: &mut [&AssetEntry]) -> Self {
assets.sort();
let joined_assets = assets
.iter()
.map(|a| a.0.clone())
.collect::<Vec<String>>()
.join(ASSET_DELIMITER);
let contract_name = vec![STAKING_CONTRACT_PREFIX, &joined_assets].join(TYPE_DELIMITER);
Self {
protocol: dex_name.to_ascii_lowercase(),
contract: contract_name,
}
}
}
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, "{}:{}", self.protocol, self.contract)
}
}
impl<'a> PrimaryKey<'a> 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<'a> Prefixer<'a> for ContractEntry {
fn prefix(&self) -> Vec<Key> {
let mut res = self.protocol.prefix();
res.extend(self.contract.prefix().into_iter());
res
}
}
impl KeyDeserialize for ContractEntry {
type Output = Self;
#[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(Self {
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())
}
#[cfg(test)]
mod test {
use super::*;
use cosmwasm_std::{testing::mock_dependencies, Addr, Order};
use cw_storage_plus::Map;
use speculoos::prelude::*;
mod implementation {
use super::*;
#[test]
fn construct_staking_entry_should_join_assets_and_add_staking() {
let expected = String::from("staking/asset1,asset2");
let ContractEntry {
contract: actual, ..
} = ContractEntry::construct_staking_entry(
"protocol",
&mut [
&AssetEntry("asset1".to_string()),
&AssetEntry("asset2".to_string()),
],
);
assert_that!(actual).is_equal_to(expected);
}
#[test]
fn construct_staking_entry_should_alpaebetize_assets() {
let expected = String::from("staking/a,b,c,d,e");
let ContractEntry {
contract: actual, ..
} = ContractEntry::construct_staking_entry(
"protocol",
&mut [
&AssetEntry("d".to_string()),
&AssetEntry("b".to_string()),
&AssetEntry("e".to_string()),
&AssetEntry("a".to_string()),
&AssetEntry("c".to_string()),
],
);
assert_that!(actual).is_equal_to(expected);
}
}
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(),
},
)
}
#[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.clone(), &42069)
.unwrap();
assert_eq!(map.load(deps.as_ref().storage, key.clone()).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));
}
#[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.clone(), Addr::unchecked("larry")),
&42069,
)
.unwrap();
map.save(
deps.as_mut().storage,
(key.clone(), 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));
}
#[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));
}
}
}