Skip to content

Commit 025ff70

Browse files
webmaster128chipshort
authored andcommitted
Add inner type AnyMsg
1 parent 07dc89f commit 025ff70

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

packages/std/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,12 @@ pub use crate::query::{
7777
DistributionQuery, FullDelegation, IbcQuery, ListChannelsResponse, PortIdResponse,
7878
QueryRequest, StakingQuery, SupplyResponse, Validator, ValidatorResponse, WasmQuery,
7979
};
80-
pub use crate::results::IntoAny;
8180
#[cfg(all(feature = "stargate", feature = "cosmwasm_1_2"))]
8281
pub use crate::results::WeightedVoteOption;
8382
pub use crate::results::{
84-
attr, wasm_execute, wasm_instantiate, Attribute, BankMsg, ContractResult, CosmosMsg, CustomMsg,
85-
Empty, Event, QueryResponse, Reply, ReplyOn, Response, SubMsg, SubMsgResponse, SubMsgResult,
86-
SystemResult, WasmMsg,
83+
attr, wasm_execute, wasm_instantiate, AnyMsg, Attribute, BankMsg, ContractResult, CosmosMsg,
84+
CustomMsg, Empty, Event, QueryResponse, Reply, ReplyOn, Response, SubMsg, SubMsgResponse,
85+
SubMsgResult, SystemResult, WasmMsg,
8786
};
8887
#[cfg(feature = "staking")]
8988
pub use crate::results::{DistributionMsg, StakingMsg};

packages/std/src/results/cosmos_msg.rs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ pub enum CosmosMsg<T = Empty> {
4040
type_url: String,
4141
value: Binary,
4242
},
43-
Any {
44-
type_url: String,
45-
value: Binary,
46-
},
43+
Any(AnyMsg),
4744
#[cfg(feature = "stargate")]
4845
Ibc(IbcMsg),
4946
Wasm(WasmMsg),
@@ -124,6 +121,12 @@ pub enum DistributionMsg {
124121
},
125122
}
126123

124+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
125+
pub struct AnyMsg {
126+
pub type_url: String,
127+
pub value: Binary,
128+
}
129+
127130
fn binary_to_string(data: &Binary, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
128131
match core::str::from_utf8(data.as_slice()) {
129132
Ok(s) => fmt.write_str(s),
@@ -384,21 +387,17 @@ impl<T> From<DistributionMsg> for CosmosMsg<T> {
384387
}
385388
}
386389

387-
impl<T> From<WasmMsg> for CosmosMsg<T> {
388-
fn from(msg: WasmMsg) -> Self {
389-
CosmosMsg::Wasm(msg)
390+
// By implementing `From<MyType> for cosmwasm_std::AnyMsg`,
391+
// you automatically get a MyType -> CosmosMsg conversion.
392+
impl<S: Into<AnyMsg>, T> From<S> for CosmosMsg<T> {
393+
fn from(source: S) -> Self {
394+
CosmosMsg::<T>::Any(source.into())
390395
}
391396
}
392397

393-
pub trait IntoAny {
394-
/// Takes self and returns a (type_url, value) pair.
395-
fn into_any(self) -> (String, Binary);
396-
}
397-
398-
impl<S: IntoAny, T> From<S> for CosmosMsg<T> {
399-
fn from(source: S) -> Self {
400-
let (type_url, value) = source.into_any();
401-
CosmosMsg::<T>::Any { type_url, value }
398+
impl<T> From<WasmMsg> for CosmosMsg<T> {
399+
fn from(msg: WasmMsg) -> Self {
400+
CosmosMsg::Wasm(msg)
402401
}
403402
}
404403

@@ -419,7 +418,7 @@ impl<T> From<GovMsg> for CosmosMsg<T> {
419418
#[cfg(test)]
420419
mod tests {
421420
use super::*;
422-
use crate::{coin, coins};
421+
use crate::{coin, coins, to_json_string};
423422

424423
#[test]
425424
fn from_bank_msg_works() {
@@ -496,6 +495,34 @@ mod tests {
496495
}
497496
}
498497

498+
#[test]
499+
#[cfg(feature = "stargate")]
500+
fn stargate_msg_serializes_to_correct_json() {
501+
let msg: CosmosMsg = CosmosMsg::Stargate {
502+
type_url: "/cosmos.foo.v1beta.MsgBar".to_string(),
503+
value: Binary::from_base64("5yu/rQ+HrMcxH1zdga7P5hpGMLE=").unwrap(),
504+
};
505+
let json = to_json_string(&msg).unwrap();
506+
assert_eq!(
507+
json,
508+
r#"{"stargate":{"type_url":"/cosmos.foo.v1beta.MsgBar","value":"5yu/rQ+HrMcxH1zdga7P5hpGMLE="}}"#,
509+
);
510+
}
511+
512+
#[test]
513+
fn any_msg_serializes_to_correct_json() {
514+
// Same serialization as CosmosMsg::Stargate (see above), except the top level key
515+
let msg: CosmosMsg = CosmosMsg::Any(AnyMsg {
516+
type_url: "/cosmos.foo.v1beta.MsgBar".to_string(),
517+
value: Binary::from_base64("5yu/rQ+HrMcxH1zdga7P5hpGMLE=").unwrap(),
518+
});
519+
let json = to_json_string(&msg).unwrap();
520+
assert_eq!(
521+
json,
522+
r#"{"any":{"type_url":"/cosmos.foo.v1beta.MsgBar","value":"5yu/rQ+HrMcxH1zdga7P5hpGMLE="}}"#,
523+
);
524+
}
525+
499526
#[test]
500527
#[cfg(feature = "cosmwasm_1_3")]
501528
fn msg_distribution_serializes_to_correct_json() {

packages/std/src/results/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use contract_result::ContractResult;
1313
#[cfg(all(feature = "stargate", feature = "cosmwasm_1_2"))]
1414
pub use cosmos_msg::WeightedVoteOption;
1515
pub use cosmos_msg::{
16-
wasm_execute, wasm_instantiate, BankMsg, CosmosMsg, CustomMsg, IntoAny, WasmMsg,
16+
wasm_execute, wasm_instantiate, AnyMsg, BankMsg, CosmosMsg, CustomMsg, WasmMsg,
1717
};
1818
#[cfg(feature = "staking")]
1919
pub use cosmos_msg::{DistributionMsg, StakingMsg};

0 commit comments

Comments
 (0)