@@ -432,26 +432,18 @@ impl<T: Config> Pallet<T> {
432432 Error :: <T >:: UnstakeRateLimitExceeded
433433 ) ;
434434
435- // If this is a nomination stake, check if total stake after removing will be above
436- // the minimum required stake.
437-
438- // If coldkey is not owner of the hotkey, it's a nomination stake.
439- if !Self :: coldkey_owns_hotkey ( & coldkey, & hotkey) {
440- let total_stake_after_remove =
441- Stake :: < T > :: get ( & hotkey, & coldkey) . saturating_sub ( stake_to_be_removed) ;
442-
443- ensure ! (
444- total_stake_after_remove >= NominatorMinRequiredStake :: <T >:: get( ) ,
445- Error :: <T >:: NomStakeBelowMinimumThreshold
446- ) ;
447- }
448-
449435 // We remove the balance from the hotkey.
450436 Self :: decrease_stake_on_coldkey_hotkey_account ( & coldkey, & hotkey, stake_to_be_removed) ;
451437
452- // We add the balancer to the coldkey. If the above fails we will not credit this coldkey.
438+ // We add the balance to the coldkey. If the above fails we will not credit this coldkey.
453439 Self :: add_balance_to_coldkey_account ( & coldkey, stake_to_be_removed) ;
454440
441+ // If the stake is below the minimum, we clear the nomination from storage.
442+ // This only applies to nominator stakes.
443+ // If the coldkey does not own the hotkey, it's a nominator stake.
444+ let new_stake = Self :: get_stake_for_coldkey_and_hotkey ( & coldkey, & hotkey) ;
445+ Self :: clear_small_nomination_if_required ( & hotkey, & coldkey, new_stake) ;
446+
455447 // Set last block for rate limiting
456448 let block: u64 = Self :: get_current_block_as_u64 ( ) ;
457449 Self :: set_last_tx_block ( & coldkey, block) ;
@@ -675,17 +667,24 @@ impl<T: Config> Pallet<T> {
675667 /// It also removes the stake entry for the hotkey-coldkey pairing and adjusts the TotalStake
676668 /// and TotalIssuance by subtracting the removed stake amount.
677669 ///
670+ /// Returns the amount of stake that was removed.
671+ ///
678672 /// # Arguments
679673 ///
680674 /// * `coldkey` - A reference to the AccountId of the coldkey involved in the staking.
681675 /// * `hotkey` - A reference to the AccountId of the hotkey associated with the coldkey.
682- pub fn empty_stake_on_coldkey_hotkey_account ( coldkey : & T :: AccountId , hotkey : & T :: AccountId ) {
676+ pub fn empty_stake_on_coldkey_hotkey_account (
677+ coldkey : & T :: AccountId ,
678+ hotkey : & T :: AccountId ,
679+ ) -> u64 {
683680 let current_stake: u64 = Stake :: < T > :: get ( hotkey, coldkey) ;
684681 TotalColdkeyStake :: < T > :: mutate ( coldkey, |old| * old = old. saturating_sub ( current_stake) ) ;
685682 TotalHotkeyStake :: < T > :: mutate ( hotkey, |stake| * stake = stake. saturating_sub ( current_stake) ) ;
686683 Stake :: < T > :: remove ( hotkey, coldkey) ;
687684 TotalStake :: < T > :: mutate ( |stake| * stake = stake. saturating_sub ( current_stake) ) ;
688685 TotalIssuance :: < T > :: mutate ( |issuance| * issuance = issuance. saturating_sub ( current_stake) ) ;
686+
687+ current_stake
689688 }
690689
691690 /// Clears the nomination for an account, if it is a nominator account and the stake is below the minimum required threshold.
@@ -700,9 +699,9 @@ impl<T: Config> Pallet<T> {
700699 if stake < Self :: get_nominator_min_required_stake ( ) {
701700 // Remove the stake from the nominator account. (this is a more forceful unstake operation which )
702701 // Actually deletes the staking account.
703- Self :: empty_stake_on_coldkey_hotkey_account ( coldkey, hotkey) ;
702+ let cleared_stake = Self :: empty_stake_on_coldkey_hotkey_account ( coldkey, hotkey) ;
704703 // Add the stake to the coldkey account.
705- Self :: add_balance_to_coldkey_account ( coldkey, stake ) ;
704+ Self :: add_balance_to_coldkey_account ( coldkey, cleared_stake ) ;
706705 }
707706 }
708707 }
0 commit comments