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
use std::fmt;
use crate::parse_contract_addr;
use cosmwasm_std::{
to_binary, Addr, Attribute, BankMsg, Binary, Coin, CosmosMsg, Event, SubMsgExecutionResponse,
WasmMsg,
};
use schemars::JsonSchema;
use serde::Serialize;
use anyhow::Result as AnyResult;
#[derive(Default, Clone, Debug)]
pub struct AppResponse {
pub events: Vec<Event>,
pub data: Option<Binary>,
}
impl AppResponse {
#[track_caller]
pub fn custom_attrs(&self, idx: usize) -> &[Attribute] {
assert_eq!(self.events[idx].ty.as_str(), "wasm");
&self.events[idx].attributes[1..]
}
pub fn has_event(&self, expected: &Event) -> bool {
self.events.iter().any(|ev| {
expected.ty == ev.ty
&& expected
.attributes
.iter()
.all(|at| ev.attributes.contains(at))
})
}
#[track_caller]
pub fn assert_event(&self, expected: &Event) {
assert!(
self.has_event(expected),
"Expected to find an event {:?}, but received: {:?}",
expected,
self.events
);
}
}
impl From<SubMsgExecutionResponse> for AppResponse {
fn from(reply: SubMsgExecutionResponse) -> Self {
AppResponse {
data: reply.data,
events: reply.events,
}
}
}
pub trait Executor<C>
where
C: Clone + fmt::Debug + PartialEq + JsonSchema + 'static,
{
fn execute(&mut self, sender: Addr, msg: CosmosMsg<C>) -> AnyResult<AppResponse>;
fn instantiate_contract<T: Serialize, U: Into<String>>(
&mut self,
code_id: u64,
sender: Addr,
init_msg: &T,
send_funds: &[Coin],
label: U,
admin: Option<String>,
) -> AnyResult<Addr> {
let init_msg = to_binary(init_msg)?;
let msg = WasmMsg::Instantiate {
admin,
code_id,
msg: init_msg,
funds: send_funds.to_vec(),
label: label.into(),
};
let res = self.execute(sender, msg.into())?;
parse_contract_addr(&res.data)
}
fn execute_contract<T: Serialize>(
&mut self,
sender: Addr,
contract_addr: Addr,
msg: &T,
send_funds: &[Coin],
) -> AnyResult<AppResponse> {
let msg = to_binary(msg)?;
let msg = WasmMsg::Execute {
contract_addr: contract_addr.into(),
msg,
funds: send_funds.to_vec(),
};
self.execute(sender, msg.into())
}
fn migrate_contract<T: Serialize>(
&mut self,
sender: Addr,
contract_addr: Addr,
msg: &T,
new_code_id: u64,
) -> AnyResult<AppResponse> {
let msg = to_binary(msg)?;
let msg = WasmMsg::Migrate {
contract_addr: contract_addr.into(),
msg,
new_code_id,
};
self.execute(sender, msg.into())
}
fn send_tokens(
&mut self,
sender: Addr,
recipient: Addr,
amount: &[Coin],
) -> AnyResult<AppResponse> {
let msg = BankMsg::Send {
to_address: recipient.to_string(),
amount: amount.to_vec(),
};
self.execute(sender, msg.into())
}
}