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