cosmwasm_std/query/
wasm.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::prelude::*;
5use crate::{Addr, Binary, Checksum};
6
7use super::query_response::QueryResponseType;
8
9#[non_exhaustive]
10#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
11#[serde(rename_all = "snake_case")]
12pub enum WasmQuery {
13    /// this queries the public API of another contract at a known address (with known ABI)
14    /// Return value is whatever the contract returns (caller should know), wrapped in a
15    /// ContractResult that is JSON encoded.
16    Smart {
17        contract_addr: String,
18        /// msg is the json-encoded QueryMsg struct
19        msg: Binary,
20    },
21    /// this queries the raw kv-store of the contract.
22    /// returns the raw, unparsed data stored at that key, which may be an empty vector if not present
23    Raw {
24        contract_addr: String,
25        /// Key is the raw key used in the contracts Storage
26        key: Binary,
27    },
28    /// Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime
29    ContractInfo { contract_addr: String },
30    /// Returns a [`CodeInfoResponse`] with metadata of the code
31    #[cfg(feature = "cosmwasm_1_2")]
32    CodeInfo { code_id: u64 },
33}
34
35#[non_exhaustive]
36#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
37pub struct ContractInfoResponse {
38    pub code_id: u64,
39    /// address that instantiated this contract
40    pub creator: Addr,
41    /// admin who can run migrations (if any)
42    pub admin: Option<Addr>,
43    /// if set, the contract is pinned to the cache, and thus uses less gas when called
44    pub pinned: bool,
45    /// set if this contract has bound an IBC port
46    pub ibc_port: Option<String>,
47}
48
49impl QueryResponseType for ContractInfoResponse {}
50
51impl_response_constructor!(
52    ContractInfoResponse,
53    code_id: u64,
54    creator: Addr,
55    admin: Option<Addr>,
56    pinned: bool,
57    ibc_port: Option<String>
58);
59
60/// The essential data from wasmd's [CodeInfo]/[CodeInfoResponse].
61///
62/// `code_hash`/`data_hash` was renamed to `checksum` to follow the CosmWasm
63/// convention and naming in `instantiate2_address`.
64///
65/// [CodeInfo]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/types.proto#L62-L72
66/// [CodeInfoResponse]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/query.proto#L184-L199
67#[non_exhaustive]
68#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
69pub struct CodeInfoResponse {
70    pub code_id: u64,
71    /// The address that initially stored the code
72    pub creator: Addr,
73    /// The hash of the Wasm blob
74    pub checksum: Checksum,
75}
76
77impl_response_constructor!(
78    CodeInfoResponse,
79    code_id: u64,
80    creator: Addr,
81    checksum: Checksum
82);
83
84impl QueryResponseType for CodeInfoResponse {}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use crate::to_json_binary;
90
91    #[test]
92    fn wasm_query_contract_info_serialization() {
93        let query = WasmQuery::ContractInfo {
94            contract_addr: "aabbccdd456".into(),
95        };
96        let json = to_json_binary(&query).unwrap();
97        assert_eq!(
98            String::from_utf8_lossy(&json),
99            r#"{"contract_info":{"contract_addr":"aabbccdd456"}}"#,
100        );
101    }
102
103    #[test]
104    #[cfg(feature = "cosmwasm_1_2")]
105    fn wasm_query_code_info_serialization() {
106        let query = WasmQuery::CodeInfo { code_id: 70 };
107        let json = to_json_binary(&query).unwrap();
108        assert_eq!(
109            String::from_utf8_lossy(&json),
110            r#"{"code_info":{"code_id":70}}"#,
111        );
112    }
113
114    #[test]
115    fn contract_info_response_serialization() {
116        let response = ContractInfoResponse {
117            code_id: 67,
118            creator: Addr::unchecked("jane"),
119            admin: Some(Addr::unchecked("king")),
120            pinned: true,
121            ibc_port: Some("wasm.123".to_string()),
122        };
123        let json = to_json_binary(&response).unwrap();
124        assert_eq!(
125            String::from_utf8_lossy(&json),
126            r#"{"code_id":67,"creator":"jane","admin":"king","pinned":true,"ibc_port":"wasm.123"}"#,
127        );
128    }
129
130    #[test]
131    #[cfg(feature = "cosmwasm_1_2")]
132    fn code_info_response_serialization() {
133        use crate::Checksum;
134
135        let response = CodeInfoResponse {
136            code_id: 67,
137            creator: Addr::unchecked("jane"),
138            checksum: Checksum::from_hex(
139                "f7bb7b18fb01bbf425cf4ed2cd4b7fb26a019a7fc75a4dc87e8a0b768c501f00",
140            )
141            .unwrap(),
142        };
143        let json = to_json_binary(&response).unwrap();
144        assert_eq!(
145            String::from_utf8_lossy(&json),
146            r#"{"code_id":67,"creator":"jane","checksum":"f7bb7b18fb01bbf425cf4ed2cd4b7fb26a019a7fc75a4dc87e8a0b768c501f00"}"#,
147        );
148    }
149}