cosmwasm_std/query/
wasm.rs1use 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 Smart {
17 contract_addr: String,
18 msg: Binary,
20 },
21 Raw {
24 contract_addr: String,
25 key: Binary,
27 },
28 ContractInfo { contract_addr: String },
30 #[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 pub creator: Addr,
41 pub admin: Option<Addr>,
43 pub pinned: bool,
45 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#[non_exhaustive]
68#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
69pub struct CodeInfoResponse {
70 pub code_id: u64,
71 pub creator: Addr,
73 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}