Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dont allow to create xyk pool if lbp is running #257

Merged
merged 7 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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