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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::prelude::*;
use crate::Binary;
use super::{Attribute, CosmosMsg, Empty, Event, SubMsg};
/// A response of a contract entry point, such as `instantiate`, `execute` or `migrate`.
///
/// This type can be constructed directly at the end of the call. Alternatively a
/// mutable response instance can be created early in the contract's logic and
/// incrementally be updated.
///
/// ## Examples
///
/// Direct:
///
/// ```
/// # use cosmwasm_std::{Binary, DepsMut, Env, MessageInfo};
/// # type InstantiateMsg = ();
/// #
/// use cosmwasm_std::{attr, Response, StdResult};
///
/// pub fn instantiate(
/// deps: DepsMut,
/// _env: Env,
/// _info: MessageInfo,
/// msg: InstantiateMsg,
/// ) -> StdResult<Response> {
/// // ...
///
/// Ok(Response::new().add_attribute("action", "instantiate"))
/// }
/// ```
///
/// Mutating:
///
/// ```
/// # use cosmwasm_std::{coins, BankMsg, Binary, DepsMut, Env, MessageInfo, SubMsg};
/// # type InstantiateMsg = ();
/// # type MyError = ();
/// #
/// use cosmwasm_std::Response;
///
/// pub fn instantiate(
/// deps: DepsMut,
/// _env: Env,
/// info: MessageInfo,
/// msg: InstantiateMsg,
/// ) -> Result<Response, MyError> {
/// let mut response = Response::new()
/// .add_attribute("Let the", "hacking begin")
/// .add_message(BankMsg::Send {
/// to_address: String::from("recipient"),
/// amount: coins(128, "uint"),
/// })
/// .add_attribute("foo", "bar")
/// .set_data(b"the result data");
/// Ok(response)
/// }
/// ```
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct Response<T = Empty> {
/// Optional list of messages to pass. These will be executed in order.
/// If the ReplyOn variant matches the result (Always, Success on Ok, Error on Err),
/// the runtime will invoke this contract's `reply` entry point
/// after execution. Otherwise, they act like "fire and forget".
/// Use `SubMsg::new` to create messages with the older "fire and forget" semantics.
pub messages: Vec<SubMsg<T>>,
/// The attributes that will be emitted as part of a "wasm" event.
///
/// More info about events (and their attributes) can be found in [*Cosmos SDK* docs].
///
/// [*Cosmos SDK* docs]: https://docs.cosmos.network/main/learn/advanced/events
pub attributes: Vec<Attribute>,
/// Extra, custom events separate from the main `wasm` one. These will have
/// `wasm-` prepended to the type.
///
/// More info about events can be found in [*Cosmos SDK* docs].
///
/// [*Cosmos SDK* docs]: https://docs.cosmos.network/main/learn/advanced/events
pub events: Vec<Event>,
/// The binary payload to include in the response.
pub data: Option<Binary>,
}
impl<T> Default for Response<T> {
fn default() -> Self {
Response {
messages: vec![],
attributes: vec![],
events: vec![],
data: None,
}
}
}
impl<T> Response<T> {
pub fn new() -> Self {
Self::default()
}
/// Add an attribute included in the main `wasm` event.
///
/// For working with optional values or optional attributes, see [`add_attributes`][Self::add_attributes].
pub fn add_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.attributes.push(Attribute::new(key, value));
self
}
/// This creates a "fire and forget" message, by using `SubMsg::new()` to wrap it,
/// and adds it to the list of messages to process.
pub fn add_message(mut self, msg: impl Into<CosmosMsg<T>>) -> Self {
self.messages.push(SubMsg::new(msg));
self
}
/// This takes an explicit SubMsg (creates via eg. `reply_on_error`)
/// and adds it to the list of messages to process.
pub fn add_submessage(mut self, msg: SubMsg<T>) -> Self {
self.messages.push(msg);
self
}
/// Adds an extra event to the response, separate from the main `wasm` event
/// that is always created.
///
/// The `wasm-` prefix will be appended by the runtime to the provided type
/// of event.
pub fn add_event(mut self, event: impl Into<Event>) -> Self {
self.events.push(event.into());
self
}
/// Bulk add attributes included in the main `wasm` event.
///
/// Anything that can be turned into an iterator and yields something
/// that can be converted into an `Attribute` is accepted.
///
/// ## Examples
///
/// Adding a list of attributes using the pair notation for key and value:
///
/// ```
/// use cosmwasm_std::Response;
///
/// let attrs = vec![
/// ("action", "reaction"),
/// ("answer", "42"),
/// ("another", "attribute"),
/// ];
/// let res: Response = Response::new().add_attributes(attrs.clone());
/// assert_eq!(res.attributes, attrs);
/// ```
///
/// Adding an optional value as an optional attribute by turning it into a list of 0 or 1 elements:
///
/// ```
/// use cosmwasm_std::{Attribute, Response};
///
/// // Some value
/// let value: Option<String> = Some("sarah".to_string());
/// let attribute: Option<Attribute> = value.map(|v| Attribute::new("winner", v));
/// let res: Response = Response::new().add_attributes(attribute);
/// assert_eq!(res.attributes, [Attribute {
/// key: "winner".to_string(),
/// value: "sarah".to_string(),
/// }]);
///
/// // No value
/// let value: Option<String> = None;
/// let attribute: Option<Attribute> = value.map(|v| Attribute::new("winner", v));
/// let res: Response = Response::new().add_attributes(attribute);
/// assert_eq!(res.attributes.len(), 0);
/// ```
pub fn add_attributes<A: Into<Attribute>>(
mut self,
attrs: impl IntoIterator<Item = A>,
) -> Self {
self.attributes.extend(attrs.into_iter().map(A::into));
self
}
/// Bulk add "fire and forget" messages to the list of messages to process.
///
/// ## Examples
///
/// ```
/// use cosmwasm_std::{CosmosMsg, Response};
///
/// fn make_response_with_msgs(msgs: Vec<CosmosMsg>) -> Response {
/// Response::new().add_messages(msgs)
/// }
/// ```
pub fn add_messages<M: Into<CosmosMsg<T>>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
self.add_submessages(msgs.into_iter().map(SubMsg::new))
}
/// Bulk add explicit SubMsg structs to the list of messages to process.
///
/// ## Examples
///
/// ```
/// use cosmwasm_std::{SubMsg, Response};
///
/// fn make_response_with_submsgs(msgs: Vec<SubMsg>) -> Response {
/// Response::new().add_submessages(msgs)
/// }
/// ```
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg<T>>) -> Self {
self.messages.extend(msgs);
self
}
/// Bulk add custom events to the response. These are separate from the main
/// `wasm` event.
///
/// The `wasm-` prefix will be appended by the runtime to the provided types
/// of events.
pub fn add_events<E>(mut self, events: impl IntoIterator<Item = E>) -> Self
where
E: Into<Event>,
{
self.events.extend(events.into_iter().map(|e| e.into()));
self
}
/// Set the binary data included in the response.
pub fn set_data(mut self, data: impl Into<Binary>) -> Self {
self.data = Some(data.into());
self
}
/// Convert this [`Response<T>`] to a [`Response<U>`] with a different custom message type.
/// This allows easier interactions between code written for a specific chain and
/// code written for multiple chains.
/// If this contains a [`CosmosMsg::Custom`] submessage, the function returns `None`.
pub fn change_custom<U>(self) -> Option<Response<U>> {
Some(Response {
messages: self
.messages
.into_iter()
.map(|msg| msg.change_custom())
.collect::<Option<Vec<_>>>()?,
attributes: self.attributes,
events: self.events,
data: self.data,
})
}
}
#[cfg(test)]
mod tests {
use super::super::BankMsg;
use super::*;
use crate::results::submessages::{ReplyOn, UNUSED_MSG_ID};
use crate::{coins, from_json, to_json_vec, ContractResult};
#[test]
fn response_add_attributes_works() {
let res = Response::<Empty>::new().add_attributes(core::iter::empty::<Attribute>());
assert_eq!(res.attributes.len(), 0);
let res = Response::<Empty>::new().add_attributes([Attribute::new("test", "ing")]);
assert_eq!(res.attributes.len(), 1);
assert_eq!(
res.attributes[0],
Attribute {
key: "test".to_string(),
value: "ing".to_string(),
}
);
let attrs = vec![
("action", "reaction"),
("answer", "42"),
("another", "attribute"),
];
let res: Response = Response::new().add_attributes(attrs.clone());
assert_eq!(res.attributes, attrs);
let optional = Option::<Attribute>::None;
let res: Response = Response::new().add_attributes(optional);
assert_eq!(res.attributes.len(), 0);
let optional = Option::<Attribute>::Some(Attribute::new("test", "ing"));
let res: Response = Response::new().add_attributes(optional);
assert_eq!(res.attributes.len(), 1);
assert_eq!(
res.attributes[0],
Attribute {
key: "test".to_string(),
value: "ing".to_string(),
}
);
}
#[test]
fn can_serialize_and_deserialize_init_response() {
let original = Response {
messages: vec![
SubMsg {
id: 12,
payload: Binary::new(vec![9, 8, 7, 6, 5]),
msg: BankMsg::Send {
to_address: String::from("checker"),
amount: coins(888, "moon"),
}
.into(),
gas_limit: Some(12345u64),
reply_on: ReplyOn::Always,
},
SubMsg {
id: UNUSED_MSG_ID,
payload: Binary::default(),
msg: BankMsg::Send {
to_address: String::from("you"),
amount: coins(1015, "earth"),
}
.into(),
gas_limit: None,
reply_on: ReplyOn::Never,
},
],
attributes: vec![Attribute {
key: "action".to_string(),
value: "release".to_string(),
}],
events: vec![],
data: Some(Binary::from([0xAA, 0xBB])),
};
let serialized = to_json_vec(&original).expect("encode contract result");
let deserialized: Response = from_json(serialized).expect("decode contract result");
assert_eq!(deserialized, original);
}
#[test]
fn contract_result_is_ok_works() {
let success = ContractResult::<()>::Ok(());
let failure = ContractResult::<()>::Err("broken".to_string());
assert!(success.is_ok());
assert!(!failure.is_ok());
}
#[test]
fn contract_result_is_err_works() {
let success = ContractResult::<()>::Ok(());
let failure = ContractResult::<()>::Err("broken".to_string());
assert!(failure.is_err());
assert!(!success.is_err());
}
// struct implements `Into<Event>`
#[derive(Clone)]
struct OurEvent {
msg: String,
}
// allow define `into` rather than `from` to define `into` clearly
#[allow(clippy::from_over_into)]
impl Into<Event> for OurEvent {
fn into(self) -> Event {
Event::new("our_event").add_attribute("msg", self.msg)
}
}
#[test]
fn add_event_takes_into_event() {
let msg = "message".to_string();
let our_event = OurEvent { msg };
let event: Event = our_event.clone().into();
let actual = Response::<Empty>::new().add_event(our_event);
let expected = Response::<Empty>::new().add_event(event);
assert_eq!(expected, actual);
}
#[test]
fn add_events_takes_into_event() {
let msg1 = "foo".to_string();
let msg2 = "bare".to_string();
let our_event1 = OurEvent { msg: msg1 };
let our_event2 = OurEvent { msg: msg2 };
let events: Vec<Event> = vec![our_event1.clone().into(), our_event2.clone().into()];
let actual = Response::<Empty>::new().add_events([our_event1, our_event2]);
let expected = Response::<Empty>::new().add_events(events);
assert_eq!(expected, actual);
}
#[test]
fn change_custom_works() {
let response: Response<Empty> = Response {
messages: vec![SubMsg::new(BankMsg::Send {
to_address: "address".to_string(),
amount: coins(123, "earth"),
})],
attributes: vec![Attribute::new("foo", "bar")],
events: vec![Event::new("our_event").add_attribute("msg", "hello")],
data: None,
};
let converted_resp: Response<String> = response.clone().change_custom().unwrap();
assert_eq!(
converted_resp.messages,
vec![SubMsg::new(BankMsg::Send {
to_address: "address".to_string(),
amount: coins(123, "earth"),
})]
);
assert_eq!(converted_resp.attributes, response.attributes);
assert_eq!(converted_resp.events, response.events);
assert_eq!(converted_resp.data, response.data);
// response with custom message
let response = Response {
messages: vec![SubMsg::new(CosmosMsg::Custom(Empty {}))],
attributes: vec![Attribute::new("foo", "bar")],
events: vec![Event::new("our_event").add_attribute("msg", "hello")],
data: None,
};
assert_eq!(response.change_custom::<String>(), None);
}
}