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
use std::fmt;
use crate::parse_contract_addr;
use cosmwasm_std::{to_binary, Addr, Attribute, BankMsg, Binary, Coin, CosmosMsg, Event, WasmMsg};
use schemars::JsonSchema;
use serde::Serialize;
#[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 trait Executor<C>
where
C: Clone + fmt::Debug + PartialEq + JsonSchema + 'static,
{
fn execute(&mut self, sender: Addr, msg: CosmosMsg<C>) -> Result<AppResponse, String>;
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>,
) -> Result<Addr, String> {
let init_msg = to_binary(init_msg).map_err(|e| e.to_string())?;
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],
) -> Result<AppResponse, String> {
let msg = to_binary(msg).map_err(|e| e.to_string())?;
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,
) -> Result<AppResponse, String> {
let msg = to_binary(msg).map_err(|e| e.to_string())?;
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],
) -> Result<AppResponse, String> {
let msg = BankMsg::Send {
to_address: recipient.to_string(),
amount: amount.to_vec(),
};
self.execute(sender, msg.into())
}
}