Skip to content

Commit 56dbe70

Browse files
committed
Fix insufficient liquidity issue
1 parent df1b927 commit 56dbe70

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

pallets/subtensor/src/tests/staking.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,67 @@ fn test_add_stake_insufficient_liquidity() {
763763
});
764764
}
765765

766+
/// cargo test --package pallet-subtensor --lib -- tests::staking::test_add_stake_insufficient_liquidity_one_side_ok --exact --show-output
767+
#[test]
768+
fn test_add_stake_insufficient_liquidity_one_side_ok() {
769+
new_test_ext(1).execute_with(|| {
770+
let subnet_owner_coldkey = U256::from(1001);
771+
let subnet_owner_hotkey = U256::from(1002);
772+
let hotkey = U256::from(2);
773+
let coldkey = U256::from(3);
774+
let amount_staked = DefaultMinStake::<Test>::get() * 10;
775+
776+
let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
777+
SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
778+
SubtensorModule::add_balance_to_coldkey_account(&coldkey, amount_staked);
779+
780+
// Set the liquidity at lowest possible value so that all staking requests fail
781+
let reserve_alpha = u64::from(mock::SwapMinimumReserve::get());
782+
let reserve_tao = u64::from(mock::SwapMinimumReserve::get()) - 1;
783+
mock::setup_reserves(netuid, reserve_tao, reserve_alpha.into());
784+
785+
// Check the error
786+
assert_ok!(SubtensorModule::add_stake(
787+
RuntimeOrigin::signed(coldkey),
788+
hotkey,
789+
netuid,
790+
amount_staked
791+
));
792+
});
793+
}
794+
795+
/// cargo test --package pallet-subtensor --lib -- tests::staking::test_add_stake_insufficient_liquidity_one_side_fail --exact --show-output
796+
#[test]
797+
fn test_add_stake_insufficient_liquidity_one_side_fail() {
798+
new_test_ext(1).execute_with(|| {
799+
let subnet_owner_coldkey = U256::from(1001);
800+
let subnet_owner_hotkey = U256::from(1002);
801+
let hotkey = U256::from(2);
802+
let coldkey = U256::from(3);
803+
let amount_staked = DefaultMinStake::<Test>::get() * 10;
804+
805+
let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
806+
SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
807+
SubtensorModule::add_balance_to_coldkey_account(&coldkey, amount_staked);
808+
809+
// Set the liquidity at lowest possible value so that all staking requests fail
810+
let reserve_alpha = u64::from(mock::SwapMinimumReserve::get()) - 1;
811+
let reserve_tao = u64::from(mock::SwapMinimumReserve::get());
812+
mock::setup_reserves(netuid, reserve_tao, reserve_alpha.into());
813+
814+
// Check the error
815+
assert_noop!(
816+
SubtensorModule::add_stake(
817+
RuntimeOrigin::signed(coldkey),
818+
hotkey,
819+
netuid,
820+
amount_staked
821+
),
822+
Error::<Test>::InsufficientLiquidity
823+
);
824+
});
825+
}
826+
766827
#[test]
767828
fn test_remove_stake_insufficient_liquidity() {
768829
new_test_ext(1).execute_with(|| {

pallets/swap/src/pallet/impls.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,17 @@ impl<T: Config> Pallet<T> {
465465
limit_sqrt_price: SqrtPrice,
466466
drop_fees: bool,
467467
) -> Result<SwapResult, Error<T>> {
468-
ensure!(
469-
T::SubnetInfo::tao_reserve(netuid.into()) >= T::MinimumReserve::get().get()
470-
&& u64::from(T::SubnetInfo::alpha_reserve(netuid.into()))
468+
match order_type {
469+
OrderType::Buy => ensure!(
470+
u64::from(T::SubnetInfo::alpha_reserve(netuid.into()))
471471
>= T::MinimumReserve::get().get(),
472-
Error::<T>::ReservesTooLow
473-
);
472+
Error::<T>::ReservesTooLow
473+
),
474+
OrderType::Sell => ensure!(
475+
T::SubnetInfo::tao_reserve(netuid.into()) >= T::MinimumReserve::get().get(),
476+
Error::<T>::ReservesTooLow
477+
),
478+
}
474479

475480
Self::maybe_initialize_v3(netuid)?;
476481

0 commit comments

Comments
 (0)