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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
mod test_bank_responses;
mod test_wasm_responses;
mod test_contracts {
/// Example smart contract for testing submessage responses.
pub mod responder {
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{
to_json_binary, BankMsg, Binary, Coin, CosmosMsg, Deps, DepsMut, Empty, Env,
MessageInfo, MsgResponse, Reply, ReplyOn, Response, StdResult, SubMsg, SubMsgResponse,
SubMsgResult, Uint128, WasmMsg,
};
/// Messages instantiating the contract.
#[cw_serde]
pub enum ResponderInstantiateMessage {
None,
}
/// Messages executed by the contract.
#[cw_serde]
pub enum ResponderExecuteMessage {
/// Adds two unsigned integers and returns the sum.
Add(u64, u64),
/// Returns submessage `WasmMsg::Execute` with `Add` message.
WasmMsgExecuteAdd(String, u64, u64),
/// Returns submessage `BankMsg::Send`.
BankSend(String, u128, String),
/// Returns submessage `BankMsg::Burn`.
BankBurn(u128, String),
}
/// Messages querying the contract.
#[cw_serde]
#[derive(QueryResponses)]
pub enum ResponderQueryMessage {
#[returns(ResponderResponse)]
Value,
}
/// Utility structure for convenient data transfer
/// from reply entry-point back to caller.
#[cw_serde]
pub struct ResponderResponse {
pub id: Option<u64>,
pub msg_responses: Vec<MsgResponse>,
}
/// Entry-point for instantiating the contract.
pub fn instantiate(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: ResponderInstantiateMessage,
) -> StdResult<Response> {
Ok(Response::default())
}
/// Entry-point for executing contract's messages.
pub fn execute(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: ResponderExecuteMessage,
) -> StdResult<Response> {
Ok(match msg {
ResponderExecuteMessage::Add(value1, value2) => {
let sum = value1.saturating_add(value2);
Response::new().set_data(to_json_binary(&sum)?)
}
ResponderExecuteMessage::WasmMsgExecuteAdd(contract_addr, value1, value2) => {
let msg = ResponderExecuteMessage::Add(value1, value2);
Response::new().add_submessage(reply_always(
3,
WasmMsg::Execute {
contract_addr,
msg: to_json_binary(&msg)?,
funds: vec![],
}
.into(),
))
}
ResponderExecuteMessage::BankSend(addr, amount, denom) => Response::new()
.add_submessage(reply_always(
1,
BankMsg::Send {
to_address: addr.clone(),
amount: coins(amount, denom),
}
.into(),
)),
ResponderExecuteMessage::BankBurn(amount, denom) => {
Response::new().add_submessage(reply_always(
2,
BankMsg::Burn {
amount: coins(amount, denom),
}
.into(),
))
}
})
}
/// Entry-point for querying the contract.
pub fn query(_deps: Deps, _env: Env, _msg: Empty) -> StdResult<Binary> {
Ok(Binary::default())
}
/// Entry-point for handling submessage replies.
pub fn reply(_deps: DepsMut, _env: Env, msg: Reply) -> StdResult<Response> {
#[allow(deprecated)]
if let Reply {
id,
result:
SubMsgResult::Ok(SubMsgResponse {
events: _,
data: _,
msg_responses,
}),
..
} = msg
{
Ok(Response::new().set_data(to_json_binary(&ResponderResponse {
id: Some(id),
msg_responses,
})?))
} else {
Ok(Response::new().set_data(to_json_binary(&ResponderResponse {
id: None,
msg_responses: vec![],
})?))
}
}
/// Utility function for creating coins.
fn coins(amount: u128, denom: String) -> Vec<Coin> {
vec![Coin::new(Uint128::new(amount), denom.clone())]
}
/// Utility function for creating submessages that require reply.
fn reply_always(id: u64, msg: CosmosMsg) -> SubMsg {
SubMsg {
id,
payload: Default::default(),
msg,
gas_limit: None,
reply_on: ReplyOn::Always,
}
}
}
}