Skip to content

Commit 6067577

Browse files
committed
Global subsidies based on ema prices
1 parent bf92ee4 commit 6067577

File tree

2 files changed

+86
-17
lines changed

2 files changed

+86
-17
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,23 @@ impl<T: Config> Pallet<T> {
3939
log::debug!("Subnets to emit to: {subnets_to_emit_to:?}");
4040

4141
// --- 2. Get sum of tao reserves ( in a later version we will switch to prices. )
42-
let mut acc_total_moving_prices = U96F32::saturating_from_num(0.0);
4342
// Only get price EMA for subnets that we emit to.
44-
for netuid_i in subnets_to_emit_to.iter() {
45-
// Get and update the moving price of each subnet adding the total together.
46-
acc_total_moving_prices =
47-
acc_total_moving_prices.saturating_add(Self::get_moving_alpha_price(*netuid_i));
48-
}
49-
let total_moving_prices = acc_total_moving_prices;
43+
let total_moving_prices = subnets_to_emit_to
44+
.iter()
45+
.map(|netuid| Self::get_moving_alpha_price(*netuid))
46+
.fold(U96F32::saturating_from_num(0.0), |acc, ema| {
47+
acc.saturating_add(ema)
48+
});
5049
log::debug!("total_moving_prices: {total_moving_prices:?}");
5150

51+
let subsidy_mode = total_moving_prices < U96F32::saturating_from_num(1.0);
52+
log::debug!("subsidy_mode: {subsidy_mode:?}");
53+
5254
// --- 3. Get subnet terms (tao_in, alpha_in, and alpha_out)
5355
// Computation is described in detail in the dtao whitepaper.
5456
let mut tao_in: BTreeMap<NetUid, U96F32> = BTreeMap::new();
5557
let mut alpha_in: BTreeMap<NetUid, U96F32> = BTreeMap::new();
5658
let mut alpha_out: BTreeMap<NetUid, U96F32> = BTreeMap::new();
57-
let mut is_subsidized: BTreeMap<NetUid, bool> = BTreeMap::new();
5859
// Only calculate for subnets that we are emitting to.
5960
for netuid_i in subnets_to_emit_to.iter() {
6061
// Get subnet price.
@@ -79,11 +80,7 @@ impl<T: Config> Pallet<T> {
7980
// Get initial alpha_in
8081
let mut alpha_in_i: U96F32;
8182
let mut tao_in_i: U96F32;
82-
let tao_in_ratio: U96F32 = default_tao_in_i.safe_div_or(
83-
U96F32::saturating_from_num(block_emission),
84-
U96F32::saturating_from_num(0.0),
85-
);
86-
if price_i < tao_in_ratio {
83+
if subsidy_mode {
8784
tao_in_i = price_i.saturating_mul(U96F32::saturating_from_num(block_emission));
8885
alpha_in_i = block_emission;
8986
let difference_tao: U96F32 = default_tao_in_i.saturating_sub(tao_in_i);
@@ -100,11 +97,9 @@ impl<T: Config> Pallet<T> {
10097
*total = total.saturating_sub(bought_alpha);
10198
});
10299
}
103-
is_subsidized.insert(*netuid_i, true);
104100
} else {
105101
tao_in_i = default_tao_in_i;
106102
alpha_in_i = tao_in_i.safe_div_or(price_i, alpha_emission_i);
107-
is_subsidized.insert(*netuid_i, false);
108103
}
109104
log::debug!("alpha_in_i: {alpha_in_i:?}");
110105

@@ -215,8 +210,7 @@ impl<T: Config> Pallet<T> {
215210
let pending_alpha: U96F32 = alpha_out_i.saturating_sub(root_alpha);
216211
log::debug!("pending_alpha: {pending_alpha:?}");
217212
// Sell root emission through the pool (do not pay fees)
218-
let subsidized: bool = *is_subsidized.get(netuid_i).unwrap_or(&false);
219-
if !subsidized {
213+
if !subsidy_mode {
220214
let swap_result = Self::swap_alpha_for_tao(
221215
*netuid_i,
222216
tou64!(root_alpha).into(),

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,81 @@ fn test_coinbase_tao_issuance_different_prices() {
207207
epsilon = 1.into(),
208208
);
209209

210+
// EMA Prices are high => full emission
211+
let tao_issued = TaoCurrency::from(emission);
212+
assert_abs_diff_eq!(
213+
TotalIssuance::<Test>::get(),
214+
tao_issued,
215+
epsilon = 10.into()
216+
);
217+
assert_abs_diff_eq!(
218+
TotalStake::<Test>::get(),
219+
emission.into(),
220+
epsilon = 10.into()
221+
);
222+
});
223+
}
224+
225+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_subsidies --exact --show-output --nocapture
226+
#[test]
227+
fn test_coinbase_subsidies() {
228+
new_test_ext(1).execute_with(|| {
229+
let netuid1 = NetUid::from(1);
230+
let netuid2 = NetUid::from(2);
231+
let emission = 100_000_000;
232+
add_network(netuid1, 1, 0);
233+
add_network(netuid2, 1, 0);
234+
235+
// Setup prices 0.1 and 0.2
236+
let initial_tao: u64 = 100_000_u64;
237+
let initial_alpha1: u64 = initial_tao * 10;
238+
let initial_alpha2: u64 = initial_tao * 5;
239+
mock::setup_reserves(netuid1, initial_tao.into(), initial_alpha1.into());
240+
mock::setup_reserves(netuid2, initial_tao.into(), initial_alpha2.into());
241+
242+
// Force the swap to initialize
243+
SubtensorModule::swap_tao_for_alpha(
244+
netuid1,
245+
TaoCurrency::ZERO,
246+
1_000_000_000_000.into(),
247+
false,
248+
)
249+
.unwrap();
250+
SubtensorModule::swap_tao_for_alpha(
251+
netuid2,
252+
TaoCurrency::ZERO,
253+
1_000_000_000_000.into(),
254+
false,
255+
)
256+
.unwrap();
257+
258+
// Make subnets dynamic.
259+
SubnetMechanism::<Test>::insert(netuid1, 1);
260+
SubnetMechanism::<Test>::insert(netuid2, 1);
261+
262+
// Set subnet EMA prices (sum < 1)
263+
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(0.1));
264+
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(0.2));
265+
266+
// Assert initial TAO reserves.
267+
assert_eq!(SubnetTAO::<Test>::get(netuid1), initial_tao.into());
268+
assert_eq!(SubnetTAO::<Test>::get(netuid2), initial_tao.into());
269+
270+
// Run the coinbase with the emission amount.
271+
SubtensorModule::run_coinbase(U96F32::from_num(emission));
272+
273+
// Assert tao emission is split evenly.
274+
assert_abs_diff_eq!(
275+
SubnetTAO::<Test>::get(netuid1),
276+
TaoCurrency::from(initial_tao + emission / 3),
277+
epsilon = 1.into(),
278+
);
279+
assert_abs_diff_eq!(
280+
SubnetTAO::<Test>::get(netuid2),
281+
TaoCurrency::from(initial_tao + 2 * emission / 3),
282+
epsilon = 1.into(),
283+
);
284+
210285
// Prices are low => we limit tao issued (buy alpha with it)
211286
let tao_issued = TaoCurrency::from(((0.1 + 0.2) * emission as f64) as u64);
212287
assert_abs_diff_eq!(

0 commit comments

Comments
 (0)