Skip to content

Commit

Permalink
Implement Hybrid Router (#1307)
Browse files Browse the repository at this point in the history
* AMM-CDA-1: Switch to AmmCdaHybrid enum field (#1274)

* switch to AmmCdaHybrid enum field

* Update zrml/parimutuel/src/tests/buy.rs

Co-authored-by: Malte Kliemann <[email protected]>

* Update zrml/parimutuel/src/tests/claim.rs

Co-authored-by: Malte Kliemann <[email protected]>

* Update zrml/parimutuel/src/tests/refund.rs

Co-authored-by: Malte Kliemann <[email protected]>

* Update storage version to 11 and add MigrateScoringRuleAmmCdaHybrid

* Update primitives/src/market.rs

* update migration

---------

Co-authored-by: Malte Kliemann <[email protected]>

* merge amm-cda-3 changes

* merge amm-cda-2 changes

* merge amm-cda-4 changes

* correct clippy

* merge amm-cda-5 changes

* merge amm-cda-6 changes

* Fix Hybrid Router clippy and tests (#1291)

* glue everything together

* add hybrid router to configuration

* fix tests

* fix conditional tests

* AMM-CDA-8 Add event information for fees and aggregated amount_out (#1293)

* add event info

* use slice as function parameter

* update test amm amount out of event

* fix order book tests

* update documentation

* removed dependency

* AMM-CDA-9 Adds soft and hard failure distinction for AMM and order book errors (#1294)

* add event info

* use slice as function parameter

* update test amm amount out of event

* wip

* handle soft and hard failure

* add order book soft failure

* fix clippy

* fix CI

* remove swaps pallet dependency

* add compact to order book struct

* fix recursion overflow

* Fix of Hybrid Router after asset system merge (#1309)

* wip

* use asset conversions

* adapt hybrid router to new asset system

* apply review suggestions

* fmt

* rename Asset to Assets

* update copyrights

* update hybrid router crate version

* correct orderbook spelling

* add amount is zero tests

* add price limit too high test

* add max order exceeded test

* use saturated conversion

* use saturated conversion again

* remove unused code

* add tests for soft failures

* add skip order test

* add changelog for devs description

* add amm soft failure test

* add numerical soft failure test for sell

* correct failing test for parachain feature

* cover remaining is zero execution path

---------

Co-authored-by: Malte Kliemann <[email protected]>
  • Loading branch information
Chralt98 and maltekliemann authored Apr 12, 2024
1 parent fd18a90 commit dc7e7f9
Show file tree
Hide file tree
Showing 93 changed files with 6,394 additions and 441 deletions.
42 changes: 42 additions & 0 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
Expand Up @@ -9,6 +9,7 @@ default-members = [
"zrml/authorized",
"zrml/asset-router",
"zrml/court",
"zrml/hybrid-router",
"zrml/global-disputes",
"zrml/liquidity-mining",
"zrml/market-commons",
Expand All @@ -34,6 +35,7 @@ members = [
"zrml/authorized",
"zrml/asset-router",
"zrml/court",
"zrml/hybrid-router",
"zrml/global-disputes",
"zrml/liquidity-mining",
"zrml/market-commons",
Expand Down Expand Up @@ -239,6 +241,7 @@ zrml-asset-router = { path = "zrml/asset-router", default-features = false }
zrml-authorized = { path = "zrml/authorized", default-features = false }
zrml-court = { path = "zrml/court", default-features = false }
zrml-global-disputes = { path = "zrml/global-disputes", default-features = false }
zrml-hybrid-router = { path = "zrml/hybrid-router", 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 }
Expand Down
21 changes: 21 additions & 0 deletions docs/changelog_for_devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ 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.2

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

### Added

- [#1307] New hybrid router for managing the trade execution using the
`neo-swaps` automated market maker and order book

- `buy`: Routes a buy order to AMM and CDA to achieve the best average
execution price.
- `sell`: Routes a sell order to AMM and CDA to achieve the best average
execution price.

The new pallet has the following events:

- `HybridRouterExecuted { tx_type, who, market_id, price_limit, asset_in, amount_in, asset_out, amount_out, external_fee_amount, swap_fee_amount }`:
A trade was executed using the Hybrid Router.

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

## v0.5.1

[#1295]: https://github.com/zeitgeistpm/zeitgeist/pull/1295
Expand Down
8 changes: 7 additions & 1 deletion primitives/src/constants.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 @@ -22,6 +22,8 @@
clippy::arithmetic_side_effects
)]

#[cfg(feature = "mock")]
pub mod base_multiples;
#[cfg(feature = "mock")]
pub mod mock;
pub mod ztg;
Expand Down Expand Up @@ -79,6 +81,10 @@ pub const GLOBAL_DISPUTES_PALLET_ID: PalletId = PalletId(*b"zge/gldp");
/// Lock identifier, mainly used for the locks on the accounts.
pub const GLOBAL_DISPUTES_LOCK_ID: [u8; 8] = *b"zge/gdlk";

// Hybrid Router
/// Pallet identifier, mainly used for named balance reserves.
pub const HYBRID_ROUTER_PALLET_ID: PalletId = PalletId(*b"zge/hybr");

// Liqudity Mining
/// Pallet identifier, mainly used for named balance reserves.
pub const LM_PALLET_ID: PalletId = PalletId(*b"zge/lymg");
Expand Down
70 changes: 70 additions & 0 deletions primitives/src/constants/base_multiples.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 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/>.

#![cfg(feature = "mock")]

use crate::constants::BASE;

pub const _1: u128 = BASE;
pub const _2: u128 = 2 * _1;
pub const _3: u128 = 3 * _1;
pub const _4: u128 = 4 * _1;
pub const _5: u128 = 5 * _1;
pub const _6: u128 = 6 * _1;
pub const _7: u128 = 7 * _1;
pub const _8: u128 = 8 * _1;
pub const _9: u128 = 9 * _1;
pub const _10: u128 = 10 * _1;
pub const _11: u128 = 11 * _1;
pub const _12: u128 = 12 * _1;
pub const _14: u128 = 14 * _1;
pub const _17: u128 = 17 * _1;
pub const _20: u128 = 20 * _1;
pub const _23: u128 = 23 * _1;
pub const _24: u128 = 24 * _1;
pub const _30: u128 = 30 * _1;
pub const _36: u128 = 36 * _1;
pub const _40: u128 = 40 * _1;
pub const _70: u128 = 70 * _1;
pub const _80: u128 = 80 * _1;
pub const _100: u128 = 100 * _1;
pub const _101: u128 = 101 * _1;
pub const _444: u128 = 444 * _1;
pub const _500: u128 = 500 * _1;
pub const _777: u128 = 777 * _1;
pub const _1000: u128 = 1_000 * _1;

pub const _1_2: u128 = _1 / 2;

pub const _1_3: u128 = _1 / 3;
pub const _2_3: u128 = _2 / 3;

pub const _1_4: u128 = _1 / 4;
pub const _3_4: u128 = _3 / 4;

pub const _1_5: u128 = _1 / 5;

pub const _1_6: u128 = _1 / 6;
pub const _5_6: u128 = _5 / 6;

pub const _1_10: u128 = _1 / 10;
pub const _2_10: u128 = _2 / 10;
pub const _3_10: u128 = _3 / 10;
pub const _4_10: u128 = _4 / 10;
pub const _9_10: u128 = _9 / 10;

pub const _1_100: u128 = _1 / 100;
6 changes: 6 additions & 0 deletions primitives/src/constants/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ parameter_types! {
pub const VotingOutcomeFee: Balance = 100 * CENT;
}

// Hybrid Router parameters
parameter_types! {
pub const HybridRouterPalletId: PalletId = PalletId(*b"zge/hybr");
pub const MaxOrders: u32 = 100;
}

// Liquidity Mining parameters
parameter_types! {
pub const LiquidityMiningPalletId: PalletId = PalletId(*b"zge/lymg");
Expand Down
63 changes: 63 additions & 0 deletions primitives/src/hybrid_router_api_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 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/>.

use frame_support::pallet_prelude::*;
use scale_info::TypeInfo;

#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, Debug, TypeInfo)]
pub struct AmmTrade<Balance> {
pub amount_in: Balance,
pub amount_out: Balance,
pub swap_fee_amount: Balance,
pub external_fee_amount: Balance,
}

#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, Debug, TypeInfo)]
pub struct ExternalFee<AccountId, Balance> {
pub account: AccountId,
pub amount: Balance,
}

#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, Debug, TypeInfo)]
pub struct OrderbookTrade<AccountId, Balance> {
pub filled_maker_amount: Balance,
pub filled_taker_amount: Balance,
pub external_fee: ExternalFee<AccountId, Balance>,
}

pub trait FailSoft {}

#[derive(Debug)]
pub enum AmmSoftFail {
Numerical,
}

impl FailSoft for AmmSoftFail {}

#[derive(Debug)]
pub enum OrderbookSoftFail {
BelowMinimumBalance,
PartialFillNearFullFillNotAllowed,
}

impl FailSoft for OrderbookSoftFail {}

#[derive(Debug)]
pub enum ApiError<S: FailSoft> {
SoftFailure(S),
HardFailure(DispatchError),
}
2 changes: 2 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ extern crate alloc;

mod assets;
pub mod constants;
pub mod hybrid_router_api_types;
mod market;
pub mod math;
mod max_runtime_usize;
pub mod orderbook;
mod outcome_report;
mod proxy_type;
pub mod traits;
Expand Down
Loading

0 comments on commit dc7e7f9

Please sign in to comment.