Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add market ID to Market struct #1248

Merged
merged 10 commits into from
Feb 19, 2024
Merged
15 changes: 10 additions & 5 deletions primitives/src/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ use sp_runtime::RuntimeDebug;
/// * `BN`: Block number
/// * `M`: Moment (time moment)
/// * `A`: Asset
/// * `MI`: Market ID
#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct Market<AI, BA, BN, M, A> {
pub struct Market<AI, BA, BN, M, A, MI> {
pub market_id: MI,
/// Base asset of the market.
pub base_asset: A,
/// Creator of this market.
Expand Down Expand Up @@ -68,7 +70,7 @@ pub struct Market<AI, BA, BN, M, A> {
pub early_close: Option<EarlyClose<BN, M>>,
}

impl<AI, BA, BN, M, A> Market<AI, BA, BN, M, A> {
impl<AI, BA, BN, M, A, MI> Market<AI, BA, BN, M, A, MI> {
pub fn resolution_mechanism(&self) -> ResolutionMechanism {
match self.scoring_rule {
ScoringRule::Lmsr | ScoringRule::Orderbook => ResolutionMechanism::RedeemTokens,
Expand Down Expand Up @@ -139,7 +141,7 @@ impl<AI, BA> Default for MarketBonds<AI, BA> {
}
}

impl<AI, BA, BN, M, A> Market<AI, BA, BN, M, A> {
impl<AI, BA, BN, M, A, MI> Market<AI, BA, BN, M, A, MI> {
// Returns the number of outcomes for a market.
pub fn outcomes(&self) -> u16 {
match self.market_type {
Expand All @@ -165,16 +167,18 @@ impl<AI, BA, BN, M, A> Market<AI, BA, BN, M, A> {
}
}

impl<AI, BA, BN, M, A> MaxEncodedLen for Market<AI, BA, BN, M, A>
impl<AI, BA, BN, M, A, MI> MaxEncodedLen for Market<AI, BA, BN, M, A, MI>
where
AI: MaxEncodedLen,
BA: MaxEncodedLen,
BN: MaxEncodedLen,
M: MaxEncodedLen,
A: MaxEncodedLen,
MI: MaxEncodedLen,
{
fn max_encoded_len() -> usize {
AI::max_encoded_len()
.saturating_add(MI::max_encoded_len())
.saturating_add(A::max_encoded_len())
.saturating_add(MarketCreation::max_encoded_len())
.saturating_add(Perbill::max_encoded_len())
Expand Down Expand Up @@ -347,7 +351,7 @@ pub enum ResolutionMechanism {
mod tests {
use crate::{market::*, types::Asset};
use test_case::test_case;
type Market = crate::market::Market<u32, u32, u32, u32, Asset<u32>>;
type Market = crate::market::Market<u32, u32, u32, u32, Asset<u32>, u32>;

#[test_case(
MarketType::Categorical(6),
Expand Down Expand Up @@ -403,6 +407,7 @@ mod tests {
expected: bool,
) {
let market = Market {
market_id: 9,
base_asset: Asset::Ztg,
creator: 1,
creation: MarketCreation::Permissionless,
Expand Down
18 changes: 10 additions & 8 deletions primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ mod complete_set_operations_api;
mod deploy_pool_api;
mod dispute_api;
mod distribute_fees;
mod market_builder;
mod market_commons_pallet_api;
mod market_id;
mod swaps;
mod zeitgeist_asset;
mod zeitgeist_multi_reservable_currency;

pub use complete_set_operations_api::CompleteSetOperationsApi;
pub use deploy_pool_api::DeployPoolApi;
pub use dispute_api::{DisputeApi, DisputeMaxWeightApi, DisputeResolutionApi};
pub use distribute_fees::DistributeFees;
pub use market_commons_pallet_api::MarketCommonsPalletApi;
pub use market_id::MarketId;
pub use swaps::Swaps;
pub use complete_set_operations_api::*;
pub use deploy_pool_api::*;
pub use dispute_api::*;
pub use distribute_fees::*;
pub use market_builder::*;
pub use market_commons_pallet_api::*;
pub use market_id::*;
pub use swaps::*;
pub use zeitgeist_asset::*;
pub use zeitgeist_multi_reservable_currency::ZeitgeistAssetManager;
pub use zeitgeist_multi_reservable_currency::*;
8 changes: 5 additions & 3 deletions primitives/src/traits/dispute_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Forecasting Technologies LTD.
// Copyright 2023-2024 Forecasting Technologies LTD.
// Copyright 2021-2022 Zeitgeist PM LLC.
//
// This file is part of Zeitgeist.
Expand Down Expand Up @@ -29,12 +29,13 @@ use sp_runtime::DispatchError;

// Abstraction of the market type, which is not a part of `DisputeApi` because Rust doesn't support
// type aliases in traits.
type MarketOfDisputeApi<T> = Market<
pub type MarketOfDisputeApi<T> = Market<
<T as DisputeApi>::AccountId,
<T as DisputeApi>::Balance,
<T as DisputeApi>::BlockNumber,
<T as DisputeApi>::Moment,
Asset<<T as DisputeApi>::MarketId>,
<T as DisputeApi>::MarketId,
>;

type GlobalDisputeItemOfDisputeApi<T> =
Expand Down Expand Up @@ -145,12 +146,13 @@ pub trait DisputeMaxWeightApi {
fn clear_max_weight() -> Weight;
}

type MarketOfDisputeResolutionApi<T> = Market<
pub type MarketOfDisputeResolutionApi<T> = Market<
<T as DisputeResolutionApi>::AccountId,
<T as DisputeResolutionApi>::Balance,
<T as DisputeResolutionApi>::BlockNumber,
<T as DisputeResolutionApi>::Moment,
Asset<<T as DisputeResolutionApi>::MarketId>,
<T as DisputeResolutionApi>::MarketId,
>;

pub trait DisputeResolutionApi {
Expand Down
61 changes: 61 additions & 0 deletions primitives/src/traits/market_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 <https://www.gnu.org/licenses/>.

use crate::types::{
Deadlines, EarlyClose, Market, MarketBonds, MarketCreation, MarketDisputeMechanism,
MarketPeriod, MarketStatus, MarketType, OutcomeReport, Report, ScoringRule,
};
use alloc::vec::Vec;
use sp_runtime::{DispatchError, Perbill};

macro_rules! builder_methods {
($($field:ident: $type:ty),*) => {
$(fn $field(&mut self, $field: $type) -> &mut Self;)*
}
}

/// Mutably referenced builder struct for the `Market` object. The `build` call is pass-by-value, so
/// the usual calling pattern is:
///
/// ```ignore
/// let builder = MarketBuilderImpl::new(args...);
/// builder.field1(value1).field2(value2);
/// builder.clone().build()
/// ```
pub trait MarketBuilder<AI, BA, BN, M, A, MI> {
fn build(self) -> Result<Market<AI, BA, BN, M, A, MI>, DispatchError>;

builder_methods! {
market_id: MI,
base_asset: A,
creator: AI,
creation: MarketCreation,
creator_fee: Perbill,
oracle: AI,
metadata: Vec<u8>,
market_type: MarketType,
period: MarketPeriod<BN, M>,
deadlines: Deadlines<BN>,
scoring_rule: ScoringRule,
status: MarketStatus,
report: Option<Report<AI, BN>>,
resolved_outcome: Option<OutcomeReport>,
dispute_mechanism: Option<MarketDisputeMechanism>,
bonds: MarketBonds<AI, BA>,
early_close: Option<EarlyClose<BN, M>>
}
}
36 changes: 32 additions & 4 deletions primitives/src/traits/market_commons_pallet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

#![allow(clippy::type_complexity)]

use crate::types::{Asset, Market, PoolId};
use crate::{
traits::MarketBuilder,
types::{Asset, Market, PoolId},
};
use frame_support::{
dispatch::{fmt::Debug, DispatchError, DispatchResult},
pallet_prelude::{MaybeSerializeDeserialize, Member},
Expand All @@ -30,12 +33,14 @@ use sp_runtime::traits::{AtLeast32Bit, AtLeast32BitUnsigned};

// Abstraction of the market type, which is not a part of `MarketCommonsPalletApi` because Rust
// doesn't support type aliases in traits.
type MarketOf<T> = Market<
type AssetOf<T> = Asset<<T as MarketCommonsPalletApi>::MarketId>;
pub type MarketOf<T> = Market<
<T as MarketCommonsPalletApi>::AccountId,
<T as MarketCommonsPalletApi>::Balance,
<T as MarketCommonsPalletApi>::BlockNumber,
<T as MarketCommonsPalletApi>::Moment,
Asset<<T as MarketCommonsPalletApi>::MarketId>,
AssetOf<T>,
<T as MarketCommonsPalletApi>::MarketId,
>;

/// Abstraction over storage operations for markets
Expand Down Expand Up @@ -78,9 +83,32 @@ pub trait MarketCommonsPalletApi {
where
F: FnOnce(&mut MarketOf<Self>) -> DispatchResult;

/// Pushes a new market into the storage, returning its related auto-incremented ID.
/// Add a `market` to the API's list of markets, overwrite its `market_id` field with a new ID
/// and return the market's new ID.
///
/// Deprecated since v0.5.1. For testing purposes only; use `build_market` in production.
fn push_market(market: MarketOf<Self>) -> Result<Self::MarketId, DispatchError>;

/// Equips a market with a market ID, writes the market to storage and then returns the ID and
/// the built market.
///
/// This function is the only public means by which new IDs are issued. The market's `market_id`
/// field is expected to be `None`. If that's not the case, this function will raise an error to
/// avoid double-writes, which are always the result of an incorrect issuance process for market
/// IDs.
fn build_market<U>(
market_builder: U,
) -> Result<(Self::MarketId, MarketOf<Self>), DispatchError>
where
U: MarketBuilder<
Self::AccountId,
Self::Balance,
Self::BlockNumber,
Self::Moment,
AssetOf<Self>,
Self::MarketId,
>;

/// Removes a market from the storage.
fn remove_market(market_id: &Self::MarketId) -> DispatchResult;

Expand Down
Loading
Loading