@@ -20,20 +20,27 @@ impl<T: Config> Pallet<T> {
2020 amount : u64 ,
2121 netuid : u16 ,
2222 ) -> DispatchResult {
23- let coldkey = ensure_signed ( origin) ?;
23+ let coldkey: T :: AccountId = ensure_signed ( origin) ?;
2424
2525 ensure ! (
2626 Self :: if_subnet_exist( netuid) ,
2727 Error :: <T >:: SubNetworkDoesNotExist
2828 ) ;
2929
3030 ensure ! (
31- Self :: coldkey_owns_hotkey ( & coldkey , & hotkey ) ,
32- Error :: <T >:: NonAssociatedColdKey
31+ netuid != Self :: get_root_netuid ( ) ,
32+ Error :: <T >:: CannotBurnOrRecycleOnRootSubnet
3333 ) ;
3434
35+ // Ensure that the hotkey account exists this is only possible through registration.
3536 ensure ! (
36- TotalHotkeyAlpha :: <T >:: get( & hotkey, netuid) >= amount,
37+ Self :: hotkey_account_exists( & hotkey) ,
38+ Error :: <T >:: HotKeyAccountNotExists
39+ ) ;
40+
41+ // Ensure that the hotkey has enough stake to withdraw.
42+ ensure ! (
43+ Self :: has_enough_stake_on_subnet( & hotkey, & coldkey, netuid, amount) ,
3744 Error :: <T >:: NotEnoughStakeToWithdraw
3845 ) ;
3946
@@ -42,19 +49,22 @@ impl<T: Config> Pallet<T> {
4249 Error :: <T >:: InsufficientLiquidity
4350 ) ;
4451
45- if TotalHotkeyAlpha :: < T > :: mutate ( & hotkey, netuid, |v| {
46- * v = v. saturating_sub ( amount) ;
47- * v
48- } ) == 0
49- {
50- TotalHotkeyAlpha :: < T > :: remove ( & hotkey, netuid) ;
51- }
52+ // Deduct from the coldkey's stake.
53+ let actual_alpha_decrease = Self :: decrease_stake_for_hotkey_and_coldkey_on_subnet (
54+ & hotkey, & coldkey, netuid, amount,
55+ ) ;
5256
57+ // Recycle means we should decrease the alpha issuance tracker.
5358 SubnetAlphaOut :: < T > :: mutate ( netuid, |total| {
54- * total = total. saturating_sub ( amount ) ;
59+ * total = total. saturating_sub ( actual_alpha_decrease ) ;
5560 } ) ;
5661
57- Self :: deposit_event ( Event :: AlphaRecycled ( coldkey, hotkey, amount, netuid) ) ;
62+ Self :: deposit_event ( Event :: AlphaRecycled (
63+ coldkey,
64+ hotkey,
65+ actual_alpha_decrease,
66+ netuid,
67+ ) ) ;
5868
5969 Ok ( ( ) )
6070 }
@@ -85,12 +95,19 @@ impl<T: Config> Pallet<T> {
8595 ) ;
8696
8797 ensure ! (
88- Self :: coldkey_owns_hotkey ( & coldkey , & hotkey ) ,
89- Error :: <T >:: NonAssociatedColdKey
98+ netuid != Self :: get_root_netuid ( ) ,
99+ Error :: <T >:: CannotBurnOrRecycleOnRootSubnet
90100 ) ;
91101
102+ // Ensure that the hotkey account exists this is only possible through registration.
92103 ensure ! (
93- TotalHotkeyAlpha :: <T >:: get( & hotkey, netuid) >= amount,
104+ Self :: hotkey_account_exists( & hotkey) ,
105+ Error :: <T >:: HotKeyAccountNotExists
106+ ) ;
107+
108+ // Ensure that the hotkey has enough stake to withdraw.
109+ ensure ! (
110+ Self :: has_enough_stake_on_subnet( & hotkey, & coldkey, netuid, amount) ,
94111 Error :: <T >:: NotEnoughStakeToWithdraw
95112 ) ;
96113
@@ -99,16 +116,20 @@ impl<T: Config> Pallet<T> {
99116 Error :: <T >:: InsufficientLiquidity
100117 ) ;
101118
102- if TotalHotkeyAlpha :: < T > :: mutate ( & hotkey, netuid, |v| {
103- * v = v. saturating_sub ( amount) ;
104- * v
105- } ) == 0
106- {
107- TotalHotkeyAlpha :: < T > :: remove ( & hotkey, netuid) ;
108- }
119+ // Deduct from the coldkey's stake.
120+ let actual_alpha_decrease = Self :: decrease_stake_for_hotkey_and_coldkey_on_subnet (
121+ & hotkey, & coldkey, netuid, amount,
122+ ) ;
123+
124+ // This is a burn, so we don't need to update AlphaOut.
109125
110126 // Deposit event
111- Self :: deposit_event ( Event :: AlphaBurned ( coldkey, hotkey, amount, netuid) ) ;
127+ Self :: deposit_event ( Event :: AlphaBurned (
128+ coldkey,
129+ hotkey,
130+ actual_alpha_decrease,
131+ netuid,
132+ ) ) ;
112133
113134 Ok ( ( ) )
114135 }
0 commit comments