Skip to content

Commit

Permalink
Rewrite cw-abc tests in cw-orch
Browse files Browse the repository at this point in the history
  • Loading branch information
ismellike committed Jul 22, 2024
1 parent 0aa5215 commit 20474ab
Show file tree
Hide file tree
Showing 18 changed files with 1,373 additions and 1,717 deletions.
627 changes: 578 additions & 49 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ test-context = "0.1"
thiserror = { version = "1.0" }
wynd-utils = "0.4"
cw-orch = "0.24.1"
cw-orch-osmosis-test-tube = "0.3"

# One commit ahead of version 0.3.0. Allows initialization with an
# optional owner.
Expand Down
12 changes: 9 additions & 3 deletions contracts/external/cw-abc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ version = { workspace = true }

[lib]
crate-type = ["cdylib", "rlib"]
doctest = false

[features]
backtraces = ["cosmwasm-std/backtraces"]
Expand All @@ -35,7 +36,10 @@ cosmwasm-schema = { workspace = true }
cw-address-like = { workspace = true }
cw-ownable = { workspace = true }
cw-paginate-storage = { workspace = true }
cw-tokenfactory-issuer = { workspace = true, features = ["library"] }
cw-tokenfactory-issuer = { workspace = true, features = [
"library",
"osmosis_tokenfactory",
] }
dao-interface = { workspace = true }
getrandom = { workspace = true, features = ["js"] }
thiserror = { workspace = true }
Expand All @@ -48,11 +52,13 @@ anyhow = { workspace = true }
cw-multi-test = { workspace = true }
dao-testing = { workspace = true, features = ["test-tube"] }
osmosis-std = { workspace = true }
osmosis-test-tube = { workspace = true }
osmosis-test-tube = { version = "25.0.0" }
serde = { workspace = true }
serde_json = { workspace = true }
cw-tokenfactory-issuer = { workspace = true }
dao-voting-token-staked = { workspace = true }
dao-proposal-single = { workspace = true }
dao-voting = { workspace = true }
dao-cw-orch = { path = "../../../packages/cw-orch" }
cw-abc = { workspace = true }
cw-orch-osmosis-test-tube = { workspace = true }
dao-proposal-sudo = { workspace = true }
178 changes: 0 additions & 178 deletions contracts/external/cw-abc/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,181 +630,3 @@ pub fn update_ownership(

Ok(Response::default().add_attributes(ownership.into_attributes()))
}

#[cfg(test)]
mod tests {
use super::*;
use crate::testing::prelude::*;
use cosmwasm_std::testing::*;

mod donate {
use super::*;
use crate::abc::CurveType;
use crate::testing::{mock_init, TEST_CREATOR};
use cosmwasm_std::coin;
use cw_utils::PaymentError;

const TEST_DONOR: &str = "donor";

fn exec_donate(deps: DepsMut, donation_amount: u128) -> Result<Response, ContractError> {
donate(
deps,
mock_env(),
mock_info(TEST_DONOR, &[coin(donation_amount, TEST_RESERVE_DENOM)]),
)
}

#[test]
fn should_fail_with_no_funds() -> Result<(), ContractError> {
let mut deps = mock_dependencies();
let curve_type = CurveType::Linear {
slope: Uint128::new(1),
scale: 1,
};
let init_msg = default_instantiate_msg(2, 8, curve_type);
mock_init(deps.as_mut(), init_msg)?;

let res = exec_donate(deps.as_mut(), 0);
assert_that!(res)
.is_err()
.is_equal_to(ContractError::Payment(PaymentError::NoFunds {}));

Ok(())
}

#[test]
fn should_fail_with_incorrect_denom() -> Result<(), ContractError> {
let mut deps = mock_dependencies();
let curve_type = CurveType::Linear {
slope: Uint128::new(1),
scale: 1,
};
let init_msg = default_instantiate_msg(2, 8, curve_type);
mock_init(deps.as_mut(), init_msg)?;

let res = donate(
deps.as_mut(),
mock_env(),
mock_info(TEST_DONOR, &[coin(1, "fake")]),
);
assert_that!(res)
.is_err()
.is_equal_to(ContractError::Payment(PaymentError::MissingDenom(
TEST_RESERVE_DENOM.to_string(),
)));

Ok(())
}

#[test]
fn should_donate_to_forwarding() -> Result<(), ContractError> {
let mut deps = mock_dependencies();
// this matches `linear_curve` test case from curves.rs
let curve_type = CurveType::SquareRoot {
slope: Uint128::new(1),
scale: 1,
};
let mut init_msg = default_instantiate_msg(2, 8, curve_type);
init_msg.funding_pool_forwarding = Some(TEST_CREATOR.to_string());
mock_init(deps.as_mut(), init_msg)?;

let donation_amount = 5;
let _res = exec_donate(deps.as_mut(), donation_amount)?;

// Check that the funding pool did not increase, because it was sent to the funding pool forwarding
// NOTE: the balance cannot be checked with mock_dependencies
let curve_state = CURVE_STATE.load(&deps.storage)?;
assert_that!(curve_state.funding).is_equal_to(Uint128::zero());

// check that the donor is in the donations map
let donation = DONATIONS.load(&deps.storage, &Addr::unchecked(TEST_DONOR))?;
assert_that!(donation).is_equal_to(Uint128::new(donation_amount));

Ok(())
}

#[test]
fn test_donate_and_withdraw() -> Result<(), ContractError> {
// Init
let mut deps = mock_dependencies();

let curve_type = CurveType::SquareRoot {
slope: Uint128::new(1),
scale: 1,
};
let init_msg = default_instantiate_msg(2, 8, curve_type);
mock_init(deps.as_mut(), init_msg)?;

// Donate
let donation_amount = 5;
let _res = exec_donate(deps.as_mut(), donation_amount)?;

// Check funding pool
let curve_state = CURVE_STATE.load(&deps.storage)?;
assert_that!(curve_state.funding).is_equal_to(Uint128::from(donation_amount));

// Check random can't withdraw from the funding pool
let result = withdraw(deps.as_mut(), mock_env(), mock_info("random", &[]), None);
assert_that!(result)
.is_err()
.is_equal_to(ContractError::Ownership(
cw_ownable::OwnershipError::NotOwner,
));

// Check owner can withdraw
let result = withdraw(
deps.as_mut(),
mock_env(),
mock_info(crate::testing::TEST_CREATOR, &[]),
None,
);
assert!(result.is_ok());

Ok(())
}

#[test]
fn test_pause() -> Result<(), ContractError> {
let mut deps = mock_dependencies();
// this matches `linear_curve` test case from curves.rs
let curve_type = CurveType::SquareRoot {
slope: Uint128::new(1),
scale: 1,
};
let init_msg = default_instantiate_msg(2, 8, curve_type);
mock_init(deps.as_mut(), init_msg)?;

// Ensure not paused on instantiate
assert!(!IS_PAUSED.load(&deps.storage)?);

// Ensure random cannot pause
let res = toggle_pause(deps.as_mut(), mock_info("random", &[]));
assert_that!(res)
.is_err()
.is_equal_to(ContractError::Ownership(
cw_ownable::OwnershipError::NotOwner,
));

// Ensure paused after toggling
toggle_pause(deps.as_mut(), mock_info(TEST_CREATOR, &[]))?;
assert!(IS_PAUSED.load(&deps.storage)?);

// Ensure random cannot do anything
let res = crate::contract::execute(
deps.as_mut(),
mock_env(),
mock_info("random", &[]),
crate::msg::ExecuteMsg::TogglePause {},
);
assert_that!(res)
.is_err()
.is_equal_to(ContractError::Paused {});

// Ensure unpaused after toggling
toggle_pause(deps.as_mut(), mock_info(TEST_CREATOR, &[]))?;
assert!(!IS_PAUSED.load(&deps.storage)?);

Ok(())
}
}
}
1 change: 1 addition & 0 deletions contracts/external/cw-abc/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub fn do_query(deps: Deps, _env: Env, msg: QueryMsg, curve_fn: CurveFn) -> StdR
QueryMsg::SellQuote { payment } => {
to_json_binary(&queries::query_sell_quote(deps, payment)?)
}
QueryMsg::SupplyDenom {} => to_json_binary(&SUPPLY_DENOM.load(deps.storage)?),
}
}

Expand Down
12 changes: 3 additions & 9 deletions contracts/external/cw-abc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ pub mod msg;
mod queries;
pub mod state;

// Integration tests using an actual chain binary, requires
// the "test-tube" feature to be enabled
// cargo test --features test-tube
#[cfg(test)]
#[cfg(feature = "test-tube")]
mod test_tube;
pub use crate::error::ContractError;

#[cfg(test)]
mod testing;

pub use crate::error::ContractError;
#[cfg(feature = "test-tube")]
mod tests;
6 changes: 6 additions & 0 deletions contracts/external/cw-abc/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ pub enum UpdatePhaseConfigMsg {
pub enum ExecuteMsg {
/// Buy will attempt to purchase as many supply tokens as possible.
/// You must send only reserve tokens.
#[cw_orch(payable)]
Buy {},
/// Sell burns supply tokens in return for the reserve token.
/// You must send only supply tokens.
#[cw_orch(payable)]
Sell {},
/// Donate will donate tokens to the funding pool.
/// You must send only reserve tokens.
#[cw_orch(payable)]
Donate {},
/// Withdraw will withdraw tokens from the funding pool.
Withdraw {
Expand Down Expand Up @@ -170,6 +173,9 @@ pub enum QueryMsg {
/// Returns the address of the cw-tokenfactory-issuer contract
#[returns(::cosmwasm_std::Addr)]
TokenContract {},
/// Returns the supply denom
#[returns(String)]
SupplyDenom {},
}

#[cw_serde]
Expand Down
Loading

0 comments on commit 20474ab

Please sign in to comment.