-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContract.rs
608 lines (542 loc) · 21.3 KB
/
Contract.rs
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_binary, to_binary, Addr, Binary, Deps, DepsMut, Env, IbcMsg, IbcQuery, MessageInfo, Order,
PortIdResponse, Response, StdError, StdResult,
};
use semver::Version;
use cw2::{get_contract_version, set_contract_version};
use cw20::{Cw20Coin, Cw20ReceiveMsg};
use cw_storage_plus::Bound;
use crate::amount::Amount;
use crate::error::ContractError;
use crate::ibc::Ics20Packet;
use crate::migrations::{v1, v2};
use crate::msg::{
AllowMsg, AllowedInfo, AllowedResponse, ChannelResponse, ConfigResponse, ExecuteMsg, InitMsg,
ListAllowedResponse, ListChannelsResponse, MigrateMsg, PortResponse, QueryMsg, TransferMsg,
};
use crate::state::{
increase_channel_balance, AllowInfo, Config, ADMIN, ALLOW_LIST, CHANNEL_INFO, CHANNEL_STATE,
CONFIG,
};
use cw_utils::{maybe_addr, nonpayable, one_coin};
// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw20-ics20";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
mut deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: InitMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
let cfg = Config {
default_timeout: msg.default_timeout,
default_gas_limit: msg.default_gas_limit,
};
CONFIG.save(deps.storage, &cfg)?;
let admin = deps.api.addr_validate(&msg.gov_contract)?;
ADMIN.set(deps.branch(), Some(admin))?;
// add all allows
for allowed in msg.allowlist {
let contract = deps.api.addr_validate(&allowed.contract)?;
let info = AllowInfo {
gas_limit: allowed.gas_limit,
};
ALLOW_LIST.save(deps.storage, &contract, &info)?;
}
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::Receive(msg) => execute_receive(deps, env, info, msg),
ExecuteMsg::Transfer(msg) => {
let coin = one_coin(&info)?;
execute_transfer(deps, env, msg, Amount::Native(coin), info.sender)
}
ExecuteMsg::Allow(allow) => execute_allow(deps, env, info, allow),
ExecuteMsg::UpdateAdmin { admin } => {
let admin = deps.api.addr_validate(&admin)?;
Ok(ADMIN.execute_update_admin(deps, info, Some(admin))?)
}
}
}
pub fn execute_receive(
deps: DepsMut,
env: Env,
info: MessageInfo,
wrapper: Cw20ReceiveMsg,
) -> Result<Response, ContractError> {
nonpayable(&info)?;
let msg: TransferMsg = from_binary(&wrapper.msg)?;
let amount = Amount::Cw20(Cw20Coin {
address: info.sender.to_string(),
amount: wrapper.amount,
});
let api = deps.api;
execute_transfer(deps, env, msg, amount, api.addr_validate(&wrapper.sender)?)
}
pub fn execute_transfer(
deps: DepsMut,
env: Env,
msg: TransferMsg,
amount: Amount,
sender: Addr,
) -> Result<Response, ContractError> {
if amount.is_empty() {
return Err(ContractError::NoFunds {});
}
// ensure the requested channel is registered
if !CHANNEL_INFO.has(deps.storage, &msg.channel) {
return Err(ContractError::NoSuchChannel { id: msg.channel });
}
let config = CONFIG.load(deps.storage)?;
// if cw20 token, validate and ensure it is whitelisted, or we set default gas limit
if let Amount::Cw20(coin) = &amount {
let addr = deps.api.addr_validate(&coin.address)?;
// if limit is set, then we always allow cw20
if config.default_gas_limit.is_none() {
ALLOW_LIST
.may_load(deps.storage, &addr)?
.ok_or(ContractError::NotOnAllowList)?;
}
};
// delta from user is in seconds
let timeout_delta = match msg.timeout {
Some(t) => t,
None => config.default_timeout,
};
// timeout is in nanoseconds
let timeout = env.block.time.plus_seconds(timeout_delta);
// build ics20 packet
let packet = Ics20Packet::new(
amount.amount(),
amount.denom(),
sender.as_ref(),
&msg.remote_address,
);
packet.validate()?;
// Update the balance now (optimistically) like ibctransfer modules.
// In on_packet_failure (ack with error message or a timeout), we reduce the balance appropriately.
// This means the channel works fine if success acks are not relayed.
increase_channel_balance(deps.storage, &msg.channel, &amount.denom(), amount.amount())?;
// prepare ibc message
let msg = IbcMsg::SendPacket {
channel_id: msg.channel,
data: to_binary(&packet)?,
timeout: timeout.into(),
};
// send response
let res = Response::new()
.add_message(msg)
.add_attribute("action", "transfer")
.add_attribute("sender", &packet.sender)
.add_attribute("receiver", &packet.receiver)
.add_attribute("denom", &packet.denom)
.add_attribute("amount", &packet.amount.to_string());
Ok(res)
}
/// The gov contract can allow new contracts, or increase the gas limit on existing contracts.
/// It cannot block or reduce the limit to avoid forcible sticking tokens in the channel.
pub fn execute_allow(
deps: DepsMut,
_env: Env,
info: MessageInfo,
allow: AllowMsg,
) -> Result<Response, ContractError> {
ADMIN.assert_admin(deps.as_ref(), &info.sender)?;
let contract = deps.api.addr_validate(&allow.contract)?;
let set = AllowInfo {
gas_limit: allow.gas_limit,
};
ALLOW_LIST.update(deps.storage, &contract, |old| {
if let Some(old) = old {
// we must ensure it increases the limit
match (old.gas_limit, set.gas_limit) {
(None, Some(_)) => return Err(ContractError::CannotLowerGas),
(Some(old), Some(new)) if new < old => return Err(ContractError::CannotLowerGas),
_ => {}
};
}
Ok(AllowInfo {
gas_limit: allow.gas_limit,
})
})?;
let gas = if let Some(gas) = allow.gas_limit {
gas.to_string()
} else {
"None".to_string()
};
let res = Response::new()
.add_attribute("action", "allow")
.add_attribute("contract", allow.contract)
.add_attribute("gas_limit", gas);
Ok(res)
}
const MIGRATE_MIN_VERSION: &str = "0.11.1";
const MIGRATE_VERSION_2: &str = "0.12.0-alpha1";
const MIGRATE_VERSION_3: &str = "0.13.2";
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(mut deps: DepsMut, env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
let version: Version = CONTRACT_VERSION.parse().map_err(from_semver)?;
let stored = get_contract_version(deps.storage)?;
let storage_version: Version = stored.version.parse().map_err(from_semver)?;
// First, ensure we are working from an equal or older version of this contract
// wrong type
if CONTRACT_NAME != stored.contract {
return Err(ContractError::CannotMigrate {
previous_contract: stored.contract,
});
}
// existing one is newer
if storage_version > version {
return Err(ContractError::CannotMigrateVersion {
previous_version: stored.version,
});
}
// Then, run the proper migration
if storage_version < MIGRATE_MIN_VERSION.parse().map_err(from_semver)? {
return Err(ContractError::CannotMigrateVersion {
previous_version: stored.version,
});
}
// run the v1->v2 converstion if we are v1 style
if storage_version <= MIGRATE_VERSION_2.parse().map_err(from_semver)? {
let old_config = v1::CONFIG.load(deps.storage)?;
ADMIN.set(deps.branch(), Some(old_config.gov_contract))?;
let config = Config {
default_timeout: old_config.default_timeout,
default_gas_limit: None,
};
CONFIG.save(deps.storage, &config)?;
}
// run the v2->v3 converstion if we are v2 style
if storage_version <= MIGRATE_VERSION_3.parse().map_err(from_semver)? {
v2::update_balances(deps.branch(), &env)?;
}
// otherwise no migration (yet) - add them here
// always allow setting the default gas limit via MigrateMsg, even if same version
// (Note this doesn't allow unsetting it now)
if msg.default_gas_limit.is_some() {
CONFIG.update(deps.storage, |mut old| -> StdResult<_> {
old.default_gas_limit = msg.default_gas_limit;
Ok(old)
})?;
}
// we don't need to save anything if migrating from the same version
if storage_version < version {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
}
Ok(Response::new())
}
fn from_semver(err: semver::Error) -> StdError {
StdError::generic_err(format!("Semver: {}", err))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Port {} => to_binary(&query_port(deps)?),
QueryMsg::ListChannels {} => to_binary(&query_list(deps)?),
QueryMsg::Channel { id } => to_binary(&query_channel(deps, id)?),
QueryMsg::Config {} => to_binary(&query_config(deps)?),
QueryMsg::Allowed { contract } => to_binary(&query_allowed(deps, contract)?),
QueryMsg::ListAllowed { start_after, limit } => {
to_binary(&list_allowed(deps, start_after, limit)?)
}
QueryMsg::Admin {} => to_binary(&ADMIN.query_admin(deps)?),
}
}
fn query_port(deps: Deps) -> StdResult<PortResponse> {
let query = IbcQuery::PortId {}.into();
let PortIdResponse { port_id } = deps.querier.query(&query)?;
Ok(PortResponse { port_id })
}
fn query_list(deps: Deps) -> StdResult<ListChannelsResponse> {
let channels = CHANNEL_INFO
.range_raw(deps.storage, None, None, Order::Ascending)
.map(|r| r.map(|(_, v)| v))
.collect::<StdResult<_>>()?;
Ok(ListChannelsResponse { channels })
}
// make public for ibc tests
pub fn query_channel(deps: Deps, id: String) -> StdResult<ChannelResponse> {
let info = CHANNEL_INFO.load(deps.storage, &id)?;
// this returns Vec<(outstanding, total)>
let state = CHANNEL_STATE
.prefix(&id)
.range(deps.storage, None, None, Order::Ascending)
.map(|r| {
r.map(|(denom, v)| {
let outstanding = Amount::from_parts(denom.clone(), v.outstanding);
let total = Amount::from_parts(denom, v.total_sent);
(outstanding, total)
})
})
.collect::<StdResult<Vec<_>>>()?;
// we want (Vec<outstanding>, Vec<total>)
let (balances, total_sent) = state.into_iter().unzip();
Ok(ChannelResponse {
info,
balances,
total_sent,
})
}
fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
let cfg = CONFIG.load(deps.storage)?;
let admin = ADMIN.get(deps)?.unwrap_or_else(|| Addr::unchecked(""));
let res = ConfigResponse {
default_timeout: cfg.default_timeout,
default_gas_limit: cfg.default_gas_limit,
gov_contract: admin.into(),
};
Ok(res)
}
fn query_allowed(deps: Deps, contract: String) -> StdResult<AllowedResponse> {
let addr = deps.api.addr_validate(&contract)?;
let info = ALLOW_LIST.may_load(deps.storage, &addr)?;
let res = match info {
None => AllowedResponse {
is_allowed: false,
gas_limit: None,
},
Some(a) => AllowedResponse {
is_allowed: true,
gas_limit: a.gas_limit,
},
};
Ok(res)
}
// settings for pagination
const MAX_LIMIT: u32 = 30;
const DEFAULT_LIMIT: u32 = 10;
fn list_allowed(
deps: Deps,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<ListAllowedResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let addr = maybe_addr(deps.api, start_after)?;
let start = addr.as_ref().map(Bound::exclusive);
let allow = ALLOW_LIST
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
item.map(|(addr, allow)| AllowedInfo {
contract: addr.into(),
gas_limit: allow.gas_limit,
})
})
.collect::<StdResult<_>>()?;
Ok(ListAllowedResponse { allow })
}
#[cfg(test)]
mod test {
use super::*;
use crate::test_helpers::*;
use cosmwasm_std::testing::{mock_env, mock_info, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{coin, coins, CosmosMsg, IbcMsg, StdError, Uint128};
use crate::state::ChannelState;
use cw_utils::PaymentError;
#[test]
fn setup_and_query() {
let deps = setup(&["channel-3", "channel-7"], &[]);
let raw_list = query(deps.as_ref(), mock_env(), QueryMsg::ListChannels {}).unwrap();
let list_res: ListChannelsResponse = from_binary(&raw_list).unwrap();
assert_eq!(2, list_res.channels.len());
assert_eq!(mock_channel_info("channel-3"), list_res.channels[0]);
assert_eq!(mock_channel_info("channel-7"), list_res.channels[1]);
let raw_channel = query(
deps.as_ref(),
mock_env(),
QueryMsg::Channel {
id: "channel-3".to_string(),
},
)
.unwrap();
let chan_res: ChannelResponse = from_binary(&raw_channel).unwrap();
assert_eq!(chan_res.info, mock_channel_info("channel-3"));
assert_eq!(0, chan_res.total_sent.len());
assert_eq!(0, chan_res.balances.len());
let err = query(
deps.as_ref(),
mock_env(),
QueryMsg::Channel {
id: "channel-10".to_string(),
},
)
.unwrap_err();
assert_eq!(err, StdError::not_found("cw20_ics20::state::ChannelInfo"));
}
#[test]
fn proper_checks_on_execute_native() {
let send_channel = "channel-5";
let mut deps = setup(&[send_channel, "channel-10"], &[]);
let mut transfer = TransferMsg {
channel: send_channel.to_string(),
remote_address: "foreign-address".to_string(),
timeout: None,
};
// works with proper funds
let msg = ExecuteMsg::Transfer(transfer.clone());
let info = mock_info("foobar", &coins(1234567, "ucosm"));
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(res.messages[0].gas_limit, None);
assert_eq!(1, res.messages.len());
if let CosmosMsg::Ibc(IbcMsg::SendPacket {
channel_id,
data,
timeout,
}) = &res.messages[0].msg
{
let expected_timeout = mock_env().block.time.plus_seconds(DEFAULT_TIMEOUT);
assert_eq!(timeout, &expected_timeout.into());
assert_eq!(channel_id.as_str(), send_channel);
let msg: Ics20Packet = from_binary(data).unwrap();
assert_eq!(msg.amount, Uint128::new(1234567));
assert_eq!(msg.denom.as_str(), "ucosm");
assert_eq!(msg.sender.as_str(), "foobar");
assert_eq!(msg.receiver.as_str(), "foreign-address");
} else {
panic!("Unexpected return message: {:?}", res.messages[0]);
}
// reject with no funds
let msg = ExecuteMsg::Transfer(transfer.clone());
let info = mock_info("foobar", &[]);
let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
assert_eq!(err, ContractError::Payment(PaymentError::NoFunds {}));
// reject with multiple tokens funds
let msg = ExecuteMsg::Transfer(transfer.clone());
let info = mock_info("foobar", &[coin(1234567, "ucosm"), coin(54321, "uatom")]);
let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
assert_eq!(err, ContractError::Payment(PaymentError::MultipleDenoms {}));
// reject with bad channel id
transfer.channel = "channel-45".to_string();
let msg = ExecuteMsg::Transfer(transfer);
let info = mock_info("foobar", &coins(1234567, "ucosm"));
let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
assert_eq!(
err,
ContractError::NoSuchChannel {
id: "channel-45".to_string()
}
);
}
#[test]
fn proper_checks_on_execute_cw20() {
let send_channel = "channel-15";
let cw20_addr = "my-token";
let mut deps = setup(&["channel-3", send_channel], &[(cw20_addr, 123456)]);
let transfer = TransferMsg {
channel: send_channel.to_string(),
remote_address: "foreign-address".to_string(),
timeout: Some(7777),
};
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: "my-account".into(),
amount: Uint128::new(888777666),
msg: to_binary(&transfer).unwrap(),
});
// works with proper funds
let info = mock_info(cw20_addr, &[]);
let res = execute(deps.as_mut(), mock_env(), info, msg.clone()).unwrap();
assert_eq!(1, res.messages.len());
assert_eq!(res.messages[0].gas_limit, None);
if let CosmosMsg::Ibc(IbcMsg::SendPacket {
channel_id,
data,
timeout,
}) = &res.messages[0].msg
{
let expected_timeout = mock_env().block.time.plus_seconds(7777);
assert_eq!(timeout, &expected_timeout.into());
assert_eq!(channel_id.as_str(), send_channel);
let msg: Ics20Packet = from_binary(data).unwrap();
assert_eq!(msg.amount, Uint128::new(888777666));
assert_eq!(msg.denom, format!("cw20:{}", cw20_addr));
assert_eq!(msg.sender.as_str(), "my-account");
assert_eq!(msg.receiver.as_str(), "foreign-address");
} else {
panic!("Unexpected return message: {:?}", res.messages[0]);
}
// reject with tokens funds
let info = mock_info("foobar", &coins(1234567, "ucosm"));
let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
assert_eq!(err, ContractError::Payment(PaymentError::NonPayable {}));
}
#[test]
fn execute_cw20_fails_if_not_whitelisted_unless_default_gas_limit() {
let send_channel = "channel-15";
let mut deps = setup(&[send_channel], &[]);
let cw20_addr = "my-token";
let transfer = TransferMsg {
channel: send_channel.to_string(),
remote_address: "foreign-address".to_string(),
timeout: Some(7777),
};
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: "my-account".into(),
amount: Uint128::new(888777666),
msg: to_binary(&transfer).unwrap(),
});
// rejected as not on allow list
let info = mock_info(cw20_addr, &[]);
let err = execute(deps.as_mut(), mock_env(), info.clone(), msg.clone()).unwrap_err();
assert_eq!(err, ContractError::NotOnAllowList);
// add a default gas limit
migrate(
deps.as_mut(),
mock_env(),
MigrateMsg {
default_gas_limit: Some(123456),
},
)
.unwrap();
// try again
execute(deps.as_mut(), mock_env(), info, msg).unwrap();
}
#[test]
fn v3_migration_works() {
// basic state with one channel
let send_channel = "channel-15";
let cw20_addr = "my-token";
let native = "ucosm";
let mut deps = setup(&[send_channel], &[(cw20_addr, 123456)]);
// mock that we sent some tokens in both native and cw20 (TODO: cw20)
// balances set high
deps.querier
.update_balance(MOCK_CONTRACT_ADDR, coins(50000, native));
// channel state a bit lower (some in-flight acks)
let state = ChannelState {
// 14000 not accounted for (in-flight)
outstanding: Uint128::new(36000),
total_sent: Uint128::new(100000),
};
CHANNEL_STATE
.save(deps.as_mut().storage, (send_channel, native), &state)
.unwrap();
// run migration
migrate(
deps.as_mut(),
mock_env(),
MigrateMsg {
default_gas_limit: Some(123456),
},
)
.unwrap();
// check new channel state
let chan = query_channel(deps.as_ref(), send_channel.into()).unwrap();
assert_eq!(chan.balances, vec![Amount::native(50000, native)]);
assert_eq!(chan.total_sent, vec![Amount::native(114000, native)]);
// check config updates
let config = query_config(deps.as_ref()).unwrap();
assert_eq!(config.default_gas_limit, Some(123456));
}
}