use derivative::Derivative;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
use crate::binary::Binary;
use crate::coin::Coin;
use crate::errors::StdResult;
#[cfg(feature = "stargate")]
use crate::ibc::IbcMsg;
use crate::serde::to_binary;
#[cfg(all(feature = "stargate", feature = "cosmwasm_1_2"))]
use crate::Decimal;
use super::Empty;
pub trait CustomMsg: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema {}
impl CustomMsg for Empty {}
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum CosmosMsg<T = Empty> {
Bank(BankMsg),
Custom(T),
#[cfg(feature = "staking")]
Staking(StakingMsg),
#[cfg(feature = "staking")]
Distribution(DistributionMsg),
#[cfg(feature = "stargate")]
Stargate {
type_url: String,
value: Binary,
},
#[cfg(feature = "stargate")]
Ibc(IbcMsg),
Wasm(WasmMsg),
#[cfg(feature = "stargate")]
Gov(GovMsg),
}
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum BankMsg {
Send {
to_address: String,
amount: Vec<Coin>,
},
Burn { amount: Vec<Coin> },
}
#[cfg(feature = "staking")]
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum StakingMsg {
Delegate { validator: String, amount: Coin },
Undelegate { validator: String, amount: Coin },
Redelegate {
src_validator: String,
dst_validator: String,
amount: Coin,
},
}
#[cfg(feature = "staking")]
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum DistributionMsg {
SetWithdrawAddress {
address: String,
},
WithdrawDelegatorReward {
validator: String,
},
}
fn binary_to_string(data: &Binary, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match std::str::from_utf8(data.as_slice()) {
Ok(s) => fmt.write_str(s),
Err(_) => write!(fmt, "{:?}", data),
}
}
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Derivative, PartialEq, Eq, JsonSchema)]
#[derivative(Debug)]
#[serde(rename_all = "snake_case")]
pub enum WasmMsg {
Execute {
contract_addr: String,
#[derivative(Debug(format_with = "binary_to_string"))]
msg: Binary,
funds: Vec<Coin>,
},
Instantiate {
admin: Option<String>,
code_id: u64,
#[derivative(Debug(format_with = "binary_to_string"))]
msg: Binary,
funds: Vec<Coin>,
label: String,
},
#[cfg(feature = "cosmwasm_1_2")]
Instantiate2 {
admin: Option<String>,
code_id: u64,
label: String,
#[derivative(Debug(format_with = "binary_to_string"))]
msg: Binary,
funds: Vec<Coin>,
salt: Binary,
},
Migrate {
contract_addr: String,
new_code_id: u64,
#[derivative(Debug(format_with = "binary_to_string"))]
msg: Binary,
},
UpdateAdmin {
contract_addr: String,
admin: String,
},
ClearAdmin { contract_addr: String },
}
#[cfg(feature = "stargate")]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum GovMsg {
Vote {
proposal_id: u64,
vote: VoteOption,
},
#[cfg(feature = "cosmwasm_1_2")]
VoteWeighted {
proposal_id: u64,
options: Vec<WeightedVoteOption>,
},
}
#[cfg(feature = "stargate")]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum VoteOption {
Yes,
No,
Abstain,
NoWithVeto,
}
#[cfg(all(feature = "stargate", feature = "cosmwasm_1_2"))]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct WeightedVoteOption {
pub option: VoteOption,
pub weight: Decimal,
}
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,
})
}
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> {
fn from(msg: BankMsg) -> Self {
CosmosMsg::Bank(msg)
}
}
#[cfg(feature = "staking")]
impl<T> From<StakingMsg> for CosmosMsg<T> {
fn from(msg: StakingMsg) -> Self {
CosmosMsg::Staking(msg)
}
}
#[cfg(feature = "staking")]
impl<T> From<DistributionMsg> for CosmosMsg<T> {
fn from(msg: DistributionMsg) -> Self {
CosmosMsg::Distribution(msg)
}
}
impl<T> From<WasmMsg> for CosmosMsg<T> {
fn from(msg: WasmMsg) -> Self {
CosmosMsg::Wasm(msg)
}
}
#[cfg(feature = "stargate")]
impl<T> From<IbcMsg> for CosmosMsg<T> {
fn from(msg: IbcMsg) -> Self {
CosmosMsg::Ibc(msg)
}
}
#[cfg(feature = "stargate")]
impl<T> From<GovMsg> for CosmosMsg<T> {
fn from(msg: GovMsg) -> Self {
CosmosMsg::Gov(msg)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{coin, 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"),
}
}
#[test]
fn wasm_msg_serializes_to_correct_json() {
let msg = WasmMsg::Instantiate {
admin: Some("king".to_string()),
code_id: 7897,
msg: br#"{"claim":{}}"#.into(),
funds: vec![],
label: "my instance".to_string(),
};
let json = to_binary(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"instantiate":{"admin":"king","code_id":7897,"msg":"eyJjbGFpbSI6e319","funds":[],"label":"my instance"}}"#,
);
let msg = WasmMsg::Instantiate {
admin: None,
code_id: 7897,
msg: br#"{"claim":{}}"#.into(),
funds: vec![],
label: "my instance".to_string(),
};
let json = to_binary(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"instantiate":{"admin":null,"code_id":7897,"msg":"eyJjbGFpbSI6e319","funds":[],"label":"my instance"}}"#,
);
let msg = WasmMsg::Instantiate {
admin: None,
code_id: 7897,
msg: br#"{"claim":{}}"#.into(),
funds: vec![coin(321, "stones")],
label: "my instance".to_string(),
};
let json = to_binary(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"instantiate":{"admin":null,"code_id":7897,"msg":"eyJjbGFpbSI6e319","funds":[{"denom":"stones","amount":"321"}],"label":"my instance"}}"#,
);
#[cfg(feature = "cosmwasm_1_2")]
{
let msg = WasmMsg::Instantiate2 {
admin: None,
code_id: 7897,
label: "my instance".to_string(),
msg: br#"{"claim":{}}"#.into(),
funds: vec![coin(321, "stones")],
salt: Binary::from_base64("UkOVazhiwoo=").unwrap(),
};
let json = to_binary(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"instantiate2":{"admin":null,"code_id":7897,"label":"my instance","msg":"eyJjbGFpbSI6e319","funds":[{"denom":"stones","amount":"321"}],"salt":"UkOVazhiwoo="}}"#,
);
}
}
#[test]
fn wasm_msg_debug_decodes_binary_string_when_possible() {
#[cosmwasm_schema::cw_serde]
enum ExecuteMsg {
Mint { coin: Coin },
}
let msg = WasmMsg::Execute {
contract_addr: "joe".to_string(),
msg: to_binary(&ExecuteMsg::Mint {
coin: coin(10, "BTC"),
})
.unwrap(),
funds: vec![],
};
assert_eq!(
format!("{:?}", msg),
"Execute { contract_addr: \"joe\", msg: {\"mint\":{\"coin\":{\"denom\":\"BTC\",\"amount\":\"10\"}}}, funds: [] }"
);
}
#[test]
fn wasm_msg_debug_dumps_binary_when_not_utf8() {
let msg = WasmMsg::Execute {
contract_addr: "joe".to_string(),
msg: Binary::from([0, 159, 146, 150]),
funds: vec![],
};
assert_eq!(
format!("{:?}", msg),
"Execute { contract_addr: \"joe\", msg: Binary(009f9296), funds: [] }"
);
}
#[test]
#[cfg(feature = "stargate")]
fn gov_msg_serializes_to_correct_json() {
let msg = GovMsg::Vote {
proposal_id: 4,
vote: VoteOption::NoWithVeto,
};
let json = to_binary(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"vote":{"proposal_id":4,"vote":"no_with_veto"}}"#,
);
#[cfg(feature = "cosmwasm_1_2")]
{
let msg = GovMsg::VoteWeighted {
proposal_id: 25,
options: vec![
WeightedVoteOption {
weight: Decimal::percent(25),
option: VoteOption::Yes,
},
WeightedVoteOption {
weight: Decimal::percent(25),
option: VoteOption::No,
},
WeightedVoteOption {
weight: Decimal::percent(50),
option: VoteOption::Abstain,
},
],
};
let json = to_binary(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"vote_weighted":{"proposal_id":25,"options":[{"option":"yes","weight":"0.25"},{"option":"no","weight":"0.25"},{"option":"abstain","weight":"0.5"}]}}"#,
);
}
}
}