@@ -569,6 +569,13 @@ impl<T: Config> Pallet<T> {
569569 hotkeys. push ( hotkey. clone ( ) ) ;
570570 OwnedHotkeys :: < T > :: insert ( coldkey, hotkeys) ;
571571 }
572+
573+ // Update StakingHotkeys map
574+ let mut staking_hotkeys = StakingHotkeys :: < T > :: get ( coldkey) ;
575+ if !staking_hotkeys. contains ( hotkey) {
576+ staking_hotkeys. push ( hotkey. clone ( ) ) ;
577+ StakingHotkeys :: < T > :: insert ( coldkey, staking_hotkeys) ;
578+ }
572579 }
573580 }
574581
@@ -648,6 +655,13 @@ impl<T: Config> Pallet<T> {
648655 Stake :: < T > :: get ( hotkey, coldkey) . saturating_add ( increment) ,
649656 ) ;
650657 TotalStake :: < T > :: put ( TotalStake :: < T > :: get ( ) . saturating_add ( increment) ) ;
658+
659+ // Update StakingHotkeys map
660+ let mut staking_hotkeys = StakingHotkeys :: < T > :: get ( coldkey) ;
661+ if !staking_hotkeys. contains ( hotkey) {
662+ staking_hotkeys. push ( hotkey. clone ( ) ) ;
663+ StakingHotkeys :: < T > :: insert ( coldkey, staking_hotkeys) ;
664+ }
651665 }
652666
653667 // Decreases the stake on the cold - hot pairing by the decrement while decreasing other counters.
@@ -668,6 +682,8 @@ impl<T: Config> Pallet<T> {
668682 Stake :: < T > :: get ( hotkey, coldkey) . saturating_sub ( decrement) ,
669683 ) ;
670684 TotalStake :: < T > :: put ( TotalStake :: < T > :: get ( ) . saturating_sub ( decrement) ) ;
685+
686+ // TODO: Tech debt: Remove StakingHotkeys entry if stake goes to 0
671687 }
672688
673689 /// Empties the stake associated with a given coldkey-hotkey account pairing.
@@ -693,6 +709,11 @@ impl<T: Config> Pallet<T> {
693709 TotalStake :: < T > :: mutate ( |stake| * stake = stake. saturating_sub ( current_stake) ) ;
694710 TotalIssuance :: < T > :: mutate ( |issuance| * issuance = issuance. saturating_sub ( current_stake) ) ;
695711
712+ // Update StakingHotkeys map
713+ let mut staking_hotkeys = StakingHotkeys :: < T > :: get ( coldkey) ;
714+ staking_hotkeys. retain ( |h| h != hotkey) ;
715+ StakingHotkeys :: < T > :: insert ( coldkey, staking_hotkeys) ;
716+
696717 current_stake
697718 }
698719
@@ -897,6 +918,25 @@ impl<T: Config> Pallet<T> {
897918 }
898919 }
899920
921+ // Unstake all delegate stake make by this coldkey to non-owned hotkeys
922+ let staking_hotkeys = StakingHotkeys :: < T > :: get ( & current_coldkey) ;
923+
924+ // iterate over all staking hotkeys.
925+ for hotkey in staking_hotkeys {
926+ // Get the current stake
927+ let current_stake: u64 =
928+ Self :: get_stake_for_coldkey_and_hotkey ( & current_coldkey, & hotkey) ;
929+
930+ // Unstake all balance if there's any stake
931+ if current_stake > 0 {
932+ Self :: do_remove_stake (
933+ RawOrigin :: Signed ( current_coldkey. clone ( ) ) . into ( ) ,
934+ hotkey. clone ( ) ,
935+ current_stake,
936+ ) ?;
937+ }
938+ }
939+
900940 let total_balance = Self :: get_coldkey_balance ( & current_coldkey) ;
901941 log:: info!( "Total Bank Balance: {:?}" , total_balance) ;
902942
0 commit comments