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
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use cosmwasm_std::{Empty, Querier, QuerierWrapper, QueryRequest, StdResult, Storage, WasmQuery};
use cw_storage_plus::Item;
pub const CONTRACT: Item<ContractVersion> = Item::new("contract_info");
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct ContractVersion {
pub contract: String,
pub version: String,
}
pub fn get_contract_version(store: &dyn Storage) -> StdResult<ContractVersion> {
CONTRACT.load(store)
}
pub fn set_contract_version<T: Into<String>, U: Into<String>>(
store: &mut dyn Storage,
name: T,
version: U,
) -> StdResult<()> {
let val = ContractVersion {
contract: name.into(),
version: version.into(),
};
CONTRACT.save(store, &val)
}
pub fn query_contract_info<Q: Querier, T: Into<String>>(
querier: &Q,
contract_addr: T,
) -> StdResult<ContractVersion> {
let req = QueryRequest::Wasm(WasmQuery::Raw {
contract_addr: contract_addr.into(),
key: CONTRACT.as_slice().into(),
});
QuerierWrapper::<Empty>::new(querier).query(&req)
}
#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::MockStorage;
#[test]
fn get_and_set_work() {
let mut store = MockStorage::new();
assert!(get_contract_version(&store).is_err());
let contract_name = "crate:cw20-base";
let contract_version = "0.2.0";
set_contract_version(&mut store, contract_name, contract_version).unwrap();
let loaded = get_contract_version(&store).unwrap();
let expected = ContractVersion {
contract: contract_name.to_string(),
version: contract_version.to_string(),
};
assert_eq!(expected, loaded);
}
}