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
269 lines (240 loc) · 7.95 KB
/
Copy pathcontract.rs
File metadata and controls
269 lines (240 loc) · 7.95 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
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, CosmosMsg, Deps, DepsMut, Empty, Env, IbcMsg, MessageInfo, Order, QueryRequest,
QueryResponse, Response, StdError, StdResult,
};
use simple_ica::PacketMsg;
use crate::ibc::PACKET_LIFETIME;
use crate::msg::{
AccountInfo, AccountResponse, AdminResponse, ExecuteMsg, InstantiateMsg, LatestQueryResponse,
ListAccountsResponse, QueryMsg,
};
use crate::state::{Config, ACCOUNTS, CONFIG, LATEST_QUERIES};
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
_env: Env,
info: MessageInfo,
_msg: InstantiateMsg,
) -> StdResult<Response> {
let cfg = Config { admin: info.sender };
CONFIG.save(deps.storage, &cfg)?;
Ok(Response::new().add_attribute("action", "instantiate"))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> StdResult<Response> {
match msg {
ExecuteMsg::UpdateAdmin { admin } => execute_update_admin(deps, info, admin),
ExecuteMsg::SendMsgs {
channel_id,
msgs,
callback_id,
} => execute_send_msgs(deps, env, info, channel_id, msgs, callback_id),
ExecuteMsg::CheckRemoteBalance { channel_id } => {
execute_check_remote_balance(deps, env, info, channel_id)
}
ExecuteMsg::IbcQuery {
channel_id,
msgs,
callback_id,
} => execute_ibc_query(deps, env, info, channel_id, msgs, callback_id),
ExecuteMsg::SendFunds {
ica_channel_id,
transfer_channel_id,
} => execute_send_funds(deps, env, info, ica_channel_id, transfer_channel_id),
}
}
pub fn execute_update_admin(
deps: DepsMut,
info: MessageInfo,
new_admin: String,
) -> StdResult<Response> {
// auth check
let mut cfg = CONFIG.load(deps.storage)?;
if info.sender != cfg.admin {
return Err(StdError::generic_err("Only admin may set new admin"));
}
cfg.admin = deps.api.addr_validate(&new_admin)?;
CONFIG.save(deps.storage, &cfg)?;
Ok(Response::new()
.add_attribute("action", "handle_update_admin")
.add_attribute("new_admin", cfg.admin))
}
pub fn execute_send_msgs(
deps: DepsMut,
env: Env,
info: MessageInfo,
channel_id: String,
msgs: Vec<CosmosMsg>,
callback_id: Option<String>,
) -> StdResult<Response> {
// auth check
let cfg = CONFIG.load(deps.storage)?;
if info.sender != cfg.admin {
return Err(StdError::generic_err("Only admin may send messages"));
}
// ensure the channel exists (not found if not registered)
ACCOUNTS.load(deps.storage, &channel_id)?;
// construct a packet to send
let sender = info.sender.into();
let packet = PacketMsg::Dispatch {
sender,
msgs,
callback_id,
};
let msg = IbcMsg::SendPacket {
channel_id,
data: to_json_binary(&packet)?,
timeout: env.block.time.plus_seconds(PACKET_LIFETIME).into(),
};
let res = Response::new()
.add_message(msg)
.add_attribute("action", "handle_send_msgs");
Ok(res)
}
pub fn execute_ibc_query(
_deps: DepsMut,
env: Env,
info: MessageInfo,
channel_id: String,
msgs: Vec<QueryRequest<Empty>>,
callback_id: Option<String>,
) -> StdResult<Response> {
// construct a packet to send
let sender = info.sender.into();
let packet = PacketMsg::IbcQuery {
sender,
msgs,
callback_id,
};
let msg = IbcMsg::SendPacket {
channel_id,
data: to_json_binary(&packet)?,
timeout: env.block.time.plus_seconds(PACKET_LIFETIME).into(),
};
let res = Response::new()
.add_message(msg)
.add_attribute("action", "handle_check_remote_balance");
Ok(res)
}
pub fn execute_check_remote_balance(
deps: DepsMut,
env: Env,
info: MessageInfo,
channel_id: String,
) -> StdResult<Response> {
// auth check
let cfg = CONFIG.load(deps.storage)?;
if info.sender != cfg.admin {
return Err(StdError::generic_err("Only admin may send messages"));
}
// ensure the channel exists (not found if not registered)
ACCOUNTS.load(deps.storage, &channel_id)?;
// construct a packet to send
let packet = PacketMsg::Balances {};
let msg = IbcMsg::SendPacket {
channel_id,
data: to_json_binary(&packet)?,
timeout: env.block.time.plus_seconds(PACKET_LIFETIME).into(),
};
let res = Response::new()
.add_message(msg)
.add_attribute("action", "handle_check_remote_balance");
Ok(res)
}
pub fn execute_send_funds(
deps: DepsMut,
env: Env,
mut info: MessageInfo,
ica_channel_id: String,
transfer_channel_id: String,
) -> StdResult<Response> {
// intentionally no auth check
// require some funds
let amount = match info.funds.pop() {
Some(coin) => coin,
None => {
return Err(StdError::generic_err(
"you must send the coins you wish to ibc transfer",
))
}
};
// if there are any more coins, reject the message
if !info.funds.is_empty() {
return Err(StdError::generic_err("you can only ibc transfer one coin"));
}
// load remote account
let data = ACCOUNTS.load(deps.storage, &ica_channel_id)?;
let remote_addr = match data.remote_addr {
Some(addr) => addr,
None => {
return Err(StdError::generic_err(
"We don't have the remote address for this channel",
))
}
};
// construct a packet to send
let msg = IbcMsg::Transfer {
channel_id: transfer_channel_id,
to_address: remote_addr,
amount,
timeout: env.block.time.plus_seconds(PACKET_LIFETIME).into(),
memo: None,
};
let res = Response::new()
.add_message(msg)
.add_attribute("action", "handle_send_funds");
Ok(res)
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
match msg {
QueryMsg::Admin {} => to_json_binary(&query_admin(deps)?),
QueryMsg::Account { channel_id } => to_json_binary(&query_account(deps, channel_id)?),
QueryMsg::ListAccounts {} => to_json_binary(&query_list_accounts(deps)?),
QueryMsg::LatestQueryResult { channel_id } => {
to_json_binary(&query_latest_ibc_query_result(deps, channel_id)?)
}
}
}
fn query_account(deps: Deps, channel_id: String) -> StdResult<AccountResponse> {
let account = ACCOUNTS.load(deps.storage, &channel_id)?;
Ok(account.into())
}
fn query_latest_ibc_query_result(deps: Deps, channel_id: String) -> StdResult<LatestQueryResponse> {
LATEST_QUERIES.load(deps.storage, &channel_id)
}
fn query_list_accounts(deps: Deps) -> StdResult<ListAccountsResponse> {
let accounts = ACCOUNTS
.range(deps.storage, None, None, Order::Ascending)
.map(|r| {
let (channel_id, account) = r?;
Ok(AccountInfo::convert(channel_id, account))
})
.collect::<StdResult<_>>()?;
Ok(ListAccountsResponse { accounts })
}
fn query_admin(deps: Deps) -> StdResult<AdminResponse> {
let Config { admin } = CONFIG.load(deps.storage)?;
Ok(AdminResponse {
admin: admin.into(),
})
}
#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env};
const CREATOR: &str = "creator";
#[test]
fn instantiate_works() {
let mut deps = mock_dependencies();
let creator = deps.api.addr_make(CREATOR);
let msg = InstantiateMsg {};
let info = message_info(&creator, &[]);
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(0, res.messages.len());
let admin = query_admin(deps.as_ref()).unwrap();
assert_eq!(creator.to_string(), admin.admin);
}
}