Skip to content

Commit

Permalink
Refactor MarketBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
maltekliemann committed Feb 8, 2024
1 parent 5e403c6 commit 052b047
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 357 deletions.
8 changes: 4 additions & 4 deletions primitives/src/traits/market_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use alloc::vec::Vec;
use sp_runtime::{DispatchError, Perbill};

macro_rules! builder_methods {
($($field:ident: $type:ty),*) => {
($($field:ident: $type:ty),* $(,)?) => {
$(fn $field(&mut self, $field: $type) -> &mut Self;)*
}
}
Expand All @@ -32,11 +32,11 @@ macro_rules! builder_methods {
/// the usual calling pattern is:
///
/// ```ignore
/// let builder = MarketBuilderImpl::new(args...);
/// let builder = MarketBuilderImpl::new();
/// builder.field1(value1).field2(value2);
/// builder.clone().build()
/// ```
pub trait MarketBuilder<AI, BA, BN, M, A, MI> {
pub trait MarketBuilderTrait<AI, BA, BN, M, A, MI> {
fn build(self) -> Result<Market<AI, BA, BN, M, A, MI>, DispatchError>;

builder_methods! {
Expand All @@ -56,6 +56,6 @@ pub trait MarketBuilder<AI, BA, BN, M, A, MI> {
resolved_outcome: Option<OutcomeReport>,
dispute_mechanism: Option<MarketDisputeMechanism>,
bonds: MarketBonds<AI, BA>,
early_close: Option<EarlyClose<BN, M>>
early_close: Option<EarlyClose<BN, M>>,
}
}
4 changes: 2 additions & 2 deletions primitives/src/traits/market_commons_pallet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#![allow(clippy::type_complexity)]

use crate::{
traits::MarketBuilder,
traits::MarketBuilderTrait,
types::{Asset, Market, PoolId},
};
use frame_support::{
Expand Down Expand Up @@ -100,7 +100,7 @@ pub trait MarketCommonsPalletApi {
market_builder: U,
) -> Result<(Self::MarketId, MarketOf<Self>), DispatchError>
where
U: MarketBuilder<
U: MarketBuilderTrait<
Self::AccountId,
Self::Balance,
Self::BlockNumber,
Expand Down
2 changes: 0 additions & 2 deletions primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

pub mod multi_hash;
pub mod primitive_market_builder;
pub mod result_with_weight_info;
pub mod type_aliases;
pub mod xcm_metadata;

pub use multi_hash::*;
pub use primitive_market_builder::*;
pub use result_with_weight_info::*;
pub use type_aliases::*;
pub use xcm_metadata::*;
122 changes: 0 additions & 122 deletions primitives/src/types/primitive_market_builder.rs

This file was deleted.

18 changes: 13 additions & 5 deletions zrml/market-commons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern crate alloc;
pub mod migrations;
mod mock;
mod tests;
pub mod types;

pub use pallet::*;
pub use zeitgeist_primitives::traits::MarketCommonsPalletApi;
Expand All @@ -50,8 +51,8 @@ mod pallet {
};
use zeitgeist_primitives::{
math::checked_ops_res::CheckedAddRes,
traits::MarketBuilder,
types::{Asset, Market, PoolId},
traits::MarketBuilderTrait,
types::{Asset, Deadlines, EarlyClose, Market, MarketBonds, MarketPeriod, PoolId, Report},
};

/// The current storage version.
Expand All @@ -61,6 +62,7 @@ mod pallet {
pub(crate) type AssetOf<T> = Asset<MarketIdOf<T>>;
pub(crate) type BalanceOf<T> = <T as Config>::Balance;
pub(crate) type BlockNumberOf<T> = <T as frame_system::Config>::BlockNumber;
pub(crate) type MarketIdOf<T> = <T as Config>::MarketId;
pub(crate) type MarketOf<T> = Market<
AccountIdOf<T>,
BalanceOf<T>,
Expand All @@ -69,8 +71,12 @@ mod pallet {
AssetOf<T>,
MarketIdOf<T>,
>;
pub type MarketIdOf<T> = <T as Config>::MarketId;
pub type MomentOf<T> = <<T as Config>::Timestamp as frame_support::traits::Time>::Moment;
pub(crate) type MomentOf<T> = <<T as Config>::Timestamp as frame_support::traits::Time>::Moment;
pub(crate) type DeadlinesOf<T> = Deadlines<BlockNumberOf<T>>;
pub(crate) type EarlyCloseOf<T> = EarlyClose<BlockNumberOf<T>, MomentOf<T>>;
pub(crate) type MarketBondsOf<T> = MarketBonds<AccountIdOf<T>, BalanceOf<T>>;
pub(crate) type MarketPeriodOf<T> = MarketPeriod<BlockNumberOf<T>, MomentOf<T>>;
pub(crate) type ReportOf<T> = Report<AccountIdOf<T>, BlockNumberOf<T>>;

#[pallet::call]
impl<T: Config> Pallet<T> {}
Expand Down Expand Up @@ -112,6 +118,8 @@ mod pallet {
NoReport,
/// There's a pool registered for this market already.
PoolAlreadyExists,
/// Unexpectedly failed to build a market due to missing data.
IncompleteMarketBuilder,
}

#[pallet::hooks]
Expand Down Expand Up @@ -193,7 +201,7 @@ mod pallet {
mut market_builder: U,
) -> Result<(Self::MarketId, MarketOf<T>), DispatchError>
where
U: MarketBuilder<
U: MarketBuilderTrait<
Self::AccountId,
Self::Balance,
Self::BlockNumber,
Expand Down
61 changes: 26 additions & 35 deletions zrml/market-commons/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,51 @@

use crate::{
mock::{ExtBuilder, MarketCommons, Runtime},
AccountIdOf, AssetOf, BalanceOf, BlockNumberOf, MarketCounter, MarketIdOf, Markets, MomentOf,
types::MarketBuilder,
AccountIdOf, MarketCounter, Markets,
};
use frame_support::{assert_err, assert_noop, assert_ok};
use sp_runtime::{DispatchError, Perbill};
use zeitgeist_primitives::{
traits::{MarketBuilder, MarketCommonsPalletApi},
traits::{MarketBuilderTrait, MarketCommonsPalletApi},
types::{
Asset, Deadlines, MarketBonds, MarketCreation, MarketDisputeMechanism, MarketPeriod,
MarketStatus, MarketType, PrimitiveMarketBuilder, ScoringRule,
MarketStatus, MarketType, ScoringRule,
},
};

type PrimitiveMarketBuilderOf<Runtime> = PrimitiveMarketBuilder<
AccountIdOf<Runtime>,
BalanceOf<Runtime>,
BlockNumberOf<Runtime>,
MomentOf<Runtime>,
AssetOf<Runtime>,
MarketIdOf<Runtime>,
>;

// Creates a sample market builder. We use the `oracle` field to tell markets apart from each other.
// For testing purposes, we allow `market_id` to be defined, as well.
fn create_market_builder(oracle: AccountIdOf<Runtime>) -> PrimitiveMarketBuilderOf<Runtime> {
PrimitiveMarketBuilder {
market_id: None,
base_asset: Asset::Ztg,
creation: MarketCreation::Permissionless,
creator_fee: Perbill::zero(),
creator: 0,
market_type: MarketType::Scalar(0..=100),
dispute_mechanism: Some(MarketDisputeMechanism::Authorized),
metadata: vec![],
oracle,
period: MarketPeriod::Block(0..100),
deadlines: Deadlines {
fn create_market_builder(oracle: AccountIdOf<Runtime>) -> MarketBuilder<Runtime> {
let mut market_builder = MarketBuilder::new();
market_builder
.base_asset(Asset::Ztg)
.creation(MarketCreation::Permissionless)
.creator_fee(Perbill::zero())
.creator(0)
.market_type(MarketType::Scalar(0..=100))
.dispute_mechanism(Some(MarketDisputeMechanism::Authorized))
.metadata(vec![])
.oracle(oracle)
.period(MarketPeriod::Block(0..100))
.deadlines(Deadlines {
grace_period: 1_u64,
oracle_duration: 1_u64,
dispute_duration: 1_u64,
},
report: None,
resolved_outcome: None,
scoring_rule: ScoringRule::Lmsr,
status: MarketStatus::Disputed,
bonds: MarketBonds {
})
.report(None)
.resolved_outcome(None)
.scoring_rule(ScoringRule::Lmsr)
.status(MarketStatus::Disputed)
.bonds(MarketBonds {
creation: None,
oracle: None,
outsider: None,
dispute: None,
close_dispute: None,
close_request: None,
},
early_close: None,
}
})
.early_close(None);
market_builder
}

#[test]
Expand Down
Loading

0 comments on commit 052b047

Please sign in to comment.