@@ -20,20 +20,27 @@ impl<T: Config> Pallet<T> {
20
20
amount : u64 ,
21
21
netuid : u16 ,
22
22
) -> DispatchResult {
23
- let coldkey = ensure_signed ( origin) ?;
23
+ let coldkey: T :: AccountId = ensure_signed ( origin) ?;
24
24
25
25
ensure ! (
26
26
Self :: if_subnet_exist( netuid) ,
27
27
Error :: <T >:: SubNetworkDoesNotExist
28
28
) ;
29
29
30
30
ensure ! (
31
- Self :: coldkey_owns_hotkey ( & coldkey , & hotkey ) ,
32
- Error :: <T >:: NonAssociatedColdKey
31
+ netuid != Self :: get_root_netuid ( ) ,
32
+ Error :: <T >:: CannotBurnOrRecycleOnRootSubnet
33
33
) ;
34
34
35
+ // Ensure that the hotkey account exists this is only possible through registration.
35
36
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) ,
37
44
Error :: <T >:: NotEnoughStakeToWithdraw
38
45
) ;
39
46
@@ -42,19 +49,22 @@ impl<T: Config> Pallet<T> {
42
49
Error :: <T >:: InsufficientLiquidity
43
50
) ;
44
51
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
+ ) ;
52
56
57
+ // Recycle means we should decrease the alpha issuance tracker.
53
58
SubnetAlphaOut :: < T > :: mutate ( netuid, |total| {
54
- * total = total. saturating_sub ( amount ) ;
59
+ * total = total. saturating_sub ( actual_alpha_decrease ) ;
55
60
} ) ;
56
61
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
+ ) ) ;
58
68
59
69
Ok ( ( ) )
60
70
}
@@ -85,12 +95,19 @@ impl<T: Config> Pallet<T> {
85
95
) ;
86
96
87
97
ensure ! (
88
- Self :: coldkey_owns_hotkey ( & coldkey , & hotkey ) ,
89
- Error :: <T >:: NonAssociatedColdKey
98
+ netuid != Self :: get_root_netuid ( ) ,
99
+ Error :: <T >:: CannotBurnOrRecycleOnRootSubnet
90
100
) ;
91
101
102
+ // Ensure that the hotkey account exists this is only possible through registration.
92
103
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) ,
94
111
Error :: <T >:: NotEnoughStakeToWithdraw
95
112
) ;
96
113
@@ -99,16 +116,20 @@ impl<T: Config> Pallet<T> {
99
116
Error :: <T >:: InsufficientLiquidity
100
117
) ;
101
118
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.
109
125
110
126
// 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
+ ) ) ;
112
133
113
134
Ok ( ( ) )
114
135
}
0 commit comments