Skip to content

Commit 606a41f

Browse files
committed
Cleaner SubMsg API
1 parent fee113a commit 606a41f

File tree

5 files changed

+40
-57
lines changed

5 files changed

+40
-57
lines changed

packages/std/src/coins.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ impl fmt::Display for Coin {
3434
/// # Examples
3535
///
3636
/// ```
37-
/// # use cosmwasm_std::{coins, BankMsg, CosmosMsg, Response};
37+
/// # use cosmwasm_std::{coins, BankMsg, CosmosMsg, Response, SubMsg};
3838
/// # use cosmwasm_std::testing::{mock_env, mock_info};
3939
/// # let env = mock_env();
4040
/// # let info = mock_info("sender", &[]);
4141
/// let tip = coins(123, "ucosm");
4242
///
4343
/// let mut response: Response = Default::default();
44-
/// response.messages = vec![BankMsg::Send {
44+
/// response.messages = vec![SubMsg::new(BankMsg::Send {
4545
/// to_address: info.sender.into(),
4646
/// amount: tip,
47-
/// }.into()];
47+
/// })];
4848
/// ```
4949
pub fn coins<S: Into<String>>(amount: u128, denom: S) -> Vec<Coin> {
5050
vec![coin(amount, denom)]
@@ -55,7 +55,7 @@ pub fn coins<S: Into<String>>(amount: u128, denom: S) -> Vec<Coin> {
5555
/// # Examples
5656
///
5757
/// ```
58-
/// # use cosmwasm_std::{call, coin, BankMsg, CosmosMsg, Response};
58+
/// # use cosmwasm_std::{coin, BankMsg, CosmosMsg, Response, SubMsg};
5959
/// # use cosmwasm_std::testing::{mock_env, mock_info};
6060
/// # let env = mock_env();
6161
/// # let info = mock_info("sender", &[]);
@@ -65,7 +65,7 @@ pub fn coins<S: Into<String>>(amount: u128, denom: S) -> Vec<Coin> {
6565
/// ];
6666
///
6767
/// let mut response: Response = Default::default();
68-
/// response.messages = vec![call(BankMsg::Send {
68+
/// response.messages = vec![SubMsg::new(BankMsg::Send {
6969
/// to_address: info.sender.into(),
7070
/// amount: tip,
7171
/// })];

packages/std/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ pub use crate::query::{
5252
#[cfg(feature = "stargate")]
5353
pub use crate::query::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse};
5454
pub use crate::results::{
55-
attr, call, wasm_execute, wasm_instantiate, Attribute, BankMsg, ContractResult, CosmosMsg,
56-
Empty, Event, QueryResponse, Reply, ReplyOn, Response, SubMsg, SubcallResponse, SystemResult,
57-
WasmMsg,
55+
attr, wasm_execute, wasm_instantiate, Attribute, BankMsg, ContractResult, CosmosMsg, Empty,
56+
Event, QueryResponse, Reply, ReplyOn, Response, SubMsg, SubcallResponse, SystemResult, WasmMsg,
5857
};
5958
#[cfg(feature = "staking")]
6059
pub use crate::results::{DistributionMsg, StakingMsg};

packages/std/src/results/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ pub use cosmos_msg::{DistributionMsg, StakingMsg};
1717
pub use empty::Empty;
1818
pub use query::QueryResponse;
1919
pub use response::Response;
20-
pub use subcall::{call, Event, Reply, ReplyOn, SubMsg, SubcallResponse};
20+
pub use subcall::{Event, Reply, ReplyOn, SubMsg, SubcallResponse};
2121
pub use system_result::SystemResult;

packages/std/src/results/response.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use super::{Attribute, Empty, SubMsg};
4141
/// Mutating:
4242
///
4343
/// ```
44-
/// # use cosmwasm_std::{coins, BankMsg, Binary, DepsMut, Env, MessageInfo};
44+
/// # use cosmwasm_std::{coins, BankMsg, Binary, DepsMut, Env, MessageInfo, SubMsg};
4545
/// # type InstantiateMsg = ();
4646
/// # type MyError = ();
4747
/// #
@@ -57,10 +57,10 @@ use super::{Attribute, Empty, SubMsg};
5757
/// // ...
5858
/// response.add_attribute("Let the", "hacking begin");
5959
/// // ...
60-
/// response.add_message(BankMsg::Send {
60+
/// response.add_message(SubMsg::new(BankMsg::Send {
6161
/// to_address: String::from("recipient"),
6262
/// amount: coins(128, "uint"),
63-
/// });
63+
/// }));
6464
/// response.add_attribute("foo", "bar");
6565
/// // ...
6666
/// response.set_data(Binary::from(b"the result data"));

packages/std/src/results/subcall.rs

+29-45
Original file line numberDiff line numberDiff line change
@@ -50,64 +50,48 @@ where
5050
/// This is used for cases when we use ReplyOn::Never and the id doesn't matter
5151
pub const UNUSED_MSG_ID: u64 = 123456789;
5252

53-
/// We implement thisas a shortcut so all existing code doesn't break.
54-
/// Up to 0.14, we could do something like:
55-
/// let messages = vec![BankMsg::Send { .. }.into()];
56-
/// In order to construct the response.
57-
///
58-
/// With 0.15, we move to requiring SubMsg there, but this allows the same
59-
/// `.into()` call to convert the BankMsg into a proper SubMsg with no reply.
60-
impl<M, T> From<M> for SubMsg<T>
61-
where
62-
M: Into<CosmosMsg<T>>,
63-
T: Clone + fmt::Debug + PartialEq + JsonSchema,
64-
{
65-
#[inline]
66-
fn from(msg: M) -> SubMsg<T> {
67-
call(msg)
68-
}
69-
}
70-
71-
/// call takes eg. BankMsg::Send{} and wraps it into a SubMsg with normal message sematics (no reply)
72-
pub fn call<M, T>(msg: M) -> SubMsg<T>
73-
where
74-
M: Into<CosmosMsg<T>>,
75-
T: Clone + fmt::Debug + PartialEq + JsonSchema,
76-
{
77-
SubMsg {
78-
id: UNUSED_MSG_ID,
79-
msg: msg.into(),
80-
reply_on: ReplyOn::Never,
81-
gas_limit: None,
82-
}
83-
}
84-
8553
impl<T> SubMsg<T>
8654
where
8755
T: Clone + fmt::Debug + PartialEq + JsonSchema,
8856
{
89-
/// new takes eg. BankMsg::Send{} and sets up for a reply. No gas limit is set.
90-
pub fn new<M: Into<CosmosMsg<T>>>(msg: M, id: u64, reply_on: ReplyOn) -> Self {
57+
/// new creates a "fire and forget" message with the pre-0.14 semantics
58+
pub fn new<M: Into<CosmosMsg<T>>>(msg: M) -> Self {
9159
SubMsg {
92-
id,
60+
id: UNUSED_MSG_ID,
9361
msg: msg.into(),
94-
reply_on,
62+
reply_on: ReplyOn::Never,
9563
gas_limit: None,
9664
}
9765
}
9866

99-
/// new_with_limit is like new but allows setting a gas limit
100-
pub fn new_with_limit<M: Into<CosmosMsg<T>>>(
101-
msg: M,
102-
id: u64,
103-
reply_on: ReplyOn,
104-
gas_limit: u64,
105-
) -> Self {
67+
/// create a `SubMsg` that will provide a `reply` with the given id if the message returns `Ok`
68+
pub fn reply_on_success<M: Into<CosmosMsg<T>>>(msg: M, id: u64) -> Self {
69+
Self::reply_on(msg.into(), id, ReplyOn::Success)
70+
}
71+
72+
/// create a `SubMsg` that will provide a `reply` with the given id if the message returns `Err`
73+
pub fn reply_on_error<M: Into<CosmosMsg<T>>>(msg: M, id: u64) -> Self {
74+
Self::reply_on(msg.into(), id, ReplyOn::Error)
75+
}
76+
77+
/// create a `SubMsg` that will always provide a `reply` with the given id
78+
pub fn reply_always<M: Into<CosmosMsg<T>>>(msg: M, id: u64) -> Self {
79+
Self::reply_on(msg.into(), id, ReplyOn::Always)
80+
}
81+
82+
/// add a gas limit to the message. Usage like:
83+
/// SubMsg::reply_always(msg, 1234).with_gas_limit(60_000)
84+
pub fn with_gas_limit(mut self, limit: u64) -> Self {
85+
self.gas_limit = Some(limit);
86+
self
87+
}
88+
89+
fn reply_on(msg: CosmosMsg<T>, id: u64, reply_on: ReplyOn) -> Self {
10690
SubMsg {
10791
id,
108-
msg: msg.into(),
92+
msg,
10993
reply_on,
110-
gas_limit: Some(gas_limit),
94+
gas_limit: None,
11195
}
11296
}
11397
}

0 commit comments

Comments
 (0)