Skip to content

Commit

Permalink
Implement AMM 2.0-light (#1092)
Browse files Browse the repository at this point in the history
* Implement neo-swaps

* Fix compile issues

* Implement `DeployPoolsApi` as noop

* Fix formatting

* Add missing copyright notices

* Fix clippy issues

* Fix more clippy issues and rename `balances` to `reserves`

* Remove `println!`

* Add missing copyright notice

* .

* Add benchmarking and include `NeoSwaps` in Runtime

* Add neo-swaps benchmarks; remove `split` (for now)

* Add benchmarks for `create_market_and_deploy_pool`

* Properly implement `DeployPool`

* Fix benchmarks, remove unnecessary parameter

* Fix formatting

* Fix dependencies

* Add math docs

* Fix typo

* Make me codeowner of neo-swaps

* Add neo-swaps to main README and fix link to PDF

* Fix link

* Update versions to v0.4.0 (#1098)

* Update weights (#1101)

* Remove unnecessary comment from toml

* Use default features

* Bump version

* Use checked math in `SoloLp`

* Make match expression explicit

* Add comment about saturation

* Use `MultiCurrency` instead of `ZeitgeistAssetManager`

* Apply suggestions from code review

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

* Remove superfluous inserts

* Fix error documentation

* Abstract reserve updates into `Pool`

* Make complete set operations require transactional

* Make test name not coffee-based

* Update zrml/neo-swaps/src/mock.rs

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

* Update zrml/neo-swaps/src/mock.rs

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

* Reorganize tests

* Format

* Fix comment

* Remove `macros.rs`

* Fix benchmarks

* Fix formatting

* Update benchmark script and add preliminary benchmarks

* Update ED buffer handling

* Fix documentation

* Implement market creation fees for neo-swaps

* Use `MarketCreatorFee` in runtime

* Add missing files

* Add weights

* Reduce length of `MarketsCollectingSubsidy` (#1118)

* Add bad block of the proof size fiasko to Battery Station chain spec (#1119)

Add bad block to Battery Station chain spec

* Update weights v0.4.0 (#1121)

* Update moonbeam dependencies (bench fix)

* Update weights

* Fix docs

* Rename `IndexType` and change its type value

* Remove commented code

* Fix order of config parameters

* Specify order of assets

* Remove commented code

* Apply suggestions from code review

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

* Fix `u16`/`u32` casts

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

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

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

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

* Update zrml/neo-swaps/src/math.rs

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

* Update primitives/src/math/fixed.rs

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

* Update primitives/src/math/fixed.rs

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

* Fix formatting

* Fix conflicts

* Fix dispute period

* Format code

---------

Co-authored-by: Harald Heckmann <[email protected]>
Co-authored-by: Chralt <[email protected]>
  • Loading branch information
3 people authored Oct 11, 2023
1 parent fdda998 commit 3d9bbff
Show file tree
Hide file tree
Showing 57 changed files with 6,720 additions and 130 deletions.
3 changes: 2 additions & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/zrml/authorized/ @Chralt98
/zrml/court/ @Chralt98
/zrml/global-disputes/ @Chralt98
/zrml/neo-swaps/ @maltekliemann
/zrml/prediction-markets/ @maltekliemann
/zrml/rikiddo/ @sea212
/zrml/simple-disputes/ @Chralt98
Expand All @@ -19,4 +20,4 @@

# Skip weight and Cargo.toml files
**/weights.rs
**/Cargo.toml
**/Cargo.toml
95 changes: 95 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ default-members = [
"zrml/global-disputes",
"zrml/liquidity-mining",
"zrml/market-commons",
"zrml/neo-swaps",
"zrml/orderbook-v1",
"zrml/prediction-markets",
"zrml/prediction-markets/runtime-api",
Expand All @@ -31,6 +32,7 @@ members = [
"zrml/global-disputes",
"zrml/liquidity-mining",
"zrml/market-commons",
"zrml/neo-swaps",
"zrml/orderbook-v1",
"zrml/orderbook-v1/fuzz",
"zrml/prediction-markets",
Expand Down Expand Up @@ -229,6 +231,7 @@ zrml-court = { path = "zrml/court", default-features = false }
zrml-global-disputes = { path = "zrml/global-disputes", default-features = false }
zrml-liquidity-mining = { path = "zrml/liquidity-mining", default-features = false }
zrml-market-commons = { path = "zrml/market-commons", default-features = false }
zrml-neo-swaps = { path = "zrml/neo-swaps", default-features = false }
zrml-orderbook-v1 = { path = "zrml/orderbook-v1", default-features = false }
zrml-prediction-markets = { path = "zrml/prediction-markets", default-features = false }
zrml-prediction-markets-runtime-api = { path = "zrml/prediction-markets/runtime-api", default-features = false }
Expand All @@ -250,6 +253,9 @@ url = "2.2.2"
arbitrary = { version = "1.3.0", default-features = false }
arrayvec = { version = "0.7.4", default-features = false }
cfg-if = { version = "1.0.0" }
fixed = { version = "=1.15.0", default-features = false, features = ["num-traits"] }
# Using math code directly from the HydraDX node repository as https://github.com/galacticcouncil/hydradx-math is outdated and has been archived in May 2023.
hydra-dx-math = { git = "https://github.com/galacticcouncil/HydraDX-node", package = "hydra-dx-math", tag = "v18.0.0", default-features = false }
# Hashbrown works in no_std by default and default features are used in Rikiddo
hashbrown = { version = "0.12.3", default-features = true }
hex-literal = { version = "0.3.4", default-features = false }
Expand All @@ -258,6 +264,7 @@ num-traits = { version = "0.2.15", default-features = false }
rand = { version = "0.8.5", default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }
serde = { version = "1.0.152", default-features = false }
typenum = { version = "1.15.0", default-features = false }

[profile.dev.package]
blake2 = { opt-level = 3 }
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

# Zeitgeist: An Evolving Blockchain for Prediction Markets and Futarchy

![Rust](https://github.com/zeitgeistpm/zeitgeist/workflows/Rust/badge.svg) [![Codecov](https://codecov.io/gh/zeitgeistpm/zeitgeist/branch/main/graph/badge.svg)](https://codecov.io/gh/zeitgeistpm/zeitgeist) [![Discord](https://img.shields.io/badge/discord-https%3A%2F%2Fdiscord.gg%2FMD3TbH3ctv-purple)](https://discord.gg/MD3TbH3ctv) [![Telegram](https://img.shields.io/badge/telegram-https%3A%2F%2Ft.me%2Fzeitgeist__official-blue)](https://t.me/zeitgeist_official)
![Rust](https://github.com/zeitgeistpm/zeitgeist/workflows/Rust/badge.svg)
[![Codecov](https://codecov.io/gh/zeitgeistpm/zeitgeist/branch/main/graph/badge.svg)](https://codecov.io/gh/zeitgeistpm/zeitgeist)
[![Discord](https://img.shields.io/badge/discord-https%3A%2F%2Fdiscord.gg%2FMD3TbH3ctv-purple)](https://discord.gg/MD3TbH3ctv)
[![Telegram](https://img.shields.io/badge/telegram-https%3A%2F%2Ft.me%2Fzeitgeist__official-blue)](https://t.me/zeitgeist_official)

Zeitgeist is a decentralized network for creating, betting on, and resolving
prediction markets. The platform's native currency, the ZTG, is used to sway the
direction of the network, and as a means of last-call dispute resolution.
Additionally, Zeitgeist is a protocol for efficient trading of prediction market
shares and will one day become the backbone of the decentralized finance
ecosystem by allowing traders to create complex financial contracts on
virtually _anything_.
ecosystem by allowing traders to create complex financial contracts on virtually
_anything_.

## Modules

Expand All @@ -27,6 +30,9 @@ virtually _anything_.
liquidity to swap pools.
- [market-commons](./zrml/market-commons) - Contains common operations on
markets that are used by multiple pallets.
- [neo-swaps](./zrml/neo-swaps) - An implementation of the Logarithmic Market
Scoring Rule as constant function market maker, tailor-made for decentralized
combinatorial markets and Futarchy.
- [orderbook-v1](./zrml/orderbook-v1) - A naive orderbook implementation that's
only part of Zeitgeist's PoC. Will be replaced by a v2 orderbook that uses
0x-style hybrid on-chain and off-chain trading.
Expand Down
32 changes: 32 additions & 0 deletions docs/changelog_for_devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@ 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.4.1

### Added

- Implement AMM-2.0-light in the form of zrml-neo-swaps. The new pallet has the
following dispatchables:

- `buy`: Buy outcome tokens from the specified market.
- `sell`: Sell outcome tokens to the specified market.
- `join`: Join the liquidity pool for the specified market.
- `exit`: Exit the liquidity pool for the specified market.
- `withdraw_fees`: Withdraw swap fees from the specified market.
- `deploy_pool`: Deploy a pool for the specified market and provide liquidity.

The new pallet has the following events:

- `BuyExecuted { who, market_id, asset_out, amount_in, amount_out, swap_fee_amount, external_fee_amount }`:
Informant bought a position.
- `SellExecuted { who, market_id, asset_in, amount_in, amount_out, swap_fee_amount, external_fee_amount }`:
Informants sold a position.
- `FeesWithdrawn { who }`: Liquidity provider withdrew fees.
- `JoinExecuted { who, market_id, pool_shares_amount, amounts_in, new_liquidity_parameter }`:
Liquidity provider joined the pool.
- `ExitExecuted { who, market_id, pool_shares_amount, amounts_out, new_liquidity_parameter }`:
Liquidity provider left the pool.
- `PoolDeployed { who, market_id, pool_shares_amount, amounts_in, liquidity_parameter }`:
Pool was created.
- `PoolDestroyed { who, market_id, pool_shares_amount, amounts_out }`: Pool
was destroyed.

For details, please refer to the `README.md` and the in-file documentation.

## v0.4.0

[#976]: https://github.com/zeitgeistpm/zeitgeist/pull/976
Expand Down
3 changes: 3 additions & 0 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[dependencies]
arbitrary = { workspace = true, optional = true }
fixed = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
orml-currencies = { workspace = true }
Expand All @@ -13,7 +14,9 @@ sp-core = { workspace = true }
sp-runtime = { workspace = true }

[dev-dependencies]
more-asserts = { workspace = true }
test-case = { workspace = true }
typenum = { workspace = true }

[features]
default = ["std"]
Expand Down
6 changes: 5 additions & 1 deletion primitives/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub const BLOCKS_PER_MINUTE: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as Bloc
pub const BLOCKS_PER_HOUR: BlockNumber = BLOCKS_PER_MINUTE * 60; // 300

// Definitions for currency
pub const BASE: u128 = 10_000_000_000;
pub const DECIMALS: u8 = 10;
pub const BASE: u128 = 10u128.pow(DECIMALS as u32);
pub const CENT: Balance = BASE / 100; // 100_000_000
pub const MILLI: Balance = CENT / 10; // 10_000_000
pub const MICRO: Balance = MILLI / 1000; // 10_000
Expand Down Expand Up @@ -82,6 +83,9 @@ pub const GLOBAL_DISPUTES_LOCK_ID: [u8; 8] = *b"zge/gdlk";
/// Pallet identifier, mainly used for named balance reserves.
pub const LM_PALLET_ID: PalletId = PalletId(*b"zge/lymg");

// NeoSwaps
pub const NS_PALLET_ID: PalletId = PalletId(*b"zge/neos");

// Prediction Markets
/// The maximum allowed market life time, measured in blocks.
pub const MAX_MARKET_LIFETIME: BlockNumber = 4 * BLOCKS_PER_YEAR;
Expand Down
10 changes: 8 additions & 2 deletions primitives/src/constants/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ parameter_types! {
pub const LiquidityMiningPalletId: PalletId = PalletId(*b"zge/lymg");
}

// NeoSwaps
parameter_types! {
pub storage NeoExitFee: Balance = CENT;
pub const NeoMaxSwapFee: Balance = 10 * CENT;
pub const NeoSwapsPalletId: PalletId = PalletId(*b"zge/neos");
}

// Prediction Market parameters
parameter_types! {
pub const AdvisoryBond: Balance = 25 * CENT;
Expand Down Expand Up @@ -146,11 +153,10 @@ parameter_types! {
}

parameter_type_with_key! {
// Well, not every asset is a currency ¯\_(ツ)_/¯
pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance {
match currency_id {
Asset::Ztg => ExistentialDeposit::get(),
_ => 0
_ => 10
}
};
}
Expand Down
2 changes: 2 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright 2023 Forecasting Technologies LTD.
// Copyright 2021-2022 Zeitgeist PM LLC.
//
// This file is part of Zeitgeist.
Expand All @@ -22,6 +23,7 @@ extern crate alloc;
mod asset;
pub mod constants;
mod market;
pub mod math;
mod max_runtime_usize;
mod outcome_report;
mod pool;
Expand Down
Loading

0 comments on commit 3d9bbff

Please sign in to comment.