Skip to content

Commit

Permalink
Add inner type AnyMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Oct 23, 2023
1 parent a0ced50 commit 0ff12a1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
7 changes: 3 additions & 4 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ pub use crate::query::{
DistributionQuery, FullDelegation, IbcQuery, ListChannelsResponse, PortIdResponse,
QueryRequest, StakingQuery, SupplyResponse, Validator, ValidatorResponse, WasmQuery,
};
pub use crate::results::IntoAny;
#[cfg(all(feature = "stargate", feature = "cosmwasm_1_2"))]
pub use crate::results::WeightedVoteOption;
pub use crate::results::{
attr, wasm_execute, wasm_instantiate, Attribute, BankMsg, ContractResult, CosmosMsg, CustomMsg,
Empty, Event, QueryResponse, Reply, ReplyOn, Response, SubMsg, SubMsgResponse, SubMsgResult,
SystemResult, WasmMsg,
attr, wasm_execute, wasm_instantiate, AnyMsg, Attribute, BankMsg, ContractResult, CosmosMsg,
CustomMsg, Empty, Event, QueryResponse, Reply, ReplyOn, Response, SubMsg, SubMsgResponse,
SubMsgResult, SystemResult, WasmMsg,
};
#[cfg(feature = "staking")]
pub use crate::results::{DistributionMsg, StakingMsg};
Expand Down
61 changes: 44 additions & 17 deletions packages/std/src/results/cosmos_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ pub enum CosmosMsg<T = Empty> {
type_url: String,
value: Binary,
},
Any {
type_url: String,
value: Binary,
},
Any(AnyMsg),
#[cfg(feature = "stargate")]
Ibc(IbcMsg),
Wasm(WasmMsg),
Expand Down Expand Up @@ -124,6 +121,12 @@ pub enum DistributionMsg {
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct AnyMsg {
pub type_url: String,
pub value: Binary,
}

fn binary_to_string(data: &Binary, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
match core::str::from_utf8(data.as_slice()) {
Ok(s) => fmt.write_str(s),
Expand Down Expand Up @@ -384,21 +387,17 @@ impl<T> From<DistributionMsg> for CosmosMsg<T> {
}
}

impl<T> From<WasmMsg> for CosmosMsg<T> {
fn from(msg: WasmMsg) -> Self {
CosmosMsg::Wasm(msg)
// By implementing `From<MyType> for cosmwasm_std::AnyMsg`,
// you automatically get a MyType -> CosmosMsg conversion.
impl<S: Into<AnyMsg>, T> From<S> for CosmosMsg<T> {
fn from(source: S) -> Self {
CosmosMsg::<T>::Any(source.into())
}
}

pub trait IntoAny {
/// Takes self and returns a (type_url, value) pair.
fn into_any(self) -> (String, Binary);
}

impl<S: IntoAny, T> From<S> for CosmosMsg<T> {
fn from(source: S) -> Self {
let (type_url, value) = source.into_any();
CosmosMsg::<T>::Any { type_url, value }
impl<T> From<WasmMsg> for CosmosMsg<T> {
fn from(msg: WasmMsg) -> Self {
CosmosMsg::Wasm(msg)
}
}

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

#[test]
fn from_bank_msg_works() {
Expand Down Expand Up @@ -496,6 +495,34 @@ mod tests {
}
}

#[test]
#[cfg(feature = "stargate")]
fn stargate_msg_serializes_to_correct_json() {
let msg: CosmosMsg = CosmosMsg::Stargate {
type_url: "/cosmos.foo.v1beta.MsgBar".to_string(),
value: Binary::from_base64("5yu/rQ+HrMcxH1zdga7P5hpGMLE=").unwrap(),
};
let json = to_json_string(&msg).unwrap();
assert_eq!(
json,
r#"{"stargate":{"type_url":"/cosmos.foo.v1beta.MsgBar","value":"5yu/rQ+HrMcxH1zdga7P5hpGMLE="}}"#,
);
}

#[test]
fn any_msg_serializes_to_correct_json() {
// Same serialization as CosmosMsg::Stargate (see above), except the top level key
let msg: CosmosMsg = CosmosMsg::Any(AnyMsg {
type_url: "/cosmos.foo.v1beta.MsgBar".to_string(),
value: Binary::from_base64("5yu/rQ+HrMcxH1zdga7P5hpGMLE=").unwrap(),
});
let json = to_json_string(&msg).unwrap();
assert_eq!(
json,
r#"{"any":{"type_url":"/cosmos.foo.v1beta.MsgBar","value":"5yu/rQ+HrMcxH1zdga7P5hpGMLE="}}"#,
);
}

#[test]
#[cfg(feature = "cosmwasm_1_3")]
fn msg_distribution_serializes_to_correct_json() {
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/results/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use contract_result::ContractResult;
#[cfg(all(feature = "stargate", feature = "cosmwasm_1_2"))]
pub use cosmos_msg::WeightedVoteOption;
pub use cosmos_msg::{
wasm_execute, wasm_instantiate, BankMsg, CosmosMsg, CustomMsg, IntoAny, WasmMsg,
wasm_execute, wasm_instantiate, AnyMsg, BankMsg, CosmosMsg, CustomMsg, WasmMsg,
};
#[cfg(feature = "staking")]
pub use cosmos_msg::{DistributionMsg, StakingMsg};
Expand Down

0 comments on commit 0ff12a1

Please sign in to comment.