@@ -40,10 +40,7 @@ pub enum CosmosMsg<T = Empty> {
40
40
type_url : String ,
41
41
value : Binary ,
42
42
} ,
43
- Any {
44
- type_url : String ,
45
- value : Binary ,
46
- } ,
43
+ Any ( AnyMsg ) ,
47
44
#[ cfg( feature = "stargate" ) ]
48
45
Ibc ( IbcMsg ) ,
49
46
Wasm ( WasmMsg ) ,
@@ -124,6 +121,12 @@ pub enum DistributionMsg {
124
121
} ,
125
122
}
126
123
124
+ #[ derive( Serialize , Deserialize , Clone , Debug , PartialEq , Eq , JsonSchema ) ]
125
+ pub struct AnyMsg {
126
+ pub type_url : String ,
127
+ pub value : Binary ,
128
+ }
129
+
127
130
fn binary_to_string ( data : & Binary , fmt : & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
128
131
match core:: str:: from_utf8 ( data. as_slice ( ) ) {
129
132
Ok ( s) => fmt. write_str ( s) ,
@@ -384,21 +387,17 @@ impl<T> From<DistributionMsg> for CosmosMsg<T> {
384
387
}
385
388
}
386
389
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 ( ) )
390
395
}
391
396
}
392
397
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)
402
401
}
403
402
}
404
403
@@ -419,7 +418,7 @@ impl<T> From<GovMsg> for CosmosMsg<T> {
419
418
#[ cfg( test) ]
420
419
mod tests {
421
420
use super :: * ;
422
- use crate :: { coin, coins} ;
421
+ use crate :: { coin, coins, to_json_string } ;
423
422
424
423
#[ test]
425
424
fn from_bank_msg_works ( ) {
@@ -496,6 +495,34 @@ mod tests {
496
495
}
497
496
}
498
497
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
+
499
526
#[ test]
500
527
#[ cfg( feature = "cosmwasm_1_3" ) ]
501
528
fn msg_distribution_serializes_to_correct_json ( ) {
0 commit comments