1
1
use super :: * ;
2
2
use sp_core:: Get ;
3
+ use substrate_fixed:: types:: I96F32 ;
3
4
4
5
impl < T : Config > Pallet < T > {
5
6
/// ---- The implementation for the extrinsic remove_stake: Removes stake from a hotkey account and adds it onto a coldkey.
@@ -58,7 +59,11 @@ impl<T: Config> Pallet<T> {
58
59
) ?;
59
60
60
61
// 3. Swap the alpba to tao and update counters for this subnet.
61
- let fee = DefaultStakingFee :: < T > :: get ( ) ;
62
+ let fee = Self :: calculate_staking_fee (
63
+ netuid,
64
+ & hotkey,
65
+ I96F32 :: saturating_from_num ( alpha_unstaked) ,
66
+ ) ;
62
67
let tao_unstaked: u64 =
63
68
Self :: unstake_from_subnet ( & hotkey, & coldkey, netuid, alpha_unstaked, fee) ;
64
69
@@ -109,8 +114,6 @@ impl<T: Config> Pallet<T> {
109
114
origin : T :: RuntimeOrigin ,
110
115
hotkey : T :: AccountId ,
111
116
) -> dispatch:: DispatchResult {
112
- let fee = DefaultStakingFee :: < T > :: get ( ) ;
113
-
114
117
// 1. We check the transaction is signed by the caller and retrieve the T::AccountId coldkey information.
115
118
let coldkey = ensure_signed ( origin) ?;
116
119
log:: info!( "do_unstake_all( origin:{:?} hotkey:{:?} )" , coldkey, hotkey) ;
@@ -126,20 +129,26 @@ impl<T: Config> Pallet<T> {
126
129
log:: debug!( "All subnet netuids: {:?}" , netuids) ;
127
130
128
131
// 4. Iterate through all subnets and remove stake.
129
- for netuid in netuids. iter ( ) {
132
+ for netuid in netuids. into_iter ( ) {
130
133
// Ensure that the hotkey has enough stake to withdraw.
131
134
let alpha_unstaked =
132
- Self :: get_stake_for_hotkey_and_coldkey_on_subnet ( & hotkey, & coldkey, * netuid) ;
135
+ Self :: get_stake_for_hotkey_and_coldkey_on_subnet ( & hotkey, & coldkey, netuid) ;
136
+ let fee = Self :: calculate_staking_fee (
137
+ netuid,
138
+ & hotkey,
139
+ I96F32 :: saturating_from_num ( alpha_unstaked) ,
140
+ ) ;
141
+
133
142
if alpha_unstaked > 0 {
134
143
// Swap the alpha to tao and update counters for this subnet.
135
144
let tao_unstaked: u64 =
136
- Self :: unstake_from_subnet ( & hotkey, & coldkey, * netuid, alpha_unstaked, fee) ;
145
+ Self :: unstake_from_subnet ( & hotkey, & coldkey, netuid, alpha_unstaked, fee) ;
137
146
138
147
// Add the balance to the coldkey. If the above fails we will not credit this coldkey.
139
148
Self :: add_balance_to_coldkey_account ( & coldkey, tao_unstaked) ;
140
149
141
150
// If the stake is below the minimum, we clear the nomination from storage.
142
- Self :: clear_small_nomination_if_required ( & hotkey, & coldkey, * netuid) ;
151
+ Self :: clear_small_nomination_if_required ( & hotkey, & coldkey, netuid) ;
143
152
}
144
153
}
145
154
@@ -177,8 +186,6 @@ impl<T: Config> Pallet<T> {
177
186
origin : T :: RuntimeOrigin ,
178
187
hotkey : T :: AccountId ,
179
188
) -> dispatch:: DispatchResult {
180
- let fee = DefaultStakingFee :: < T > :: get ( ) ;
181
-
182
189
// 1. We check the transaction is signed by the caller and retrieve the T::AccountId coldkey information.
183
190
let coldkey = ensure_signed ( origin) ?;
184
191
log:: info!( "do_unstake_all( origin:{:?} hotkey:{:?} )" , coldkey, hotkey) ;
@@ -195,22 +202,28 @@ impl<T: Config> Pallet<T> {
195
202
196
203
// 4. Iterate through all subnets and remove stake.
197
204
let mut total_tao_unstaked: u64 = 0 ;
198
- for netuid in netuids. iter ( ) {
205
+ for netuid in netuids. into_iter ( ) {
199
206
// If not Root network.
200
- if * netuid != Self :: get_root_netuid ( ) {
207
+ if netuid != Self :: get_root_netuid ( ) {
201
208
// Ensure that the hotkey has enough stake to withdraw.
202
209
let alpha_unstaked =
203
- Self :: get_stake_for_hotkey_and_coldkey_on_subnet ( & hotkey, & coldkey, * netuid) ;
210
+ Self :: get_stake_for_hotkey_and_coldkey_on_subnet ( & hotkey, & coldkey, netuid) ;
211
+ let fee = Self :: calculate_staking_fee (
212
+ netuid,
213
+ & hotkey,
214
+ I96F32 :: saturating_from_num ( alpha_unstaked) ,
215
+ ) ;
216
+
204
217
if alpha_unstaked > 0 {
205
218
// Swap the alpha to tao and update counters for this subnet.
206
- let tao_unstaked: u64 =
207
- Self :: unstake_from_subnet ( & hotkey, & coldkey, * netuid, alpha_unstaked, fee) ;
219
+ let tao_unstaked =
220
+ Self :: unstake_from_subnet ( & hotkey, & coldkey, netuid, alpha_unstaked, fee) ;
208
221
209
222
// Increment total
210
223
total_tao_unstaked = total_tao_unstaked. saturating_add ( tao_unstaked) ;
211
224
212
225
// If the stake is below the minimum, we clear the nomination from storage.
213
- Self :: clear_small_nomination_if_required ( & hotkey, & coldkey, * netuid) ;
226
+ Self :: clear_small_nomination_if_required ( & hotkey, & coldkey, netuid) ;
214
227
}
215
228
}
216
229
}
@@ -302,8 +315,12 @@ impl<T: Config> Pallet<T> {
302
315
) ?;
303
316
304
317
// 4. Swap the alpha to tao and update counters for this subnet.
305
- let fee = DefaultStakingFee :: < T > :: get ( ) ;
306
- let tao_unstaked: u64 =
318
+ let fee = Self :: calculate_staking_fee (
319
+ netuid,
320
+ & hotkey,
321
+ I96F32 :: saturating_from_num ( alpha_unstaked) ,
322
+ ) ;
323
+ let tao_unstaked =
307
324
Self :: unstake_from_subnet ( & hotkey, & coldkey, netuid, possible_alpha, fee) ;
308
325
309
326
// 5. We add the balance to the coldkey. If the above fails we will not credit this coldkey.
0 commit comments