From e5bc0b29b2cb3ed4e7f4b2183502e4a4b3872e43 Mon Sep 17 00:00:00 2001 From: Hasky Dragon Date: Wed, 4 Sep 2024 19:11:45 +0530 Subject: [PATCH 1/2] Outcome assertion added --- contract/src/execute.rs | 2 ++ contract/src/tests.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/contract/src/execute.rs b/contract/src/execute.rs index 98a9e0f..df2b021 100644 --- a/contract/src/execute.rs +++ b/contract/src/execute.rs @@ -104,6 +104,8 @@ fn add_market( }); } + assert!(outcomes.len() == 2, "Supports only two outcomes"); + let funds = funds.require_funds(&denom)?; let id = LAST_MARKET_ID .may_load(deps.storage)? diff --git a/contract/src/tests.rs b/contract/src/tests.rs index 0cce668..479e510 100644 --- a/contract/src/tests.rs +++ b/contract/src/tests.rs @@ -533,6 +533,41 @@ fn house_always_wins() { assert!(house_balance > Uint128::zero()); } +#[test] +#[should_panic] +fn market_with_only_one_outcome() { + let app = Predict::new(); + let params = AddMarketParams { + title: "Test market".to_owned(), + description: "Test description".to_owned(), + arbitrator: app.arbitrator.clone().to_string(), + outcomes: vec![OutcomeDef { + label: "Yes".to_owned(), + initial_amount: Collateral(100u16.into()), + }], + denom: DENOM.to_owned(), + deposit_fee: "0.01".parse().unwrap(), + withdrawal_fee: "0.01".parse().unwrap(), + withdrawal_stop_date: app.app.borrow().block_info().time.plus_days(1), + deposit_stop_date: app.app.borrow().block_info().time.plus_days(2), + house: app.house.clone().into_string(), + }; + app.app + .borrow_mut() + .execute_contract( + app.admin.clone(), + app.contract.clone(), + &ExecuteMsg::AddMarket { + params: params.into(), + }, + &[Coin { + denom: DENOM.to_owned(), + amount: 100u16.into(), + }], + ) + .unwrap(); +} + proptest! { #[test] fn test_cpmm_buy_sell(pool_one in 1..1000u32, pool_two in 1..1000u32, buy in 2..50u32) { From 8a536884e02b661205d70107b21bc2e3d7170bbc Mon Sep 17 00:00:00 2001 From: Hasky Dragon Date: Wed, 4 Sep 2024 20:04:58 +0530 Subject: [PATCH 2/2] Use errors instead of panic --- contract/src/error.rs | 2 ++ contract/src/execute.rs | 5 ++++- contract/src/tests.rs | 1 - 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/contract/src/error.rs b/contract/src/error.rs index dec1dc0..84468e5 100644 --- a/contract/src/error.rs +++ b/contract/src/error.rs @@ -102,4 +102,6 @@ pub enum Error { idx: String, source: std::num::TryFromIntError, }, + #[error("Supported only 2 outcomes, received {total_outcomes}")] + UnsupportedOutcomes { total_outcomes: usize }, } diff --git a/contract/src/execute.rs b/contract/src/execute.rs index df2b021..6bb6a45 100644 --- a/contract/src/execute.rs +++ b/contract/src/execute.rs @@ -104,7 +104,10 @@ fn add_market( }); } - assert!(outcomes.len() == 2, "Supports only two outcomes"); + let total_outcomes = outcomes.len(); + if !total_outcomes == 2 { + return Err(Error::UnsupportedOutcomes { total_outcomes }); + } let funds = funds.require_funds(&denom)?; let id = LAST_MARKET_ID diff --git a/contract/src/tests.rs b/contract/src/tests.rs index 479e510..db0c343 100644 --- a/contract/src/tests.rs +++ b/contract/src/tests.rs @@ -534,7 +534,6 @@ fn house_always_wins() { } #[test] -#[should_panic] fn market_with_only_one_outcome() { let app = Predict::new(); let params = AddMarketParams {