-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.rs
206 lines (188 loc) · 5.62 KB
/
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
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
use std::env;
use std::fmt::Debug;
use std::{collections::HashMap, str::FromStr};
use cosmwasm_std::{Coin, StdError, StdResult, Uint128};
use osmosis_std::types::cosmos::bank::v1beta1::{
MsgSend, MsgSendResponse, QueryAllBalancesRequest, QueryAllBalancesResponse,
QueryBalanceRequest,
};
use osmosis_std::types::cosmos::base::query::v1beta1::PageRequest;
use osmosis_std::types::cosmos::base::v1beta1::Coin as ProtoCoin;
use serde::Serialize;
use test_tube::{Account, Module, Runner, RunnerExecuteResult, RunnerResult, SigningAccount};
use test_tube::{Bank, Wasm};
use crate::error::CwItError;
use crate::traits::CwItRunner;
use crate::{ArtifactError, ContractType};
#[cfg(feature = "tokio")]
use std::future::Future;
#[cfg(feature = "tokio")]
pub fn block_on<F: Future>(f: F) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(f)
}
pub fn upload_wasm_files<'a, R: CwItRunner<'a>>(
runner: &'a R,
signer: &SigningAccount,
contracts: HashMap<String, ContractType>,
) -> Result<HashMap<String, u64>, CwItError> {
contracts
.into_iter()
.map(|(name, contract)| {
let code_id = upload_wasm_file(runner, signer, contract)?;
Ok((name, code_id))
})
.collect()
}
/// Instantiates the liquidity helper contract
pub fn instantiate_contract_with_funds<'a, R, M, S>(
app: &'a R,
admin: &SigningAccount,
code_id: u64,
instantite_msg: &M,
funds: &[Coin],
) -> RunnerResult<S>
where
R: Runner<'a>,
M: Serialize,
S: From<String>,
{
let wasm = Wasm::new(app);
// Instantiate the contract
println!("Instantiating contract with code id {}", code_id);
wasm.instantiate(
code_id,
instantite_msg,
Some(&admin.address()), // contract admin used for migration
None,
funds,
admin, // signer
)
.map(|r| r.data.address.into())
}
pub fn instantiate_contract<'a, R, M, S>(
app: &'a R,
admin: &SigningAccount,
code_id: u64,
instantite_msg: &M,
) -> RunnerResult<S>
where
R: Runner<'a>,
M: Serialize,
S: From<String>,
{
instantiate_contract_with_funds(app, admin, code_id, instantite_msg, &[])
}
/// Uploads a wasm file to the chain and returns the code_id
pub fn upload_wasm_file<'a, R: CwItRunner<'a>>(
runner: &'a R,
signer: &SigningAccount,
contract: ContractType,
) -> Result<u64, CwItError> {
let error_msg = format!("Failed to upload wasm file: {:?}", contract);
runner.store_code(contract, signer).map_err(|e| {
CwItError::ArtifactError(ArtifactError::Generic(format!(
"{:?}. Error: {:?}",
error_msg, e
)))
})
}
pub fn bank_balance_query<'a>(
runner: &'a impl Runner<'a>,
address: String,
denom: String,
) -> StdResult<Uint128> {
Bank::new(runner)
.query_balance(&QueryBalanceRequest { address, denom })
.unwrap()
.balance
.map(|c| Uint128::from_str(&c.amount).unwrap())
.ok_or_else(|| StdError::generic_err("Bank balance query failed"))
}
pub fn bank_all_balances_query<'a>(
runner: &'a impl Runner<'a>,
address: String,
pagination: Option<PageRequest>,
) -> StdResult<QueryAllBalancesResponse> {
Bank::new(runner)
.query_all_balances(&QueryAllBalancesRequest {
address,
pagination,
})
.map_err(|_| StdError::generic_err("Bank all balances query failed"))
}
pub fn bank_send<'a>(
runner: &'a impl Runner<'a>,
sender: &SigningAccount,
recipient: &str,
coins: Vec<Coin>,
) -> RunnerExecuteResult<MsgSendResponse> {
let bank = Bank::new(runner);
bank.send(
MsgSend {
from_address: sender.address(),
to_address: recipient.to_string(),
amount: coins
.iter()
.map(|c| ProtoCoin {
denom: c.denom.clone(),
amount: c.amount.to_string(),
})
.collect(),
},
sender,
)
}
pub fn get_current_working_dir() -> String {
let res = env::current_dir();
match res {
Ok(path) => path.into_os_string().into_string().unwrap(),
Err(_) => "FAILED".to_string(),
}
}
/// An enum to choose which type of unwrap to use. When using `Unwrap::Err`, the
/// result must be an `Err` or the test will panic. If the result contains an
/// `Err`, the test will pass only if the error message contains the provided
/// string.
pub enum Unwrap {
Ok,
Err(&'static str),
}
impl Unwrap {
pub fn unwrap<T: Debug, E: Debug>(self, result: Result<T, E>) -> Option<T> {
match self {
Unwrap::Ok => {
let res = result.unwrap();
Some(res)
}
Unwrap::Err(s) => {
let err = result.unwrap_err();
assert!(
format!("{:?}", err).contains(s),
"Expected error message to contain {:?}, got {:?}",
s,
err
);
None
}
}
}
}
#[test]
fn test_unwrap() {
let res: Result<u32, &str> = Ok(5);
assert_eq!(Unwrap::Ok.unwrap(res), Some(5));
let res: Result<u32, &str> = Err("test");
assert_eq!(Unwrap::Err("test").unwrap(res), None);
let res: Result<u32, &str> = Err("test2");
assert_eq!(Unwrap::Err("test").unwrap(res), None);
}
#[test]
#[should_panic(expected = "Expected error message to contain \"test\", got \"random\"")]
fn test_unwrap_panic() {
let res: Result<u32, &str> = Err("random");
Unwrap::Err("test").unwrap(res);
}