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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
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),
#[cfg(feature = "staking")]
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,
},
#[cfg(feature = "stargate")]
Ibc(IbcMsg),
Wasm(WasmMsg),
#[cfg(feature = "stargate")]
Gov(GovMsg),
}
/// 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: String,
amount: Vec<Coin>,
},
/// This will burn the given coins from the contract's account.
/// There is no Cosmos SDK message that performs this, but it can be done by calling the bank keeper.
/// Important if a contract controls significant token supply that must be retired.
Burn { 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
#[cfg(feature = "staking")]
#[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: String, 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: String, amount: Coin },
/// 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: String,
dst_validator: String,
amount: Coin,
},
}
/// The message types of the distribution module.
///
/// See https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/proto/cosmos/distribution/v1beta1/tx.proto
#[cfg(feature = "staking")]
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum DistributionMsg {
/// This is translated to a [MsgSetWithdrawAddress](https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/proto/cosmos/distribution/v1beta1/tx.proto#L29-L37).
/// `delegator_address` is automatically filled with the current contract's address.
SetWithdrawAddress {
/// The `withdraw_address`
address: String,
},
/// This is translated to a [[MsgWithdrawDelegatorReward](https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/proto/cosmos/distribution/v1beta1/tx.proto#L42-L50).
/// `delegator_address` is automatically filled with the current contract's address.
WithdrawDelegatorReward {
/// The `validator_address`
validator: String,
},
}
/// 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: String,
/// msg is the json-encoded ExecuteMsg struct (as raw Binary)
msg: Binary,
funds: Vec<Coin>,
},
/// Instantiates a new contracts from previously uploaded Wasm code.
///
/// This is translated to a [MsgInstantiateContract](https://github.com/CosmWasm/wasmd/blob/v0.16.0-alpha1/x/wasm/internal/types/tx.proto#L47-L61).
/// `sender` is automatically filled with the current contract's address.
Instantiate {
admin: Option<String>,
code_id: u64,
/// msg is the JSON-encoded InstantiateMsg struct (as raw Binary)
msg: Binary,
funds: 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: String,
/// 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,
},
/// Sets a new admin (for migrate) on the given contract.
/// Fails if this contract is not currently admin of the target contract.
UpdateAdmin {
contract_addr: String,
admin: String,
},
/// Clears the admin on the given contract, so no more migration possible.
/// Fails if this contract is not currently admin of the target contract.
ClearAdmin { contract_addr: String },
}
#[cfg(feature = "stargate")]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum GovMsg {
/// This maps directly to [MsgVote](https://github.com/cosmos/cosmos-sdk/blob/v0.42.5/proto/cosmos/gov/v1beta1/tx.proto#L46-L56) in the Cosmos SDK with voter set to the contract address.
Vote { proposal_id: u64, vote: VoteOption },
}
#[cfg(feature = "stargate")]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum VoteOption {
Yes,
No,
Abstain,
NoWithVeto,
}
/// Shortcut helper as the construction of WasmMsg::Instantiate can be quite verbose in contract code.
///
/// When using this, `admin` is always unset. If you need more flexibility, create the message directly.
pub fn wasm_instantiate(
code_id: u64,
msg: &impl Serialize,
funds: Vec<Coin>,
label: String,
) -> StdResult<WasmMsg> {
let payload = to_binary(msg)?;
Ok(WasmMsg::Instantiate {
admin: None,
code_id,
msg: payload,
funds,
label,
})
}
/// Shortcut helper as the construction of WasmMsg::Instantiate can be quite verbose in contract code
pub fn wasm_execute(
contract_addr: impl Into<String>,
msg: &impl Serialize,
funds: Vec<Coin>,
) -> StdResult<WasmMsg> {
let payload = to_binary(msg)?;
Ok(WasmMsg::Execute {
contract_addr: contract_addr.into(),
msg: payload,
funds,
})
}
impl<T> From<BankMsg> for CosmosMsg<T>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
fn from(msg: BankMsg) -> Self {
CosmosMsg::Bank(msg)
}
}
#[cfg(feature = "staking")]
impl<T> From<StakingMsg> for CosmosMsg<T>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
fn from(msg: StakingMsg) -> Self {
CosmosMsg::Staking(msg)
}
}
#[cfg(feature = "staking")]
impl<T> From<DistributionMsg> for CosmosMsg<T>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
fn from(msg: DistributionMsg) -> Self {
CosmosMsg::Distribution(msg)
}
}
impl<T> From<WasmMsg> for CosmosMsg<T>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
fn from(msg: WasmMsg) -> Self {
CosmosMsg::Wasm(msg)
}
}
#[cfg(feature = "stargate")]
impl<T> From<IbcMsg> for CosmosMsg<T>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
fn from(msg: IbcMsg) -> Self {
CosmosMsg::Ibc(msg)
}
}
#[cfg(feature = "stargate")]
impl<T> From<GovMsg> for CosmosMsg<T>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
fn from(msg: GovMsg) -> Self {
CosmosMsg::Gov(msg)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::coins;
#[test]
fn from_bank_msg_works() {
let to_address = String::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"),
}
}
}