Skip to content

Commit 22a4d3f

Browse files
committed
burn lock for new subnets, refund for old
1 parent a75c6f5 commit 22a4d3f

File tree

1 file changed

+57
-36
lines changed

1 file changed

+57
-36
lines changed

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -449,43 +449,58 @@ impl<T: Config> Pallet<T> {
449449
let owner_coldkey: T::AccountId = SubnetOwner::<T>::get(netuid);
450450
let lock_cost: TaoCurrency = Self::get_subnet_locked_balance(netuid);
451451

452-
// 3) Compute owner's received emission in TAO at current price.
452+
// Determine if this subnet is eligible for a lock refund (legacy).
453+
let reg_at: u64 = NetworkRegisteredAt::<T>::get(netuid);
454+
let start_block: u64 = NetworkRegistrationStartBlock::<T>::get();
455+
let should_refund_owner: bool = reg_at < start_block;
456+
457+
// 3) Compute owner's received emission in TAO at current price (ONLY if we may refund).
453458
// Emission::<T> is Vec<AlphaCurrency>. We:
454459
// - sum emitted α,
455460
// - apply owner fraction to get owner α,
456461
// - price that α using a *simulated* AMM swap.
457-
let total_emitted_alpha_u128: u128 =
458-
Emission::<T>::get(netuid)
459-
.into_iter()
460-
.fold(0u128, |acc, e_alpha| {
461-
let e_u64: u64 = Into::<u64>::into(e_alpha);
462-
acc.saturating_add(e_u64 as u128)
463-
});
464-
465-
let owner_fraction: U96F32 = Self::get_float_subnet_owner_cut();
466-
let owner_alpha_u64: u64 = U96F32::from_num(total_emitted_alpha_u128)
467-
.saturating_mul(owner_fraction)
468-
.floor()
469-
.saturating_to_num::<u64>();
470-
471-
let owner_emission_tao: TaoCurrency = if owner_alpha_u64 > 0 {
472-
match T::SwapInterface::sim_swap(netuid.into(), OrderType::Sell, owner_alpha_u64) {
473-
Ok(sim) => TaoCurrency::from(sim.amount_paid_out),
474-
Err(e) => {
475-
log::debug!(
476-
"destroy_alpha_in_out_stakes: sim_swap owner α→τ failed (netuid={netuid:?}, alpha={owner_alpha_u64}, err={e:?}); falling back to price multiply.",
477-
);
478-
let cur_price: U96F32 = T::SwapInterface::current_alpha_price(netuid.into());
479-
let val_u64: u64 = U96F32::from_num(owner_alpha_u64)
480-
.saturating_mul(cur_price)
481-
.floor()
482-
.saturating_to_num::<u64>();
483-
TaoCurrency::from(val_u64)
484-
}
462+
let mut owner_emission_tao: TaoCurrency = TaoCurrency::ZERO;
463+
if should_refund_owner && !lock_cost.is_zero() {
464+
let total_emitted_alpha_u128: u128 =
465+
Emission::<T>::get(netuid)
466+
.into_iter()
467+
.fold(0u128, |acc, e_alpha| {
468+
let e_u64: u64 = Into::<u64>::into(e_alpha);
469+
acc.saturating_add(e_u64 as u128)
470+
});
471+
472+
if total_emitted_alpha_u128 > 0 {
473+
let owner_fraction: U96F32 = Self::get_float_subnet_owner_cut();
474+
let owner_alpha_u64: u64 = U96F32::from_num(total_emitted_alpha_u128)
475+
.saturating_mul(owner_fraction)
476+
.floor()
477+
.saturating_to_num::<u64>();
478+
479+
owner_emission_tao = if owner_alpha_u64 > 0 {
480+
match T::SwapInterface::sim_swap(
481+
netuid.into(),
482+
OrderType::Sell,
483+
owner_alpha_u64,
484+
) {
485+
Ok(sim) => TaoCurrency::from(sim.amount_paid_out),
486+
Err(e) => {
487+
log::debug!(
488+
"destroy_alpha_in_out_stakes: sim_swap owner α→τ failed (netuid={netuid:?}, alpha={owner_alpha_u64}, err={e:?}); falling back to price multiply.",
489+
);
490+
let cur_price: U96F32 =
491+
T::SwapInterface::current_alpha_price(netuid.into());
492+
let val_u64: u64 = U96F32::from_num(owner_alpha_u64)
493+
.saturating_mul(cur_price)
494+
.floor()
495+
.saturating_to_num::<u64>();
496+
TaoCurrency::from(val_u64)
497+
}
498+
}
499+
} else {
500+
TaoCurrency::ZERO
501+
};
485502
}
486-
} else {
487-
TaoCurrency::ZERO
488-
};
503+
}
489504

490505
// 4) Enumerate all α entries on this subnet to build distribution weights and cleanup lists.
491506
// - collect keys to remove,
@@ -594,13 +609,19 @@ impl<T: Config> Pallet<T> {
594609
SubnetAlphaInProvided::<T>::remove(netuid);
595610
SubnetAlphaOut::<T>::remove(netuid);
596611

597-
// 8) Refund remaining lock to subnet owner:
598-
// refund = max(0, lock_cost(τ) − owner_received_emission_in_τ).
599-
let refund: TaoCurrency = lock_cost.saturating_sub(owner_emission_tao);
600-
601612
// Clear the locked balance on the subnet.
602613
Self::set_subnet_locked_balance(netuid, TaoCurrency::ZERO);
603614

615+
// 8) Finalize lock handling:
616+
// - Legacy subnets (registered before NetworkRegistrationStartBlock) receive:
617+
// refund = max(0, lock_cost(τ) − owner_received_emission_in_τ).
618+
// - New subnets: no refund.
619+
let refund: TaoCurrency = if should_refund_owner {
620+
lock_cost.saturating_sub(owner_emission_tao)
621+
} else {
622+
TaoCurrency::ZERO
623+
};
624+
604625
if !refund.is_zero() {
605626
Self::add_balance_to_coldkey_account(&owner_coldkey, refund.to_u64());
606627
}

0 commit comments

Comments
 (0)