Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 12 additions & 2 deletions pallets/subtensor/src/staking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ impl<T: Config> Pallet<T> {
T::SwapInterface::max_price(),
true,
)
.map(|r| r.amount_paid_out.saturating_add(r.fee_paid))
.map(|r| {
let fee: u64 = U96F32::saturating_from_num(r.fee_paid)
.saturating_mul(T::SwapInterface::current_alpha_price(netuid))
.saturating_to_num();
r.amount_paid_out.saturating_add(fee)
})
.unwrap_or_default()
})
.sum()
Expand All @@ -80,7 +85,12 @@ impl<T: Config> Pallet<T> {
T::SwapInterface::max_price(),
true,
)
.map(|r| r.amount_paid_out.saturating_add(r.fee_paid))
.map(|r| {
let fee: u64 = U96F32::saturating_from_num(r.fee_paid)
.saturating_mul(T::SwapInterface::current_alpha_price(netuid))
.saturating_to_num();
r.amount_paid_out.saturating_add(fee)
})
.unwrap_or_default()
})
.sum::<u64>()
Expand Down
25 changes: 24 additions & 1 deletion pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,15 @@ impl<T: Config> Pallet<T> {
// Get the minimum balance (and amount) that satisfies the transaction
let min_amount = {
let default_stake = DefaultMinStake::<T>::get();
let fee = T::SwapInterface::approx_fee_amount(netuid, default_stake);
let fee = T::SwapInterface::swap(
netuid,
OrderType::Buy,
default_stake,
T::SwapInterface::max_price(),
true,
)
.map(|res| res.fee_paid)
.unwrap_or(T::SwapInterface::approx_fee_amount(netuid, default_stake));
default_stake.saturating_add(fee)
};

Expand Down Expand Up @@ -951,6 +959,21 @@ impl<T: Config> Pallet<T> {
Error::<T>::NotEnoughStakeToWithdraw
);

// Ensure that the stake amount to be removed is above the minimum in tao equivalent.
let tao_equivalent = T::SwapInterface::swap(
origin_netuid,
OrderType::Sell,
alpha_amount,
T::SwapInterface::max_price(),
true,
)
.map(|res| res.amount_paid_out)
.map_err(|_| Error::<T>::InsufficientLiquidity)?;
ensure!(
tao_equivalent > DefaultMinStake::<T>::get(),
Error::<T>::AmountTooLow
);

// Ensure that if partial execution is not allowed, the amount will not cause
// slippage over desired
if let Some(allow_partial) = maybe_allow_partial {
Expand Down
21 changes: 16 additions & 5 deletions pallets/subtensor/src/tests/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,11 +2218,12 @@ fn test_do_remove_stake_clears_pending_childkeys() {
// Set non-default value for childkey stake threshold
StakeThreshold::<Test>::set(1_000_000_000_000);

let (_, fee) = mock::swap_tao_to_alpha(netuid, StakeThreshold::<Test>::get());
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey,
&coldkey,
netuid,
StakeThreshold::<Test>::get(),
StakeThreshold::<Test>::get() + fee,
);

// Attempt to set child
Expand Down Expand Up @@ -2415,13 +2416,19 @@ fn test_revoke_child_no_min_stake_check() {
add_network(netuid, 13, 0);
register_ok_neuron(netuid, parent, coldkey, 0);

let reserve = 1_000_000_000_000_000;
mock::setup_reserves(netuid, reserve, reserve);
mock::setup_reserves(root, reserve, reserve);

// Set minimum stake for setting children
StakeThreshold::<Test>::put(1_000_000_000_000);

let (_, fee) = mock::swap_tao_to_alpha(root, StakeThreshold::<Test>::get());
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&parent,
&coldkey,
root,
StakeThreshold::<Test>::get(),
StakeThreshold::<Test>::get() + fee,
);

// Schedule parent-child relationship
Expand All @@ -2441,7 +2448,7 @@ fn test_revoke_child_no_min_stake_check() {
&parent,
&coldkey,
root,
StakeThreshold::<Test>::get(),
StakeThreshold::<Test>::get() + fee,
);

// Ensure the childkeys are applied
Expand Down Expand Up @@ -2487,13 +2494,17 @@ fn test_do_set_child_registration_disabled() {
add_network(netuid, 13, 0);
register_ok_neuron(netuid, parent, coldkey, 0);

let reserve = 1_000_000_000_000_000;
mock::setup_reserves(netuid, reserve, reserve);

// Set minimum stake for setting children
StakeThreshold::<Test>::put(1_000_000_000_000);
let (_, fee) = mock::swap_tao_to_alpha(netuid, StakeThreshold::<Test>::get());
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&parent,
&coldkey,
netuid,
StakeThreshold::<Test>::get(),
StakeThreshold::<Test>::get() + fee,
);

// Disable subnet registrations
Expand All @@ -2512,7 +2523,7 @@ fn test_do_set_child_registration_disabled() {
&parent,
&coldkey,
netuid,
StakeThreshold::<Test>::get(),
StakeThreshold::<Test>::get() + fee,
);

// Ensure the childkeys are applied
Expand Down
3 changes: 3 additions & 0 deletions pallets/subtensor/src/tests/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,9 @@ fn test_run_coinbase_not_started() {
// Set weight-set limit to 0.
SubtensorModule::set_weights_set_rate_limit(netuid, 0);

let reserve = init_stake * 1000;
mock::setup_reserves(netuid, reserve, reserve);

register_ok_neuron(netuid, hotkey, coldkey, 0);
register_ok_neuron(netuid, miner_hk, miner_ck, 0);
register_ok_neuron(netuid, sn_owner_hk, sn_owner_ck, 0);
Expand Down
Loading
Loading