Skip to content

Commit

Permalink
chore: final cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Jul 3, 2024
1 parent 619a8fa commit ad963ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
28 changes: 10 additions & 18 deletions contracts/sumtree-orderbook/src/tests/e2e/cases/test_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use rand::seq::SliceRandom;
use rand::{Rng, SeedableRng};

use super::super::utils::*;
use crate::ContractError;
use crate::{
constants::MIN_TICK,
msg::{DenomsResponse, GetTotalPoolLiquidityResponse, QueryMsg},
setup,
tests::e2e::modules::cosmwasm_pool::CosmwasmPool,
tests::e2e::test_env::TestEnv,
tick_math::{amount_to_value, tick_to_price, RoundingDirection},
types::OrderDirection,
};

Expand Down Expand Up @@ -269,7 +269,7 @@ fn run_fuzz_linear(amount_limit_orders: u64, tick_range: (i64, i64), cancel_prob

// Attempt to claim & cancel all limit orders
let (bid_unclaimable_amount, ask_unclaimable_amount) =
clear_remaining_orders(&mut t, &mut rng, &mut orders);
clear_remaining_orders(&mut t, &mut rng, &orders);
// At most one order should remain in each direction
assert!(
bid_unclaimable_amount <= 1,
Expand Down Expand Up @@ -376,6 +376,8 @@ impl MixedFuzzOperation {

// Determine if the order can be cancelled
if amount_claimable > 0 {
let res = orders::cancel_limit(t, &username, tick_id, order_id).unwrap_err();
assert::contract_err(ContractError::CancelFilledOrder, res);
return Ok(false);
}

Expand Down Expand Up @@ -406,19 +408,8 @@ impl MixedFuzzOperation {

// Determine if the order can be claimed
if amount_claimable == 0 {
return Ok(false);
}

let price = tick_to_price(order.tick_id).unwrap();
let expected_received_u256 = amount_to_value(
order.order_direction,
Uint128::from(amount_claimable),
price,
RoundingDirection::Down,
)
.unwrap();

if expected_received_u256.is_zero() {
let res = orders::claim(t, &username, tick_id, order_id).unwrap_err();
assert::contract_err(ContractError::ZeroClaim, res);
return Ok(false);
}

Expand Down Expand Up @@ -509,7 +500,8 @@ fn run_fuzz_mixed(amount_of_orders: u64, tick_bounds: (i64, i64)) {
assert::has_liquidity(&t);
}

clear_remaining_orders(&mut t, &mut rng, &mut orders);
// Attempt to claim/cancel all remaining orders
clear_remaining_orders(&mut t, &mut rng, &orders);

// -- Post test assertions --

Expand Down Expand Up @@ -669,7 +661,7 @@ fn get_random_market_direction<'a>(
fn clear_remaining_orders(
t: &mut TestEnv,
rng: &mut StdRng,
orders: &mut HashMap<u64, (String, i64)>,
orders: &HashMap<u64, (String, i64)>,
) -> (u64, u64) {
// Shuffle the order of recorded orders (as liquidity is fully filled (except the possibility of a 1 remainder))
// every order should be claimable and the order should not matter
Expand All @@ -680,7 +672,7 @@ fn clear_remaining_orders(
let mut bid_unclaimable_amount = 0;
let mut ask_unclaimable_amount = 0;

for (order_id, (username, tick_id)) in orders.clone().iter() {
for (order_id, (username, tick_id)) in orders_vec.iter().cloned() {
let maybe_order = t
.contract
.get_order(t.accounts[username].address(), *tick_id, *order_id);
Expand Down
17 changes: 16 additions & 1 deletion contracts/sumtree-orderbook/src/tests/e2e/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ pub mod assert {
tests::e2e::test_env::TestEnv,
tick_math::{amount_to_value, tick_to_price, RoundingDirection},
types::{OrderDirection, Orderbook},
ContractError,
};
use cosmwasm_std::{Coin, Coins, Fraction, Uint128};
use osmosis_test_tube::{cosmrs::proto::prost::Message, RunnerExecuteResult};
use osmosis_test_tube::{cosmrs::proto::prost::Message, RunnerError, RunnerExecuteResult};

// -- Contract State Assertions

Expand Down Expand Up @@ -436,6 +437,20 @@ pub mod assert {

Ok(result)
}

pub(crate) fn contract_err(expected: ContractError, actual: RunnerError) {
match actual {
RunnerError::ExecuteError { msg } => {
if !msg.contains(&expected.to_string()) {
panic!(
"assertion failed:\n\n must contain \t: \"{}\",\n actual \t: \"{}\"\n",
expected, msg
)
}
}
_ => panic!("unexpected error, expect execute error but got: {}", actual),
};
}
}

/// Utility functions for interacting with the orderbook
Expand Down

0 comments on commit ad963ea

Please sign in to comment.