Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b6cb088
impl tests benchmarks
camfairchild Nov 5, 2025
3118bb7
bump spec
camfairchild Nov 5, 2025
843bc88
remove duplicate remove
camfairchild Nov 5, 2025
fb053c8
use alpha out here
camfairchild Nov 5, 2025
cb2085e
Hotfix/vune/epoch sub fix (#2186)
0xcacti Nov 5, 2025
b068d02
emit full alpha if subsidized
camfairchild Nov 5, 2025
7d77cf0
fix coinbase test
camfairchild Nov 5, 2025
e792dd1
fix tests
camfairchild Nov 5, 2025
d7a28ba
Revert "Hotfix/vune/epoch sub fix (#2186)"
camfairchild Nov 5, 2025
a837632
add total issuance bump
camfairchild Nov 5, 2025
6bddb56
no alpha out thing
camfairchild Nov 6, 2025
b857d4d
add tests for emissions w/ subs
camfairchild Nov 6, 2025
e1feb05
bump spec
camfairchild Nov 6, 2025
ad602c1
Fix clippy warnings
shamil-gadelshin Nov 6, 2025
cac43c5
Hotfix/vune/epoch sub fix (#2186)
0xcacti Nov 5, 2025
85fe2d4
rename drain_pending to distr em
camfairchild Nov 6, 2025
90d4be4
remove arg and total internally
camfairchild Nov 6, 2025
f81986b
test pending em
camfairchild Nov 6, 2025
be16df6
make sure we check subsidize during those tests
camfairchild Nov 6, 2025
69249f4
fix root claim tests
camfairchild Nov 6, 2025
9762f6a
helper and clippy
camfairchild Nov 6, 2025
94d31d7
bump spec
camfairchild Nov 6, 2025
186c717
rename subsidy to root flag
camfairchild Nov 6, 2025
25290f1
only pull these values if root selling
camfairchild Nov 6, 2025
d3698bf
rename subsidy checks to the root_sell flag
camfairchild Nov 6, 2025
62ecfe1
rename subsidy to excess_tao
camfairchild Nov 6, 2025
c5a7f51
use recycle alpha helper
camfairchild Nov 6, 2025
75440be
Hotfix/vune/subnet-dereg-burn-use-issuance (#2190)
camfairchild Nov 6, 2025
40b4ffc
remove log out
camfairchild Nov 6, 2025
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
2 changes: 1 addition & 1 deletion pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ mod pallet_benchmarks {
);

let pending_root_alpha = 10_000_000u64;
Subtensor::<T>::drain_pending_emission(
Subtensor::<T>::distribute_emission(
netuid,
AlphaCurrency::ZERO,
pending_root_alpha.into(),
Expand Down
1 change: 0 additions & 1 deletion pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ impl<T: Config> Pallet<T> {
RAORecycledForRegistration::<T>::remove(netuid);
MaxRegistrationsPerBlock::<T>::remove(netuid);
WeightsVersionKey::<T>::remove(netuid);
PendingRootAlphaDivs::<T>::remove(netuid);

// --- 17. Subtoken / feature flags.
LiquidAlphaOn::<T>::remove(netuid);
Expand Down
145 changes: 88 additions & 57 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ impl<T: Config> Pallet<T> {
// 2. Get subnets to emit to and emissions
let subnet_emissions = Self::get_subnet_block_emissions(&subnets, block_emission);
let subnets_to_emit_to: Vec<NetUid> = subnet_emissions.keys().copied().collect();
let root_sell_flag = Self::get_network_root_sell_flag(&subnets_to_emit_to);

// --- 3. Get subnet terms (tao_in, alpha_in, and alpha_out)
// Computation is described in detail in the dtao whitepaper.
let mut tao_in: BTreeMap<NetUid, U96F32> = BTreeMap::new();
let mut alpha_in: BTreeMap<NetUid, U96F32> = BTreeMap::new();
let mut alpha_out: BTreeMap<NetUid, U96F32> = BTreeMap::new();
let mut is_subsidized: BTreeMap<NetUid, bool> = BTreeMap::new();
let mut excess_tao: BTreeMap<NetUid, U96F32> = BTreeMap::new();

// Only calculate for subnets that we are emitting to.
for netuid_i in subnets_to_emit_to.iter() {
// Get subnet price.
Expand All @@ -52,6 +54,9 @@ impl<T: Config> Pallet<T> {
.copied()
.unwrap_or(asfloat!(0));
log::debug!("default_tao_in_i: {default_tao_in_i:?}");
let default_alpha_in_i: U96F32 =
default_tao_in_i.safe_div_or(price_i, U96F32::saturating_from_num(0.0));
log::debug!("default_alpha_in_i: {default_alpha_in_i:?}");
// Get alpha_emission total
let alpha_emission_i: U96F32 = asfloat!(
Self::get_block_emission_for_issuance(Self::get_alpha_issuance(*netuid_i).into())
Expand All @@ -62,32 +67,16 @@ impl<T: Config> Pallet<T> {
// Get initial alpha_in
let mut alpha_in_i: U96F32;
let mut tao_in_i: U96F32;
let tao_in_ratio: U96F32 = default_tao_in_i.safe_div_or(
U96F32::saturating_from_num(block_emission),
U96F32::saturating_from_num(0.0),
);
if price_i < tao_in_ratio {
tao_in_i = price_i.saturating_mul(U96F32::saturating_from_num(block_emission));
alpha_in_i = block_emission;

if default_alpha_in_i > alpha_emission_i {
alpha_in_i = alpha_emission_i;
tao_in_i = alpha_in_i.saturating_mul(price_i);
let difference_tao: U96F32 = default_tao_in_i.saturating_sub(tao_in_i);
// Difference becomes buy.
let buy_swap_result = Self::swap_tao_for_alpha(
*netuid_i,
tou64!(difference_tao).into(),
T::SwapInterface::max_price(),
true,
);
if let Ok(buy_swap_result_ok) = buy_swap_result {
let bought_alpha = AlphaCurrency::from(buy_swap_result_ok.amount_paid_out);
SubnetAlphaOut::<T>::mutate(*netuid_i, |total| {
*total = total.saturating_sub(bought_alpha);
});
}
is_subsidized.insert(*netuid_i, true);
excess_tao.insert(*netuid_i, difference_tao);
} else {
tao_in_i = default_tao_in_i;
alpha_in_i = tao_in_i.safe_div_or(price_i, alpha_emission_i);
is_subsidized.insert(*netuid_i, false);
alpha_in_i = default_alpha_in_i;
excess_tao.insert(*netuid_i, U96F32::from_num(0.0));
}
log::debug!("alpha_in_i: {alpha_in_i:?}");

Expand All @@ -109,10 +98,34 @@ impl<T: Config> Pallet<T> {
log::debug!("tao_in: {tao_in:?}");
log::debug!("alpha_in: {alpha_in:?}");
log::debug!("alpha_out: {alpha_out:?}");
log::debug!("excess_tao: {excess_tao:?}");
log::debug!("root_sell_flag: {root_sell_flag:?}");

// --- 4. Inject and buy Alpha with any excess TAO.
for netuid_i in subnets_to_emit_to.iter() {
let tao_in_i: TaoCurrency =
tou64!(*tao_in.get(netuid_i).unwrap_or(&asfloat!(0))).into();
let alpha_in_i: AlphaCurrency =
AlphaCurrency::from(tou64!(*alpha_in.get(netuid_i).unwrap_or(&asfloat!(0))));
let difference_tao: U96F32 = *excess_tao.get(netuid_i).unwrap_or(&asfloat!(0));

T::SwapInterface::adjust_protocol_liquidity(*netuid_i, tao_in_i, alpha_in_i);

if difference_tao > asfloat!(0) {
let buy_swap_result = Self::swap_tao_for_alpha(
*netuid_i,
tou64!(difference_tao).into(),
T::SwapInterface::max_price(),
true,
);
if let Ok(buy_swap_result_ok) = buy_swap_result {
let bought_alpha = AlphaCurrency::from(buy_swap_result_ok.amount_paid_out);
Self::recycle_subnet_alpha(*netuid_i, bought_alpha);
}
}
}

// --- 4. Injection.
// Actually perform the injection of alpha_in, alpha_out and tao_in into the subnet pool.
// This operation changes the pool liquidity each block.
// --- 5. Update counters
for netuid_i in subnets_to_emit_to.iter() {
// Inject Alpha in.
let alpha_in_i =
Expand All @@ -138,14 +151,15 @@ impl<T: Config> Pallet<T> {
TotalStake::<T>::mutate(|total| {
*total = total.saturating_add(tao_in_i.into());
});

let difference_tao: U96F32 = *excess_tao.get(netuid_i).unwrap_or(&asfloat!(0));
TotalIssuance::<T>::mutate(|total| {
*total = total.saturating_add(tao_in_i.into());
*total = total.saturating_add(tou64!(difference_tao).into());
});
// Adjust protocol liquidity based on new reserves
T::SwapInterface::adjust_protocol_liquidity(*netuid_i, tao_in_i, alpha_in_i);
}

// --- 5. Compute owner cuts and remove them from alpha_out remaining.
// --- 6. Compute owner cuts and remove them from alpha_out remaining.
// Remove owner cuts here so that we can properly seperate root dividends in the next step.
// Owner cuts are accumulated and then fed to the drain at the end of this func.
let cut_percent: U96F32 = Self::get_float_subnet_owner_cut();
Expand Down Expand Up @@ -174,51 +188,55 @@ impl<T: Config> Pallet<T> {
let tao_weight: U96F32 = root_tao.saturating_mul(Self::get_tao_weight());
log::debug!("tao_weight: {tao_weight:?}");

// --- 6. Seperate out root dividends in alpha and keep them.
// --- 7. Seperate out root dividends in alpha and keep them.
// Then accumulate those dividends for later.
for netuid_i in subnets_to_emit_to.iter() {
// Get remaining alpha out.
let alpha_out_i: U96F32 = *alpha_out.get(netuid_i).unwrap_or(&asfloat!(0.0));
log::debug!("alpha_out_i: {alpha_out_i:?}");
// Get total ALPHA on subnet.
let alpha_issuance: U96F32 = asfloat!(Self::get_alpha_issuance(*netuid_i));
log::debug!("alpha_issuance: {alpha_issuance:?}");
// Get root proportional dividends.
let root_proportion: U96F32 = tao_weight
.checked_div(tao_weight.saturating_add(alpha_issuance))
.unwrap_or(asfloat!(0.0));
log::debug!("root_proportion: {root_proportion:?}");

// Get root proportion of alpha_out dividends.
let root_alpha: U96F32 = root_proportion
.saturating_mul(alpha_out_i) // Total alpha emission per block remaining.
.saturating_mul(asfloat!(0.5)); // 50% to validators.
let mut root_alpha: U96F32 = asfloat!(0.0);
if root_sell_flag {
// Get ALPHA issuance.
let alpha_issuance: U96F32 = asfloat!(Self::get_alpha_issuance(*netuid_i));
log::debug!("alpha_issuance: {alpha_issuance:?}");

// Get root proportional dividends.
let root_proportion: U96F32 = tao_weight
.checked_div(tao_weight.saturating_add(alpha_issuance))
.unwrap_or(asfloat!(0.0));
log::debug!("root_proportion: {root_proportion:?}");

// Get root alpha from root prop.
root_alpha = root_proportion
.saturating_mul(alpha_out_i) // Total alpha emission per block remaining.
.saturating_mul(asfloat!(0.5)); // 50% to validators.
PendingRootAlphaDivs::<T>::mutate(*netuid_i, |total| {
*total = total.saturating_add(tou64!(root_alpha).into());
});
}
// Remove root alpha from alpha_out.
log::debug!("root_alpha: {root_alpha:?}");

// Get pending alpha as original alpha_out - root_alpha.
let pending_alpha: U96F32 = alpha_out_i.saturating_sub(root_alpha);
log::debug!("pending_alpha: {pending_alpha:?}");

let subsidized: bool = *is_subsidized.get(netuid_i).unwrap_or(&false);
if !subsidized {
PendingRootAlphaDivs::<T>::mutate(*netuid_i, |total| {
*total = total.saturating_add(tou64!(root_alpha).into());
});
}

// Accumulate alpha emission in pending.
PendingEmission::<T>::mutate(*netuid_i, |total| {
*total = total.saturating_add(tou64!(pending_alpha).into());
});
}

// --- 7. Update moving prices after using them in the emission calculation.
// --- 8. Update moving prices after using them in the emission calculation.
// Only update price EMA for subnets that we emit to.
for netuid_i in subnets_to_emit_to.iter() {
// Update moving prices after using them above.
Self::update_moving_price(*netuid_i);
}

// --- 8. Drain pending emission through the subnet based on tempo.
// --- 9. Drain pending emission through the subnet based on tempo.
// Run the epoch for *all* subnets, even if we don't emit anything.
for &netuid in subnets.iter() {
// Reveal matured weights.
Expand All @@ -245,15 +263,26 @@ impl<T: Config> Pallet<T> {
let owner_cut = PendingOwnerCut::<T>::get(netuid);
PendingOwnerCut::<T>::insert(netuid, AlphaCurrency::ZERO);

// Drain pending root alpha divs, alpha emission, and owner cut.
Self::drain_pending_emission(netuid, pending_alpha, pending_root_alpha, owner_cut);
// Distribute the emission.
Self::distribute_emission(netuid, pending_alpha, pending_root_alpha, owner_cut);
} else {
// Increment
BlocksSinceLastStep::<T>::mutate(netuid, |total| *total = total.saturating_add(1));
}
}
}

pub fn get_network_root_sell_flag(subnets_to_emit_to: &[NetUid]) -> bool {
let total_ema_price: U96F32 = subnets_to_emit_to
.iter()
.map(|netuid| Self::get_moving_alpha_price(*netuid))
.sum();

// If the total EMA price is less than or equal to 1
// then we WILL NOT root sell.
total_ema_price > U96F32::saturating_from_num(1)
}

pub fn calculate_dividends_and_incentives(
netuid: NetUid,
hotkey_emission: Vec<(T::AccountId, AlphaCurrency, AlphaCurrency)>,
Expand Down Expand Up @@ -482,6 +511,7 @@ impl<T: Config> Pallet<T> {
let destination = maybe_dest.clone().unwrap_or(hotkey.clone());

if let Some(dest) = maybe_dest {
log::debug!("incentives: auto staking {incentive:?} to {dest:?}");
Self::deposit_event(Event::<T>::AutoStakeAdded {
netuid,
destination: dest,
Expand All @@ -490,6 +520,7 @@ impl<T: Config> Pallet<T> {
incentive,
});
}

Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
&destination,
&owner,
Expand Down Expand Up @@ -600,7 +631,7 @@ impl<T: Config> Pallet<T> {
(incentives, (alpha_dividends, root_alpha_dividends))
}

pub fn drain_pending_emission(
pub fn distribute_emission(
netuid: NetUid,
pending_alpha: AlphaCurrency,
pending_root_alpha: AlphaCurrency,
Expand All @@ -611,10 +642,11 @@ impl<T: Config> Pallet<T> {
);

let tao_weight = Self::get_tao_weight();
let total_alpha = pending_alpha.saturating_add(pending_root_alpha);

// Run the epoch.
let hotkey_emission: Vec<(T::AccountId, AlphaCurrency, AlphaCurrency)> =
Self::epoch_with_mechanisms(netuid, pending_alpha.saturating_add(pending_root_alpha));
Self::epoch_with_mechanisms(netuid, total_alpha);
log::debug!("hotkey_emission: {hotkey_emission:?}");

// Compute the pending validator alpha.
Expand All @@ -630,8 +662,7 @@ impl<T: Config> Pallet<T> {
log::debug!("incentive_sum: {incentive_sum:?}");

let pending_validator_alpha = if !incentive_sum.is_zero() {
pending_alpha
.saturating_add(pending_root_alpha)
total_alpha
.saturating_div(2.into())
.saturating_sub(pending_root_alpha)
} else {
Expand Down
23 changes: 11 additions & 12 deletions pallets/subtensor/src/coinbase/subnet_emissions.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
use super::*;
use crate::alloc::borrow::ToOwned;
use alloc::collections::BTreeMap;
use safe_math::FixedExt;
use substrate_fixed::transcendental::{exp, ln};
use substrate_fixed::types::{I32F32, I64F64, U64F64, U96F32};
use subtensor_swap_interface::SwapHandler;

impl<T: Config> Pallet<T> {
pub fn get_subnet_block_emissions(
subnets: &[NetUid],
block_emission: U96F32,
) -> BTreeMap<NetUid, U96F32> {
pub fn get_subnets_to_emit_to(subnets: &[NetUid]) -> Vec<NetUid> {
// Filter out subnets with no first emission block number.
let subnets_to_emit_to: Vec<NetUid> = subnets
.to_owned()
.clone()
.into_iter()
subnets
.iter()
.filter(|netuid| FirstEmissionBlockNumber::<T>::get(*netuid).is_some())
.collect();
log::debug!("Subnets to emit to: {subnets_to_emit_to:?}");
.copied()
.collect()
}

pub fn get_subnet_block_emissions(
subnets_to_emit_to: &[NetUid],
block_emission: U96F32,
) -> BTreeMap<NetUid, U96F32> {
// Get subnet TAO emissions.
let shares = Self::get_shares(&subnets_to_emit_to);
let shares = Self::get_shares(subnets_to_emit_to);
log::debug!("Subnet emission shares = {shares:?}");

shares
Expand Down
34 changes: 9 additions & 25 deletions pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,19 +447,13 @@ impl<T: Config> Pallet<T> {
let should_refund_owner: bool = reg_at < start_block;

// 3) Compute owner's received emission in TAO at current price (ONLY if we may refund).
// Emission::<T> is Vec<AlphaCurrency>. We:
// - sum emitted α,
// We:
// - get the current alpha issuance,
// - apply owner fraction to get owner α,
// - price that α using a *simulated* AMM swap.
let mut owner_emission_tao = TaoCurrency::ZERO;
if should_refund_owner && !lock_cost.is_zero() {
let total_emitted_alpha_u128: u128 =
Emission::<T>::get(netuid)
.into_iter()
.fold(0u128, |acc, e_alpha| {
let e_u64: u64 = Into::<u64>::into(e_alpha);
acc.saturating_add(e_u64 as u128)
});
let total_emitted_alpha_u128: u128 = Self::get_alpha_issuance(netuid).to_u64() as u128;

if total_emitted_alpha_u128 > 0 {
let owner_fraction: U96F32 = Self::get_float_subnet_owner_cut();
Expand All @@ -469,22 +463,12 @@ impl<T: Config> Pallet<T> {
.saturating_to_num::<u64>();

owner_emission_tao = if owner_alpha_u64 > 0 {
let order = GetTaoForAlpha::with_amount(owner_alpha_u64);
match T::SwapInterface::sim_swap(netuid.into(), order) {
Ok(sim) => TaoCurrency::from(sim.amount_paid_out),
Err(e) => {
log::debug!(
"destroy_alpha_in_out_stakes: sim_swap owner α→τ failed (netuid={netuid:?}, alpha={owner_alpha_u64}, err={e:?}); falling back to price multiply.",
);
let cur_price: U96F32 =
T::SwapInterface::current_alpha_price(netuid.into());
let val_u64 = U96F32::from_num(owner_alpha_u64)
.saturating_mul(cur_price)
.floor()
.saturating_to_num::<u64>();
TaoCurrency::from(val_u64)
}
}
let cur_price: U96F32 = T::SwapInterface::current_alpha_price(netuid.into());
let val_u64 = U96F32::from_num(owner_alpha_u64)
.saturating_mul(cur_price)
.floor()
.saturating_to_num::<u64>();
TaoCurrency::from(val_u64)
} else {
TaoCurrency::ZERO
};
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/tests/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2827,7 +2827,7 @@ fn test_set_weights_no_parent() {
});
}

/// Test that drain_pending_emission sends childkey take fully to the nominators if childkey
/// Test that distribute_emission sends childkey take fully to the nominators if childkey
/// doesn't have its own stake, independently of parent hotkey take.
/// cargo test --package pallet-subtensor --lib -- tests::children::test_childkey_take_drain --exact --show-output
#[allow(clippy::assertions_on_constants)]
Expand Down
Loading
Loading