Skip to content

Commit

Permalink
Merge pull request #256 from galacticcouncil/feat/xyk-can-create
Browse files Browse the repository at this point in the history
feat: expose possibility to control pool creation
  • Loading branch information
enthusiastmartin authored Nov 23, 2021
2 parents 7fbd10a + cebf9b1 commit d7eee2b
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion pallets/asset-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false }

# HydraDX dependencies
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="32f3a9ab84b86b200d3ad702db762ac7de06b20c", default-features = false }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="a5cb40ae5313bb158f8b28e0a78479148be3b9f5", default-features = false }

# Optionals
frame-system-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion pallets/exchange/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-li
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "ee37b76a7a8cf159e6c41851942758746318bb84", default-features = false }

# HydraDX dependencies
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="32f3a9ab84b86b200d3ad702db762ac7de06b20c", default-features = false }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="a5cb40ae5313bb158f8b28e0a78479148be3b9f5", default-features = false }

# Substrate dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion pallets/exchange/benchmarking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-li
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "ee37b76a7a8cf159e6c41851942758746318bb84", default-features = false }

# HydraDX dependencies
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="32f3a9ab84b86b200d3ad702db762ac7de06b20c", default-features = false }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="a5cb40ae5313bb158f8b28e0a78479148be3b9f5", default-features = false }

# Substrate dependencies
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions pallets/exchange/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl pallet_xyk::Config for Test {
type MinPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = pallet_xyk::AllowAllPools;
}

impl pallet_exchange::Config for Test {
Expand Down
1 change: 1 addition & 0 deletions pallets/exchange/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl xyk::Config for Test {
type MinPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = pallet_xyk::AllowAllPools;
}

impl Config for Test {
Expand Down
2 changes: 1 addition & 1 deletion pallets/lbp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde = { features = ["derive"], optional = true, version = "1.0.101" }

# HydraDX dependencies
hydra-dx-math = { default-features = false, version = "4.0.0" }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="32f3a9ab84b86b200d3ad702db762ac7de06b20c", default-features = false }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="a5cb40ae5313bb158f8b28e0a78479148be3b9f5", default-features = false }

## Local dependencies
primitives = { default-features = false, path = "../../primitives" }
Expand Down
2 changes: 1 addition & 1 deletion pallets/transaction-multi-payment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-li
orml-utilities = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "ee37b76a7a8cf159e6c41851942758746318bb84", default-features = false }

# HydraDX dependencies
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="32f3a9ab84b86b200d3ad702db762ac7de06b20c", default-features = false }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="a5cb40ae5313bb158f8b28e0a78479148be3b9f5", default-features = false }

# Substrate dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion pallets/transaction-multi-payment/benchmarking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-li
orml-utilities = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "ee37b76a7a8cf159e6c41851942758746318bb84", default-features = false }

# HydraDX dependencies
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="32f3a9ab84b86b200d3ad702db762ac7de06b20c", default-features = false }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="a5cb40ae5313bb158f8b28e0a78479148be3b9f5", default-features = false }

# Substrate dependencies
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions pallets/transaction-multi-payment/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl pallet_xyk::Config for Test {
type MinPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = pallet_xyk::AllowAllPools;
}

parameter_type_with_key! {
Expand Down
1 change: 1 addition & 0 deletions pallets/transaction-multi-payment/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl pallet_xyk::Config for Test {
type MinPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = pallet_xyk::AllowAllPools;
}

parameter_type_with_key! {
Expand Down
4 changes: 2 additions & 2 deletions pallets/xyk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = 'pallet-xyk'
version = '2.1.0'
version = '3.0.0'
description = 'XYK automated market maker'
authors = ['GalacticCouncil']
edition = '2018'
Expand Down Expand Up @@ -31,7 +31,7 @@ orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-li
orml-utilities = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "ee37b76a7a8cf159e6c41851942758746318bb84", default-features = false }

# HydraDX dependencies
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="32f3a9ab84b86b200d3ad702db762ac7de06b20c", default-features = false }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="a5cb40ae5313bb158f8b28e0a78479148be3b9f5", default-features = false }

# Substrate dependencies
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false, optional = true }
Expand Down
21 changes: 20 additions & 1 deletion pallets/xyk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use frame_support::sp_runtime::{traits::Zero, DispatchError};
use frame_support::{dispatch::DispatchResult, ensure, traits::Get, transactional};
use frame_system::ensure_signed;
use hydradx_traits::{AMMTransfer, AssetPairAccountIdFor, AMM};
use hydradx_traits::{AMMTransfer, AssetPairAccountIdFor, CanCreatePool, AMM};
use primitives::{asset::AssetPair, constants::chain::MIN_POOL_LIQUIDITY, fee, AssetId, Balance, Price};
use sp_std::{vec, vec::Vec};

Expand Down Expand Up @@ -107,6 +107,9 @@ pub mod pallet {
/// Max fraction of pool to buy in single transaction
#[pallet::constant]
type MaxOutRatio: Get<u128>;

/// Called to ensure that pool can be created
type CanCreatePool: CanCreatePool<AssetId>;
}

#[pallet::error]
Expand Down Expand Up @@ -178,6 +181,9 @@ pub mod pallet {

/// Overflow
Overflow,

/// Pool cannot be created due to outside factors.
CannotCreatePool,
}

#[pallet::event]
Expand Down Expand Up @@ -257,6 +263,11 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;

ensure!(
T::CanCreatePool::can_create(asset_a, asset_b),
Error::<T>::CannotCreatePool
);

ensure!(amount >= T::MinPoolLiquidity::get(), Error::<T>::InsufficientLiquidity);

ensure!(!(initial_price == Price::zero()), Error::<T>::ZeroInitialPrice);
Expand Down Expand Up @@ -917,3 +928,11 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, Balance> for Pallet<T> {
T::MaxOutRatio::get()
}
}

pub struct AllowAllPools();

impl CanCreatePool<AssetId> for AllowAllPools {
fn can_create(_asset_a: AssetId, _asset_b: AssetId) -> bool {
true
}
}
14 changes: 11 additions & 3 deletions pallets/xyk/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ use sp_runtime::{
};

use frame_support::traits::{Everything, GenesisBuild, Get};
use hydradx_traits::{AssetPairAccountIdFor, CanCreatePool};
use primitives::{
constants::chain::{MAX_IN_RATIO, MAX_OUT_RATIO, MIN_POOL_LIQUIDITY, MIN_TRADING_LIMIT},
fee,
AssetId, Balance,
fee, AssetId, Balance,
};
use hydradx_traits::AssetPairAccountIdFor;

use frame_system::EnsureSigned;
use std::cell::RefCell;
Expand Down Expand Up @@ -158,6 +157,14 @@ parameter_types! {
pub const MaxOutRatio: u128 = MAX_OUT_RATIO;
}

pub struct Disallow10_10Pool();

impl CanCreatePool<AssetId> for Disallow10_10Pool {
fn can_create(asset_a: AssetId, asset_b: AssetId) -> bool {
!matches!((asset_a, asset_b), (10u32,10u32))
}
}

impl Config for Test {
type Event = Event;
type AssetRegistry = AssetRegistry;
Expand All @@ -170,6 +177,7 @@ impl Config for Test {
type MinPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = Disallow10_10Pool;
}

pub struct ExtBuilder {
Expand Down
20 changes: 19 additions & 1 deletion pallets/xyk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
}

fn expect_events(e: Vec<TestEvent>) {
e.into_iter().for_each( frame_system::Pallet::<Test>::assert_has_event);
e.into_iter().for_each(frame_system::Pallet::<Test>::assert_has_event);
}

#[test]
Expand Down Expand Up @@ -1873,3 +1873,21 @@ fn fee_calculation() {
assert_noop!(XYK::calculate_fee(100000), Error::<Test>::FeeAmountInvalid);
});
}

#[test]
fn can_create_pool_should_work() {
new_test_ext().execute_with(|| {
let asset_a = 10u32;
let asset_b = 10u32;
assert_noop!(
XYK::create_pool(
Origin::signed(ALICE),
asset_a,
asset_b,
100_000_000_000_000,
Price::from(10)
),
Error::<Test>::CannotCreatePool
);
});
}
2 changes: 1 addition & 1 deletion runtime/basilisk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pallet-elections-phragmen = { git = "https://github.com/paritytech/substrate", b
pallet-tips = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false }

# HydraDX dependencies
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="32f3a9ab84b86b200d3ad702db762ac7de06b20c", default-features = false }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="a5cb40ae5313bb158f8b28e0a78479148be3b9f5", default-features = false }

# collator support
pallet-collator-selection = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.12", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions runtime/basilisk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ impl pallet_xyk::Config for Runtime {
type MinPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = pallet_xyk::AllowAllPools;
}

impl pallet_exchange::Config for Runtime {
Expand Down
2 changes: 1 addition & 1 deletion runtime/testing-basilisk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pallet-elections-phragmen = { git = "https://github.com/paritytech/substrate", b
pallet-tips = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false }

# HydraDX dependencies
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="32f3a9ab84b86b200d3ad702db762ac7de06b20c", default-features = false }
hydradx-traits = { git = "https://github.com/galacticcouncil/warehouse", rev="a5cb40ae5313bb158f8b28e0a78479148be3b9f5", default-features = false }

# collator support
pallet-collator-selection = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.12", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions runtime/testing-basilisk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ impl pallet_xyk::Config for Runtime {
type MinPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = pallet_xyk::AllowAllPools;
}

impl pallet_exchange::Config for Runtime {
Expand Down

0 comments on commit d7eee2b

Please sign in to comment.