Skip to content

Commit

Permalink
Sunset old AMMs and their pools (#1197)
Browse files Browse the repository at this point in the history
* Replace `bmul` and `bdiv` with traited versions

* Restructure directories

* Replace `saturating_*` from neo-swaps

* Fix formatting

* Restructure zrml-swaps math

* Implement and test `b*`

* Fix formatting

* Use new math in orderbook-v1

* Replace checked multiplication with new math

* Use correct rounding in neo-swaps

* Add docs

* Update licenses

* Remove `fixed` module from `primitives`

* Fix formatting

* .

* Rewrite math functions

* Remove training wheels

* Fix docs.pdf

* Fix quotes

* Add tests for buying

* Add tests for selling and improve error names

* Update docs

* Check adjusted amount in for numerical bounds

* Remove unused implementations

* Adjust docs

* Add stress test exploring various scenarios

* Add swap fees to stress test

* Add underscore separators

* Clean up

* Benchmark `buy` as function of `asset_count`

* Update benchmarks

* Clippy fix

* Fix benchmark tests

* Update benchmarks of zrml-prediction-markets

* Fix broken comment

* Add comment explaining benchmark spot prices

* Use clearer constants for `amount_in` in tests

* Update zrml/neo-swaps/src/traits/pool_operations.rs

Co-authored-by: Chralt <[email protected]>

* Fix botched merge

* Fix merge

* Update benchmarks

* Remove `pool_*_subsidy`

* Remove `distribute_pool_share_rewards`

* Remove `end_subsidy_phase`

* Remove `destroy_pool_in_subsidy_phase`

* Remove `MinSubsidy*`

* Remove `SubsidyProviders`

* Remove `start_subsidy`

* Rewrite `create_pool`

* Rewrite `swap_exact_amount_in`

* Rewrite `swap_exact_amount_out`

* Rewrite utility functions

* Remove Rikiddo from weight calculation

* Remove Rikiddo from zrml-swaps

* Remove unused errors

* Remove `ScoringRule::Rikiddo...`

* Remove `*SubsidyPeriod`

* Remove Rikiddo-related storage and events

* Remove automatic opening of pools

* Remove `open_pool` from prediction-markets

* Remove `Swaps::close_pool` from prediction-markets

* Remove `clean_up_pool` from prediction-markets

* Remove `clean_up_pool` from `Swaps` trait

* Remove CPMM deployment from prediction-markets

* Remove automatic arbitrage

* Move `market_account` back to prediction-markets

* Remove unused market states

* Fix fuzz tests

* Implement market migration

* Minor changes

* Fix migration behavior

* Remove creator fees from swaps

* Fix try-runtime

* Fix clippy issues

* Remove `LiquidityMining` from swaps

* Fix `get_spot_prices`

* Take first step to remove `MarketCommons` from swaps

* Remove `MarketCommons` from swaps

* Rewrite `PoolStatus`

* Move `Pool*` to swaps

* Use `Bounded*` types in `Pool`

* Finish swaps migration

* Add missing files

* Fix formatting and clippy errors

* Remove `pool_status.rs`

* Ignore doctests

* Fix fuzz tests

* Add prediciton-markets migration

* Test prediction-markets migration

* Finish tests of the market-commons migration

* Add migrations to runtime and fix various errors

* Clean up

* Clean up

* Format code

* Fix pool migration behavior

* Remove `MarketId` from swaps

* Fix formatting

* Fix formatting

* Remove `CPMM` and allow other scoring rules on Battery Station

* Update macros/Cargo.toml

Co-authored-by: Harald Heckmann <[email protected]>

* Update primitives/src/traits/zeitgeist_asset.rs

Co-authored-by: Harald Heckmann <[email protected]>

* Update zrml/market-commons/src/migrations.rs

Co-authored-by: Harald Heckmann <[email protected]>

* Update zrml/swaps/src/migrations.rs

Co-authored-by: Harald Heckmann <[email protected]>

* Update zrml/swaps/src/migrations.rs

Co-authored-by: Harald Heckmann <[email protected]>

* Update zrml/prediction-markets/src/migrations.rs

Co-authored-by: Harald Heckmann <[email protected]>

* Update zrml/market-commons/src/migrations.rs

Co-authored-by: Harald Heckmann <[email protected]>

* Clean up TODOs/FIXMEs

* Update changelog

* Make more changes to changelog

* Clear zrml-swaps storage

* Remove cfg-if dependency

* Fix formatting

* Trigger CI

* Update copyright notices

* Update docs/changelog_for_devs.md

Co-authored-by: Chralt <[email protected]>

* Make benchmark helper only available if feature flags are set

* Remove `ZeitgeistAsset` trait

* Remove preliminary benchmarks with more steps

* Format code

* Fix copyright notice

---------

Co-authored-by: Chralt <[email protected]>
Co-authored-by: Harald Heckmann <[email protected]>
  • Loading branch information
3 people authored Jan 8, 2024
1 parent 3761f96 commit 9e1cc2c
Show file tree
Hide file tree
Showing 83 changed files with 2,396 additions and 8,641 deletions.
11 changes: 7 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
default-members = [
"macros",
"node",
"primitives",
"runtime/common",
Expand All @@ -23,6 +24,7 @@ default-members = [
"zrml/styx",
]
members = [
"macros",
"node",
"primitives",
"runtime/common",
Expand Down Expand Up @@ -228,6 +230,7 @@ zrml-swaps-rpc = { path = "zrml/swaps/rpc" }

# Zeitgeist (wasm)
common-runtime = { path = "runtime/common", default-features = false }
zeitgeist-macros = { path = "macros", default-features = false }
zeitgeist-primitives = { path = "primitives", default-features = false }
zrml-authorized = { path = "zrml/authorized", default-features = false }
zrml-court = { path = "zrml/court", default-features = false }
Expand Down
73 changes: 73 additions & 0 deletions docs/changelog_for_devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,79 @@ As of 0.3.9, the changelog's format is based on
components which query the chain's storage, the extrinsics or the runtime
APIs/RPC interface.

## v0.5.0

[#1197]: https://github.com/zeitgeistpm/zeitgeist/pull/1197
[#1178]: https://github.com/zeitgeistpm/zeitgeist/pull/1178

### Changes

- ⚠️ Move the `zeitgeist_primitives::Pool` struct to `zrml_swaps::types::Pool` and change the following fields ([#1197]):
- Remove `market_id`
- Make `swap_fee` non-optional
- Remove `total_subsidy`
- Make `total_weight` non-optional
- Make `weights` non-optional
- ⚠️ Change the type of `liquidity_shares_manager` in `zrml_neo_swaps::types::Pool` from `zrml_neo_swaps::types::SoloLp` to `zrml_neo_swaps::types::LiquidityTree`. Details on the liquidity tree can be found in the `README.md` of zrml-neo-swaps and the documentation of the `LiquidityTree` object ([#1179]).

### Migrations

- Closed all CPMM pools. Withdrawals are still allowed. Creating new pools will
be impossible until further updates are deployed. ([#1197])
- Remove all Rikiddo storage elements. ([#1197])
- Migrate neo-swaps `Pools` storage. The market creator's liquidity position is translated into a position in the liquidity tree of the same value ([#1178]).

### Removed

- ⚠️ Remove the `Disputes` storage element from zrml-prediction-markets.
([#1197])
- ⚠️ Remove the following extrinsics from zrml-prediction-markets. ([#1197]):
- `create_cpmm_market_and_deploy_assets`
- `deploy_swap_pool_and_additional_liquidity`
- `deploy_swap_pool_for_market`
- ⚠️ Remove the following config values from zrml-prediction-markets ([#1197]):
- `MaxSubsidyPeriod`
- `MinSubsidyPeriod`
- `Swaps`
- Remove automatic arbitrage for CPMM pools. ([#1197])
- ⚠️ Remove the following extrinsics from zrml-swaps ([#1197]):
- `admin_clean_up_pool`
- `pool_exit_subsidy`
- `pool_join_subsidy`
- ⚠️ Remove the following config values from zrml-swaps ([#1197]):
- `FixedTypeU`
- `FixedTypeS`
- `LiquidityMining`
- `MarketCommons`
- `MinSubsidy`
- `MinSubsidyPerAccount`
- `RikiddoSigmoidFeeMarketEma`
- ⚠️ Remove `CPMM` and `RikiddoSigmoidFeeMarketEma` from `ScoringRule`.
([#1197])
- ⚠️ Remove `Suspended`, `CollectingSubsidy` and `InsufficientSubsidy` from
`MarketStatus`. ([#1197])

### Deprecate

- ⚠️ Deprecate the following storage elements of zrml-prediction-markets (will
be removed in v0.5.1; [#1197]):
- `MarketIdsPerOpenBlock`
- `MarketIdsPerOpenTimeFrame`
- `MarketsCollectingSubsidy`
- ⚠️ Deprecate the following storage elements of zrml-market-commons (will be
removed at an unspecified point in time; [#1197]):
- `MarketPool`
- ⚠️ Deprecate the following storage elements of zrml-swaps (will be removed in
v0.5.1; [#1197]):
- `SubsidyProviders`
- `PoolsCachedForArbitrage`

## v0.4.3

### Removed

- Remove old storage migrations

## v0.4.2

[#1127]: https://github.com/zeitgeistpm/zeitgeist/pull/1127
Expand Down
5 changes: 5 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = ["Zeitgeist PM <[email protected]>"]
edition = "2021"
name = "zeitgeist-macros"
version = "0.4.3"
34 changes: 34 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023-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/>.

/// Creates an `alloc::collections::BTreeMap` from the pattern `{ key => value, ... }`.
///
/// ```ignore
/// // Example:
/// let m = create_b_tree_map!({ 0 => 1, 2 => 3 });
/// assert_eq!(m[2], 3);
///
/// // Overwriting a key:)
/// let m = create_b_tree_map!({ 0 => "foo", 0 => "bar" });
/// assert_eq!(m[0], "bar");
/// ```
#[macro_export]
macro_rules! create_b_tree_map {
({ $($key:expr => $value:expr),* $(,)? } $(,)?) => {
[$(($key, $value),)*].iter().cloned().collect::<alloc::collections::BTreeMap<_, _>>()
}
}
1 change: 1 addition & 0 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typenum = { workspace = true }
[features]
default = ["std"]
mock = []
runtime-benchmarks = []
std = [
"frame-support/std",
"frame-system/std",
Expand Down
22 changes: 20 additions & 2 deletions primitives/src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2023 Forecasting Technologies LTD.
// Copyright 2022-2024 Forecasting Technologies LTD.
// Copyright 2021-2022 Zeitgeist PM LLC.
//
// This file is part of Zeitgeist.
Expand All @@ -16,7 +16,12 @@
// 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::{CategoryIndex, PoolId, SerdeWrapper};
#[cfg(feature = "runtime-benchmarks")]
use crate::traits::ZeitgeistAssetEnumerator;
use crate::{
traits::PoolSharesId,
types::{CategoryIndex, PoolId, SerdeWrapper},
};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

Expand Down Expand Up @@ -53,6 +58,19 @@ pub enum Asset<MI: MaxEncodedLen> {
ParimutuelShare(MI, CategoryIndex),
}

impl<MI: MaxEncodedLen> PoolSharesId<SerdeWrapper<PoolId>> for Asset<MI> {
fn pool_shares_id(pool_id: SerdeWrapper<PoolId>) -> Self {
Self::PoolShare(pool_id)
}
}

#[cfg(feature = "runtime-benchmarks")]
impl<MI: MaxEncodedLen> ZeitgeistAssetEnumerator<MI> for Asset<MI> {
fn create_asset_id(t: MI) -> Self {
Asset::CategoricalOutcome(t, 0)
}
}

/// In a scalar market, users can either choose a `Long` position,
/// meaning that they think the outcome will be closer to the upper bound
/// or a `Short` position meaning that they think the outcome will be closer
Expand Down
8 changes: 1 addition & 7 deletions primitives/src/constants/mock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2023 Forecasting Technologies LTD.
// Copyright 2022-2024 Forecasting Technologies LTD.
// Copyright 2022 Zeitgeist PM LLC.
//
// This file is part of Zeitgeist.
Expand Down Expand Up @@ -97,13 +97,9 @@ parameter_types! {
pub const MaxMarketLifetime: BlockNumber = 100_000_000_000;
pub const MaxOracleDuration: BlockNumber = 30;
pub const MaxRejectReasonLen: u32 = 1024;
// 2_678_400_000 = 31 days.
pub const MaxSubsidyPeriod: Moment = 2_678_400_000;
pub const MinCategories: u16 = 2;
pub const MinDisputeDuration: BlockNumber = 2;
pub const MinOracleDuration: BlockNumber = 2;
// 60_000 = 1 minute. Should be raised to something more reasonable in the future.
pub const MinSubsidyPeriod: Moment = 60_000;
pub const OracleBond: Balance = 50 * CENT;
pub const OutsiderBond: Balance = 2 * OracleBond::get();
pub const PmPalletId: PalletId = PalletId(*b"zge/pred");
Expand All @@ -129,8 +125,6 @@ parameter_types! {
pub const MaxSwapFee: Balance = BASE / 10; // 10%
pub const MaxTotalWeight: Balance = 50 * BASE;
pub const MaxWeight: Balance = 50 * BASE;
pub const MinSubsidy: Balance = 100 * BASE;
pub const MinSubsidyPerAccount: Balance = MinSubsidy::get();
pub const MinWeight: Balance = BASE;
pub const SwapsPalletId: PalletId = PalletId(*b"zge/swap");
}
Expand Down
4 changes: 1 addition & 3 deletions primitives/src/lib.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 @@ -26,8 +26,6 @@ mod market;
pub mod math;
mod max_runtime_usize;
mod outcome_report;
mod pool;
mod pool_status;
mod proxy_type;
mod serde_wrapper;
pub mod traits;
Expand Down
32 changes: 11 additions & 21 deletions primitives/src/market.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2023 Forecasting Technologies LTD.
// Copyright 2022-2024 Forecasting Technologies LTD.
// Copyright 2021-2022 Zeitgeist PM LLC.
//
// This file is part of Zeitgeist.
Expand All @@ -16,7 +16,7 @@
// 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::{pool::ScoringRule, types::OutcomeReport};
use crate::types::OutcomeReport;
use alloc::vec::Vec;
use core::ops::{Range, RangeInclusive};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
Expand Down Expand Up @@ -71,10 +71,7 @@ pub struct Market<AI, BA, BN, M, A> {
impl<AI, BA, BN, M, A> Market<AI, BA, BN, M, A> {
pub fn resolution_mechanism(&self) -> ResolutionMechanism {
match self.scoring_rule {
ScoringRule::CPMM
| ScoringRule::Lmsr
| ScoringRule::Orderbook
| ScoringRule::RikiddoSigmoidFeeMarketEma => ResolutionMechanism::RedeemTokens,
ScoringRule::Lmsr | ScoringRule::Orderbook => ResolutionMechanism::RedeemTokens,
ScoringRule::Parimutuel => ResolutionMechanism::Noop,
}
}
Expand Down Expand Up @@ -220,14 +217,6 @@ pub struct GlobalDisputeItem<AccountId, Balance> {
pub initial_vote_amount: Balance,
}

// TODO to remove, when Disputes storage item is removed
#[derive(Clone, Decode, Encode, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub struct OldMarketDispute<AccountId, BlockNumber> {
pub at: BlockNumber,
pub by: AccountId,
pub outcome: OutcomeReport,
}

#[derive(Clone, Decode, Encode, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub struct MarketDispute<AccountId, BlockNumber, Balance> {
pub at: BlockNumber,
Expand Down Expand Up @@ -294,6 +283,13 @@ pub struct Deadlines<BN> {
pub dispute_duration: BN,
}

#[derive(TypeInfo, Clone, Copy, Encode, Eq, Decode, MaxEncodedLen, PartialEq, RuntimeDebug)]
pub enum ScoringRule {
Lmsr,
Orderbook,
Parimutuel,
}

/// Defines the state of the market.
#[derive(Clone, Copy, Decode, Encode, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub enum MarketStatus {
Expand All @@ -302,14 +298,8 @@ pub enum MarketStatus {
Proposed,
/// Trading on the market is active.
Active,
/// Trading on the market is temporarily paused.
Suspended,
/// Trading on the market has concluded.
Closed,
/// The market is collecting subsidy.
CollectingSubsidy,
/// The market was discarded due to insufficient subsidy.
InsufficientSubsidy,
/// The market has been reported.
Reported,
/// The market outcome is being disputed.
Expand Down Expand Up @@ -439,7 +429,7 @@ mod tests {
oracle_duration: 1_u32,
dispute_duration: 1_u32,
},
scoring_rule: ScoringRule::CPMM,
scoring_rule: ScoringRule::Lmsr,
status: MarketStatus::Active,
report: None,
resolved_outcome: None,
Expand Down
6 changes: 3 additions & 3 deletions primitives/src/math/fixed.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 @@ -197,13 +197,13 @@ where
}

fn bmul_bdiv_floor(&self, _multiplier: Self, _divisor: Self) -> Result<Self, DispatchError> {
// FIXME Untested!
// TODO(#1217): Commented mplementation below should work, but remains untested!
// bmul_bdiv_common(self, multiplier, divisor, Zero::zero())
Err(DispatchError::Other("not implemented"))
}

fn bmul_bdiv_ceil(&self, _multiplier: Self, _divisor: Self) -> Result<Self, DispatchError> {
// FIXME Untested!
// TODO(#1217): Commented mplementation below should work, but remains untested!
// let adjustment = ZeitgeistBase::<T>::get()?.checked_sub_res(&1u8.into())?;
// bmul_bdiv_common(self, multiplier, divisor, adjustment)
Err(DispatchError::Other("not implemented"))
Expand Down
Loading

0 comments on commit 9e1cc2c

Please sign in to comment.