@@ -606,6 +606,7 @@ impl<T: Config> Pallet<T> {
606
606
coldkey : & T :: AccountId ,
607
607
netuid : u16 ,
608
608
alpha : u64 ,
609
+ fee : u64 ,
609
610
) -> u64 {
610
611
// Step 1: Swap the alpha for TAO.
611
612
let tao: u64 = Self :: swap_alpha_for_tao ( netuid, alpha) ;
@@ -621,25 +622,35 @@ impl<T: Config> Pallet<T> {
621
622
// });
622
623
// }
623
624
624
- // Step 4. Deposit and log the unstaking event.
625
+ // Step 4. Reduce tao amount by staking fee and credit this fee to SubnetTAO
626
+ let tao_unstaked = tao. saturating_sub ( fee) ;
627
+ let actual_fee = tao. saturating_sub ( tao_unstaked) ;
628
+ SubnetTAO :: < T > :: mutate ( netuid, |total| {
629
+ * total = total. saturating_add ( actual_fee) ;
630
+ } ) ;
631
+ TotalStake :: < T > :: mutate ( |total| {
632
+ * total = total. saturating_add ( actual_fee) ;
633
+ } ) ;
634
+
635
+ // Step 5. Deposit and log the unstaking event.
625
636
Self :: deposit_event ( Event :: StakeRemoved (
626
637
coldkey. clone ( ) ,
627
638
hotkey. clone ( ) ,
628
- tao ,
639
+ tao_unstaked ,
629
640
alpha,
630
641
netuid,
631
642
) ) ;
632
643
log:: info!(
633
644
"StakeRemoved( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?} )" ,
634
645
coldkey. clone( ) ,
635
646
hotkey. clone( ) ,
636
- tao ,
647
+ tao_unstaked ,
637
648
alpha,
638
649
netuid
639
650
) ;
640
651
641
- // Step 5 : Return the amount of TAO unstaked.
642
- tao
652
+ // Step 6 : Return the amount of TAO unstaked.
653
+ tao_unstaked
643
654
}
644
655
645
656
/// Stakes TAO into a subnet for a given hotkey and coldkey pair.
@@ -650,38 +661,54 @@ impl<T: Config> Pallet<T> {
650
661
coldkey : & T :: AccountId ,
651
662
netuid : u16 ,
652
663
tao : u64 ,
664
+ fee : u64 ,
653
665
) -> u64 {
654
- // Step 1. Swap the tao to alpha.
655
- let alpha: u64 = Self :: swap_tao_for_alpha ( netuid, tao) ;
656
-
657
- // Step 2: Increase the alpha on the hotkey account.
658
- Self :: increase_stake_for_hotkey_and_coldkey_on_subnet ( hotkey, coldkey, netuid, alpha) ;
659
-
660
- // Step 4: Update the list of hotkeys staking for this coldkey
661
- let mut staking_hotkeys = StakingHotkeys :: < T > :: get ( coldkey) ;
662
- if !staking_hotkeys. contains ( hotkey) {
663
- staking_hotkeys. push ( hotkey. clone ( ) ) ;
664
- StakingHotkeys :: < T > :: insert ( coldkey, staking_hotkeys. clone ( ) ) ;
666
+ // Step 1. Reduce tao amount by staking fee and credit this fee to SubnetTAO
667
+ // At this point tao was already withdrawn from the user balance and is considered
668
+ // available
669
+ let tao_staked = tao. saturating_sub ( fee) ;
670
+ let actual_fee = tao. saturating_sub ( tao_staked) ;
671
+
672
+ // Step 2. Swap the tao to alpha.
673
+ let alpha: u64 = Self :: swap_tao_for_alpha ( netuid, tao_staked) ;
674
+ if ( tao_staked > 0 ) && ( alpha > 0 ) {
675
+ // Step 3: Increase the alpha on the hotkey account.
676
+ Self :: increase_stake_for_hotkey_and_coldkey_on_subnet ( hotkey, coldkey, netuid, alpha) ;
677
+
678
+ // Step 4: Update the list of hotkeys staking for this coldkey
679
+ let mut staking_hotkeys = StakingHotkeys :: < T > :: get ( coldkey) ;
680
+ if !staking_hotkeys. contains ( hotkey) {
681
+ staking_hotkeys. push ( hotkey. clone ( ) ) ;
682
+ StakingHotkeys :: < T > :: insert ( coldkey, staking_hotkeys. clone ( ) ) ;
683
+ }
665
684
}
666
685
667
- // Step 5. Deposit and log the staking event.
686
+ // Step 5. Increase Tao reserves by the fee amount.
687
+ SubnetTAO :: < T > :: mutate ( netuid, |total| {
688
+ * total = total. saturating_add ( actual_fee) ;
689
+ } ) ;
690
+ TotalStake :: < T > :: mutate ( |total| {
691
+ * total = total. saturating_add ( actual_fee) ;
692
+ } ) ;
693
+
694
+ // Step 6. Deposit and log the staking event.
668
695
Self :: deposit_event ( Event :: StakeAdded (
669
696
coldkey. clone ( ) ,
670
697
hotkey. clone ( ) ,
671
- tao ,
698
+ tao_staked ,
672
699
alpha,
673
700
netuid,
674
701
) ) ;
675
702
log:: info!(
676
703
"StakeAdded( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?} )" ,
677
704
coldkey. clone( ) ,
678
705
hotkey. clone( ) ,
679
- tao ,
706
+ tao_staked ,
680
707
alpha,
681
708
netuid
682
709
) ;
683
710
684
- // Step 6 : Return the amount of alpha staked
711
+ // Step 7 : Return the amount of alpha staked
685
712
alpha
686
713
}
687
714
0 commit comments