cosmwasm_vm/testing/
calls.rs

1//! This file has some helpers for integration tests.
2//! They should be imported via full path to ensure there is no confusion
3//! use cosmwasm_vm::testing::X
4use serde::{de::DeserializeOwned, Serialize};
5
6use cosmwasm_std::{
7    ContractResult, CustomMsg, Env, MessageInfo, MigrateInfo, QueryResponse, Reply, Response,
8};
9#[cfg(feature = "stargate")]
10use cosmwasm_std::{
11    Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg,
12    IbcChannelOpenMsg, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg,
13    IbcReceiveResponse,
14};
15
16use crate::calls::{
17    call_execute, call_instantiate, call_migrate, call_migrate_with_info, call_query, call_reply,
18    call_sudo,
19};
20#[cfg(feature = "stargate")]
21use crate::calls::{
22    call_ibc_channel_close, call_ibc_channel_connect, call_ibc_channel_open, call_ibc_packet_ack,
23    call_ibc_packet_receive, call_ibc_packet_timeout,
24};
25use crate::instance::Instance;
26use crate::serde::to_vec;
27use crate::{BackendApi, Querier, Storage};
28
29/// Mimics the call signature of the smart contracts.
30/// Thus it moves env and msg rather than take them as reference.
31/// This is inefficient here, but only used in test code.
32pub fn instantiate<A, S, Q, M, U>(
33    instance: &mut Instance<A, S, Q>,
34    env: Env,
35    info: MessageInfo,
36    msg: M,
37) -> ContractResult<Response<U>>
38where
39    A: BackendApi + 'static,
40    S: Storage + 'static,
41    Q: Querier + 'static,
42    M: Serialize,
43    U: DeserializeOwned + CustomMsg,
44{
45    let serialized_msg = to_vec(&msg).expect("Testing error: Could not serialize request message");
46    call_instantiate(instance, &env, &info, &serialized_msg).expect("VM error")
47}
48
49// execute mimics the call signature of the smart contracts.
50// thus it moves env and msg rather than take them as reference.
51// this is inefficient here, but only used in test code
52pub fn execute<A, S, Q, M, U>(
53    instance: &mut Instance<A, S, Q>,
54    env: Env,
55    info: MessageInfo,
56    msg: M,
57) -> ContractResult<Response<U>>
58where
59    A: BackendApi + 'static,
60    S: Storage + 'static,
61    Q: Querier + 'static,
62    M: Serialize,
63    U: DeserializeOwned + CustomMsg,
64{
65    let serialized_msg = to_vec(&msg).expect("Testing error: Could not serialize request message");
66    call_execute(instance, &env, &info, &serialized_msg).expect("VM error")
67}
68
69// migrate mimics the call signature of the smart contracts.
70// thus it moves env and msg rather than take them as reference.
71// this is inefficient here, but only used in test code
72pub fn migrate<A, S, Q, M, U>(
73    instance: &mut Instance<A, S, Q>,
74    env: Env,
75    msg: M,
76) -> ContractResult<Response<U>>
77where
78    A: BackendApi + 'static,
79    S: Storage + 'static,
80    Q: Querier + 'static,
81    M: Serialize,
82    U: DeserializeOwned + CustomMsg,
83{
84    let serialized_msg = to_vec(&msg).expect("Testing error: Could not serialize request message");
85    call_migrate(instance, &env, &serialized_msg).expect("VM error")
86}
87
88// migrate mimics the call signature of the smart contracts.
89// thus it moves env and msg rather than take them as reference.
90// this is inefficient here, but only used in test code
91pub fn migrate_with_info<A, S, Q, M, U>(
92    instance: &mut Instance<A, S, Q>,
93    env: Env,
94    msg: M,
95    migrate_info: MigrateInfo,
96) -> ContractResult<Response<U>>
97where
98    A: BackendApi + 'static,
99    S: Storage + 'static,
100    Q: Querier + 'static,
101    M: Serialize,
102    U: DeserializeOwned + CustomMsg,
103{
104    let serialized_msg = to_vec(&msg).expect("Testing error: Could not serialize request message");
105    call_migrate_with_info(instance, &env, &serialized_msg, &migrate_info).expect("VM error")
106}
107
108// sudo mimics the call signature of the smart contracts.
109// thus it moves env and msg rather than take them as reference.
110// this is inefficient here, but only used in test code
111pub fn sudo<A, S, Q, M, U>(
112    instance: &mut Instance<A, S, Q>,
113    env: Env,
114    msg: M,
115) -> ContractResult<Response<U>>
116where
117    A: BackendApi + 'static,
118    S: Storage + 'static,
119    Q: Querier + 'static,
120    M: Serialize,
121    U: DeserializeOwned + CustomMsg,
122{
123    let serialized_msg = to_vec(&msg).expect("Testing error: Could not serialize request message");
124    call_sudo(instance, &env, &serialized_msg).expect("VM error")
125}
126
127// reply mimics the call signature of the smart contracts.
128// thus it moves env and msg rather than take them as reference.
129// this is inefficient here, but only used in test code
130pub fn reply<A, S, Q, U>(
131    instance: &mut Instance<A, S, Q>,
132    env: Env,
133    msg: Reply,
134) -> ContractResult<Response<U>>
135where
136    A: BackendApi + 'static,
137    S: Storage + 'static,
138    Q: Querier + 'static,
139    U: DeserializeOwned + CustomMsg,
140{
141    call_reply(instance, &env, &msg).expect("VM error")
142}
143
144// query mimics the call signature of the smart contracts.
145// thus it moves env and msg rather than take them as reference.
146// this is inefficient here, but only used in test code
147pub fn query<A, S, Q, M>(
148    instance: &mut Instance<A, S, Q>,
149    env: Env,
150    msg: M,
151) -> ContractResult<QueryResponse>
152where
153    A: BackendApi + 'static,
154    S: Storage + 'static,
155    Q: Querier + 'static,
156    M: Serialize,
157{
158    let serialized_msg = to_vec(&msg).expect("Testing error: Could not serialize request message");
159    call_query(instance, &env, &serialized_msg).expect("VM error")
160}
161
162// ibc_channel_open mimics the call signature of the smart contracts.
163// thus it moves env and channel rather than take them as reference.
164// this is inefficient here, but only used in test code
165#[cfg(feature = "stargate")]
166pub fn ibc_channel_open<A, S, Q>(
167    instance: &mut Instance<A, S, Q>,
168    env: Env,
169    msg: IbcChannelOpenMsg,
170) -> ContractResult<Option<Ibc3ChannelOpenResponse>>
171where
172    A: BackendApi + 'static,
173    S: Storage + 'static,
174    Q: Querier + 'static,
175{
176    call_ibc_channel_open(instance, &env, &msg).expect("VM error")
177}
178
179// ibc_channel_connect mimics the call signature of the smart contracts.
180// thus it moves env and channel rather than take them as reference.
181// this is inefficient here, but only used in test code
182#[cfg(feature = "stargate")]
183pub fn ibc_channel_connect<A, S, Q, U>(
184    instance: &mut Instance<A, S, Q>,
185    env: Env,
186    msg: IbcChannelConnectMsg,
187) -> ContractResult<IbcBasicResponse<U>>
188where
189    A: BackendApi + 'static,
190    S: Storage + 'static,
191    Q: Querier + 'static,
192    U: DeserializeOwned + CustomMsg,
193{
194    call_ibc_channel_connect(instance, &env, &msg).expect("VM error")
195}
196
197// ibc_channel_close mimics the call signature of the smart contracts.
198// thus it moves env and channel rather than take them as reference.
199// this is inefficient here, but only used in test code
200#[cfg(feature = "stargate")]
201pub fn ibc_channel_close<A, S, Q, U>(
202    instance: &mut Instance<A, S, Q>,
203    env: Env,
204    msg: IbcChannelCloseMsg,
205) -> ContractResult<IbcBasicResponse<U>>
206where
207    A: BackendApi + 'static,
208    S: Storage + 'static,
209    Q: Querier + 'static,
210    U: DeserializeOwned + CustomMsg,
211{
212    call_ibc_channel_close(instance, &env, &msg).expect("VM error")
213}
214
215// ibc_packet_receive mimics the call signature of the smart contracts.
216// thus it moves env and packet rather than take them as reference.
217// this is inefficient here, but only used in test code
218#[cfg(feature = "stargate")]
219pub fn ibc_packet_receive<A, S, Q, U>(
220    instance: &mut Instance<A, S, Q>,
221    env: Env,
222    msg: IbcPacketReceiveMsg,
223) -> ContractResult<IbcReceiveResponse<U>>
224where
225    A: BackendApi + 'static,
226    S: Storage + 'static,
227    Q: Querier + 'static,
228    U: DeserializeOwned + CustomMsg,
229{
230    call_ibc_packet_receive(instance, &env, &msg).expect("VM error")
231}
232
233// ibc_packet_ack mimics the call signature of the smart contracts.
234// thus it moves env and acknowledgement rather than take them as reference.
235// this is inefficient here, but only used in test code
236#[cfg(feature = "stargate")]
237pub fn ibc_packet_ack<A, S, Q, U>(
238    instance: &mut Instance<A, S, Q>,
239    env: Env,
240    msg: IbcPacketAckMsg,
241) -> ContractResult<IbcBasicResponse<U>>
242where
243    A: BackendApi + 'static,
244    S: Storage + 'static,
245    Q: Querier + 'static,
246    U: DeserializeOwned + CustomMsg,
247{
248    call_ibc_packet_ack(instance, &env, &msg).expect("VM error")
249}
250
251// ibc_packet_timeout mimics the call signature of the smart contracts.
252// thus it moves env and packet rather than take them as reference.
253// this is inefficient here, but only used in test code
254#[cfg(feature = "stargate")]
255pub fn ibc_packet_timeout<A, S, Q, U>(
256    instance: &mut Instance<A, S, Q>,
257    env: Env,
258    msg: IbcPacketTimeoutMsg,
259) -> ContractResult<IbcBasicResponse<U>>
260where
261    A: BackendApi + 'static,
262    S: Storage + 'static,
263    Q: Querier + 'static,
264    U: DeserializeOwned + CustomMsg,
265{
266    call_ibc_packet_timeout(instance, &env, &msg).expect("VM error")
267}