-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.rs
86 lines (75 loc) · 2.64 KB
/
test_helpers.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
#![cfg(test)]
use crate::contract::instantiate;
use crate::ibc::{ibc_channel_connect, ibc_channel_open, ICS20_ORDERING, ICS20_VERSION};
use crate::state::ChannelInfo;
use cosmwasm_std::testing::{
mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage,
};
use cosmwasm_std::{
DepsMut, IbcChannel, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcEndpoint, OwnedDeps,
};
use crate::msg::{AllowMsg, InitMsg};
pub const DEFAULT_TIMEOUT: u64 = 3600; // 1 hour,
pub const CONTRACT_PORT: &str = "ibc:wasm1234567890abcdef";
pub const REMOTE_PORT: &str = "transfer";
pub const CONNECTION_ID: &str = "connection-2";
pub fn mock_channel(channel_id: &str) -> IbcChannel {
IbcChannel::new(
IbcEndpoint {
port_id: CONTRACT_PORT.into(),
channel_id: channel_id.into(),
},
IbcEndpoint {
port_id: REMOTE_PORT.into(),
channel_id: format!("{}5", channel_id),
},
ICS20_ORDERING,
ICS20_VERSION,
CONNECTION_ID,
)
}
pub fn mock_channel_info(channel_id: &str) -> ChannelInfo {
ChannelInfo {
id: channel_id.to_string(),
counterparty_endpoint: IbcEndpoint {
port_id: REMOTE_PORT.into(),
channel_id: format!("{}5", channel_id),
},
connection_id: CONNECTION_ID.into(),
}
}
// we simulate instantiate and ack here
pub fn add_channel(mut deps: DepsMut, channel_id: &str) {
let channel = mock_channel(channel_id);
let open_msg = IbcChannelOpenMsg::new_init(channel.clone());
ibc_channel_open(deps.branch(), mock_env(), open_msg).unwrap();
let connect_msg = IbcChannelConnectMsg::new_ack(channel, ICS20_VERSION);
ibc_channel_connect(deps.branch(), mock_env(), connect_msg).unwrap();
}
pub fn setup(
channels: &[&str],
allow: &[(&str, u64)],
) -> OwnedDeps<MockStorage, MockApi, MockQuerier> {
let mut deps = mock_dependencies();
let allowlist = allow
.iter()
.map(|(contract, gas)| AllowMsg {
contract: contract.to_string(),
gas_limit: Some(*gas),
})
.collect();
// instantiate an empty contract
let instantiate_msg = InitMsg {
default_gas_limit: None,
default_timeout: DEFAULT_TIMEOUT,
gov_contract: "gov".to_string(),
allowlist,
};
let info = mock_info(&String::from("anyone"), &[]);
let res = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap();
assert_eq!(0, res.messages.len());
for channel in channels {
add_channel(deps.as_mut(), channel);
}
deps
}