From 6a22be8fb6f604a6f86f0f366547e5493ffdd50c Mon Sep 17 00:00:00 2001 From: Malte Kliemann Date: Fri, 9 Feb 2024 00:16:01 +0100 Subject: [PATCH] Add missing files --- .../src/types/market_builder.rs | 167 ++++++++++++++++++ zrml/market-commons/src/types/mod.rs | 20 +++ 2 files changed, 187 insertions(+) create mode 100644 zrml/market-commons/src/types/market_builder.rs create mode 100644 zrml/market-commons/src/types/mod.rs diff --git a/zrml/market-commons/src/types/market_builder.rs b/zrml/market-commons/src/types/market_builder.rs new file mode 100644 index 000000000..dcf28a2c4 --- /dev/null +++ b/zrml/market-commons/src/types/market_builder.rs @@ -0,0 +1,167 @@ +// Copyright 2024 Forecasting Technologies LTD. +// +// This file is part of Zeitgeist. +// +// Zeitgeist is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// Zeitgeist is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Zeitgeist. If not, see . + +use crate::{ + AccountIdOf, AssetOf, BalanceOf, BlockNumberOf, Config, DeadlinesOf, EarlyCloseOf, Error, + MarketBondsOf, MarketIdOf, MarketOf, MarketPeriodOf, MomentOf, ReportOf, +}; +use alloc::vec::Vec; +use sp_runtime::{DispatchError, Perbill}; +use zeitgeist_primitives::{ + traits::MarketBuilderTrait, + types::{ + Market, MarketCreation, MarketDisputeMechanism, MarketStatus, MarketType, OutcomeReport, + ScoringRule, + }, +}; + +/// Fully-fledged mutably referenced builder struct for `Market`. +#[derive(Clone)] +pub struct MarketBuilder +where + T: Config, +{ + market_id: Option>, + base_asset: Option>, + creator: Option>, + creation: Option, + creator_fee: Option, + oracle: Option>, + metadata: Option>, + market_type: Option, + period: Option>, + deadlines: Option>, + scoring_rule: Option, + status: Option, + report: Option>>, + resolved_outcome: Option>, + dispute_mechanism: Option>, + bonds: Option>, + early_close: Option>>, +} + +impl MarketBuilder +where + T: Config, +{ + pub fn new() -> Self { + MarketBuilder { + market_id: None, + base_asset: None, + creator: None, + creation: None, + creator_fee: None, + oracle: None, + metadata: None, + market_type: None, + period: None, + deadlines: None, + scoring_rule: None, + status: None, + report: None, + resolved_outcome: None, + dispute_mechanism: None, + bonds: None, + early_close: None, + } + } +} + +impl Default for MarketBuilder +where + T: Config, +{ + fn default() -> Self { + Self::new() + } +} + +/// Implements setter methods for a mutably referenced builder struct. Fields are specified using +/// the pattern `{ field: type, ... }`. +macro_rules! impl_builder_methods { + ($($field:ident: $type:ty),* $(,)?) => { + $( + fn $field(&mut self, $field: $type) -> &mut Self { + self.$field = Some($field); + self + } + )* + } +} + +/// Unwraps `opt` and throws `IncompleteMarketBuilder` in case of failure. +fn ok_or_incomplete(opt: Option) -> Result +where + T: Config, +{ + opt.ok_or(Error::::IncompleteMarketBuilder.into()) +} + +impl + MarketBuilderTrait< + AccountIdOf, + BalanceOf, + BlockNumberOf, + MomentOf, + AssetOf, + MarketIdOf, + > for MarketBuilder +where + T: Config, +{ + fn build(self) -> Result, DispatchError> { + Ok(Market { + market_id: ok_or_incomplete::(self.market_id)?, + base_asset: ok_or_incomplete::(self.base_asset)?, + creator: ok_or_incomplete::(self.creator)?, + creation: ok_or_incomplete::(self.creation)?, + creator_fee: ok_or_incomplete::(self.creator_fee)?, + oracle: ok_or_incomplete::(self.oracle)?, + metadata: ok_or_incomplete::(self.metadata)?, + market_type: ok_or_incomplete::(self.market_type)?, + period: ok_or_incomplete::(self.period)?, + deadlines: ok_or_incomplete::(self.deadlines)?, + scoring_rule: ok_or_incomplete::(self.scoring_rule)?, + status: ok_or_incomplete::(self.status)?, + report: ok_or_incomplete::(self.report)?, + resolved_outcome: ok_or_incomplete::(self.resolved_outcome)?, + dispute_mechanism: ok_or_incomplete::(self.dispute_mechanism)?, + bonds: ok_or_incomplete::(self.bonds)?, + early_close: ok_or_incomplete::(self.early_close)?, + }) + } + + impl_builder_methods! { + market_id: MarketIdOf, + base_asset: AssetOf, + creator: AccountIdOf, + creation: MarketCreation, + creator_fee: Perbill, + oracle: AccountIdOf, + metadata: Vec, + market_type: MarketType, + period: MarketPeriodOf, + deadlines: DeadlinesOf, + scoring_rule: ScoringRule, + status: MarketStatus, + report: Option>, + resolved_outcome: Option, + dispute_mechanism: Option, + bonds: MarketBondsOf, + early_close: Option>, + } +} diff --git a/zrml/market-commons/src/types/mod.rs b/zrml/market-commons/src/types/mod.rs new file mode 100644 index 000000000..699f70ab2 --- /dev/null +++ b/zrml/market-commons/src/types/mod.rs @@ -0,0 +1,20 @@ +// Copyright 2024 Forecasting Technologies LTD. +// +// This file is part of Zeitgeist. +// +// Zeitgeist is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// Zeitgeist is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Zeitgeist. If not, see . + +mod market_builder; + +pub use market_builder::*;