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

Allow MDMs to directly resolve the market #860

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod market_id;
mod swaps;
mod zeitgeist_multi_reservable_currency;

pub use dispute_api::DisputeApi;
pub use dispute_api::{DisputeApi, DisputeResolutionApi};
pub use market_id::MarketId;
pub use swaps::Swaps;
pub use zeitgeist_multi_reservable_currency::ZeitgeistAssetManager;
20 changes: 20 additions & 0 deletions primitives/src/traits/dispute_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,23 @@ pub trait DisputeApi {
market: &Market<Self::AccountId, Self::BlockNumber, Self::Moment>,
) -> Result<Option<OutcomeReport>, DispatchError>;
}

pub trait DisputeResolutionApi {
type AccountId;
type BlockNumber;
type MarketId;
type Moment;

/// Resolve a market. Fails if `on_resolution` from zrml-prediction-markets fails.
///
/// **Should only be called if the market dispute**
/// **mechanism is ready for the resolution (`DisputeApi::on_resolution`).**
///
/// # Returns
///
/// Returns the consumed weight.
fn resolve(
market_id: &Self::MarketId,
market: &Market<Self::AccountId, Self::BlockNumber, Self::Moment>,
) -> Result<u64, DispatchError>;
}
1 change: 1 addition & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ macro_rules! impl_config_traits {

impl zrml_authorized::Config for Runtime {
type Event = Event;
type DisputeResolution = zrml_prediction_markets::Pallet<Runtime>;
type MarketCommons = MarketCommons;
type PalletId = AuthorizedPalletId;
type WeightInfo = zrml_authorized::weights::WeightInfo<Runtime>;
Expand Down
11 changes: 10 additions & 1 deletion zrml/authorized/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod pallet {
use frame_system::{ensure_signed, pallet_prelude::OriginFor};
use sp_runtime::DispatchError;
use zeitgeist_primitives::{
traits::DisputeApi,
traits::{DisputeApi, DisputeResolutionApi},
types::{Market, MarketDispute, MarketDisputeMechanism, MarketStatus, OutcomeReport},
};
use zrml_market_commons::MarketCommonsPalletApi;
Expand Down Expand Up @@ -82,6 +82,8 @@ mod pallet {
return Err(Error::<T>::MarketDoesNotHaveDisputeMechanismAuthorized.into());
}
AuthorizedOutcomeReports::<T>::insert(market_id, outcome);
// TODO(#851): Allow a small correction period (if authority made a mistake)!
let _resolution_weight = T::DisputeResolution::resolve(&market_id, &market)?;
Ok(())
}
}
Expand All @@ -91,6 +93,13 @@ mod pallet {
/// Event
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

type DisputeResolution: DisputeResolutionApi<
AccountId = Self::AccountId,
BlockNumber = Self::BlockNumber,
MarketId = MarketIdOf<Self>,
Moment = MomentOf<Self>,
>;

/// Market commons
type MarketCommons: MarketCommonsPalletApi<
AccountId = Self::AccountId,
Expand Down
23 changes: 21 additions & 2 deletions zrml/authorized/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
#![cfg(test)]

use crate::{self as zrml_authorized};
use frame_support::{construct_runtime, traits::Everything};
use frame_support::{construct_runtime, pallet_prelude::DispatchError, traits::Everything};
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};
use zeitgeist_primitives::{
constants::mock::{AuthorizedPalletId, BlockHashCount, MaxReserves, MinimumPeriod, BASE},
traits::DisputeResolutionApi,
types::{
AccountIdTest, Balance, BlockNumber, BlockTest, Hash, Index, MarketId, Moment,
AccountIdTest, Balance, BlockNumber, BlockTest, Hash, Index, Market, MarketId, Moment,
UncheckedExtrinsicTest,
},
};
Expand All @@ -50,8 +51,26 @@ construct_runtime!(
}
);

// NoopResolution implements DisputeResolutionApi with no-ops.
pub struct NoopResolution;

impl DisputeResolutionApi for NoopResolution {
type AccountId = AccountIdTest;
type BlockNumber = BlockNumber;
type MarketId = MarketId;
type Moment = Moment;

fn resolve(
_market_id: &Self::MarketId,
_market: &Market<Self::AccountId, Self::BlockNumber, Self::Moment>,
) -> Result<u64, DispatchError> {
Ok(0)
}
}

impl crate::Config for Runtime {
type Event = ();
type DisputeResolution = NoopResolution;
type MarketCommons = MarketCommons;
type PalletId = AuthorizedPalletId;
type WeightInfo = crate::weights::WeightInfo<Runtime>;
Expand Down
58 changes: 0 additions & 58 deletions zrml/prediction-markets/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ benchmarks! {
admin_destroy_disputed_market{
// The number of assets.
let a in (T::MinCategories::get().into())..T::MaxCategories::get().into();
// The number of disputes.
let d in 1..T::MaxDisputes::get();
// The number of market ids per open time frame.
let o in 0..63;
// The number of market ids per close time frame.
Expand Down Expand Up @@ -269,16 +267,6 @@ benchmarks! {
).unwrap();
}

let disputes = Disputes::<T>::get(market_id);
let last_dispute = disputes.last().unwrap();
let dispute_at = last_dispute.at;
for i in 0..r {
MarketIdsPerDisputeBlock::<T>::try_mutate(
dispute_at,
|ids| ids.try_push(i.into()),
).unwrap();
}

let destroy_origin = T::DestroyOrigin::successful_origin();
let call = Call::<T>::admin_destroy_market { market_id };
}: {
Expand Down Expand Up @@ -443,7 +431,6 @@ benchmarks! {

admin_move_market_to_resolved_scalar_disputed {
let r in 0..63;
let d in 1..T::MaxDisputes::get();

let (_, market_id) = create_close_and_report_market::<T>(
MarketCreation::Permissionless,
Expand All @@ -464,28 +451,6 @@ benchmarks! {
panic!("Must create scalar market");
}

for i in 1..=d {
let outcome = OutcomeReport::Scalar(i.saturated_into());
let disputor = account("disputor", i, 0);
let dispute_bond = crate::pallet::default_dispute_bond::<T>(i as usize);
T::AssetManager::deposit(
Asset::Ztg,
&disputor,
dispute_bond,
)?;
Pallet::<T>::dispute(RawOrigin::Signed(disputor).into(), market_id, outcome)?;
}
let disputes = Disputes::<T>::get(market_id);

let last_dispute = disputes.last().unwrap();
let dispute_at = last_dispute.at;
for i in 0..r {
MarketIdsPerDisputeBlock::<T>::try_mutate(
dispute_at,
|ids| ids.try_push(i.into()),
).unwrap();
}

let close_origin = T::CloseOrigin::successful_origin();
let call = Call::<T>::admin_move_market_to_resolved { market_id };
}: {
Expand All @@ -500,7 +465,6 @@ benchmarks! {

admin_move_market_to_resolved_categorical_disputed {
let r in 0..63;
let d in 1..T::MaxDisputes::get();

let categories = T::MaxCategories::get();
let (caller, market_id) =
Expand All @@ -515,28 +479,6 @@ benchmarks! {
Ok(())
})?;

for i in 1..=d {
let outcome = OutcomeReport::Categorical((i % 2).saturated_into::<u16>());
let disputor = account("disputor", i, 0);
let dispute_bond = crate::pallet::default_dispute_bond::<T>(i as usize);
T::AssetManager::deposit(
Asset::Ztg,
&disputor,
dispute_bond,
)?;
Pallet::<T>::dispute(RawOrigin::Signed(disputor).into(), market_id, outcome)?;
}
let disputes = Disputes::<T>::get(market_id);

let last_dispute = disputes.last().unwrap();
let dispute_at = last_dispute.at;
for i in 0..r {
MarketIdsPerDisputeBlock::<T>::try_mutate(
dispute_at,
|ids| ids.try_push(i.into()),
).unwrap();
}

let close_origin = T::CloseOrigin::successful_origin();
let call = Call::<T>::admin_move_market_to_resolved { market_id };
}: {
Expand Down
Loading