From 58b755a60b5c05f1815a502ccba731d465b4b7bf Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 17 Jun 2021 14:36:46 +0200 Subject: [PATCH] Cleaner SubMsg API --- packages/std/src/coins.rs | 10 ++-- packages/std/src/lib.rs | 5 +- packages/std/src/results/mod.rs | 2 +- packages/std/src/results/response.rs | 6 +-- packages/std/src/results/subcall.rs | 74 +++++++++++----------------- 5 files changed, 40 insertions(+), 57 deletions(-) diff --git a/packages/std/src/coins.rs b/packages/std/src/coins.rs index 3ef53f831a..2b05d67cd7 100644 --- a/packages/std/src/coins.rs +++ b/packages/std/src/coins.rs @@ -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>(amount: u128, denom: S) -> Vec { vec![coin(amount, denom)] @@ -55,7 +55,7 @@ pub fn coins>(amount: u128, denom: S) -> Vec { /// # 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", &[]); @@ -65,7 +65,7 @@ pub fn coins>(amount: u128, denom: S) -> Vec { /// ]; /// /// 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, /// })]; diff --git a/packages/std/src/lib.rs b/packages/std/src/lib.rs index cdd208f49e..0cfa80fdbd 100644 --- a/packages/std/src/lib.rs +++ b/packages/std/src/lib.rs @@ -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}; diff --git a/packages/std/src/results/mod.rs b/packages/std/src/results/mod.rs index 5bfc8a497f..ac2057bef9 100644 --- a/packages/std/src/results/mod.rs +++ b/packages/std/src/results/mod.rs @@ -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; diff --git a/packages/std/src/results/response.rs b/packages/std/src/results/response.rs index 6d0b4c26be..6f74383e7e 100644 --- a/packages/std/src/results/response.rs +++ b/packages/std/src/results/response.rs @@ -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 = (); /// # @@ -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")); diff --git a/packages/std/src/results/subcall.rs b/packages/std/src/results/subcall.rs index 51634c4c59..f7bce35717 100644 --- a/packages/std/src/results/subcall.rs +++ b/packages/std/src/results/subcall.rs @@ -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 From for SubMsg -where - M: Into>, - T: Clone + fmt::Debug + PartialEq + JsonSchema, -{ - #[inline] - fn from(msg: M) -> SubMsg { - call(msg) - } -} - -/// call takes eg. BankMsg::Send{} and wraps it into a SubMsg with normal message sematics (no reply) -pub fn call(msg: M) -> SubMsg -where - M: Into>, - T: Clone + fmt::Debug + PartialEq + JsonSchema, -{ - SubMsg { - id: UNUSED_MSG_ID, - msg: msg.into(), - reply_on: ReplyOn::Never, - gas_limit: None, - } -} - impl SubMsg 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>>(msg: M, id: u64, reply_on: ReplyOn) -> Self { + /// new creates a "fire and forget" message with the pre-0.14 semantics + pub fn new>>(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>>( - 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>>(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>>(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>>(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, id: u64, reply_on: ReplyOn) -> Self { SubMsg { id, - msg: msg.into(), + msg, reply_on, - gas_limit: Some(gas_limit), + gas_limit: None, } } }