1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use std::fmt; use crate::addresses::HumanAddr; use crate::binary::Binary; use crate::coins::Coin; use crate::errors::StdResult; #[cfg(feature = "stargate")] use crate::ibc::IbcMsg; use crate::serde::to_binary; use super::Empty; #[non_exhaustive] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] // See https://github.com/serde-rs/serde/issues/1296 why we cannot add De-Serialize trait bounds to T pub enum CosmosMsg<T = Empty> where T: Clone + fmt::Debug + PartialEq + JsonSchema, { Bank(BankMsg), // by default we use RawMsg, but a contract can override that // to call into more app-specific code (whatever they define) Custom(T), Staking(StakingMsg), /// A Stargate message encoded the same way as a protobof [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, }, #[cfg(feature = "stargate")] Ibc(IbcMsg), Wasm(WasmMsg), } /// The message types of the bank module. /// /// See https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto #[non_exhaustive] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum BankMsg { /// Sends native tokens from the contract to the given address. /// /// This is translated to a [MsgSend](https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto#L19-L28). /// `from_address` is automatically filled with the current contract's address. Send { to_address: HumanAddr, amount: Vec<Coin>, }, } /// The message types of the staking module. /// /// See https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/tx.proto #[non_exhaustive] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum StakingMsg { /// This is translated to a [MsgDelegate](https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/tx.proto#L81-L90). /// `delegator_address` is automatically filled with the current contract's address. Delegate { validator: HumanAddr, amount: Coin }, /// This is translated to a [MsgUndelegate](https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/tx.proto#L112-L121). /// `delegator_address` is automatically filled with the current contract's address. Undelegate { validator: HumanAddr, amount: Coin }, /// This is translated to a [MsgSetWithdrawAddress](https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/distribution/v1beta1/tx.proto#L29-L37) /// followed by a [MsgWithdrawDelegatorReward](https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/distribution/v1beta1/tx.proto#L42-L50). /// `delegator_address` is automatically filled with the current contract's address. Withdraw { validator: HumanAddr, /// this is the "withdraw address", the one that should receive the rewards /// if None, then use delegator address recipient: Option<HumanAddr>, }, /// This is translated to a [MsgBeginRedelegate](https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/tx.proto#L95-L105). /// `delegator_address` is automatically filled with the current contract's address. Redelegate { src_validator: HumanAddr, dst_validator: HumanAddr, amount: Coin, }, } /// The message types of the wasm module. /// /// See https://github.com/CosmWasm/wasmd/blob/v0.14.0/x/wasm/internal/types/tx.proto #[non_exhaustive] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum WasmMsg { /// Dispatches a call to another contract at a known address (with known ABI). /// /// This is translated to a [MsgExecuteContract](https://github.com/CosmWasm/wasmd/blob/v0.14.0/x/wasm/internal/types/tx.proto#L68-L78). /// `sender` is automatically filled with the current contract's address. Execute { contract_addr: HumanAddr, /// msg is the json-encoded ExecuteMsg struct (as raw Binary) msg: Binary, send: Vec<Coin>, }, /// Instantiates a new contracts from previously uploaded Wasm code. /// /// This is translated to a [MsgInstantiateContract](https://github.com/CosmWasm/wasmd/blob/v0.14.0/x/wasm/internal/types/tx.proto#L47-L61). /// `sender` is automatically filled with the current contract's address. Instantiate { code_id: u64, /// msg is the JSON-encoded InstantiateMsg struct (as raw Binary) msg: Binary, send: Vec<Coin>, /// A human-readbale label for the contract label: String, }, /// Migrates a given contracts to use new wasm code. Passes a MigrateMsg to allow us to /// customize behavior. /// /// Only the contract admin (as defined in wasmd), if any, is able to make this call. /// /// This is translated to a [MsgMigrateContract](https://github.com/CosmWasm/wasmd/blob/v0.14.0/x/wasm/internal/types/tx.proto#L86-L96). /// `sender` is automatically filled with the current contract's address. Migrate { contract_addr: HumanAddr, /// the code_id of the new logic to place in the given contract new_code_id: u64, /// msg is the json-encoded MigrateMsg struct that will be passed to the new code msg: Binary, }, } /// Shortcut helper as the construction of WasmMsg::Instantiate can be quite verbose in contract code pub fn wasm_instantiate<T>( code_id: u64, msg: &T, send: Vec<Coin>, label: String, ) -> StdResult<WasmMsg> where T: Serialize, { let payload = to_binary(msg)?; Ok(WasmMsg::Instantiate { code_id, msg: payload, send, label, }) } /// Shortcut helper as the construction of WasmMsg::Instantiate can be quite verbose in contract code pub fn wasm_execute<T, U>(contract_addr: T, msg: &U, send: Vec<Coin>) -> StdResult<WasmMsg> where T: Into<HumanAddr>, U: Serialize, { let payload = to_binary(msg)?; Ok(WasmMsg::Execute { contract_addr: contract_addr.into(), msg: payload, send, }) } impl<T: Clone + fmt::Debug + PartialEq + JsonSchema> From<BankMsg> for CosmosMsg<T> { fn from(msg: BankMsg) -> Self { CosmosMsg::Bank(msg) } } #[cfg(feature = "staking")] impl<T: Clone + fmt::Debug + PartialEq + JsonSchema> From<StakingMsg> for CosmosMsg<T> { fn from(msg: StakingMsg) -> Self { CosmosMsg::Staking(msg) } } impl<T: Clone + fmt::Debug + PartialEq + JsonSchema> From<WasmMsg> for CosmosMsg<T> { fn from(msg: WasmMsg) -> Self { CosmosMsg::Wasm(msg) } } #[cfg(feature = "stargate")] impl<T: Clone + fmt::Debug + PartialEq + JsonSchema> From<IbcMsg> for CosmosMsg<T> { fn from(msg: IbcMsg) -> Self { CosmosMsg::Ibc(msg) } } #[cfg(test)] mod tests { use super::*; use crate::coins; #[test] fn from_bank_msg_works() { let to_address = HumanAddr::from("you"); let amount = coins(1015, "earth"); let bank = BankMsg::Send { to_address, amount }; let msg: CosmosMsg = bank.clone().into(); match msg { CosmosMsg::Bank(msg) => assert_eq!(bank, msg), _ => panic!("must encode in Bank variant"), } } }