Skip to content

Commit f3e76e7

Browse files
committed
subsidize outside the main loop to prevent price changes
1 parent 63f961b commit f3e76e7

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,6 @@ impl<T: Config> Pallet<T> {
8787

8888
if total_moving_prices < U96F32::saturating_from_num(1.0) {
8989
let difference_tao: U96F32 = default_tao_in_i.saturating_sub(tao_in_i);
90-
// Difference becomes buy.
91-
let buy_swap_result = Self::swap_tao_for_alpha(
92-
*netuid_i,
93-
tou64!(difference_tao).into(),
94-
T::SwapInterface::max_price(),
95-
true,
96-
);
97-
if let Ok(buy_swap_result_ok) = buy_swap_result {
98-
let bought_alpha = AlphaCurrency::from(buy_swap_result_ok.amount_paid_out);
99-
SubnetAlphaOut::<T>::mutate(*netuid_i, |total| {
100-
*total = total.saturating_sub(bought_alpha);
101-
});
102-
}
10390
is_subsidized.insert(*netuid_i, true);
10491
subsidy_amount.insert(*netuid_i, difference_tao);
10592
}
@@ -132,7 +119,33 @@ impl<T: Config> Pallet<T> {
132119
log::debug!("alpha_in: {alpha_in:?}");
133120
log::debug!("alpha_out: {alpha_out:?}");
134121

135-
// --- 4. Injection.
122+
// --- 5. Subsidization
123+
// When sum of moving prices is < 1.0, we subsidize by buying ALPHA with TAO.
124+
if total_moving_prices < U96F32::saturating_from_num(1.0) {
125+
for netuid_i in subnets_to_emit_to.iter() {
126+
let subsidized: bool = *is_subsidized.get(netuid_i).unwrap_or(&false);
127+
if !subsidized {
128+
continue;
129+
}
130+
let subsidy_amount_i: U96F32 =
131+
*subsidy_amount.get(netuid_i).unwrap_or(&asfloat!(0.0));
132+
133+
let buy_swap_result = Self::swap_tao_for_alpha(
134+
*netuid_i,
135+
tou64!(subsidy_amount_i).into(),
136+
T::SwapInterface::max_price(),
137+
true,
138+
);
139+
if let Ok(buy_swap_result_ok) = buy_swap_result {
140+
let bought_alpha = AlphaCurrency::from(buy_swap_result_ok.amount_paid_out);
141+
SubnetAlphaOut::<T>::mutate(*netuid_i, |total| {
142+
*total = total.saturating_sub(bought_alpha);
143+
});
144+
}
145+
}
146+
}
147+
148+
// --- 5. Injection.
136149
// Actually perform the injection of alpha_in, alpha_out and tao_in into the subnet pool.
137150
// This operation changes the pool liquidity each block.
138151
for netuid_i in subnets_to_emit_to.iter() {
@@ -174,7 +187,7 @@ impl<T: Config> Pallet<T> {
174187
T::SwapInterface::adjust_protocol_liquidity(*netuid_i, tao_in_i, alpha_in_i);
175188
}
176189

177-
// --- 5. Compute owner cuts and remove them from alpha_out remaining.
190+
// --- 6. Compute owner cuts and remove them from alpha_out remaining.
178191
// Remove owner cuts here so that we can properly seperate root dividends in the next step.
179192
// Owner cuts are accumulated and then fed to the drain at the end of this func.
180193
let cut_percent: U96F32 = Self::get_float_subnet_owner_cut();
@@ -203,7 +216,7 @@ impl<T: Config> Pallet<T> {
203216
let tao_weight: U96F32 = root_tao.saturating_mul(Self::get_tao_weight());
204217
log::debug!("tao_weight: {tao_weight:?}");
205218

206-
// --- 6. Seperate out root dividends in alpha and sell them into tao.
219+
// --- 7. Seperate out root dividends in alpha and sell them into tao.
207220
// Then accumulate those dividends for later.
208221
for netuid_i in subnets_to_emit_to.iter() {
209222
// Get remaining alpha out.
@@ -252,14 +265,14 @@ impl<T: Config> Pallet<T> {
252265
});
253266
}
254267

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

262-
// --- 8. Drain pending emission through the subnet based on tempo.
275+
// --- 9. Drain pending emission through the subnet based on tempo.
263276
// Run the epoch for *all* subnets, even if we don't emit anything.
264277
for &netuid in subnets.iter() {
265278
// Reveal matured weights.

0 commit comments

Comments
 (0)