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
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use cosmwasm_std::{
HumanAddr, Querier, QueryRequest, ReadonlyStorage, StdResult, Storage, WasmQuery,
};
use cosmwasm_storage::{to_length_prefixed, ReadonlySingleton, Singleton};
pub const PREFIX_INFO: &[u8] = b"contract_info";
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct ContractVersion {
pub contract: String,
pub version: String,
}
pub fn get_contract_version<S: ReadonlyStorage>(storage: &S) -> StdResult<ContractVersion> {
ReadonlySingleton::new(storage, PREFIX_INFO).load()
}
pub fn set_contract_version<S: Storage, T: Into<String>, U: Into<String>>(
storage: &mut S,
name: T,
version: U,
) -> StdResult<()> {
Singleton::new(storage, PREFIX_INFO).save(&ContractVersion {
contract: name.into(),
version: version.into(),
})
}
pub fn query_contract_info<Q: Querier, T: Into<HumanAddr>>(
querier: &Q,
contract_addr: T,
) -> StdResult<ContractVersion> {
let req = QueryRequest::Wasm(WasmQuery::Raw {
contract_addr: contract_addr.into(),
key: to_length_prefixed(PREFIX_INFO).into(),
});
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);
}
}