Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions packages/std/src/query/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pub enum WasmQuery {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct ContractInfoResponse {
pub code_id: u64,
/// Checksum was added in CosmWasm XXXX.YYY. Unfortunately we need to keep this optional
/// because we otherwise had to break the ContractInfoResponse constructor.
/// See <https://github.com/CosmWasm/cosmwasm/issues/1545>.
pub checksum: Option<String>,
/// address that instantiated this contract
pub creator: String,
/// admin who can run migrations (if any)
Expand All @@ -46,10 +50,92 @@ impl ContractInfoResponse {
pub fn new(code_id: u64, creator: impl Into<String>) -> Self {
Self {
code_id,
checksum: None,
creator: creator.into(),
admin: None,
pinned: false,
ibc_port: None,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::from_slice;

#[test]
fn contractinforesponse_deserialize_works() {
let info: ContractInfoResponse = from_slice(br#"{"code_id":3456,"creator":"tgrade1js7ezrm55fqgxu3p62d9xn6patjku2z7ne5dvg","admin":"tgrade1z363ulwcrxged4z5jswyt5dn5v3lzsemwz9ewj","pinned":false,"ibc_port":"wasm.abcdef"}"#).unwrap();
assert_eq!(
info,
ContractInfoResponse {
code_id: 3456,
checksum: None,
creator: "tgrade1js7ezrm55fqgxu3p62d9xn6patjku2z7ne5dvg".to_string(),
admin: Some("tgrade1z363ulwcrxged4z5jswyt5dn5v3lzsemwz9ewj".to_string()),
pinned: false,
ibc_port: Some("wasm.abcdef".to_string()),
}
);

// JSON now contains extra checksum
let info: ContractInfoResponse = from_slice(br#"{"code_id":3456,"checksum":"75b6183689b80a229ea27994a7c8cd9c17ddd29e947998f2734abda825eac3c0","creator":"tgrade1js7ezrm55fqgxu3p62d9xn6patjku2z7ne5dvg","admin":"tgrade1z363ulwcrxged4z5jswyt5dn5v3lzsemwz9ewj","pinned":false,"ibc_port":"wasm.abcdef"}"#).unwrap();
assert_eq!(
info,
ContractInfoResponse {
code_id: 3456,
checksum: Some(
"75b6183689b80a229ea27994a7c8cd9c17ddd29e947998f2734abda825eac3c0".to_string()
),
creator: "tgrade1js7ezrm55fqgxu3p62d9xn6patjku2z7ne5dvg".to_string(),
admin: Some("tgrade1z363ulwcrxged4z5jswyt5dn5v3lzsemwz9ewj".to_string()),
pinned: false,
ibc_port: Some("wasm.abcdef".to_string()),
}
);
}

#[test]
fn contractinforesponse_from_cosmwasm_1_0_deserialize_works() {
// This is the version sipped in 1.0 contracts.
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct ContractInfoResponseCosmWasm1 {
pub code_id: u64,
/// address that instantiated this contract
pub creator: String,
/// admin who can run migrations (if any)
pub admin: Option<String>,
/// if set, the contract is pinned to the cache, and thus uses less gas when called
pub pinned: bool,
/// set if this contract has bound an IBC port
pub ibc_port: Option<String>,
}

let info: ContractInfoResponseCosmWasm1 = from_slice(br#"{"code_id":3456,"creator":"tgrade1js7ezrm55fqgxu3p62d9xn6patjku2z7ne5dvg","admin":"tgrade1z363ulwcrxged4z5jswyt5dn5v3lzsemwz9ewj","pinned":false,"ibc_port":"wasm.abcdef"}"#).unwrap();
assert_eq!(
info,
ContractInfoResponseCosmWasm1 {
code_id: 3456,
creator: "tgrade1js7ezrm55fqgxu3p62d9xn6patjku2z7ne5dvg".to_string(),
admin: Some("tgrade1z363ulwcrxged4z5jswyt5dn5v3lzsemwz9ewj".to_string()),
pinned: false,
ibc_port: Some("wasm.abcdef".to_string()),
}
);

// JSON now contains extra checksum
let info: ContractInfoResponseCosmWasm1 = from_slice(br#"{"code_id":3456,"checksum":"75b6183689b80a229ea27994a7c8cd9c17ddd29e947998f2734abda825eac3c0","creator":"tgrade1js7ezrm55fqgxu3p62d9xn6patjku2z7ne5dvg","admin":"tgrade1z363ulwcrxged4z5jswyt5dn5v3lzsemwz9ewj","pinned":false,"ibc_port":"wasm.abcdef"}"#).unwrap();
assert_eq!(
info,
ContractInfoResponseCosmWasm1 {
code_id: 3456,
creator: "tgrade1js7ezrm55fqgxu3p62d9xn6patjku2z7ne5dvg".to_string(),
admin: Some("tgrade1z363ulwcrxged4z5jswyt5dn5v3lzsemwz9ewj".to_string()),
pinned: false,
ibc_port: Some("wasm.abcdef".to_string()),
}
);
}
}
9 changes: 6 additions & 3 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,10 @@ mod tests {
if *contract_addr == constract1 {
let response = ContractInfoResponse {
code_id: 4,
checksum: Some(
"58d8c1d619e9ac9d0a9d01f81148101730917511ac0dc70ac8e537a68b591c61"
.to_string(),
),
creator: "lalala".into(),
admin: None,
pinned: false,
Expand Down Expand Up @@ -1519,9 +1523,8 @@ mod tests {
});
match result {
SystemResult::Ok(ContractResult::Ok(value)) => assert_eq!(
value,
br#"{"code_id":4,"creator":"lalala","admin":null,"pinned":false,"ibc_port":null}"#
as &[u8]
String::from_utf8_lossy(value.as_slice()),
r#"{"code_id":4,"checksum":"58d8c1d619e9ac9d0a9d01f81148101730917511ac0dc70ac8e537a68b591c61","creator":"lalala","admin":null,"pinned":false,"ibc_port":null}"#
),
res => panic!("Unexpected result: {:?}", res),
}
Expand Down
29 changes: 11 additions & 18 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,17 @@ mod tests {
2
}

fn mock_resp() -> ContractInfoResponse {
ContractInfoResponse {
code_id: 17,
checksum: Some("aabbccdd".to_string()),
creator: "creator".to_string(),
admin: None,
pinned: false,
ibc_port: None,
}
}

// this just needs to compile to prove we can use it
#[test]
fn use_querier_wrapper_as_querier() {
Expand Down Expand Up @@ -425,15 +436,6 @@ mod tests {
#[test]
fn contract_info() {
const ACCT: &str = "foobar";
fn mock_resp() -> ContractInfoResponse {
ContractInfoResponse {
code_id: 0,
creator: "creator".to_string(),
admin: None,
pinned: false,
ibc_port: None,
}
}

let mut querier: MockQuerier<Empty> = MockQuerier::new(&[(ACCT, &coins(5, "BTC"))]);
querier.update_wasm(|q| -> QuerierResult {
Expand All @@ -456,15 +458,6 @@ mod tests {
#[test]
fn contract_info_err() {
const ACCT: &str = "foobar";
fn mock_resp() -> ContractInfoResponse {
ContractInfoResponse {
code_id: 0,
creator: "creator".to_string(),
admin: None,
pinned: false,
ibc_port: None,
}
}

let mut querier: MockQuerier<Empty> = MockQuerier::new(&[(ACCT, &coins(5, "BTC"))]);
querier.update_wasm(|q| -> QuerierResult {
Expand Down