11use super :: * ;
2- use frame_support:: storage:: IterableStorageDoubleMap ;
32use substrate_fixed:: types:: I64F64 ;
43use substrate_fixed:: types:: I96F32 ;
54
@@ -262,7 +261,7 @@ impl<T: Config> Pallet<T> {
262261 PendingdHotkeyEmission :: < T > :: insert ( hotkey, 0 ) ;
263262
264263 // --- 2 Retrieve the last time this hotkey's emissions were drained.
265- let last_hotkey_emission_drain : u64 = LastHotkeyEmissionDrain :: < T > :: get ( hotkey) ;
264+ let last_emission_drain : u64 = LastHotkeyEmissionDrain :: < T > :: get ( hotkey) ;
266265
267266 // --- 3 Update the block value to the current block number.
268267 LastHotkeyEmissionDrain :: < T > :: insert ( hotkey, block_number) ;
@@ -282,43 +281,48 @@ impl<T: Config> Pallet<T> {
282281 // --- 7 Calculate the remaining emission after the hotkey's take.
283282 let mut remainder: u64 = emission_minus_take;
284283
285- // --- 8 Iterate over each nominator.
286- for ( nominator, nominator_stake) in
287- <Stake < T > as IterableStorageDoubleMap < T :: AccountId , T :: AccountId , u64 > >:: iter_prefix (
288- hotkey,
289- )
290- {
291- // --- 9 Check if the stake was manually increased by the user since the last emission drain for this hotkey.
292- // If it was, skip this nominator as they will not receive their proportion of the emission.
293- if LastAddStakeIncrease :: < T > :: get ( hotkey, nominator. clone ( ) )
294- > last_hotkey_emission_drain
295- {
296- continue ;
284+ // --- 8 Iterate over each nominator and get all viable stake.
285+ let mut total_viable_nominator_stake: u64 = total_hotkey_stake;
286+ for ( nominator, nominator_stake) in Stake :: < T > :: iter_prefix ( hotkey) {
287+ if LastAddStakeIncrease :: < T > :: get ( hotkey, nominator) > last_emission_drain {
288+ total_viable_nominator_stake =
289+ total_viable_nominator_stake. saturating_sub ( nominator_stake) ;
297290 }
291+ }
298292
299- // --- 10 Calculate this nominator's share of the emission.
300- let nominator_emission: I64F64 = I64F64 :: from_num ( emission_minus_take)
301- . saturating_mul ( I64F64 :: from_num ( nominator_stake) )
302- . checked_div ( I64F64 :: from_num ( total_hotkey_stake) )
303- . unwrap_or ( I64F64 :: from_num ( 0 ) ) ;
304-
305- // --- 11 Increase the stake for the nominator.
306- Self :: increase_stake_on_coldkey_hotkey_account (
307- & nominator,
308- hotkey,
309- nominator_emission. to_num :: < u64 > ( ) ,
310- ) ;
293+ // --- 9 Iterate over each nominator.
294+ if total_viable_nominator_stake != 0 {
295+ for ( nominator, nominator_stake) in Stake :: < T > :: iter_prefix ( hotkey) {
296+ // --- 10 Check if the stake was manually increased by the user since the last emission drain for this hotkey.
297+ // If it was, skip this nominator as they will not receive their proportion of the emission.
298+ if LastAddStakeIncrease :: < T > :: get ( hotkey, nominator. clone ( ) ) > last_emission_drain {
299+ continue ;
300+ }
311301
312- // --- 11* Record event and Subtract the nominator's emission from the remainder.
313- total_new_tao = total_new_tao. saturating_add ( nominator_emission. to_num :: < u64 > ( ) ) ;
314- remainder = remainder. saturating_sub ( nominator_emission. to_num :: < u64 > ( ) ) ;
302+ // --- 11 Calculate this nominator's share of the emission.
303+ let nominator_emission: I64F64 = I64F64 :: from_num ( emission_minus_take)
304+ . saturating_mul ( I64F64 :: from_num ( nominator_stake) )
305+ . checked_div ( I64F64 :: from_num ( total_viable_nominator_stake) )
306+ . unwrap_or ( I64F64 :: from_num ( 0 ) ) ;
307+
308+ // --- 12 Increase the stake for the nominator.
309+ Self :: increase_stake_on_coldkey_hotkey_account (
310+ & nominator,
311+ hotkey,
312+ nominator_emission. to_num :: < u64 > ( ) ,
313+ ) ;
314+
315+ // --- 13* Record event and Subtract the nominator's emission from the remainder.
316+ total_new_tao = total_new_tao. saturating_add ( nominator_emission. to_num :: < u64 > ( ) ) ;
317+ remainder = remainder. saturating_sub ( nominator_emission. to_num :: < u64 > ( ) ) ;
318+ }
315319 }
316320
317- // --- 13 Finally, add the stake to the hotkey itself, including its take and the remaining emission.
321+ // --- 14 Finally, add the stake to the hotkey itself, including its take and the remaining emission.
318322 let hotkey_new_tao: u64 = hotkey_take. saturating_add ( remainder) ;
319323 Self :: increase_stake_on_hotkey_account ( hotkey, hotkey_new_tao) ;
320324
321- // --- 14 Record new tao creation event and return the amount created.
325+ // --- 15 Record new tao creation event and return the amount created.
322326 total_new_tao = total_new_tao. saturating_add ( hotkey_new_tao) ;
323327 total_new_tao
324328 }
0 commit comments