Skip to content
Draft
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
34 changes: 22 additions & 12 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,25 @@ impl<T: Config> Pallet<T> {
let subnet_emissions = Self::get_subnet_block_emissions(&subnets, block_emission);
let subnets_to_emit_to: Vec<NetUid> = subnet_emissions.keys().copied().collect();

// Get sum of tao reserves ( in a later version we will switch to prices. )
// Only get price EMA for subnets that we emit to.
let total_moving_prices = subnets_to_emit_to
.iter()
.map(|netuid| Self::get_moving_alpha_price(*netuid))
.fold(U96F32::saturating_from_num(0.0), |acc, ema| {
acc.saturating_add(ema)
});
log::debug!("total_moving_prices: {total_moving_prices:?}");

let subsidy_mode = total_moving_prices < U96F32::saturating_from_num(1.0);
log::debug!("subsidy_mode: {subsidy_mode:?}");

// --- 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 tao_issued: 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();
// Only calculate for subnets that we are emitting to.
for netuid_i in subnets_to_emit_to.iter() {
// Get subnet price.
Expand All @@ -62,11 +75,7 @@ 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 {
if subsidy_mode {
tao_in_i = price_i.saturating_mul(U96F32::saturating_from_num(block_emission));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tao_in_i should only be changed for subnets where ALPHA price < TAO emissions (EMA / sum of EMAs), otherwise it would inject more TAO, and would result in more than 1 TAO/block being emitted. The only reason to change the tao_in_i according to the ALPHA price * Block Emission is so that one can calculate how much TAO should be used to swap for ALPHA (to bring the ALPHA price back up).

Here a scenario:

SoS is 0.8 and there is a subnet A that is overpriced, so ALPHA price is 0.5 while TAO emissions are 0.25, the chain would inject 0.5 TAO into the subnet because it looks at ALPHA price * Block Emission, instead of the 0.25 TAO that he should get based on TAO emissions. The difference_tao_in would clamp to 0 because it would be otherwise negative (tao_in > default_tao_in). Then subnet B has 0.75 TAO emissions but an ALPHA price of 0.3, so the chain would use 0.3 TAO to inject, and 0.45 TAO to swap for ALPHA to push ALPHA price towards TAO emissions (still using 0.75 TAO in total).

Thus subnet A gets 0.5 TAO and subnet B gets 0.75 TAO, so the chain emits 1.25 TAO/block

If one were to use ALPHA prices only as commented at line 38 I think this wouldn't happen, because then there is no discrepancy between ALPHA price and EMA price, as only ALPHA price is used.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason to downscale TAO is if ALPHA needs to be downscaled, so that the injections can maintain the price, while the difference_tao_in is used to swap for ALPHA (increasing the ALPHA price towards TAO emissions), which it doesn't when ALPHA price > TAO emissions

alpha_in_i = block_emission;
let difference_tao: U96F32 = default_tao_in_i.saturating_sub(tao_in_i);
Expand All @@ -83,11 +92,9 @@ impl<T: Config> Pallet<T> {
*total = total.saturating_sub(bought_alpha);
});
}
is_subsidized.insert(*netuid_i, true);
} 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);
}
log::debug!("alpha_in_i: {alpha_in_i:?}");

Expand All @@ -103,6 +110,7 @@ impl<T: Config> Pallet<T> {
}
// Insert values into maps
tao_in.insert(*netuid_i, tao_in_i);
tao_issued.insert(*netuid_i, default_tao_in_i);
Copy link

@mcjkula mcjkula Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As commented above that wouldn't be true, when ALPHA price > TAO emissions, with the global subsidy, because the tao_in_i (ALPHA price * Block Emission) would be larger than default_tao_in_i (which is based on EMA price).

alpha_in.insert(*netuid_i, alpha_in_i);
alpha_out.insert(*netuid_i, alpha_out_i);
}
Expand Down Expand Up @@ -138,10 +146,13 @@ impl<T: Config> Pallet<T> {
TotalStake::<T>::mutate(|total| {
*total = total.saturating_add(tao_in_i.into());
});
// TAO issued
let tao_issued_i: TaoCurrency =
tou64!(*tao_issued.get(netuid_i).unwrap_or(&asfloat!(0))).into();
TotalIssuance::<T>::mutate(|total| {
*total = total.saturating_add(tao_in_i.into());
*total = total.saturating_add(tao_issued_i.into());
});
// Adjust protocol liquidity based on new reserves
// Adjust protocol liquidity based on added reserves
T::SwapInterface::adjust_protocol_liquidity(*netuid_i, tao_in_i, alpha_in_i);
}

Expand Down Expand Up @@ -198,8 +209,7 @@ impl<T: Config> Pallet<T> {
let pending_alpha: U96F32 = alpha_out_i.saturating_sub(root_alpha);
log::debug!("pending_alpha: {pending_alpha:?}");
// Sell root emission through the pool (do not pay fees)
let subsidized: bool = *is_subsidized.get(netuid_i).unwrap_or(&false);
if !subsidized {
if !subsidy_mode {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could technically stay so that the root proportion on "overpriced" subnets (ALPHA price > TAO emissions) is not countering the SoS to increase at/above 1

let swap_result = Self::swap_alpha_for_tao(
*netuid_i,
tou64!(root_alpha).into(),
Expand Down
87 changes: 81 additions & 6 deletions pallets/subtensor/src/tests/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,83 @@ fn test_coinbase_tao_issuance_different_prices() {
epsilon = 1.into(),
);

// Prices are low => we limit tao issued (buy alpha with it)
let tao_issued = TaoCurrency::from(((0.1 + 0.2) * emission as f64) as u64);
// EMA Prices are high => full emission
let tao_issued = TaoCurrency::from(emission);
assert_abs_diff_eq!(
TotalIssuance::<Test>::get(),
tao_issued,
epsilon = 10.into()
);
assert_abs_diff_eq!(
TotalStake::<Test>::get(),
emission.into(),
epsilon = 10.into()
);
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_subsidies --exact --show-output --nocapture
#[test]
fn test_coinbase_subsidies() {
new_test_ext(1).execute_with(|| {
let netuid1 = NetUid::from(1);
let netuid2 = NetUid::from(2);
let emission = 100_000_000;
add_network(netuid1, 1, 0);
add_network(netuid2, 1, 0);

// Setup prices 0.1 and 0.2
let initial_tao: u64 = 100_000_u64;
let initial_alpha1: u64 = initial_tao * 10;
let initial_alpha2: u64 = initial_tao * 5;
mock::setup_reserves(netuid1, initial_tao.into(), initial_alpha1.into());
mock::setup_reserves(netuid2, initial_tao.into(), initial_alpha2.into());

// Force the swap to initialize
SubtensorModule::swap_tao_for_alpha(
netuid1,
TaoCurrency::ZERO,
1_000_000_000_000.into(),
false,
)
.unwrap();
SubtensorModule::swap_tao_for_alpha(
netuid2,
TaoCurrency::ZERO,
1_000_000_000_000.into(),
false,
)
.unwrap();

// Make subnets dynamic.
SubnetMechanism::<Test>::insert(netuid1, 1);
SubnetMechanism::<Test>::insert(netuid2, 1);

// Set subnet EMA prices (sum < 1)
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(0.1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(0.2));

// Assert initial TAO reserves.
assert_eq!(SubnetTAO::<Test>::get(netuid1), initial_tao.into());
assert_eq!(SubnetTAO::<Test>::get(netuid2), initial_tao.into());

// Run the coinbase with the emission amount.
SubtensorModule::run_coinbase(U96F32::from_num(emission));

// Assert tao emission is split evenly and SubnetTAO additions sum to full emission
assert_abs_diff_eq!(
SubnetTAO::<Test>::get(netuid1),
TaoCurrency::from(initial_tao + emission / 3),
epsilon = 1.into(),
);
assert_abs_diff_eq!(
SubnetTAO::<Test>::get(netuid2),
TaoCurrency::from(initial_tao + 2 * emission / 3),
epsilon = 1.into(),
);

// Prices are low => we buy alpha with TAO issued, but full emission is issued
let tao_issued = TaoCurrency::from(emission);
assert_abs_diff_eq!(
TotalIssuance::<Test>::get(),
tao_issued,
Expand Down Expand Up @@ -430,8 +505,8 @@ fn test_coinbase_alpha_issuance_with_cap_trigger() {
SubnetTAO::<Test>::insert(netuid2, TaoCurrency::from(initial));
SubnetAlphaIn::<Test>::insert(netuid2, AlphaCurrency::from(initial_alpha)); // Make price extremely low.
// Set subnet prices.
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(0.1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(0.2));
// Run coinbase
SubtensorModule::run_coinbase(U96F32::from_num(emission));
// tao_in = 333_333
Expand Down Expand Up @@ -468,8 +543,8 @@ fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() {
// Enable emission
FirstEmissionBlockNumber::<Test>::insert(netuid1, 0);
FirstEmissionBlockNumber::<Test>::insert(netuid2, 0);
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(0.1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(0.2));

// Force the swap to initialize
SubtensorModule::swap_tao_for_alpha(
Expand Down
Loading