Skip to content

Commit

Permalink
Tightly coupled market-commons with prediction markets. (#900)
Browse files Browse the repository at this point in the history
* Remove use of MarketCommonsApi from prediction markets pallet

* Fix build

* Changes as per review comments

* Move MarketCommonsPalletApi trait to primitives and fixes as per review comment.

* Changes as per review comments

* Fix clippy warning
  • Loading branch information
vivekvpandya committed Dec 13, 2022
1 parent 7ec79a9 commit 5d73988
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 148 deletions.
2 changes: 2 additions & 0 deletions primitives/src/traits.rs
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;
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
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
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> {
<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
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

0 comments on commit 5d73988

Please sign in to comment.