Skip to content

feat: Remove the generic param from CosmosMsg #2475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions contracts/hackatom/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ fn execute_cpu_loop() {

let execute_info = mock_info(creator.as_str(), &[]);
// Note: we need to use the production-call, not the testing call (which unwraps any vm error)
let execute_res = call_execute::<_, _, _, Empty>(
let execute_res = call_execute::<_, _, _>(
&mut deps,
&mock_env(),
&execute_info,
Expand All @@ -343,7 +343,7 @@ fn execute_storage_loop() {

let execute_info = mock_info(creator.as_str(), &[]);
// Note: we need to use the production-call, not the testing call (which unwraps any vm error)
let execute_res = call_execute::<_, _, _, Empty>(
let execute_res = call_execute::<_, _, _>(
&mut deps,
&mock_env(),
&execute_info,
Expand All @@ -365,7 +365,7 @@ fn execute_memory_loop() {

let execute_info = mock_info(creator.as_str(), &[]);
// Note: we need to use the production-call, not the testing call (which unwraps any vm error)
let execute_res = call_execute::<_, _, _, Empty>(
let execute_res = call_execute::<_, _, _>(
&mut deps,
&mock_env(),
&execute_info,
Expand Down Expand Up @@ -445,7 +445,7 @@ fn execute_panic() {
let execute_info = mock_info(creator.as_str(), &[]);
// panic inside contract should not panic out here
// Note: we need to use the production-call, not the testing call (which unwraps any vm error)
let execute_res = call_execute::<_, _, _, Empty>(
let execute_res = call_execute::<_, _, _>(
&mut deps,
&mock_env(),
&execute_info,
Expand Down
4 changes: 2 additions & 2 deletions contracts/ibc-reflect-send/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ fn send_remote_funds() {
transfer_channel_id: transfer_channel_id.into(),
};
let info = mock_info(CREATOR, &coins(12344, "utrgd"));
execute::<_, _, _, _, Empty>(&mut deps, mock_env(), info, msg).unwrap_err();
execute::<_, _, _, _>(&mut deps, mock_env(), info, msg).unwrap_err();

// let's try with no sent funds in the message
let msg = ExecuteMsg::SendFunds {
reflect_channel_id: reflect_channel_id.into(),
transfer_channel_id: transfer_channel_id.into(),
};
let info = mock_info(CREATOR, &[]);
execute::<_, _, _, _, Empty>(&mut deps, mock_env(), info, msg).unwrap_err();
execute::<_, _, _, _>(&mut deps, mock_env(), info, msg).unwrap_err();

// 3rd times the charm
let msg = ExecuteMsg::SendFunds {
Expand Down
2 changes: 1 addition & 1 deletion contracts/ibc2/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn ibc2_packet_receive(
deps: DepsMut,
env: Env,
msg: Ibc2PacketReceiveMsg,
) -> StdResult<IbcReceiveResponse> {
) -> Result<IbcReceiveResponse, StdError> {
let binary_payload = &msg.payload.value;
let json_payload: IbcPayload = from_json(binary_payload)?;

Expand Down
32 changes: 16 additions & 16 deletions packages/std/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};

use crate::coin::Coin;
use crate::prelude::*;
use crate::results::{Attribute, CosmosMsg, Empty, Event, SubMsg};
use crate::results::{Attribute, CosmosMsg, Event, SubMsg};
use crate::StdResult;
use crate::{to_json_binary, Binary};
use crate::{Addr, Timestamp};
Expand Down Expand Up @@ -560,12 +560,12 @@ impl IbcPacketTimeoutMsg {
/// will use other Response types
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct IbcBasicResponse<T = Empty> {
pub struct IbcBasicResponse {
/// Optional list of messages to pass. These will be executed in order.
/// If the ReplyOn member is set, they 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>>,
pub messages: Vec<SubMsg>,
/// 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].
Expand All @@ -582,7 +582,7 @@ pub struct IbcBasicResponse<T = Empty> {
}

// Custom implementation in order to implement it for all `T`, even if `T` is not `Default`.
impl<T> Default for IbcBasicResponse<T> {
impl Default for IbcBasicResponse {
fn default() -> Self {
IbcBasicResponse {
messages: vec![],
Expand All @@ -592,7 +592,7 @@ impl<T> Default for IbcBasicResponse<T> {
}
}

impl<T> IbcBasicResponse<T> {
impl IbcBasicResponse {
pub fn new() -> Self {
Self::default()
}
Expand All @@ -605,14 +605,14 @@ impl<T> IbcBasicResponse<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.
pub fn add_message(mut self, msg: impl Into<CosmosMsg<T>>) -> Self {
pub fn add_message(mut self, msg: impl Into<CosmosMsg>) -> Self {
self.messages.push(SubMsg::new(msg));
self
}

/// This takes an explicit SubMsg (creates via e.g. `reply_on_error`)
/// and adds it to the list of messages to process.
pub fn add_submessage(mut self, msg: SubMsg<T>) -> Self {
pub fn add_submessage(mut self, msg: SubMsg) -> Self {
self.messages.push(msg);
self
}
Expand Down Expand Up @@ -664,7 +664,7 @@ impl<T> IbcBasicResponse<T> {
/// IbcBasicResponse::new().add_messages(msgs)
/// }
/// ```
pub fn add_messages<M: Into<CosmosMsg<T>>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
pub fn add_messages<M: Into<CosmosMsg>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
self.add_submessages(msgs.into_iter().map(SubMsg::new))
}

Expand All @@ -679,7 +679,7 @@ impl<T> IbcBasicResponse<T> {
/// IbcBasicResponse::new().add_submessages(msgs)
/// }
/// ```
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg<T>>) -> Self {
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg>) -> Self {
self.messages.extend(msgs);
self
}
Expand All @@ -702,7 +702,7 @@ impl<T> IbcBasicResponse<T> {
/// and not inform the calling chain).
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct IbcReceiveResponse<T = Empty> {
pub struct IbcReceiveResponse {
/// The bytes we return to the contract that sent the packet.
/// This may represent a success or error of execution.
/// In case of `None`, no acknowledgement is written.
Expand All @@ -711,7 +711,7 @@ pub struct IbcReceiveResponse<T = Empty> {
/// If the ReplyOn member is set, they will invoke this contract's `reply` entry point
/// after execution. Otherwise, they act like "fire and forget".
/// Use `call` or `msg.into()` to create messages with the older "fire and forget" semantics.
pub messages: Vec<SubMsg<T>>,
pub messages: Vec<SubMsg>,
/// 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].
Expand All @@ -727,7 +727,7 @@ pub struct IbcReceiveResponse<T = Empty> {
pub events: Vec<Event>,
}

impl<T> IbcReceiveResponse<T> {
impl IbcReceiveResponse {
/// Create a new response with the given acknowledgement.
///
/// ## Examples
Expand Down Expand Up @@ -803,14 +803,14 @@ impl<T> IbcReceiveResponse<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.
pub fn add_message(mut self, msg: impl Into<CosmosMsg<T>>) -> Self {
pub fn add_message(mut self, msg: impl Into<CosmosMsg>) -> Self {
self.messages.push(SubMsg::new(msg));
self
}

/// This takes an explicit SubMsg (creates via e.g. `reply_on_error`)
/// and adds it to the list of messages to process.
pub fn add_submessage(mut self, msg: SubMsg<T>) -> Self {
pub fn add_submessage(mut self, msg: SubMsg) -> Self {
self.messages.push(msg);
self
}
Expand Down Expand Up @@ -862,7 +862,7 @@ impl<T> IbcReceiveResponse<T> {
/// IbcReceiveResponse::new(StdAck::success(b"\x01")).add_messages(msgs)
/// }
/// ```
pub fn add_messages<M: Into<CosmosMsg<T>>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
pub fn add_messages<M: Into<CosmosMsg>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
self.add_submessages(msgs.into_iter().map(SubMsg::new))
}

Expand All @@ -877,7 +877,7 @@ impl<T> IbcReceiveResponse<T> {
/// IbcReceiveResponse::new(StdAck::success(b"\x01")).add_submessages(msgs)
/// }
/// ```
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg<T>>) -> Self {
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg>) -> Self {
self.messages.extend(msgs);
self
}
Expand Down
78 changes: 11 additions & 67 deletions packages/std/src/results/cosmos_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ impl CustomMsg for Empty {}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
// See https://github.com/serde-rs/serde/issues/1296 why we cannot add De-Serialize trait bounds to T
pub enum CosmosMsg<T = Empty> {
pub enum CosmosMsg {
Bank(BankMsg),
// by default we use RawMsg, but a contract can override that
// to call into more app-specific code (whatever they define)
Custom(T),
Custom(Binary),
#[cfg(feature = "staking")]
Staking(StakingMsg),
#[cfg(feature = "staking")]
Expand Down Expand Up @@ -90,34 +90,6 @@ pub enum CosmosMsg<T = Empty> {
Ibc2(Ibc2Msg),
}

impl<T> CosmosMsg<T> {
/// Convert this [`CosmosMsg<T>`] to a [`CosmosMsg<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 is the [`CosmosMsg::Custom`] variant, the function returns `None`.
pub fn change_custom<U>(self) -> Option<CosmosMsg<U>> {
Some(match self {
CosmosMsg::Bank(msg) => CosmosMsg::Bank(msg),
CosmosMsg::Custom(_) => return None,
#[cfg(feature = "staking")]
CosmosMsg::Staking(msg) => CosmosMsg::Staking(msg),
#[cfg(feature = "staking")]
CosmosMsg::Distribution(msg) => CosmosMsg::Distribution(msg),
#[cfg(feature = "stargate")]
CosmosMsg::Stargate { type_url, value } => CosmosMsg::Stargate { type_url, value },
#[cfg(feature = "cosmwasm_2_0")]
CosmosMsg::Any(msg) => CosmosMsg::Any(msg),
#[cfg(feature = "stargate")]
CosmosMsg::Ibc(msg) => CosmosMsg::Ibc(msg),
CosmosMsg::Wasm(msg) => CosmosMsg::Wasm(msg),
#[cfg(feature = "stargate")]
CosmosMsg::Gov(msg) => CosmosMsg::Gov(msg),
#[cfg(feature = "ibc2")]
CosmosMsg::Ibc2(msg) => CosmosMsg::Ibc2(msg),
})
}
}

/// The message types of the bank module.
///
/// See https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto
Expand Down Expand Up @@ -443,21 +415,21 @@ pub fn wasm_execute(
})
}

impl<T> From<BankMsg> for CosmosMsg<T> {
impl From<BankMsg> for CosmosMsg {
fn from(msg: BankMsg) -> Self {
CosmosMsg::Bank(msg)
}
}

#[cfg(feature = "staking")]
impl<T> From<StakingMsg> for CosmosMsg<T> {
impl From<StakingMsg> for CosmosMsg {
fn from(msg: StakingMsg) -> Self {
CosmosMsg::Staking(msg)
}
}

#[cfg(feature = "staking")]
impl<T> From<DistributionMsg> for CosmosMsg<T> {
impl From<DistributionMsg> for CosmosMsg {
fn from(msg: DistributionMsg) -> Self {
CosmosMsg::Distribution(msg)
}
Expand All @@ -466,34 +438,34 @@ impl<T> From<DistributionMsg> for CosmosMsg<T> {
// By implementing `From<MyType> for cosmwasm_std::AnyMsg`,
// you automatically get a MyType -> CosmosMsg conversion.
#[cfg(feature = "cosmwasm_2_0")]
impl<S: Into<AnyMsg>, T> From<S> for CosmosMsg<T> {
impl<S: Into<AnyMsg>> From<S> for CosmosMsg {
fn from(source: S) -> Self {
CosmosMsg::<T>::Any(source.into())
CosmosMsg::Any(source.into())
}
}

impl<T> From<WasmMsg> for CosmosMsg<T> {
impl From<WasmMsg> for CosmosMsg {
fn from(msg: WasmMsg) -> Self {
CosmosMsg::Wasm(msg)
}
}

#[cfg(feature = "stargate")]
impl<T> From<IbcMsg> for CosmosMsg<T> {
impl From<IbcMsg> for CosmosMsg {
fn from(msg: IbcMsg) -> Self {
CosmosMsg::Ibc(msg)
}
}

#[cfg(feature = "stargate")]
impl<T> From<GovMsg> for CosmosMsg<T> {
impl From<GovMsg> for CosmosMsg {
fn from(msg: GovMsg) -> Self {
CosmosMsg::Gov(msg)
}
}

#[cfg(feature = "ibc2")]
impl<T> From<Ibc2Msg> for CosmosMsg<T> {
impl From<Ibc2Msg> for CosmosMsg {
fn from(msg: Ibc2Msg) -> Self {
CosmosMsg::Ibc2(msg)
}
Expand All @@ -503,7 +475,6 @@ impl<T> From<Ibc2Msg> for CosmosMsg<T> {
mod tests {
use super::*;
use crate::{coin, coins};
use fmt::Debug;

#[test]
fn from_bank_msg_works() {
Expand Down Expand Up @@ -736,31 +707,4 @@ mod tests {
);
}
}

#[test]
fn change_custom_works() {
#[derive(Debug, PartialEq, Eq, Clone)]
struct Custom {
_a: i32,
}
let send = BankMsg::Send {
to_address: "you".to_string(),
amount: coins(1015, "earth"),
};
// Custom to Empty
let msg: CosmosMsg<Custom> = send.clone().into();
let msg2: CosmosMsg<Empty> = msg.change_custom().unwrap();
assert_eq!(msg2, CosmosMsg::Bank(send.clone()));
let custom = CosmosMsg::Custom(Custom { _a: 5 });
let converted = custom.change_custom::<Empty>();
assert_eq!(converted, None);

// Empty to Custom
let msg: CosmosMsg<Empty> = send.clone().into();
let msg2: CosmosMsg<Custom> = msg.change_custom().unwrap();
assert_eq!(msg2, CosmosMsg::Bank(send));
let empty = CosmosMsg::Custom(Empty {});
let converted = empty.change_custom::<Custom>();
assert_eq!(converted, None);
}
}
Loading
Loading