@@ -34,19 +34,13 @@ impl<T: Config> Pallet<T> {
3434 // 2. Get subnets to emit to and emissions
3535 let subnet_emissions = Self :: get_subnet_block_emissions ( & subnets, block_emission) ;
3636 let subnets_to_emit_to: Vec < NetUid > = subnet_emissions. keys ( ) . copied ( ) . collect ( ) ;
37- let total_ema_price: U96F32 = subnets_to_emit_to
38- . iter ( )
39- . map ( |netuid| Self :: get_moving_alpha_price ( * netuid) )
40- . sum ( ) ;
41- let subsidy_mode = total_ema_price <= U96F32 :: saturating_from_num ( 1 ) ;
4237
4338 // --- 3. Get subnet terms (tao_in, alpha_in, and alpha_out)
4439 // Computation is described in detail in the dtao whitepaper.
4540 let mut tao_in: BTreeMap < NetUid , U96F32 > = BTreeMap :: new ( ) ;
4641 let mut alpha_in: BTreeMap < NetUid , U96F32 > = BTreeMap :: new ( ) ;
4742 let mut alpha_out: BTreeMap < NetUid , U96F32 > = BTreeMap :: new ( ) ;
48- let mut subsidy_amount: BTreeMap < NetUid , U96F32 > = BTreeMap :: new ( ) ;
49-
43+ let mut is_subsidized: BTreeMap < NetUid , bool > = BTreeMap :: new ( ) ;
5044 // Only calculate for subnets that we are emitting to.
5145 for netuid_i in subnets_to_emit_to. iter ( ) {
5246 // Get subnet price.
@@ -68,16 +62,32 @@ impl<T: Config> Pallet<T> {
6862 // Get initial alpha_in
6963 let mut alpha_in_i: U96F32 ;
7064 let mut tao_in_i: U96F32 ;
71-
72- if default_alpha_in_i > alpha_emission_i {
73- alpha_in_i = alpha_emission_i;
74- tao_in_i = alpha_in_i. saturating_mul ( price_i) ;
65+ let tao_in_ratio: U96F32 = default_tao_in_i. safe_div_or (
66+ U96F32 :: saturating_from_num ( block_emission) ,
67+ U96F32 :: saturating_from_num ( 0.0 ) ,
68+ ) ;
69+ if price_i < tao_in_ratio {
70+ tao_in_i = price_i. saturating_mul ( U96F32 :: saturating_from_num ( block_emission) ) ;
71+ alpha_in_i = block_emission;
7572 let difference_tao: U96F32 = default_tao_in_i. saturating_sub ( tao_in_i) ;
76- subsidy_amount. insert ( * netuid_i, difference_tao) ;
73+ // Difference becomes buy.
74+ let buy_swap_result = Self :: swap_tao_for_alpha (
75+ * netuid_i,
76+ tou64 ! ( difference_tao) . into ( ) ,
77+ T :: SwapInterface :: max_price ( ) ,
78+ true ,
79+ ) ;
80+ if let Ok ( buy_swap_result_ok) = buy_swap_result {
81+ let bought_alpha = AlphaCurrency :: from ( buy_swap_result_ok. amount_paid_out ) ;
82+ SubnetAlphaOut :: < T > :: mutate ( * netuid_i, |total| {
83+ * total = total. saturating_sub ( bought_alpha) ;
84+ } ) ;
85+ }
86+ is_subsidized. insert ( * netuid_i, true ) ;
7787 } else {
7888 tao_in_i = default_tao_in_i;
79- alpha_in_i = alpha_in_default_i ;
80- subsidy_amount . insert ( * netuid_i, U96F32 :: from_num ( 0.0 ) ) ;
89+ alpha_in_i = tao_in_i . safe_div_or ( price_i , alpha_emission_i ) ;
90+ is_subsidized . insert ( * netuid_i, false ) ;
8191 }
8292 log:: debug!( "alpha_in_i: {alpha_in_i:?}" ) ;
8393
@@ -100,33 +110,9 @@ impl<T: Config> Pallet<T> {
100110 log:: debug!( "alpha_in: {alpha_in:?}" ) ;
101111 log:: debug!( "alpha_out: {alpha_out:?}" ) ;
102112
103- // --- 4. Inject and subsidize
104- for netuid_i in subnets_to_emit_to. iter ( ) {
105- let tao_in_i: TaoCurrency =
106- tou64 ! ( * tao_in. get( netuid_i) . unwrap_or( & asfloat!( 0 ) ) ) . into ( ) ;
107- let alpha_in_i: AlphaCurrency =
108- AlphaCurrency :: from ( tou64 ! ( * alpha_in. get( netuid_i) . unwrap_or( & asfloat!( 0 ) ) ) ) ;
109- let difference_tao: U96F32 = * subsidy_amount. get ( netuid_i) . unwrap_or ( & asfloat ! ( 0 ) ) ;
110-
111- T :: SwapInterface :: adjust_protocol_liquidity ( * netuid_i, tao_in_i, alpha_in_i) ;
112-
113- if difference_tao > asfloat ! ( 0 ) {
114- let buy_swap_result = Self :: swap_tao_for_alpha (
115- * netuid_i,
116- tou64 ! ( difference_tao) . into ( ) ,
117- T :: SwapInterface :: max_price ( ) ,
118- true ,
119- ) ;
120- if let Ok ( buy_swap_result_ok) = buy_swap_result {
121- let bought_alpha = AlphaCurrency :: from ( buy_swap_result_ok. amount_paid_out ) ;
122- SubnetAlphaOut :: < T > :: mutate ( * netuid_i, |total| {
123- * total = total. saturating_sub ( bought_alpha) ;
124- } ) ;
125- }
126- }
127- }
128-
129- // --- 5. Update counters
113+ // --- 4. Injection.
114+ // Actually perform the injection of alpha_in, alpha_out and tao_in into the subnet pool.
115+ // This operation changes the pool liquidity each block.
130116 for netuid_i in subnets_to_emit_to. iter ( ) {
131117 // Inject Alpha in.
132118 let alpha_in_i =
@@ -152,15 +138,14 @@ impl<T: Config> Pallet<T> {
152138 TotalStake :: < T > :: mutate ( |total| {
153139 * total = total. saturating_add ( tao_in_i. into ( ) ) ;
154140 } ) ;
155-
156- let difference_tao: U96F32 = * subsidy_amount. get ( netuid_i) . unwrap_or ( & asfloat ! ( 0 ) ) ;
157141 TotalIssuance :: < T > :: mutate ( |total| {
158142 * total = total. saturating_add ( tao_in_i. into ( ) ) ;
159- * total = total. saturating_add ( tou64 ! ( difference_tao) . into ( ) ) ;
160143 } ) ;
144+ // Adjust protocol liquidity based on new reserves
145+ T :: SwapInterface :: adjust_protocol_liquidity ( * netuid_i, tao_in_i, alpha_in_i) ;
161146 }
162147
163- // --- 6 . Compute owner cuts and remove them from alpha_out remaining.
148+ // --- 5 . Compute owner cuts and remove them from alpha_out remaining.
164149 // Remove owner cuts here so that we can properly seperate root dividends in the next step.
165150 // Owner cuts are accumulated and then fed to the drain at the end of this func.
166151 let cut_percent: U96F32 = Self :: get_float_subnet_owner_cut ( ) ;
@@ -189,7 +174,7 @@ impl<T: Config> Pallet<T> {
189174 let tao_weight: U96F32 = root_tao. saturating_mul ( Self :: get_tao_weight ( ) ) ;
190175 log:: debug!( "tao_weight: {tao_weight:?}" ) ;
191176
192- // --- 7 . Seperate out root dividends in alpha and keep them.
177+ // --- 6 . Seperate out root dividends in alpha and keep them.
193178 // Then accumulate those dividends for later.
194179 for netuid_i in subnets_to_emit_to. iter ( ) {
195180 // Get remaining alpha out.
@@ -228,14 +213,14 @@ impl<T: Config> Pallet<T> {
228213 } ) ;
229214 }
230215
231- // --- 8 . Update moving prices after using them in the emission calculation.
216+ // --- 7 . Update moving prices after using them in the emission calculation.
232217 // Only update price EMA for subnets that we emit to.
233218 for netuid_i in subnets_to_emit_to. iter ( ) {
234219 // Update moving prices after using them above.
235220 Self :: update_moving_price ( * netuid_i) ;
236221 }
237222
238- // --- 9 . Drain pending emission through the subnet based on tempo.
223+ // --- 8 . Drain pending emission through the subnet based on tempo.
239224 // Run the epoch for *all* subnets, even if we don't emit anything.
240225 for & netuid in subnets. iter ( ) {
241226 // Reveal matured weights.
0 commit comments