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
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
use crate::{Binary, ContractResult};
use super::{Attribute, CosmosMsg, Empty};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ReplyOn {
Always,
Error,
Success,
Never,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct SubMsg<T = Empty>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
pub id: u64,
pub msg: CosmosMsg<T>,
pub gas_limit: Option<u64>,
pub reply_on: ReplyOn,
}
pub const UNUSED_MSG_ID: u64 = 0;
impl<T> SubMsg<T>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
pub fn new<M: Into<CosmosMsg<T>>>(msg: M) -> Self {
SubMsg {
id: UNUSED_MSG_ID,
msg: msg.into(),
reply_on: ReplyOn::Never,
gas_limit: None,
}
}
pub fn reply_on_success<M: Into<CosmosMsg<T>>>(msg: M, id: u64) -> Self {
Self::reply_on(msg.into(), id, ReplyOn::Success)
}
pub fn reply_on_error<M: Into<CosmosMsg<T>>>(msg: M, id: u64) -> Self {
Self::reply_on(msg.into(), id, ReplyOn::Error)
}
pub fn reply_always<M: Into<CosmosMsg<T>>>(msg: M, id: u64) -> Self {
Self::reply_on(msg.into(), id, ReplyOn::Always)
}
pub fn with_gas_limit(mut self, limit: u64) -> Self {
self.gas_limit = Some(limit);
self
}
fn reply_on(msg: CosmosMsg<T>, id: u64, reply_on: ReplyOn) -> Self {
SubMsg {
id,
msg,
reply_on,
gas_limit: None,
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Reply {
pub id: u64,
pub result: ContractResult<SubMsgExecutionResponse>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct SubMsgExecutionResponse {
pub events: Vec<Event>,
pub data: Option<Binary>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Event {
#[serde(rename = "type")]
pub ty: String,
pub attributes: Vec<Attribute>,
}
impl Event {
pub fn new(ty: impl Into<String>) -> Self {
Event {
ty: ty.into(),
attributes: Vec::with_capacity(10),
}
}
pub fn attr(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.attributes.push(Attribute {
key: key.into(),
value: value.into(),
});
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::attr;
#[test]
fn event_construction() {
let event_direct = Event {
ty: "test".to_string(),
attributes: vec![attr("foo", "bar"), attr("bar", "baz")],
};
let event_builder = Event::new("test").attr("foo", "bar").attr("bar", "baz");
assert_eq!(event_direct, event_builder);
}
}