From ad963eaaf79d81f57ea53855e287a51443ea1c18 Mon Sep 17 00:00:00 2001 From: Connor Barr Date: Wed, 3 Jul 2024 12:37:47 +0100 Subject: [PATCH] chore: final cleanup --- .../src/tests/e2e/cases/test_fuzz.rs | 28 +++++++------------ .../sumtree-orderbook/src/tests/e2e/utils.rs | 17 ++++++++++- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/contracts/sumtree-orderbook/src/tests/e2e/cases/test_fuzz.rs b/contracts/sumtree-orderbook/src/tests/e2e/cases/test_fuzz.rs index f1114b1..c025c24 100644 --- a/contracts/sumtree-orderbook/src/tests/e2e/cases/test_fuzz.rs +++ b/contracts/sumtree-orderbook/src/tests/e2e/cases/test_fuzz.rs @@ -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, }; @@ -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, @@ -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); } @@ -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); } @@ -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 -- @@ -669,7 +661,7 @@ fn get_random_market_direction<'a>( fn clear_remaining_orders( t: &mut TestEnv, rng: &mut StdRng, - orders: &mut HashMap, + orders: &HashMap, ) -> (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 @@ -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); diff --git a/contracts/sumtree-orderbook/src/tests/e2e/utils.rs b/contracts/sumtree-orderbook/src/tests/e2e/utils.rs index 224f6e2..5e890d0 100644 --- a/contracts/sumtree-orderbook/src/tests/e2e/utils.rs +++ b/contracts/sumtree-orderbook/src/tests/e2e/utils.rs @@ -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 @@ -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