This repository was archived by the owner on May 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcontract.rs
More file actions
330 lines (293 loc) · 10.2 KB
/
Copy pathcontract.rs
File metadata and controls
330 lines (293 loc) · 10.2 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Binary, CosmosMsg, Deps, DepsMut, Empty, Env, MessageInfo, QueryRequest,
Response, StdResult, WasmMsg,
};
use cw2::set_contract_version;
use simple_ica::ReceiveIcaResponseMsg;
use crate::error::ContractError;
use crate::msg::{AdminResponse, ExecuteMsg, InstantiateMsg, QueryMsg, ResultResponse};
use crate::state::{Config, CONFIG, RESULTS};
// version info for migration info
const CONTRACT_NAME: &str = "crates.io:callback-capturer";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
let cfg = Config {
admin: info.sender,
simple_ica_controller: deps.api.addr_validate(&msg.simple_ica_controller)?,
};
CONFIG.save(deps.storage, &cfg)?;
Ok(Response::default())
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::SendMsgs {
channel_id,
msgs,
callback_id,
} => execute_send_msgs(deps, env, info, channel_id, msgs, callback_id),
ExecuteMsg::IbcQuery {
channel_id,
msgs,
callback_id,
} => execute_ibc_query(deps, env, info, channel_id, msgs, callback_id),
ExecuteMsg::CheckRemoteBalance { channel_id } => {
execute_check_remote_balance(deps, env, info, channel_id)
}
ExecuteMsg::SendFunds {
ica_channel_id,
transfer_channel_id,
} => execute_send_funds(deps, env, info, ica_channel_id, transfer_channel_id),
ExecuteMsg::ReceiveIcaResponse(resp) => execute_receive_ibc_response(deps, env, info, resp),
}
}
pub fn execute_send_msgs(
deps: DepsMut,
_env: Env,
info: MessageInfo,
channel_id: String,
msgs: Vec<CosmosMsg<Empty>>,
callback_id: String,
) -> Result<Response, ContractError> {
let cfg = CONFIG.load(deps.storage)?;
if !cfg.admin.eq(&info.sender) {
return Err(ContractError::Unauthorized {});
}
let ica_msg = simple_ica_controller::msg::ExecuteMsg::SendMsgs {
channel_id,
msgs,
callback_id: Some(callback_id),
};
let msg = WasmMsg::Execute {
contract_addr: cfg.simple_ica_controller.into(),
msg: to_json_binary(&ica_msg)?,
funds: vec![],
};
let res = Response::new().add_message(msg);
Ok(res)
}
pub fn execute_ibc_query(
deps: DepsMut,
_env: Env,
info: MessageInfo,
channel_id: String,
msgs: Vec<QueryRequest<Empty>>,
callback_id: String,
) -> Result<Response, ContractError> {
let cfg = CONFIG.load(deps.storage)?;
if !cfg.admin.eq(&info.sender) {
return Err(ContractError::Unauthorized {});
}
let ica_msg = simple_ica_controller::msg::ExecuteMsg::IbcQuery {
channel_id,
msgs,
callback_id: Some(callback_id),
};
let msg = WasmMsg::Execute {
contract_addr: cfg.simple_ica_controller.into(),
msg: to_json_binary(&ica_msg)?,
funds: vec![],
};
let res = Response::new().add_message(msg);
Ok(res)
}
pub fn execute_check_remote_balance(
deps: DepsMut,
_env: Env,
info: MessageInfo,
channel_id: String,
) -> Result<Response, ContractError> {
let cfg = CONFIG.load(deps.storage)?;
if !cfg.admin.eq(&info.sender) {
return Err(ContractError::Unauthorized {});
}
let ica_msg = simple_ica_controller::msg::ExecuteMsg::CheckRemoteBalance { channel_id };
let msg = WasmMsg::Execute {
contract_addr: cfg.simple_ica_controller.into(),
msg: to_json_binary(&ica_msg)?,
funds: vec![],
};
let res = Response::new().add_message(msg);
Ok(res)
}
pub fn execute_send_funds(
deps: DepsMut,
_env: Env,
info: MessageInfo,
ica_channel_id: String,
transfer_channel_id: String,
) -> Result<Response, ContractError> {
let cfg = CONFIG.load(deps.storage)?;
if !cfg.admin.eq(&info.sender) {
return Err(ContractError::Unauthorized {});
}
let ica_msg = simple_ica_controller::msg::ExecuteMsg::SendFunds {
ica_channel_id,
transfer_channel_id,
};
let msg = WasmMsg::Execute {
contract_addr: cfg.simple_ica_controller.into(),
msg: to_json_binary(&ica_msg)?,
funds: info.funds,
};
let res = Response::new().add_message(msg);
Ok(res)
}
pub fn execute_receive_ibc_response(
deps: DepsMut,
_env: Env,
info: MessageInfo,
resp: ReceiveIcaResponseMsg,
) -> Result<Response, ContractError> {
// only the simple ica controller can send this message as callback
let cfg = CONFIG.load(deps.storage)?;
if !cfg.simple_ica_controller.eq(&info.sender) {
return Err(ContractError::Unauthorized {});
}
RESULTS.save(deps.storage, &resp.id, &resp.msg)?;
let res = Response::new()
.add_attribute("action", "receive_callback")
.add_attribute("id", resp.id);
Ok(res)
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Admin {} => to_json_binary(&query_admin(deps)?),
QueryMsg::Result { id } => to_json_binary(&query_result(deps, id)?),
}
}
pub fn query_admin(deps: Deps) -> StdResult<AdminResponse> {
let cfg = CONFIG.load(deps.storage)?;
Ok(AdminResponse {
admin: cfg.admin.into(),
})
}
pub fn query_result(deps: Deps, id: String) -> StdResult<ResultResponse> {
let result = RESULTS.load(deps.storage, &id)?;
Ok(ResultResponse { result })
}
#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env};
use cosmwasm_std::{coins, BankMsg, BankQuery, SubMsg, WasmMsg};
use simple_ica::{IbcQueryResponse, StdAck};
#[test]
fn send_message_enforces_permissions() {
let mut deps = mock_dependencies();
let alice = deps.api.addr_make("alice");
let bob = deps.api.addr_make("bob");
let carl = deps.api.addr_make("carl");
let ica = deps.api.addr_make("simple_ica");
let channel = "channel-23";
// instantiate the contract
let instantiate_msg = InstantiateMsg {
simple_ica_controller: ica.to_string(),
};
let info = message_info(&alice, &[]);
instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap();
// try to send without permissions
let msgs = vec![BankMsg::Send {
to_address: carl.to_string(),
amount: coins(10000, "DAI"),
}
.into()];
let execute_msg = ExecuteMsg::SendMsgs {
channel_id: channel.to_string(),
msgs: msgs.clone(),
callback_id: "test".to_string(),
};
// bob cannot execute them
let info = message_info(&bob, &[]);
let err = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()).unwrap_err();
assert_eq!(err, ContractError::Unauthorized {});
// but alice can (original owner)
let info = message_info(&alice, &[]);
let res = execute(deps.as_mut(), mock_env(), info, execute_msg).unwrap();
let expected = vec![SubMsg::new(WasmMsg::Execute {
contract_addr: ica.to_string(),
msg: to_json_binary(&simple_ica_controller::msg::ExecuteMsg::SendMsgs {
channel_id: channel.to_string(),
msgs,
callback_id: Some("test".to_string()),
})
.unwrap(),
funds: vec![],
})];
assert_eq!(res.messages, expected);
}
#[test]
fn query_and_callback_work() {
let mut deps = mock_dependencies();
let alice = deps.api.addr_make("alice");
let bob = deps.api.addr_make("bob");
let ica = deps.api.addr_make("simple_ica");
let channel = "channel-23";
let callback = "my-balance";
// instantiate the contract
let instantiate_msg = InstantiateMsg {
simple_ica_controller: ica.to_string(),
};
let info = message_info(&alice, &[]);
instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap();
// try to send without permissions
let queries = vec![BankQuery::Balance {
address: bob.to_string(),
denom: "ujuno".to_string(),
}
.into()];
let execute_msg = ExecuteMsg::IbcQuery {
channel_id: channel.to_string(),
msgs: queries.clone(),
callback_id: callback.to_string(),
};
// alice can execute
let info = message_info(&alice, &[]);
let res = execute(deps.as_mut(), mock_env(), info, execute_msg).unwrap();
let expected = vec![SubMsg::new(WasmMsg::Execute {
contract_addr: ica.to_string(),
msg: to_json_binary(&simple_ica_controller::msg::ExecuteMsg::IbcQuery {
channel_id: channel.to_string(),
msgs: queries,
callback_id: Some(callback.to_string()),
})
.unwrap(),
funds: vec![],
})];
assert_eq!(res.messages, expected);
// we get a callback
let ack = StdAck::Result(
to_json_binary(&IbcQueryResponse {
results: vec![b"{}".into()],
})
.unwrap(),
);
let info = message_info(&ica, &[]);
let msg = ExecuteMsg::ReceiveIcaResponse(ReceiveIcaResponseMsg {
id: callback.to_string(),
msg: ack.clone(),
});
execute(deps.as_mut(), mock_env(), info, msg).unwrap();
// now make sure we can query this
let data = query_result(deps.as_ref(), callback.to_string()).unwrap();
assert_eq!(data.result, ack);
// and show how to parse those results
let result: IbcQueryResponse = data.result.unwrap_into();
assert_eq!(result.results, vec![Binary::from(b"{}")]);
}
}