Skip to content

Commit

Permalink
Cleaner SubMsg API
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Jun 17, 2021
1 parent 8e6a097 commit 58b755a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 57 deletions.
10 changes: 5 additions & 5 deletions packages/std/src/coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ impl fmt::Display for Coin {
/// # Examples
///
/// ```
/// # use cosmwasm_std::{coins, BankMsg, CosmosMsg, Response};
/// # use cosmwasm_std::{coins, BankMsg, CosmosMsg, Response, SubMsg};
/// # use cosmwasm_std::testing::{mock_env, mock_info};
/// # let env = mock_env();
/// # let info = mock_info("sender", &[]);
/// let tip = coins(123, "ucosm");
///
/// let mut response: Response = Default::default();
/// response.messages = vec![BankMsg::Send {
/// response.messages = vec![SubMsg::new(BankMsg::Send {
/// to_address: info.sender.into(),
/// amount: tip,
/// }.into()];
/// })];
/// ```
pub fn coins<S: Into<String>>(amount: u128, denom: S) -> Vec<Coin> {
vec![coin(amount, denom)]
Expand All @@ -55,7 +55,7 @@ pub fn coins<S: Into<String>>(amount: u128, denom: S) -> Vec<Coin> {
/// # Examples
///
/// ```
/// # use cosmwasm_std::{call, coin, BankMsg, CosmosMsg, Response};
/// # use cosmwasm_std::{coin, BankMsg, CosmosMsg, Response, SubMsg};
/// # use cosmwasm_std::testing::{mock_env, mock_info};
/// # let env = mock_env();
/// # let info = mock_info("sender", &[]);
Expand All @@ -65,7 +65,7 @@ pub fn coins<S: Into<String>>(amount: u128, denom: S) -> Vec<Coin> {
/// ];
///
/// let mut response: Response = Default::default();
/// response.messages = vec![call(BankMsg::Send {
/// response.messages = vec![SubMsg::new(BankMsg::Send {
/// to_address: info.sender.into(),
/// amount: tip,
/// })];
Expand Down
5 changes: 2 additions & 3 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ pub use crate::query::{
#[cfg(feature = "stargate")]
pub use crate::query::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse};
pub use crate::results::{
attr, call, wasm_execute, wasm_instantiate, Attribute, BankMsg, ContractResult, CosmosMsg,
Empty, Event, QueryResponse, Reply, ReplyOn, Response, SubMsg, SubcallResponse, SystemResult,
WasmMsg,
attr, wasm_execute, wasm_instantiate, Attribute, BankMsg, ContractResult, CosmosMsg, Empty,
Event, QueryResponse, Reply, ReplyOn, Response, SubMsg, SubcallResponse, SystemResult, WasmMsg,
};
#[cfg(feature = "staking")]
pub use crate::results::{DistributionMsg, StakingMsg};
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/results/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ pub use cosmos_msg::{DistributionMsg, StakingMsg};
pub use empty::Empty;
pub use query::QueryResponse;
pub use response::Response;
pub use subcall::{call, Event, Reply, ReplyOn, SubMsg, SubcallResponse};
pub use subcall::{Event, Reply, ReplyOn, SubMsg, SubcallResponse};
pub use system_result::SystemResult;
6 changes: 3 additions & 3 deletions packages/std/src/results/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use super::{Attribute, Empty, SubMsg};
/// Mutating:
///
/// ```
/// # use cosmwasm_std::{coins, BankMsg, Binary, DepsMut, Env, MessageInfo};
/// # use cosmwasm_std::{coins, BankMsg, Binary, DepsMut, Env, MessageInfo, SubMsg};
/// # type InstantiateMsg = ();
/// # type MyError = ();
/// #
Expand All @@ -57,10 +57,10 @@ use super::{Attribute, Empty, SubMsg};
/// // ...
/// response.add_attribute("Let the", "hacking begin");
/// // ...
/// response.add_message(BankMsg::Send {
/// response.add_message(SubMsg::new(BankMsg::Send {
/// to_address: String::from("recipient"),
/// amount: coins(128, "uint"),
/// });
/// }));
/// response.add_attribute("foo", "bar");
/// // ...
/// response.set_data(Binary::from(b"the result data"));
Expand Down
74 changes: 29 additions & 45 deletions packages/std/src/results/subcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,64 +50,48 @@ where
/// This is used for cases when we use ReplyOn::Never and the id doesn't matter
pub const UNUSED_MSG_ID: u64 = 123456789;

/// We implement thisas a shortcut so all existing code doesn't break.
/// Up to 0.14, we could do something like:
/// let messages = vec![BankMsg::Send { .. }.into()];
/// In order to construct the response.
///
/// With 0.15, we move to requiring SubMsg there, but this allows the same
/// `.into()` call to convert the BankMsg into a proper SubMsg with no reply.
impl<M, T> From<M> for SubMsg<T>
where
M: Into<CosmosMsg<T>>,
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
#[inline]
fn from(msg: M) -> SubMsg<T> {
call(msg)
}
}

/// call takes eg. BankMsg::Send{} and wraps it into a SubMsg with normal message sematics (no reply)
pub fn call<M, T>(msg: M) -> SubMsg<T>
where
M: Into<CosmosMsg<T>>,
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
SubMsg {
id: UNUSED_MSG_ID,
msg: msg.into(),
reply_on: ReplyOn::Never,
gas_limit: None,
}
}

impl<T> SubMsg<T>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
/// new takes eg. BankMsg::Send{} and sets up for a reply. No gas limit is set.
pub fn new<M: Into<CosmosMsg<T>>>(msg: M, id: u64, reply_on: ReplyOn) -> Self {
/// new creates a "fire and forget" message with the pre-0.14 semantics
pub fn new<M: Into<CosmosMsg<T>>>(msg: M) -> Self {
SubMsg {
id,
id: UNUSED_MSG_ID,
msg: msg.into(),
reply_on,
reply_on: ReplyOn::Never,
gas_limit: None,
}
}

/// new_with_limit is like new but allows setting a gas limit
pub fn new_with_limit<M: Into<CosmosMsg<T>>>(
msg: M,
id: u64,
reply_on: ReplyOn,
gas_limit: u64,
) -> Self {
/// create a `SubMsg` that will provide a `reply` with the given id if the message returns `Ok`
pub fn reply_on_success<M: Into<CosmosMsg<T>>>(msg: M, id: u64) -> Self {
Self::reply_on(msg.into(), id, ReplyOn::Success)
}

/// create a `SubMsg` that will provide a `reply` with the given id if the message returns `Err`
pub fn reply_on_error<M: Into<CosmosMsg<T>>>(msg: M, id: u64) -> Self {
Self::reply_on(msg.into(), id, ReplyOn::Error)
}

/// create a `SubMsg` that will always provide a `reply` with the given id
pub fn reply_always<M: Into<CosmosMsg<T>>>(msg: M, id: u64) -> Self {
Self::reply_on(msg.into(), id, ReplyOn::Always)
}

/// add a gas limit to the message. Usage like:
/// SubMsg::reply_always(msg, 1234).with_gas_limit(60_000)
pub fn with_gas_limit(mut self, limit: u64) -> Self {
self.gas_limit = Some(limit);
self
}

fn reply_on(msg: CosmosMsg<T>, id: u64, reply_on: ReplyOn) -> Self {
SubMsg {
id,
msg: msg.into(),
msg,
reply_on,
gas_limit: Some(gas_limit),
gas_limit: None,
}
}
}
Expand Down

0 comments on commit 58b755a

Please sign in to comment.