pub struct Response<T = Empty> {
pub messages: Vec<SubMsg<T>, Global>,
pub attributes: Vec<Attribute, Global>,
pub events: Vec<Event, Global>,
pub data: Option<Binary>,
}
Expand description
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 secret_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 secret_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)
}
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.messages: Vec<SubMsg<T>, Global>
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.
attributes: Vec<Attribute, Global>
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.
events: Vec<Event, Global>
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.
data: Option<Binary>
The binary payload to include in the response.
Implementations§
source§impl<T> Response<T>
impl<T> Response<T>
pub fn new() -> Response<T>
sourcepub fn add_attribute(
self,
key: impl Into<String>,
value: impl Into<String>
) -> Response<T>
pub fn add_attribute( self, key: impl Into<String>, value: impl Into<String> ) -> Response<T>
Add an ENCRYPTED attribute included in the main wasm
event.
For working with optional values or optional attributes, see add_attributes
.
sourcepub fn add_attribute_plaintext(
self,
key: impl Into<String>,
value: impl Into<String>
) -> Response<T>
pub fn add_attribute_plaintext( self, key: impl Into<String>, value: impl Into<String> ) -> Response<T>
Add a NON-ENCRYPTED attribute included in the main wasm
event.
For working with optional values or optional attributes, see add_attribute_plaintext
.
sourcepub fn add_message(self, msg: impl Into<CosmosMsg<T>>) -> Response<T>
pub fn add_message(self, msg: impl Into<CosmosMsg<T>>) -> Response<T>
This creates a “fire and forget” message, by using SubMsg::new()
to wrap it,
and adds it to the list of messages to process.
sourcepub fn add_submessage(self, msg: SubMsg<T>) -> Response<T>
pub fn add_submessage(self, msg: SubMsg<T>) -> Response<T>
This takes an explicit SubMsg (creates via eg. reply_on_error
)
and adds it to the list of messages to process.
sourcepub fn add_event(self, event: Event) -> Response<T>
pub fn add_event(self, event: Event) -> Response<T>
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.
sourcepub fn add_attributes<A>(
self,
attrs: impl IntoIterator<Item = A>
) -> Response<T>where
A: Into<Attribute>,
pub fn add_attributes<A>( self, attrs: impl IntoIterator<Item = A> ) -> Response<T>where A: Into<Attribute>,
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 secret_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 secret_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(),
encrypted: true
}]);
// 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);
sourcepub fn add_messages<M>(self, msgs: impl IntoIterator<Item = M>) -> Response<T>where
M: Into<CosmosMsg<T>>,
pub fn add_messages<M>(self, msgs: impl IntoIterator<Item = M>) -> Response<T>where M: Into<CosmosMsg<T>>,
Bulk add “fire and forget” messages to the list of messages to process.
Examples
use secret_cosmwasm_std::{CosmosMsg, Response};
fn make_response_with_msgs(msgs: Vec<CosmosMsg>) -> Response {
Response::new().add_messages(msgs)
}
sourcepub fn add_submessages(
self,
msgs: impl IntoIterator<Item = SubMsg<T>>
) -> Response<T>
pub fn add_submessages( self, msgs: impl IntoIterator<Item = SubMsg<T>> ) -> Response<T>
Bulk add explicit SubMsg structs to the list of messages to process.
Examples
use secret_cosmwasm_std::{SubMsg, Response};
fn make_response_with_submsgs(msgs: Vec<SubMsg>) -> Response {
Response::new().add_submessages(msgs)
}
sourcepub fn add_events(self, events: impl IntoIterator<Item = Event>) -> Response<T>
pub fn add_events(self, events: impl IntoIterator<Item = Event>) -> Response<T>
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.
Trait Implementations§
source§impl<'de, T> Deserialize<'de> for Response<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Response<T>where T: Deserialize<'de>,
source§fn deserialize<__D>(
__deserializer: __D
) -> Result<Response<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>( __deserializer: __D ) -> Result<Response<T>, <__D as Deserializer<'de>>::Error>where __D: Deserializer<'de>,
source§impl<T> JsonSchema for Response<T>where
T: JsonSchema,
impl<T> JsonSchema for Response<T>where T: JsonSchema,
source§fn schema_name() -> String
fn schema_name() -> String
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moresource§impl<T> PartialEq<Response<T>> for Response<T>where
T: PartialEq<T>,
impl<T> PartialEq<Response<T>> for Response<T>where T: PartialEq<T>,
source§impl ResponseExt for Response
impl ResponseExt for Response
source§fn pad(self) -> Self
fn pad(self) -> Self
BLOCK_SIZE
if it is set.