Skip to content

Commit

Permalink
Reorganize prediction-market tests (#1242)
Browse files Browse the repository at this point in the history
* Rename `tests.rs` to `old_tests.rs` to avoid naming conflicts

* Move `buy_complete_set` tests to new tests

* Clean up `buy_complete_set` tests

* Extract `sell_complete_set_tests`

* Clean up `sell_complete_set` tests

* Extract `admin_move_market_to_closed` tests

* Extract `create_market` tests

* Extract `admin_move_market_to_resolved` tests

* Remove superfluous test

* Extract more `create_market` tests

* Extract `approve_market` tests

* Extract `edit_market` tests

* Extract `edit_market` tests

* Add `on_market_close` tests

* Extract `manually_close_market` tests

* Extract `on_initialize` tests

* Extract `report` tests

* Extract `dispute` tests

* Extract `schedule_early_close` tests

* Extract `dispute_early_close` tests

* Extract `reject_early_close` tests

* Extract more `dispute` tests

* Extract `close_trusted_market` tests

* Extract `start_global_dispute` tests

* Extract `redeem_shares` tests and add missing tests

* Extract `on_resolution` and additional `redeem_shares` tests

* Move integration tests into new test module

* Automatically set block to `1` at the start of test

* Replace `crate::Config` with `Config`

* Access constant through `Config`

* Add TODOs for missing execution paths

* Fix formatting

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
maltekliemann and mergify[bot] authored Jan 23, 2024
1 parent e1ff1e1 commit 761cc59
Show file tree
Hide file tree
Showing 27 changed files with 6,468 additions and 5,878 deletions.
51 changes: 22 additions & 29 deletions zrml/prediction-markets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,28 @@ mod pallet {
/// Currently 10 blocks is 2 minutes (assuming block time is 12 seconds).
pub(crate) const MAX_RECOVERY_TIME_FRAMES: TimeFrame = 10;

pub(crate) type BalanceOf<T> = <T as zrml_market_commons::Config>::Balance;
pub(crate) type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub(crate) type NegativeImbalanceOf<T> =
<<T as Config>::Currency as Currency<AccountIdOf<T>>>::NegativeImbalance;
pub(crate) type TimeFrame = u64;
pub(crate) type AssetOf<T> = Asset<MarketIdOf<T>>;
pub(crate) type BalanceOf<T> = <T as zrml_market_commons::Config>::Balance;
pub(crate) type CacheSize = ConstU32<64>;
pub(crate) type EditReason<T> = BoundedVec<u8, <T as Config>::MaxEditReasonLen>;
pub(crate) type InitialItemOf<T> = InitialItem<AccountIdOf<T>, BalanceOf<T>>;
pub(crate) type MarketBondsOf<T> = MarketBonds<AccountIdOf<T>, BalanceOf<T>>;
pub(crate) type MarketIdOf<T> = <T as zrml_market_commons::Config>::MarketId;
pub(crate) type MomentOf<T> =
<<T as zrml_market_commons::Config>::Timestamp as frame_support::traits::Time>::Moment;
pub type MarketOf<T> = Market<
pub(crate) type MarketOf<T> = Market<
AccountIdOf<T>,
BalanceOf<T>,
<T as frame_system::Config>::BlockNumber,
MomentOf<T>,
Asset<MarketIdOf<T>>,
AssetOf<T>,
>;
pub(crate) type MomentOf<T> =
<<T as zrml_market_commons::Config>::Timestamp as frame_support::traits::Time>::Moment;
pub(crate) type NegativeImbalanceOf<T> =
<<T as Config>::Currency as Currency<AccountIdOf<T>>>::NegativeImbalance;
pub(crate) type RejectReason<T> = BoundedVec<u8, <T as Config>::MaxRejectReasonLen>;
pub(crate) type ReportOf<T> = Report<AccountIdOf<T>, <T as frame_system::Config>::BlockNumber>;
pub type CacheSize = ConstU32<64>;
pub type EditReason<T> = BoundedVec<u8, <T as Config>::MaxEditReasonLen>;
pub type RejectReason<T> = BoundedVec<u8, <T as Config>::MaxRejectReasonLen>;
type InitialItemOf<T> = InitialItem<AccountIdOf<T>, BalanceOf<T>>;
pub(crate) type TimeFrame = u64;

macro_rules! impl_unreserve_bond {
($fn_name:ident, $bond_type:ident) => {
Expand Down Expand Up @@ -591,7 +593,7 @@ mod pallet {
#[transactional]
pub fn create_market(
origin: OriginFor<T>,
base_asset: Asset<MarketIdOf<T>>,
base_asset: AssetOf<T>,
creator_fee: Perbill,
oracle: T::AccountId,
period: MarketPeriod<T::BlockNumber, MomentOf<T>>,
Expand Down Expand Up @@ -643,7 +645,7 @@ mod pallet {
#[transactional]
pub fn edit_market(
origin: OriginFor<T>,
base_asset: Asset<MarketIdOf<T>>,
base_asset: AssetOf<T>,
market_id: MarketIdOf<T>,
oracle: T::AccountId,
period: MarketPeriod<T::BlockNumber, MomentOf<T>>,
Expand Down Expand Up @@ -1062,7 +1064,7 @@ mod pallet {
#[pallet::call_index(17)]
pub fn create_market_and_deploy_pool(
origin: OriginFor<T>,
base_asset: Asset<MarketIdOf<T>>,
base_asset: AssetOf<T>,
creator_fee: Perbill,
oracle: T::AccountId,
period: MarketPeriod<T::BlockNumber, MomentOf<T>>,
Expand Down Expand Up @@ -1902,13 +1904,7 @@ mod pallet {
SoldCompleteSet(MarketIdOf<T>, BalanceOf<T>, AccountIdOf<T>),
/// An amount of winning outcomes have been redeemed.
/// \[market_id, currency_id, amount_redeemed, payout, who\]
TokensRedeemed(
MarketIdOf<T>,
Asset<MarketIdOf<T>>,
BalanceOf<T>,
BalanceOf<T>,
AccountIdOf<T>,
),
TokensRedeemed(MarketIdOf<T>, AssetOf<T>, BalanceOf<T>, BalanceOf<T>, AccountIdOf<T>),
/// The global dispute was started. \[market_id\]
GlobalDisputeStarted(MarketIdOf<T>),
/// The recovery limit for timestamp based markets was reached due to a prolonged chain stall.
Expand Down Expand Up @@ -2090,7 +2086,7 @@ mod pallet {
#[require_transactional]
fn do_create_market(
who: T::AccountId,
base_asset: Asset<MarketIdOf<T>>,
base_asset: AssetOf<T>,
creator_fee: Perbill,
oracle: T::AccountId,
period: MarketPeriod<T::BlockNumber, MomentOf<T>>,
Expand Down Expand Up @@ -2148,10 +2144,7 @@ mod pallet {
Ok((ids_amount, market_id))
}

pub fn outcome_assets(
market_id: MarketIdOf<T>,
market: &MarketOf<T>,
) -> Vec<Asset<MarketIdOf<T>>> {
pub fn outcome_assets(market_id: MarketIdOf<T>, market: &MarketOf<T>) -> Vec<AssetOf<T>> {
match market.market_type {
MarketType::Categorical(categories) => {
let mut assets = Vec::new();
Expand Down Expand Up @@ -2886,7 +2879,7 @@ mod pallet {
}

fn construct_market(
base_asset: Asset<MarketIdOf<T>>,
base_asset: AssetOf<T>,
creator: T::AccountId,
creator_fee: Perbill,
oracle: T::AccountId,
Expand All @@ -2899,7 +2892,7 @@ mod pallet {
scoring_rule: ScoringRule,
report: Option<ReportOf<T>>,
resolved_outcome: Option<OutcomeReport>,
bonds: MarketBonds<T::AccountId, BalanceOf<T>>,
bonds: MarketBondsOf<T>,
) -> Result<MarketOf<T>, DispatchError> {
let valid_base_asset = match base_asset {
Asset::Ztg => true,
Expand Down
5 changes: 4 additions & 1 deletion zrml/prediction-markets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ impl ExtBuilder {
}
.assimilate_storage(&mut t)
.unwrap();
t.into()

let mut test_ext: sp_io::TestExternalities = t.into();
test_ext.execute_with(|| System::set_block_number(1));
test_ext
}
}

Expand Down
Loading

0 comments on commit 761cc59

Please sign in to comment.