Skip to content

Commit

Permalink
Merge pull request #257 from galacticcouncil/feat/lbp-pool-control
Browse files Browse the repository at this point in the history
feat: dont allow to create xyk pool if  lbp is running
  • Loading branch information
mrq1911 authored Nov 23, 2021
2 parents b88d696 + e0f9413 commit f501dd6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
16 changes: 15 additions & 1 deletion pallets/lbp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use frame_support::{
};
use frame_system::ensure_signed;
use hydra_dx_math::types::LBPWeight;
use hydradx_traits::{AMMTransfer, AssetPairAccountIdFor, AMM};
use hydradx_traits::{AMMTransfer, AssetPairAccountIdFor, CanCreatePool, AMM};
use orml_traits::{MultiCurrency, MultiCurrencyExtended, MultiReservableCurrency};
use primitives::{
asset::AssetPair,
Expand Down Expand Up @@ -996,3 +996,17 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, BalanceOf<T>> for Pallet<T
T::MaxOutRatio::get()
}
}

pub struct DisallowWhenLBPPoolRunning<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> CanCreatePool<AssetId> for DisallowWhenLBPPoolRunning<T> {
fn can_create(asset_a: AssetId, asset_b: AssetId) -> bool {
let pool_id = Pallet::<T>::pair_account_from_assets(asset_a, asset_b);
let now = T::BlockNumberProvider::current_block_number();
match <PoolData<T>>::try_get(&pool_id) {
// returns true if the pool exists and the sale ended
Ok(pool_data) => pool_data.end != Zero::zero() && pool_data.end < now,
Err(_) => true
}
}
}
53 changes: 52 additions & 1 deletion pallets/lbp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
ext
}

use hydradx_traits::AMMTransfer;
use primitives::{
asset::AssetPair,
constants::chain::{MAX_IN_RATIO, MAX_OUT_RATIO},
fee::Fee,
};
use hydradx_traits::AMMTransfer;

pub fn predefined_test_ext() -> sp_io::TestExternalities {
let mut ext = new_test_ext();
Expand Down Expand Up @@ -2744,3 +2744,54 @@ fn calculate_fees_should_work() {
);
});
}

#[test]
fn can_create_should_work() {
new_test_ext().execute_with(|| {
let asset_pair = AssetPair{ asset_in: ACA, asset_out: DOT };
// pool doesn't exist
assert!(DisallowWhenLBPPoolRunning::<Test>::can_create(asset_pair.asset_in, asset_pair.asset_out));

assert_ok!(LBPPallet::create_pool(
Origin::root(),
ALICE,
ACA,
1_000_000_000,
DOT,
2_000_000_000,
20_000_000,
80_000_000,
WeightCurveType::Linear,
Fee::default(),
CHARLIE,
));
// pool is not initialized
assert!(!DisallowWhenLBPPoolRunning::<Test>::can_create(asset_pair.asset_in, asset_pair.asset_out));

assert_ok!(LBPPallet::update_pool_data(
Origin::signed(ALICE),
ACA_DOT_POOL_ID,
None,
Some(10),
Some(20),
None,
None,
None,
None
));
// pool is initialized but is not running
assert!(!DisallowWhenLBPPoolRunning::<Test>::can_create(asset_pair.asset_in, asset_pair.asset_out));

run_to_block::<Test>(15);
// pool is running
assert!(!DisallowWhenLBPPoolRunning::<Test>::can_create(asset_pair.asset_in, asset_pair.asset_out));

run_to_block::<Test>(30);
// sale ended
assert!(DisallowWhenLBPPoolRunning::<Test>::can_create(asset_pair.asset_in, asset_pair.asset_out));

assert_ok!(LBPPallet::remove_liquidity(Origin::signed(ALICE), ACA_DOT_POOL_ID,));
// pool was destroyed
assert!(DisallowWhenLBPPoolRunning::<Test>::can_create(asset_pair.asset_in, asset_pair.asset_out));
});
}
2 changes: 1 addition & 1 deletion runtime/basilisk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl pallet_xyk::Config for Runtime {
type MinPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = pallet_xyk::AllowAllPools;
type CanCreatePool = pallet_lbp::DisallowWhenLBPPoolRunning<Runtime>;
}

impl pallet_exchange::Config for Runtime {
Expand Down
2 changes: 1 addition & 1 deletion runtime/testing-basilisk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ impl pallet_xyk::Config for Runtime {
type MinPoolLiquidity = MinPoolLiquidity;
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = pallet_xyk::AllowAllPools;
type CanCreatePool = pallet_lbp::DisallowWhenLBPPoolRunning<Runtime>;
}

impl pallet_exchange::Config for Runtime {
Expand Down

0 comments on commit f501dd6

Please sign in to comment.