diff --git a/CHANGELOG.md b/CHANGELOG.md index 624ab95eea..7aba9851a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,8 @@ and this project adheres to - cosmwasm-std: Change `DistributionQuerier::new` to take `IntoIterator` instead of `HashMap`. ([#1941]) - cosmwasm-vm: Make `instantiate` entrypoint optional. ([#1933]) +- cosmwasm-std: Rename `CosmosMsg::Stargate` to `CosmosMsg::Any` and use a + nested msg struct like in other messages. ([#1926]) [#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874 [#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876 @@ -55,6 +57,7 @@ and this project adheres to [#1884]: https://github.com/CosmWasm/cosmwasm/pull/1884 [#1898]: https://github.com/CosmWasm/cosmwasm/pull/1898 [#1902]: https://github.com/CosmWasm/cosmwasm/pull/1902 +[#1926]: https://github.com/CosmWasm/cosmwasm/pull/1926 [#1933]: https://github.com/CosmWasm/cosmwasm/pull/1933 [#1939]: https://github.com/CosmWasm/cosmwasm/pull/1939 [#1940]: https://github.com/CosmWasm/cosmwasm/pull/1940 diff --git a/MIGRATING.md b/MIGRATING.md index cc5450d2e6..d43e8ec50f 100644 --- a/MIGRATING.md +++ b/MIGRATING.md @@ -118,6 +118,13 @@ major releases of `cosmwasm`. Note that you can also view the +Ok(IbcReceiveResponse::new(b"")) ``` +- Replace all uses of `CosmosMsg::Stargate` with `CosmosMsg::Any`: + + ```diff + -CosmosMsg::Stargate { type_url, value } + +CosmosMsg::Any(AnyMsg { type_url, value }) + ``` + ## 1.4.x -> 1.5.0 - Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use): diff --git a/contracts/ibc-reflect-send/schema/ibc-reflect-send.json b/contracts/ibc-reflect-send/schema/ibc-reflect-send.json index f863c79d43..2d20c0778c 100644 --- a/contracts/ibc-reflect-send/schema/ibc-reflect-send.json +++ b/contracts/ibc-reflect-send/schema/ibc-reflect-send.json @@ -114,6 +114,22 @@ } ], "definitions": { + "AnyMsg": { + "description": "A message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", + "type": "object", + "required": [ + "type_url", + "value" + ], + "properties": { + "type_url": { + "type": "string" + }, + "value": { + "$ref": "#/definitions/Binary" + } + } + }, "BankMsg": { "description": "The message types of the bank module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto", "oneOf": [ @@ -241,26 +257,13 @@ "additionalProperties": false }, { - "description": "A Stargate message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", "type": "object", "required": [ - "stargate" + "any" ], "properties": { - "stargate": { - "type": "object", - "required": [ - "type_url", - "value" - ], - "properties": { - "type_url": { - "type": "string" - }, - "value": { - "$ref": "#/definitions/Binary" - } - } + "any": { + "$ref": "#/definitions/AnyMsg" } }, "additionalProperties": false diff --git a/contracts/ibc-reflect-send/schema/ibc/packet_msg.json b/contracts/ibc-reflect-send/schema/ibc/packet_msg.json index f8f8640a5c..5f767c7ef9 100644 --- a/contracts/ibc-reflect-send/schema/ibc/packet_msg.json +++ b/contracts/ibc-reflect-send/schema/ibc/packet_msg.json @@ -52,6 +52,22 @@ } ], "definitions": { + "AnyMsg": { + "description": "A message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", + "type": "object", + "required": [ + "type_url", + "value" + ], + "properties": { + "type_url": { + "type": "string" + }, + "value": { + "$ref": "#/definitions/Binary" + } + } + }, "BankMsg": { "description": "The message types of the bank module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto", "oneOf": [ @@ -179,26 +195,13 @@ "additionalProperties": false }, { - "description": "A Stargate message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", "type": "object", "required": [ - "stargate" + "any" ], "properties": { - "stargate": { - "type": "object", - "required": [ - "type_url", - "value" - ], - "properties": { - "type_url": { - "type": "string" - }, - "value": { - "$ref": "#/definitions/Binary" - } - } + "any": { + "$ref": "#/definitions/AnyMsg" } }, "additionalProperties": false diff --git a/contracts/ibc-reflect-send/schema/raw/execute.json b/contracts/ibc-reflect-send/schema/raw/execute.json index 1765e73a74..9d3924037c 100644 --- a/contracts/ibc-reflect-send/schema/raw/execute.json +++ b/contracts/ibc-reflect-send/schema/raw/execute.json @@ -103,6 +103,22 @@ } ], "definitions": { + "AnyMsg": { + "description": "A message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", + "type": "object", + "required": [ + "type_url", + "value" + ], + "properties": { + "type_url": { + "type": "string" + }, + "value": { + "$ref": "#/definitions/Binary" + } + } + }, "BankMsg": { "description": "The message types of the bank module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto", "oneOf": [ @@ -230,26 +246,13 @@ "additionalProperties": false }, { - "description": "A Stargate message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", "type": "object", "required": [ - "stargate" + "any" ], "properties": { - "stargate": { - "type": "object", - "required": [ - "type_url", - "value" - ], - "properties": { - "type_url": { - "type": "string" - }, - "value": { - "$ref": "#/definitions/Binary" - } - } + "any": { + "$ref": "#/definitions/AnyMsg" } }, "additionalProperties": false diff --git a/contracts/ibc-reflect/schema/ibc/packet_msg.json b/contracts/ibc-reflect/schema/ibc/packet_msg.json index 6856f57368..be30fef8c5 100644 --- a/contracts/ibc-reflect/schema/ibc/packet_msg.json +++ b/contracts/ibc-reflect/schema/ibc/packet_msg.json @@ -112,6 +112,22 @@ } ], "definitions": { + "AnyMsg": { + "description": "A message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", + "type": "object", + "required": [ + "type_url", + "value" + ], + "properties": { + "type_url": { + "type": "string" + }, + "value": { + "$ref": "#/definitions/Binary" + } + } + }, "BankMsg": { "description": "The message types of the bank module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto", "oneOf": [ @@ -215,26 +231,13 @@ "additionalProperties": false }, { - "description": "A Stargate message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", "type": "object", "required": [ - "stargate" + "any" ], "properties": { - "stargate": { - "type": "object", - "required": [ - "type_url", - "value" - ], - "properties": { - "type_url": { - "type": "string" - }, - "value": { - "$ref": "#/definitions/Binary" - } - } + "any": { + "$ref": "#/definitions/AnyMsg" } }, "additionalProperties": false diff --git a/contracts/reflect/schema/raw/execute.json b/contracts/reflect/schema/raw/execute.json index abd024e972..0af550511d 100644 --- a/contracts/reflect/schema/raw/execute.json +++ b/contracts/reflect/schema/raw/execute.json @@ -73,6 +73,22 @@ } ], "definitions": { + "AnyMsg": { + "description": "A message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", + "type": "object", + "required": [ + "type_url", + "value" + ], + "properties": { + "type_url": { + "type": "string" + }, + "value": { + "$ref": "#/definitions/Binary" + } + } + }, "BankMsg": { "description": "The message types of the bank module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto", "oneOf": [ @@ -200,26 +216,13 @@ "additionalProperties": false }, { - "description": "A Stargate message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", "type": "object", "required": [ - "stargate" + "any" ], "properties": { - "stargate": { - "type": "object", - "required": [ - "type_url", - "value" - ], - "properties": { - "type_url": { - "type": "string" - }, - "value": { - "$ref": "#/definitions/Binary" - } - } + "any": { + "$ref": "#/definitions/AnyMsg" } }, "additionalProperties": false diff --git a/contracts/reflect/schema/reflect.json b/contracts/reflect/schema/reflect.json index 21b899f174..09a84719e2 100644 --- a/contracts/reflect/schema/reflect.json +++ b/contracts/reflect/schema/reflect.json @@ -83,6 +83,22 @@ } ], "definitions": { + "AnyMsg": { + "description": "A message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", + "type": "object", + "required": [ + "type_url", + "value" + ], + "properties": { + "type_url": { + "type": "string" + }, + "value": { + "$ref": "#/definitions/Binary" + } + } + }, "BankMsg": { "description": "The message types of the bank module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto", "oneOf": [ @@ -210,26 +226,13 @@ "additionalProperties": false }, { - "description": "A Stargate message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)", "type": "object", "required": [ - "stargate" + "any" ], "properties": { - "stargate": { - "type": "object", - "required": [ - "type_url", - "value" - ], - "properties": { - "type_url": { - "type": "string" - }, - "value": { - "$ref": "#/definitions/Binary" - } - } + "any": { + "$ref": "#/definitions/AnyMsg" } }, "additionalProperties": false diff --git a/packages/std/src/lib.rs b/packages/std/src/lib.rs index df60c6debc..559e9852f3 100644 --- a/packages/std/src/lib.rs +++ b/packages/std/src/lib.rs @@ -80,9 +80,9 @@ pub use crate::query::{ #[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}; diff --git a/packages/std/src/results/cosmos_msg.rs b/packages/std/src/results/cosmos_msg.rs index 27ebd8249b..9c60c25bbf 100644 --- a/packages/std/src/results/cosmos_msg.rs +++ b/packages/std/src/results/cosmos_msg.rs @@ -33,13 +33,7 @@ pub enum CosmosMsg { Staking(StakingMsg), #[cfg(feature = "staking")] Distribution(DistributionMsg), - /// A Stargate message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). - /// This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md) - #[cfg(feature = "stargate")] - Stargate { - type_url: String, - value: Binary, - }, + Any(AnyMsg), #[cfg(feature = "stargate")] Ibc(IbcMsg), Wasm(WasmMsg), @@ -120,6 +114,14 @@ pub enum DistributionMsg { }, } +/// A message encoded the same way as a protobuf [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto). +/// This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md) +#[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), @@ -380,6 +382,14 @@ impl From for CosmosMsg { } } +// By implementing `From for cosmwasm_std::AnyMsg`, +// you automatically get a MyType -> CosmosMsg conversion. +impl, T> From for CosmosMsg { + fn from(source: S) -> Self { + CosmosMsg::::Any(source.into()) + } +} + impl From for CosmosMsg { fn from(msg: WasmMsg) -> Self { CosmosMsg::Wasm(msg) @@ -403,7 +413,7 @@ impl From for CosmosMsg { #[cfg(test)] mod tests { use super::*; - use crate::{coin, coins}; + use crate::{coin, coins, to_json_string}; #[test] fn from_bank_msg_works() { @@ -417,6 +427,33 @@ mod tests { } } + #[test] + fn from_any_msg_works() { + // should work with AnyMsg + let any = AnyMsg { + type_url: "/cosmos.foo.v1beta.MsgBar".to_string(), + value: Binary::from_base64("5yu/rQ+HrMcxH1zdga7P5hpGMLE=").unwrap(), + }; + let msg: CosmosMsg = any.clone().into(); + assert!(matches!(msg, CosmosMsg::Any(a) if a == any)); + + // should work with Into + struct IntoAny; + impl From for AnyMsg { + fn from(_: IntoAny) -> Self { + AnyMsg { + type_url: "/cosmos.foo.v1beta.MsgBar".to_string(), + value: Binary::from_base64("5yu/rQ+HrMcxH1zdga7P5hpGMLE=").unwrap(), + } + } + } + let msg: CosmosMsg = IntoAny.into(); + assert!(matches!( + msg, + CosmosMsg::Any(a) if a == any + )); + } + #[test] fn wasm_msg_serializes_to_correct_json() { // Instantiate with admin @@ -480,6 +517,20 @@ mod tests { } } + #[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() { diff --git a/packages/std/src/results/mod.rs b/packages/std/src/results/mod.rs index a45afbd14a..2df471c3ea 100644 --- a/packages/std/src/results/mod.rs +++ b/packages/std/src/results/mod.rs @@ -12,7 +12,9 @@ mod system_result; 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, WasmMsg}; +pub use cosmos_msg::{ + wasm_execute, wasm_instantiate, AnyMsg, BankMsg, CosmosMsg, CustomMsg, WasmMsg, +}; #[cfg(feature = "staking")] pub use cosmos_msg::{DistributionMsg, StakingMsg}; #[cfg(feature = "stargate")]