Skip to content

Commit

Permalink
Implement Combinatorial Pools (#1389)
Browse files Browse the repository at this point in the history
* Define and implement `CombinatorialTokensApi`

* Abstract position calculation

* Implement `CombinatorialTokensUnsafeApi::combinatorial_position`

* Add `CombinatorialTokens*Api` to `neo-swaps`

* Fix formatting

* Add copyright notices

* .

* .

* Implement `deploy_combinatorial_pool`

* Replace `market_id` with `pool_id` where appropriate

* pool storage

* .

* Implement `PoolType`

* Use `PoolOperations::is_active`

* Use market ID for complete set operations

* Use PoolId more

* Rewrite `distribute_fees` to make use of all markets

* .

* Fix duplicate pool problem / adapter

* .

* Fix sell tests

* Fix really annoying problem

* clean up tests

* .

* .

* Update copyright

* Fix tests
  • Loading branch information
maltekliemann authored Oct 30, 2024
1 parent a56f929 commit 84396ca
Show file tree
Hide file tree
Showing 42 changed files with 2,054 additions and 622 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
// You should have received a copy of the GNU General Public License
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

mod combinatorial_tokens_api;
mod combinatorial_tokens_benchmark_helper;
mod combinatorial_tokens_unsafe_api;
mod complete_set_operations_api;
mod deploy_pool_api;
mod dispute_api;
Expand All @@ -32,7 +34,9 @@ mod payout_api;
mod swaps;
mod zeitgeist_asset;

pub use combinatorial_tokens_api::*;
pub use combinatorial_tokens_benchmark_helper::*;
pub use combinatorial_tokens_unsafe_api::*;
pub use complete_set_operations_api::*;
pub use deploy_pool_api::*;
pub use dispute_api::*;
Expand Down
36 changes: 36 additions & 0 deletions primitives/src/traits/combinatorial_tokens_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 crate::types::SplitPositionDispatchInfo;
use alloc::vec::Vec;
use sp_runtime::DispatchError;

pub trait CombinatorialTokensApi {
type AccountId;
type Balance;
type CombinatorialId;
type MarketId;

fn split_position(
who: Self::AccountId,
parent_collection_id: Option<Self::CombinatorialId>,
market_id: Self::MarketId,
partition: Vec<Vec<bool>>,
amount: Self::Balance,
force_max_work: bool,
) -> Result<SplitPositionDispatchInfo<Self::CombinatorialId, Self::MarketId>, DispatchError>;
}
42 changes: 42 additions & 0 deletions primitives/src/traits/combinatorial_tokens_unsafe_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 crate::types::Asset;
use alloc::vec::Vec;
use sp_runtime::DispatchResult;

// Very fast and very unsafe API for splitting and merging combinatorial tokens. Calling the exposed
// functions with a bad `assets` argument can break the reserve.
pub trait CombinatorialTokensUnsafeApi {
type AccountId;
type Balance;
type MarketId;

fn split_position_unsafe(
who: Self::AccountId,
collateral: Asset<Self::MarketId>,
assets: Vec<Asset<Self::MarketId>>,
amount: Self::Balance,
) -> DispatchResult;

fn merge_position_unsafe(
who: Self::AccountId,
collateral: Asset<Self::MarketId>,
assets: Vec<Asset<Self::MarketId>>,
amount: Self::Balance,
) -> DispatchResult;
}
36 changes: 32 additions & 4 deletions primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@
// 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::traits::CombinatorialTokensBenchmarkHelper;
pub use crate::{
asset::*, market::*, max_runtime_usize::*, outcome_report::OutcomeReport, proxy_type::*,
serde_wrapper::*,
};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Result, Unstructured};
use frame_support::weights::Weight;
use alloc::vec::Vec;
use core::marker::PhantomData;
use frame_support::{dispatch::PostDispatchInfo, weights::Weight};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{
generic,
traits::{BlakeTwo256, IdentifyAccount, Verify},
MultiSignature, OpaqueExtrinsic,
DispatchResult, MultiSignature, OpaqueExtrinsic,
};

#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Result, Unstructured};

/// Signed counter-part of Balance
pub type Amount = i128;

Expand Down Expand Up @@ -180,3 +184,27 @@ pub struct XcmMetadata {
/// Should be updated regularly.
pub fee_factor: Option<Balance>,
}

pub struct NoopCombinatorialTokensBenchmarkHelper<Balance, MarketId>(
PhantomData<(Balance, MarketId)>,
);

impl<Balance, MarketId> CombinatorialTokensBenchmarkHelper
for NoopCombinatorialTokensBenchmarkHelper<Balance, MarketId>
{
type Balance = Balance;
type MarketId = MarketId;

fn setup_payout_vector(
_market_id: Self::MarketId,
_payout: Option<Vec<Self::Balance>>,
) -> DispatchResult {
Ok(())
}
}

pub struct SplitPositionDispatchInfo<CombinatorialId, MarketId> {
pub collection_ids: Vec<CombinatorialId>,
pub position_ids: Vec<Asset<MarketId>>,
pub post_dispatch_info: PostDispatchInfo,
}
1 change: 1 addition & 0 deletions runtime/battery-station/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ parameter_types! {
pub const NeoSwapsMaxSwapFee: Balance = 10 * CENT;
pub const NeoSwapsPalletId: PalletId = NS_PALLET_ID;
pub const MaxLiquidityTreeDepth: u32 = 9u32;
pub const MaxSplits: u16 = 128u16;

// ORML
pub const GetNativeCurrencyId: CurrencyId = Asset::Ztg;
Expand Down
4 changes: 4 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,9 @@ macro_rules! impl_config_traits {
common_runtime::impl_market_creator_fees!();

impl zrml_neo_swaps::Config for Runtime {
type CombinatorialId = CombinatorialId;
type CombinatorialTokens = CombinatorialTokens;
type CombinatorialTokensUnsafe = CombinatorialTokens;
type CompleteSetOperations = PredictionMarkets;
type ExternalFees = MarketCreatorFee;
type MarketCommons = MarketCommons;
Expand All @@ -1307,6 +1310,7 @@ macro_rules! impl_config_traits {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = zrml_neo_swaps::weights::WeightInfo<Runtime>;
type MaxLiquidityTreeDepth = MaxLiquidityTreeDepth;
type MaxSplits = MaxSplits;
type MaxSwapFee = NeoSwapsMaxSwapFee;
type PalletId = NeoSwapsPalletId;
}
Expand Down
1 change: 1 addition & 0 deletions runtime/zeitgeist/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ parameter_types! {
pub const NeoSwapsMaxSwapFee: Balance = 10 * CENT;
pub const NeoSwapsPalletId: PalletId = NS_PALLET_ID;
pub const MaxLiquidityTreeDepth: u32 = 9u32;
pub const MaxSplits: u16 = 128u16;

// ORML
pub const GetNativeCurrencyId: CurrencyId = Asset::Ztg;
Expand Down
Loading

0 comments on commit 84396ca

Please sign in to comment.