1
1
use super :: * ;
2
- use frame_support:: storage:: IterableStorageDoubleMap ;
3
2
use substrate_fixed:: types:: I64F64 ;
4
3
use substrate_fixed:: types:: I96F32 ;
5
4
@@ -262,7 +261,7 @@ impl<T: Config> Pallet<T> {
262
261
PendingdHotkeyEmission :: < T > :: insert ( hotkey, 0 ) ;
263
262
264
263
// --- 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) ;
266
265
267
266
// --- 3 Update the block value to the current block number.
268
267
LastHotkeyEmissionDrain :: < T > :: insert ( hotkey, block_number) ;
@@ -282,43 +281,48 @@ impl<T: Config> Pallet<T> {
282
281
// --- 7 Calculate the remaining emission after the hotkey's take.
283
282
let mut remainder: u64 = emission_minus_take;
284
283
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) ;
297
290
}
291
+ }
298
292
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
+ }
311
301
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
+ }
315
319
}
316
320
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.
318
322
let hotkey_new_tao: u64 = hotkey_take. saturating_add ( remainder) ;
319
323
Self :: increase_stake_on_hotkey_account ( hotkey, hotkey_new_tao) ;
320
324
321
- // --- 14 Record new tao creation event and return the amount created.
325
+ // --- 15 Record new tao creation event and return the amount created.
322
326
total_new_tao = total_new_tao. saturating_add ( hotkey_new_tao) ;
323
327
total_new_tao
324
328
}
0 commit comments