Skip to content

Commit a0268c5

Browse files
committed
split more functions
1 parent d735f7c commit a0268c5

File tree

1 file changed

+89
-75
lines changed

1 file changed

+89
-75
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 89 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -98,84 +98,28 @@ impl<T: Config> Pallet<T> {
9898
}
9999

100100
pub fn emission_to_subnet(netuid: NetUid, block_emission: U96F32, total_moving_prices: U96F32) {
101-
// --- 3. Get subnet terms (tao_in, alpha_in, and alpha_out)
102-
// Computation is described in detail in the dtao whitepaper.
103-
// let mut tao_in: BTreeMap<NetUid, U96F32> = BTreeMap::new();
104-
// let mut alpha_in: BTreeMap<NetUid, U96F32> = BTreeMap::new();
105-
// let mut alpha_out: BTreeMap<NetUid, U96F32> = BTreeMap::new();
106-
// let mut is_subsidized: BTreeMap<NetUid, bool> = BTreeMap::new();
107-
108-
// Get subnet price.
109-
let price = T::SwapInterface::current_alpha_price((netuid).into());
110-
log::debug!("price as :{price:?} in subnet {netuid:?}");
111-
112-
// Get subnet TAO.
113-
let moving_price: U96F32 = Self::get_moving_alpha_price(netuid);
114-
log::debug!("moving_price as {moving_price:?} in subnet {netuid:?}");
115-
116-
let moving_price_ratio: U96F32 =
117-
moving_price.safe_div_or(total_moving_prices, asfloat!(0.0));
118-
log::debug!("moving_price_ratio as {moving_price_ratio:?} in subnet {netuid:?}");
119-
120-
// Emission is moving price ratio over total.
121-
let default_tao_in: U96F32 = block_emission.saturating_mul(moving_price_ratio);
122-
log::debug!("default_tao_in as {default_tao_in:?} in subnet {netuid:?}");
123-
101+
let allow_registration = Self::get_network_registration_allowed(netuid)
102+
|| Self::get_network_pow_registration_allowed(netuid);
124103
// Get alpha_emission total
125104
let alpha_emission: U96F32 = asfloat!(
126105
Self::get_block_emission_for_issuance(Self::get_alpha_issuance(netuid).into())
127106
.unwrap_or(0)
128107
);
129-
log::debug!("alpha_emission as {alpha_emission:?} in subnet {netuid:?}");
130108

131-
// Get initial alpha_in
132-
let mut alpha_in_value: U96F32;
133-
let mut tao_in_value: U96F32;
134-
let is_subsidized: bool;
135-
136-
// If the price is equal to or greater than the moving_price_ratio, tao in from the moving price ratio.
137-
// alpha in from the tao in div price.
138-
// Else tao in from price multiple block emission.
139-
if price < moving_price_ratio {
140-
tao_in_value = price.saturating_mul(U96F32::saturating_from_num(block_emission));
141-
alpha_in_value = block_emission;
142-
// get the difference from the price multiple block emission and the default tao in.
143-
let difference_tao: U96F32 = default_tao_in.saturating_sub(tao_in_value);
144-
145-
// Difference becomes buy.
146-
let buy_swap_result = Self::swap_tao_for_alpha(
147-
netuid,
148-
tou64!(difference_tao).into(),
149-
T::SwapInterface::max_price(),
150-
true,
151-
);
109+
let (tao_in_value, alpha_in_value, is_subsidized) = Self::calculate_subnet_injection(
110+
netuid,
111+
block_emission,
112+
alpha_emission,
113+
total_moving_prices,
114+
allow_registration,
115+
);
152116

153-
// bought alpha go to alpha out.
154-
if let Ok(buy_swap_result_ok) = buy_swap_result {
155-
let bought_alpha = AlphaCurrency::from(buy_swap_result_ok.amount_paid_out);
156-
SubnetAlphaOut::<T>::mutate(netuid, |total| {
157-
*total = total.saturating_sub(bought_alpha);
158-
});
159-
}
160-
is_subsidized = true;
117+
// Get alpha_out.
118+
let alpha_out_value = if allow_registration {
119+
alpha_emission
161120
} else {
162-
tao_in_value = default_tao_in;
163-
alpha_in_value = tao_in_value.safe_div_or(price, alpha_emission);
164-
is_subsidized = false;
121+
asfloat!(0.0)
165122
};
166-
log::debug!("alpha_in_value as {alpha_in_value:?} in subnet {netuid:?}");
167-
168-
// Get alpha_out.
169-
let mut alpha_out_value = alpha_emission;
170-
171-
// Only emit TAO if the subnetwork allows registration.
172-
if !Self::get_network_registration_allowed(netuid)
173-
&& !Self::get_network_pow_registration_allowed(netuid)
174-
{
175-
tao_in_value = asfloat!(0.0);
176-
alpha_in_value = asfloat!(0.0);
177-
alpha_out_value = asfloat!(0.0);
178-
}
179123

180124
// Inject Alpha in.
181125
let alpha_in_currency = AlphaCurrency::from(tou64!(alpha_in_value));
@@ -207,9 +151,17 @@ impl<T: Config> Pallet<T> {
207151
// Adjust protocol liquidity based on new reserves
208152
T::SwapInterface::adjust_protocol_liquidity(netuid, tao_in_currency, alpha_in_currency);
209153

210-
// --- 5. Compute owner cuts and remove them from alpha_out remaining.
211-
// Remove owner cuts here so that we can properly seperate root dividends in the next step.
212-
// Owner cuts are accumulated and then fed to the drain at the end of this func.
154+
if allow_registration {
155+
Self::split_alpha_out(netuid, alpha_out_value, is_subsidized);
156+
}
157+
158+
// --- 7. Update moving prices after using them in the emission calculation.
159+
// Update moving prices after using them above.
160+
Self::update_moving_price(netuid);
161+
}
162+
163+
pub fn split_alpha_out(netuid: NetUid, alpha_out_value: U96F32, is_subsidized: bool) {
164+
let mut alpha_out_value = alpha_out_value;
213165
let cut_percent: U96F32 = Self::get_float_subnet_owner_cut();
214166
let mut owner_cuts: BTreeMap<NetUid, U96F32> = BTreeMap::new();
215167

@@ -293,10 +245,72 @@ impl<T: Config> Pallet<T> {
293245
PendingEmission::<T>::mutate(netuid, |total| {
294246
*total = total.saturating_add(tou64!(pending_alpha).into());
295247
});
248+
}
296249

297-
// --- 7. Update moving prices after using them in the emission calculation.
298-
// Update moving prices after using them above.
299-
Self::update_moving_price(netuid);
250+
/// Calculates the injection of TAO and Alpha into the subnet.
251+
pub fn calculate_subnet_injection(
252+
netuid: NetUid,
253+
block_emission: U96F32,
254+
alpha_emission: U96F32,
255+
total_moving_prices: U96F32,
256+
allow_registration: bool,
257+
) -> (U96F32, U96F32, bool) {
258+
// Get subnet price.
259+
let price = T::SwapInterface::current_alpha_price((netuid).into());
260+
log::debug!("price as :{price:?} in subnet {netuid:?}");
261+
262+
// Get subnet TAO.
263+
let moving_price: U96F32 = Self::get_moving_alpha_price(netuid);
264+
log::debug!("moving_price as {moving_price:?} in subnet {netuid:?}");
265+
266+
let moving_price_ratio: U96F32 =
267+
moving_price.safe_div_or(total_moving_prices, asfloat!(0.0));
268+
log::debug!("moving_price_ratio as {moving_price_ratio:?} in subnet {netuid:?}");
269+
270+
// Emission is moving price ratio over total.
271+
let default_tao_in: U96F32 = block_emission.saturating_mul(moving_price_ratio);
272+
log::debug!("default_tao_in as {default_tao_in:?} in subnet {netuid:?}");
273+
274+
// Get initial alpha_in
275+
let alpha_in_value: U96F32;
276+
let tao_in_value: U96F32;
277+
let is_subsidized: bool = price < moving_price_ratio;
278+
log::debug!("is_subsidized as {is_subsidized:?} in subnet {netuid:?}");
279+
280+
// TODO confirm if we should swap tao for alpha for registration not allowed subnet.
281+
if is_subsidized {
282+
tao_in_value = price.saturating_mul(U96F32::saturating_from_num(block_emission));
283+
alpha_in_value = block_emission;
284+
// get the difference from the price multiple block emission and the default tao in.
285+
let difference_tao: U96F32 = default_tao_in.saturating_sub(tao_in_value);
286+
287+
// Difference becomes buy.
288+
let buy_swap_result = Self::swap_tao_for_alpha(
289+
netuid,
290+
tou64!(difference_tao).into(),
291+
T::SwapInterface::max_price(),
292+
true,
293+
);
294+
295+
// bought alpha go to alpha out.
296+
if let Ok(buy_swap_result_ok) = buy_swap_result {
297+
let bought_alpha = AlphaCurrency::from(buy_swap_result_ok.amount_paid_out);
298+
SubnetAlphaOut::<T>::mutate(netuid, |total| {
299+
*total = total.saturating_sub(bought_alpha);
300+
});
301+
}
302+
} else {
303+
tao_in_value = default_tao_in;
304+
alpha_in_value = tao_in_value.safe_div_or(price, alpha_emission);
305+
};
306+
log::debug!("alpha_in_value as {alpha_in_value:?} in subnet {netuid:?}");
307+
308+
// Only emit TAO if the subnetwork allows registration.
309+
if allow_registration {
310+
(tao_in_value, alpha_in_value, is_subsidized)
311+
} else {
312+
(asfloat!(0.0), asfloat!(0.0), is_subsidized)
313+
}
300314
}
301315

302316
pub fn calculate_dividends_and_incentives(

0 commit comments

Comments
 (0)