Skip to content

Commit a3db8e3

Browse files
committed
init ema flows from zero
1 parent 6304dbe commit a3db8e3

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

pallets/subtensor/src/coinbase/subnet_emissions.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use alloc::collections::BTreeMap;
33
use safe_math::FixedExt;
44
use substrate_fixed::transcendental::{exp, ln};
55
use substrate_fixed::types::{I32F32, I64F64, U64F64, U96F32};
6-
use subtensor_swap_interface::SwapHandler;
76

87
impl<T: Config> Pallet<T> {
98
pub fn get_subnets_to_emit_to(subnets: &[NetUid]) -> Vec<NetUid> {
@@ -56,32 +55,24 @@ impl<T: Config> Pallet<T> {
5655

5756
// Calculate net ema flow for the next block
5857
let block_flow = I64F64::saturating_from_num(SubnetTaoFlow::<T>::get(netuid));
59-
if let Some((last_block, last_block_ema)) = SubnetEmaTaoFlow::<T>::get(netuid) {
60-
// EMA flow already initialized
61-
if last_block != current_block {
62-
let flow_alpha = I64F64::saturating_from_num(FlowEmaSmoothingFactor::<T>::get())
63-
.safe_div(I64F64::saturating_from_num(i64::MAX));
64-
let one = I64F64::saturating_from_num(1);
65-
let ema_flow = (one.saturating_sub(flow_alpha))
66-
.saturating_mul(last_block_ema)
67-
.saturating_add(flow_alpha.saturating_mul(block_flow));
68-
SubnetEmaTaoFlow::<T>::insert(netuid, (current_block, ema_flow));
69-
70-
// Drop the accumulated flow in the last block
71-
Self::reset_tao_outflow(netuid);
72-
ema_flow
73-
} else {
74-
last_block_ema
75-
}
76-
} else {
77-
// Initialize EMA flow, set S(current_block) = min(price, ema_price) * init_factor
78-
let init_factor = I64F64::saturating_from_num(1_000_000_000);
79-
let moving_price = I64F64::saturating_from_num(Self::get_moving_alpha_price(netuid));
80-
let current_price =
81-
I64F64::saturating_from_num(T::SwapInterface::current_alpha_price(netuid));
82-
let ema_flow = init_factor.saturating_mul(moving_price.min(current_price));
58+
let (last_block, last_block_ema) =
59+
SubnetEmaTaoFlow::<T>::get(netuid).unwrap_or((0, I64F64::saturating_from_num(0)));
60+
61+
// EMA flow already initialized
62+
if last_block != current_block {
63+
let flow_alpha = I64F64::saturating_from_num(FlowEmaSmoothingFactor::<T>::get())
64+
.safe_div(I64F64::saturating_from_num(i64::MAX));
65+
let one = I64F64::saturating_from_num(1);
66+
let ema_flow = (one.saturating_sub(flow_alpha))
67+
.saturating_mul(last_block_ema)
68+
.saturating_add(flow_alpha.saturating_mul(block_flow));
8369
SubnetEmaTaoFlow::<T>::insert(netuid, (current_block, ema_flow));
70+
71+
// Drop the accumulated flow in the last block
72+
Self::reset_tao_outflow(netuid);
8473
ema_flow
74+
} else {
75+
last_block_ema
8576
}
8677
}
8778

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
220220
// `spec_version`, and `authoring_version` are the same between Wasm and native.
221221
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
222222
// the compatible custom types.
223-
spec_version: 347,
223+
spec_version: 348,
224224
impl_version: 1,
225225
apis: RUNTIME_API_VERSIONS,
226226
transaction_version: 1,

0 commit comments

Comments
 (0)