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

Tightly coupled market-commons with prediction markets. #900

Merged
merged 8 commits into from
Dec 13, 2022
2 changes: 2 additions & 0 deletions primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

mod dispute_api;
mod market_commons_pallet_api;
mod market_id;
mod swaps;
mod zeitgeist_multi_reservable_currency;

pub use dispute_api::DisputeApi;
pub use market_commons_pallet_api::MarketCommonsPalletApi;
pub use market_id::MarketId;
pub use swaps::Swaps;
pub use zeitgeist_multi_reservable_currency::ZeitgeistAssetManager;
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

#![allow(clippy::type_complexity)]
use crate::types::{Market, PoolId};
use frame_support::{
dispatch::{DispatchError, DispatchResult},
pallet_prelude::{MaybeSerializeDeserialize, Member},
Expand All @@ -25,7 +26,6 @@ use frame_support::{
};
use parity_scale_codec::MaxEncodedLen;
use sp_runtime::traits::AtLeast32Bit;
use zeitgeist_primitives::types::{Market, PoolId};

/// Simple disputes - Pallet Api
pub trait MarketCommonsPalletApi {
Expand Down
1 change: 0 additions & 1 deletion runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,6 @@ macro_rules! impl_config_traits {
// NoopLiquidityMining will be applied only to mainnet once runtimes are separated.
type LiquidityMining = NoopLiquidityMining;
// type LiquidityMining = LiquidityMining;
type MarketCommons = MarketCommons;
type MaxCategories = MaxCategories;
type MaxDisputes = MaxDisputes;
type MinDisputeDuration = MinDisputeDuration;
Expand Down
36 changes: 13 additions & 23 deletions zrml/market-commons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@

extern crate alloc;

mod market_commons_pallet_api;
pub mod migrations;
mod mock;
mod tests;

pub use market_commons_pallet_api::MarketCommonsPalletApi;
pub use pallet::*;
pub use zeitgeist_primitives::traits::MarketCommonsPalletApi;

#[frame_support::pallet]
mod pallet {
Expand Down Expand Up @@ -55,6 +54,12 @@ mod pallet {

pub type MomentOf<T> = <<T as Config>::Timestamp as frame_support::traits::Time>::Moment;

type MarketOf<T> = Market<
<T as frame_system::Config>::AccountId,
<T as frame_system::Config>::BlockNumber,
MomentOf<T>,
>;

#[pallet::call]
impl<T: Config> Pallet<T> {}

Expand Down Expand Up @@ -115,7 +120,7 @@ mod pallet {
// on the storage so next following calls will return yet another incremented number.
//
// Returns `Err` if `MarketId` addition overflows.
fn next_market_id() -> Result<T::MarketId, DispatchError> {
pub fn next_market_id() -> Result<T::MarketId, DispatchError> {
let id = MarketCounter::<T>::get();
let new_counter = id.checked_add(&1u8.into()).ok_or(ArithmeticError::Overflow)?;
<MarketCounter<T>>::put(new_counter);
Expand Down Expand Up @@ -145,25 +150,17 @@ mod pallet {
}
}

fn market_iter() -> PrefixIterator<(
Self::MarketId,
Market<Self::AccountId, Self::BlockNumber, Self::Moment>,
)> {
fn market_iter() -> PrefixIterator<(Self::MarketId, MarketOf<T>)> {
<Markets<T>>::iter()
}

fn market(
market_id: &Self::MarketId,
) -> Result<Market<Self::AccountId, Self::BlockNumber, Self::Moment>, DispatchError>
{
fn market(market_id: &Self::MarketId) -> Result<MarketOf<T>, DispatchError> {
Chralt98 marked this conversation as resolved.
Show resolved Hide resolved
<Markets<T>>::try_get(market_id).map_err(|_err| Error::<T>::MarketDoesNotExist.into())
}

fn mutate_market<F>(market_id: &Self::MarketId, cb: F) -> DispatchResult
where
F: FnOnce(
&mut Market<Self::AccountId, Self::BlockNumber, Self::Moment>,
) -> DispatchResult,
F: FnOnce(&mut MarketOf<T>) -> DispatchResult,
{
<Markets<T>>::try_mutate(market_id, |opt| {
if let Some(market) = opt {
Expand All @@ -174,9 +171,7 @@ mod pallet {
})
}

fn push_market(
market: Market<Self::AccountId, Self::BlockNumber, Self::Moment>,
) -> Result<Self::MarketId, DispatchError> {
fn push_market(market: MarketOf<T>) -> Result<Self::MarketId, DispatchError> {
let market_id = Self::next_market_id()?;
<Markets<T>>::insert(market_id, market);
Ok(market_id)
Expand Down Expand Up @@ -228,12 +223,7 @@ mod pallet {

/// Holds all markets
#[pallet::storage]
pub type Markets<T: Config> = StorageMap<
_,
Blake2_128Concat,
T::MarketId,
Market<T::AccountId, T::BlockNumber, MomentOf<T>>,
>;
pub type Markets<T: Config> = StorageMap<_, Blake2_128Concat, T::MarketId, MarketOf<T>>;

/// The number of markets that have been created (including removed markets) and the next
/// identifier for a created market.
Expand Down
10 changes: 6 additions & 4 deletions zrml/market-commons/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
#![cfg(test)]

use crate::{
market_commons_pallet_api::MarketCommonsPalletApi,
mock::{ExtBuilder, MarketCommons, Runtime},
MarketCounter, Markets,
};
use frame_support::{assert_err, assert_noop, assert_ok};
use sp_runtime::DispatchError;
use zeitgeist_primitives::types::{
AccountIdTest, BlockNumber, Deadlines, Market, MarketCreation, MarketDisputeMechanism,
MarketPeriod, MarketStatus, MarketType, Moment, ScoringRule,
use zeitgeist_primitives::{
traits::MarketCommonsPalletApi,
types::{
AccountIdTest, BlockNumber, Deadlines, Market, MarketCreation, MarketDisputeMechanism,
MarketPeriod, MarketStatus, MarketType, Moment, ScoringRule,
},
};

const MARKET_DUMMY: Market<AccountIdTest, BlockNumber, Moment> = Market {
Expand Down