Skip to content

Commit dca8b2e

Browse files
committed
feat: Remove the generic param from CosmosMsg
1 parent b24e652 commit dca8b2e

File tree

12 files changed

+147
-280
lines changed

12 files changed

+147
-280
lines changed

contracts/ibc2/src/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn ibc2_packet_receive(
4545
deps: DepsMut,
4646
env: Env,
4747
msg: Ibc2PacketReceiveMsg,
48-
) -> StdResult<IbcReceiveResponse> {
48+
) -> Result<IbcReceiveResponse, StdError> {
4949
let binary_payload = &msg.payload.value;
5050
let json_payload: IbcPayload = from_json(binary_payload)?;
5151

packages/std/src/ibc.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
77

88
use crate::coin::Coin;
99
use crate::prelude::*;
10-
use crate::results::{Attribute, CosmosMsg, Empty, Event, SubMsg};
10+
use crate::results::{Attribute, CosmosMsg, Event, SubMsg};
1111
use crate::StdResult;
1212
use crate::{to_json_binary, Binary};
1313
use crate::{Addr, Timestamp};
@@ -560,12 +560,12 @@ impl IbcPacketTimeoutMsg {
560560
/// will use other Response types
561561
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
562562
#[non_exhaustive]
563-
pub struct IbcBasicResponse<T = Empty> {
563+
pub struct IbcBasicResponse {
564564
/// Optional list of messages to pass. These will be executed in order.
565565
/// If the ReplyOn member is set, they will invoke this contract's `reply` entry point
566566
/// after execution. Otherwise, they act like "fire and forget".
567567
/// Use `SubMsg::new` to create messages with the older "fire and forget" semantics.
568-
pub messages: Vec<SubMsg<T>>,
568+
pub messages: Vec<SubMsg>,
569569
/// The attributes that will be emitted as part of a `wasm` event.
570570
///
571571
/// More info about events (and their attributes) can be found in [*Cosmos SDK* docs].
@@ -582,7 +582,7 @@ pub struct IbcBasicResponse<T = Empty> {
582582
}
583583

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

595-
impl<T> IbcBasicResponse<T> {
595+
impl IbcBasicResponse {
596596
pub fn new() -> Self {
597597
Self::default()
598598
}
@@ -605,14 +605,14 @@ impl<T> IbcBasicResponse<T> {
605605

606606
/// This creates a "fire and forget" message, by using `SubMsg::new()` to wrap it,
607607
/// and adds it to the list of messages to process.
608-
pub fn add_message(mut self, msg: impl Into<CosmosMsg<T>>) -> Self {
608+
pub fn add_message(mut self, msg: impl Into<CosmosMsg>) -> Self {
609609
self.messages.push(SubMsg::new(msg));
610610
self
611611
}
612612

613613
/// This takes an explicit SubMsg (creates via e.g. `reply_on_error`)
614614
/// and adds it to the list of messages to process.
615-
pub fn add_submessage(mut self, msg: SubMsg<T>) -> Self {
615+
pub fn add_submessage(mut self, msg: SubMsg) -> Self {
616616
self.messages.push(msg);
617617
self
618618
}
@@ -664,7 +664,7 @@ impl<T> IbcBasicResponse<T> {
664664
/// IbcBasicResponse::new().add_messages(msgs)
665665
/// }
666666
/// ```
667-
pub fn add_messages<M: Into<CosmosMsg<T>>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
667+
pub fn add_messages<M: Into<CosmosMsg>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
668668
self.add_submessages(msgs.into_iter().map(SubMsg::new))
669669
}
670670

@@ -679,7 +679,7 @@ impl<T> IbcBasicResponse<T> {
679679
/// IbcBasicResponse::new().add_submessages(msgs)
680680
/// }
681681
/// ```
682-
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg<T>>) -> Self {
682+
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg>) -> Self {
683683
self.messages.extend(msgs);
684684
self
685685
}
@@ -702,7 +702,7 @@ impl<T> IbcBasicResponse<T> {
702702
/// and not inform the calling chain).
703703
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
704704
#[non_exhaustive]
705-
pub struct IbcReceiveResponse<T = Empty> {
705+
pub struct IbcReceiveResponse {
706706
/// The bytes we return to the contract that sent the packet.
707707
/// This may represent a success or error of execution.
708708
/// In case of `None`, no acknowledgement is written.
@@ -711,7 +711,7 @@ pub struct IbcReceiveResponse<T = Empty> {
711711
/// If the ReplyOn member is set, they will invoke this contract's `reply` entry point
712712
/// after execution. Otherwise, they act like "fire and forget".
713713
/// Use `call` or `msg.into()` to create messages with the older "fire and forget" semantics.
714-
pub messages: Vec<SubMsg<T>>,
714+
pub messages: Vec<SubMsg>,
715715
/// The attributes that will be emitted as part of a "wasm" event.
716716
///
717717
/// More info about events (and their attributes) can be found in [*Cosmos SDK* docs].
@@ -727,7 +727,7 @@ pub struct IbcReceiveResponse<T = Empty> {
727727
pub events: Vec<Event>,
728728
}
729729

730-
impl<T> IbcReceiveResponse<T> {
730+
impl IbcReceiveResponse {
731731
/// Create a new response with the given acknowledgement.
732732
///
733733
/// ## Examples
@@ -803,14 +803,14 @@ impl<T> IbcReceiveResponse<T> {
803803

804804
/// This creates a "fire and forget" message, by using `SubMsg::new()` to wrap it,
805805
/// and adds it to the list of messages to process.
806-
pub fn add_message(mut self, msg: impl Into<CosmosMsg<T>>) -> Self {
806+
pub fn add_message(mut self, msg: impl Into<CosmosMsg>) -> Self {
807807
self.messages.push(SubMsg::new(msg));
808808
self
809809
}
810810

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

@@ -877,7 +877,7 @@ impl<T> IbcReceiveResponse<T> {
877877
/// IbcReceiveResponse::new(StdAck::success(b"\x01")).add_submessages(msgs)
878878
/// }
879879
/// ```
880-
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg<T>>) -> Self {
880+
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg>) -> Self {
881881
self.messages.extend(msgs);
882882
self
883883
}

packages/std/src/results/cosmos_msg.rs

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ impl CustomMsg for Empty {}
5656
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
5757
#[serde(rename_all = "snake_case")]
5858
// See https://github.com/serde-rs/serde/issues/1296 why we cannot add De-Serialize trait bounds to T
59-
pub enum CosmosMsg<T = Empty> {
59+
pub enum CosmosMsg {
6060
Bank(BankMsg),
6161
// by default we use RawMsg, but a contract can override that
6262
// to call into more app-specific code (whatever they define)
63-
Custom(T),
63+
Custom(Binary),
6464
#[cfg(feature = "staking")]
6565
Staking(StakingMsg),
6666
#[cfg(feature = "staking")]
@@ -90,34 +90,6 @@ pub enum CosmosMsg<T = Empty> {
9090
Ibc2(Ibc2Msg),
9191
}
9292

93-
impl<T> CosmosMsg<T> {
94-
/// Convert this [`CosmosMsg<T>`] to a [`CosmosMsg<U>`] with a different custom message type.
95-
/// This allows easier interactions between code written for a specific chain and
96-
/// code written for multiple chains.
97-
/// If this is the [`CosmosMsg::Custom`] variant, the function returns `None`.
98-
pub fn change_custom<U>(self) -> Option<CosmosMsg<U>> {
99-
Some(match self {
100-
CosmosMsg::Bank(msg) => CosmosMsg::Bank(msg),
101-
CosmosMsg::Custom(_) => return None,
102-
#[cfg(feature = "staking")]
103-
CosmosMsg::Staking(msg) => CosmosMsg::Staking(msg),
104-
#[cfg(feature = "staking")]
105-
CosmosMsg::Distribution(msg) => CosmosMsg::Distribution(msg),
106-
#[cfg(feature = "stargate")]
107-
CosmosMsg::Stargate { type_url, value } => CosmosMsg::Stargate { type_url, value },
108-
#[cfg(feature = "cosmwasm_2_0")]
109-
CosmosMsg::Any(msg) => CosmosMsg::Any(msg),
110-
#[cfg(feature = "stargate")]
111-
CosmosMsg::Ibc(msg) => CosmosMsg::Ibc(msg),
112-
CosmosMsg::Wasm(msg) => CosmosMsg::Wasm(msg),
113-
#[cfg(feature = "stargate")]
114-
CosmosMsg::Gov(msg) => CosmosMsg::Gov(msg),
115-
#[cfg(feature = "ibc2")]
116-
CosmosMsg::Ibc2(msg) => CosmosMsg::Ibc2(msg),
117-
})
118-
}
119-
}
120-
12193
/// The message types of the bank module.
12294
///
12395
/// See https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto
@@ -443,21 +415,21 @@ pub fn wasm_execute(
443415
})
444416
}
445417

446-
impl<T> From<BankMsg> for CosmosMsg<T> {
418+
impl From<BankMsg> for CosmosMsg {
447419
fn from(msg: BankMsg) -> Self {
448420
CosmosMsg::Bank(msg)
449421
}
450422
}
451423

452424
#[cfg(feature = "staking")]
453-
impl<T> From<StakingMsg> for CosmosMsg<T> {
425+
impl From<StakingMsg> for CosmosMsg {
454426
fn from(msg: StakingMsg) -> Self {
455427
CosmosMsg::Staking(msg)
456428
}
457429
}
458430

459431
#[cfg(feature = "staking")]
460-
impl<T> From<DistributionMsg> for CosmosMsg<T> {
432+
impl From<DistributionMsg> for CosmosMsg {
461433
fn from(msg: DistributionMsg) -> Self {
462434
CosmosMsg::Distribution(msg)
463435
}
@@ -466,34 +438,34 @@ impl<T> From<DistributionMsg> for CosmosMsg<T> {
466438
// By implementing `From<MyType> for cosmwasm_std::AnyMsg`,
467439
// you automatically get a MyType -> CosmosMsg conversion.
468440
#[cfg(feature = "cosmwasm_2_0")]
469-
impl<S: Into<AnyMsg>, T> From<S> for CosmosMsg<T> {
441+
impl<S: Into<AnyMsg>> From<S> for CosmosMsg {
470442
fn from(source: S) -> Self {
471-
CosmosMsg::<T>::Any(source.into())
443+
CosmosMsg::Any(source.into())
472444
}
473445
}
474446

475-
impl<T> From<WasmMsg> for CosmosMsg<T> {
447+
impl From<WasmMsg> for CosmosMsg {
476448
fn from(msg: WasmMsg) -> Self {
477449
CosmosMsg::Wasm(msg)
478450
}
479451
}
480452

481453
#[cfg(feature = "stargate")]
482-
impl<T> From<IbcMsg> for CosmosMsg<T> {
454+
impl From<IbcMsg> for CosmosMsg {
483455
fn from(msg: IbcMsg) -> Self {
484456
CosmosMsg::Ibc(msg)
485457
}
486458
}
487459

488460
#[cfg(feature = "stargate")]
489-
impl<T> From<GovMsg> for CosmosMsg<T> {
461+
impl From<GovMsg> for CosmosMsg {
490462
fn from(msg: GovMsg) -> Self {
491463
CosmosMsg::Gov(msg)
492464
}
493465
}
494466

495467
#[cfg(feature = "ibc2")]
496-
impl<T> From<Ibc2Msg> for CosmosMsg<T> {
468+
impl From<Ibc2Msg> for CosmosMsg {
497469
fn from(msg: Ibc2Msg) -> Self {
498470
CosmosMsg::Ibc2(msg)
499471
}
@@ -736,31 +708,4 @@ mod tests {
736708
);
737709
}
738710
}
739-
740-
#[test]
741-
fn change_custom_works() {
742-
#[derive(Debug, PartialEq, Eq, Clone)]
743-
struct Custom {
744-
_a: i32,
745-
}
746-
let send = BankMsg::Send {
747-
to_address: "you".to_string(),
748-
amount: coins(1015, "earth"),
749-
};
750-
// Custom to Empty
751-
let msg: CosmosMsg<Custom> = send.clone().into();
752-
let msg2: CosmosMsg<Empty> = msg.change_custom().unwrap();
753-
assert_eq!(msg2, CosmosMsg::Bank(send.clone()));
754-
let custom = CosmosMsg::Custom(Custom { _a: 5 });
755-
let converted = custom.change_custom::<Empty>();
756-
assert_eq!(converted, None);
757-
758-
// Empty to Custom
759-
let msg: CosmosMsg<Empty> = send.clone().into();
760-
let msg2: CosmosMsg<Custom> = msg.change_custom().unwrap();
761-
assert_eq!(msg2, CosmosMsg::Bank(send));
762-
let empty = CosmosMsg::Custom(Empty {});
763-
let converted = empty.change_custom::<Custom>();
764-
assert_eq!(converted, None);
765-
}
766711
}

0 commit comments

Comments
 (0)