-
Notifications
You must be signed in to change notification settings - Fork 341
/
cosmos_msg.rs
254 lines (238 loc) · 8.8 KB
/
cosmos_msg.rs
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
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 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: 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,
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.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,
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: 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,
},
}
/// 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<T>(
code_id: u64,
msg: &T,
send: Vec<Coin>,
label: String,
) -> StdResult<WasmMsg>
where
T: Serialize,
{
let payload = to_binary(msg)?;
Ok(WasmMsg::Instantiate {
admin: None,
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<String>,
U: Serialize,
{
let payload = to_binary(msg)?;
Ok(WasmMsg::Execute {
contract_addr: contract_addr.into(),
msg: payload,
send,
})
}
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(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"),
}
}
}