Skip to content

Commit 6814322

Browse files
committed
Get test-tube tests running
1 parent 967e6e4 commit 6814322

File tree

7 files changed

+306
-71
lines changed

7 files changed

+306
-71
lines changed

contracts/external/cw-abc/Cargo.toml

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ crate-type = ["cdylib", "rlib"]
2020
backtraces = ["cosmwasm-std/backtraces"]
2121
# use library feature to disable all instantiate/execute/query exports
2222
library = []
23-
# interface = ["dep:cw-orch"] # Adds the dependency when the feature is enabled
23+
# use test tube feature to enable test-tube integration tests, for example
24+
# cargo test --features "test-tube"
25+
test-tube = []
26+
# when writing tests you may wish to enable test-tube as a default feature
27+
default = ["test-tube"]
2428

2529
[dependencies]
2630
cw-utils = { workspace = true }
@@ -39,15 +43,17 @@ token-bindings = { workspace = true }
3943
cw-ownable = { workspace = true }
4044
cw-paginate-storage = { workspace = true }
4145
cw-tokenfactory-issuer = { workspace = true, features = ["library"] }
42-
# cw-orch = { version = "0.13.3", optional = true }
4346

4447
[dev-dependencies]
4548
# TODO move to workspace?
4649
speculoos = "0.11.0"
4750
anyhow = { workspace = true }
4851
cw-multi-test = { workspace = true }
49-
dao-testing = { workspace = true }
52+
dao-testing = { workspace = true, features = ["test-tube"] }
53+
osmosis-std = { workspace = true }
5054
osmosis-test-tube = { workspace = true }
55+
serde = { workspace = true }
56+
serde_json = { workspace = true }
5157

5258
[profile.release]
5359
rpath = false

contracts/external/cw-abc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub mod state;
1212
// cargo test --features test-tube
1313
#[cfg(test)]
1414
#[cfg(feature = "test-tube")]
15-
mod testtube;
15+
mod test_tube;
1616

1717
#[cfg(test)]
1818
mod testing;

contracts/external/cw-abc/src/test_tube/integration_tests.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::test_env::{TestEnv, TestEnvBuilder};
12
use crate::{
23
abc::{
34
ClosedConfig, CommonsPhaseConfig, HatchConfig, MinMax, OpenConfig, ReserveToken,
@@ -13,5 +14,7 @@ use token_bindings::Metadata;
1314
fn test_happy_path() {
1415
let app = OsmosisTestApp::new();
1516

17+
let env = TestEnvBuilder::new();
18+
let TestEnv { abc, accounts, .. } = env.default_setup(&app);
1619
// TODO
1720
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
mod integration_tests;
12
mod test_env;

contracts/external/cw-abc/src/test_tube/test_env.rs

+217-18
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,36 @@
33
#![allow(dead_code)]
44

55
use crate::{
6-
msg::{ExecuteMsg, InitialBalance, InstantiateMsg, NewTokenInfo, QueryMsg, TokenInfo},
6+
abc::{
7+
ClosedConfig, CommonsPhaseConfig, CurveType, HatchConfig, MinMax, OpenConfig, ReserveToken,
8+
SupplyToken,
9+
},
10+
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg},
711
ContractError,
812
};
913

10-
use cosmwasm_std::{Coin, Uint128};
14+
use cosmwasm_std::{Coin, Decimal, Uint128};
1115
use cw_tokenfactory_issuer::msg::{DenomResponse, DenomUnit};
1216
use cw_utils::Duration;
13-
use dao_interface::voting::{IsActiveResponse, VotingPowerAtHeightResponse};
14-
use dao_testing::test_tube::{cw_abc::CwAbc, cw_tokenfactory_issuer::TokenfactoryIssuer};
15-
use dao_voting::threshold::ActiveThreshold;
16-
use osmosis_std::types::{
17-
cosmos::bank::v1beta1::QueryAllBalancesRequest, cosmwasm::wasm::v1::MsgExecuteContractResponse,
18-
};
17+
use dao_testing::test_tube::cw_tokenfactory_issuer::TokenfactoryIssuer;
1918
use osmosis_test_tube::{
19+
osmosis_std::types::cosmos::bank::v1beta1::QueryAllBalancesRequest,
20+
osmosis_std::types::cosmwasm::wasm::v1::{
21+
MsgExecuteContractResponse, MsgMigrateContract, MsgMigrateContractResponse,
22+
},
2023
Account, Bank, Module, OsmosisTestApp, RunnerError, RunnerExecuteResult, RunnerResult,
2124
SigningAccount, Wasm,
2225
};
2326
use serde::de::DeserializeOwned;
27+
use std::fmt::Debug;
2428
use std::path::PathBuf;
2529

2630
pub const DENOM: &str = "ucat";
2731
pub const JUNO: &str = "ujuno";
2832

2933
pub struct TestEnv<'a> {
3034
pub app: &'a OsmosisTestApp,
31-
pub abc: TfDaoVotingContract<'a>,
35+
pub abc: CwAbc<'a>,
3236
pub tf_issuer: TokenfactoryIssuer<'a>,
3337
pub accounts: Vec<SigningAccount>,
3438
}
@@ -105,17 +109,46 @@ impl TestEnvBuilder {
105109
.init_accounts(&[Coin::new(1000000000000000u128, "uosmo")], 10)
106110
.unwrap();
107111

108-
let initial_balances: Vec<InitialBalance> = accounts
109-
.iter()
110-
.map(|acc| InitialBalance {
111-
address: acc.address(),
112-
amount: Uint128::new(100),
113-
})
114-
.collect();
115-
116112
let issuer_id = TokenfactoryIssuer::upload(app, &accounts[0]).unwrap();
117113

118-
let abc = CwAbc::deploy(app, &InstantiateMsg {}, &accounts[0]).unwrap();
114+
let abc = CwAbc::deploy(
115+
app,
116+
&InstantiateMsg {
117+
token_issuer_code_id: issuer_id,
118+
supply: SupplyToken {
119+
subdenom: DENOM.to_string(),
120+
metadata: None,
121+
decimals: 6,
122+
},
123+
reserve: ReserveToken {
124+
denom: JUNO.to_string(),
125+
decimals: 6,
126+
},
127+
phase_config: CommonsPhaseConfig {
128+
hatch: HatchConfig {
129+
initial_raise: MinMax {
130+
min: Uint128::one(),
131+
max: Uint128::from(1000000u128),
132+
},
133+
initial_price: Uint128::one(),
134+
initial_allocation_ratio: Decimal::percent(10u64),
135+
exit_tax: Decimal::zero(),
136+
},
137+
open: OpenConfig {
138+
allocation_percentage: Decimal::percent(10u64),
139+
exit_tax: Decimal::percent(10u64),
140+
},
141+
closed: ClosedConfig {},
142+
},
143+
hatcher_allowlist: None,
144+
curve_type: CurveType::Constant {
145+
value: Uint128::one(),
146+
scale: 1,
147+
},
148+
},
149+
&accounts[0],
150+
)
151+
.unwrap();
119152

120153
let issuer_addr = CwAbc::query(&abc, &QueryMsg::TokenContract {}).unwrap();
121154

@@ -193,3 +226,169 @@ pub fn assert_contract_err(expected: ContractError, actual: RunnerError) {
193226
_ => panic!("unexpected error, expect execute error but got: {}", actual),
194227
};
195228
}
229+
230+
#[derive(Debug)]
231+
pub struct CwAbc<'a> {
232+
pub app: &'a OsmosisTestApp,
233+
pub code_id: u64,
234+
pub contract_addr: String,
235+
}
236+
237+
impl<'a> CwAbc<'a> {
238+
pub fn deploy(
239+
app: &'a OsmosisTestApp,
240+
instantiate_msg: &InstantiateMsg,
241+
signer: &SigningAccount,
242+
) -> Result<Self, RunnerError> {
243+
let wasm = Wasm::new(app);
244+
let token_creation_fee = Coin::new(10000000, "uosmo");
245+
246+
let code_id = wasm
247+
.store_code(&Self::get_wasm_byte_code(), None, signer)?
248+
.data
249+
.code_id;
250+
251+
let contract_addr = wasm
252+
.instantiate(
253+
code_id,
254+
&instantiate_msg,
255+
Some(&signer.address()),
256+
None,
257+
&[token_creation_fee],
258+
signer,
259+
)?
260+
.data
261+
.address;
262+
263+
Ok(Self {
264+
app,
265+
code_id,
266+
contract_addr,
267+
})
268+
}
269+
270+
pub fn new_with_values(
271+
app: &'a OsmosisTestApp,
272+
code_id: u64,
273+
contract_addr: String,
274+
) -> Result<Self, RunnerError> {
275+
Ok(Self {
276+
app,
277+
code_id,
278+
contract_addr,
279+
})
280+
}
281+
282+
/// uploads contract and returns a code ID
283+
pub fn upload(app: &OsmosisTestApp, signer: &SigningAccount) -> Result<u64, RunnerError> {
284+
let wasm = Wasm::new(app);
285+
286+
let code_id = wasm
287+
.store_code(&Self::get_wasm_byte_code(), None, signer)?
288+
.data
289+
.code_id;
290+
291+
Ok(code_id)
292+
}
293+
294+
pub fn instantiate(
295+
app: &'a OsmosisTestApp,
296+
code_id: u64,
297+
instantiate_msg: &InstantiateMsg,
298+
signer: &SigningAccount,
299+
) -> Result<Self, RunnerError> {
300+
let wasm = Wasm::new(app);
301+
let contract_addr = wasm
302+
.instantiate(
303+
code_id,
304+
&instantiate_msg,
305+
Some(&signer.address()),
306+
None,
307+
&[],
308+
signer,
309+
)?
310+
.data
311+
.address;
312+
313+
Ok(Self {
314+
app,
315+
code_id,
316+
contract_addr,
317+
})
318+
}
319+
320+
// executes
321+
pub fn execute(
322+
&self,
323+
execute_msg: &ExecuteMsg,
324+
funds: &[Coin],
325+
signer: &SigningAccount,
326+
) -> RunnerExecuteResult<MsgExecuteContractResponse> {
327+
let wasm = Wasm::new(self.app);
328+
wasm.execute(&self.contract_addr, execute_msg, funds, signer)
329+
}
330+
331+
// queries
332+
pub fn query<T>(&self, query_msg: &QueryMsg) -> Result<T, RunnerError>
333+
where
334+
T: DeserializeOwned,
335+
{
336+
let wasm = Wasm::new(self.app);
337+
wasm.query(&self.contract_addr, query_msg)
338+
}
339+
340+
// pub fn migrate(
341+
// &self,
342+
// testdata: &str,
343+
// signer: &SigningAccount,
344+
// ) -> RunnerExecuteResult<MsgMigrateContractResponse> {
345+
// let wasm = Wasm::new(self.app);
346+
// let manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
347+
// let wasm_byte_code =
348+
// std::fs::read(manifest_path.join("tests").join("testdata").join(testdata)).unwrap();
349+
350+
// let code_id = wasm.store_code(&wasm_byte_code, None, signer)?.data.code_id;
351+
// self.app.execute(
352+
// MsgMigrateContract {
353+
// sender: signer.address(),
354+
// contract: self.contract_addr.clone(),
355+
// code_id,
356+
// msg: serde_json::to_vec(&MigrateMsg {}).unwrap(),
357+
// },
358+
// "/cosmwasm.wasm.v1.MsgMigrateContract",
359+
// signer,
360+
// )
361+
// }
362+
363+
fn get_wasm_byte_code() -> Vec<u8> {
364+
let manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
365+
let byte_code = std::fs::read(
366+
manifest_path
367+
.join("..")
368+
.join("..")
369+
.join("artifacts")
370+
.join("cw_tokenfactory_issuer.wasm"),
371+
);
372+
match byte_code {
373+
Ok(byte_code) => byte_code,
374+
// On arm processors, the above path is not found, so we try the following path
375+
Err(_) => std::fs::read(
376+
manifest_path
377+
.join("..")
378+
.join("..")
379+
.join("artifacts")
380+
.join("cw_tokenfactory_issuer-aarch64.wasm"),
381+
)
382+
.unwrap(),
383+
}
384+
}
385+
386+
pub fn execute_error(err: ContractError) -> RunnerError {
387+
RunnerError::ExecuteError {
388+
msg: format!(
389+
"failed to execute message; message index: 0: {}: execute wasm contract failed",
390+
err
391+
),
392+
}
393+
}
394+
}

0 commit comments

Comments
 (0)