Skip to content

Commit 0897a2e

Browse files
committed
calculate staking fee based on everything
1 parent 10d9843 commit 0897a2e

File tree

4 files changed

+66
-26
lines changed

4 files changed

+66
-26
lines changed

pallets/subtensor/src/staking/move_stake.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,10 @@ impl<T: Config> Pallet<T> {
339339

340340
// Unstake from the origin subnet, returning TAO (or a 1:1 equivalent).
341341
let fee = Self::calculate_staking_fee(
342-
origin_netuid,
343-
origin_hotkey,
342+
Some((origin_hotkey, origin_netuid)),
343+
origin_coldkey,
344+
Some((destination_hotkey, destination_netuid)),
345+
destination_coldkey,
344346
I96F32::saturating_from_num(alpha_amount),
345347
)
346348
.safe_div(2);

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ impl<T: Config> Pallet<T> {
5959

6060
// 3. Swap the alpba to tao and update counters for this subnet.
6161
let fee = Self::calculate_staking_fee(
62-
netuid,
63-
&hotkey,
62+
Some((&hotkey, netuid)),
63+
&coldkey,
64+
None,
65+
&coldkey,
6466
I96F32::saturating_from_num(alpha_unstaked),
6567
);
6668
let tao_unstaked: u64 =
@@ -133,8 +135,10 @@ impl<T: Config> Pallet<T> {
133135
let alpha_unstaked =
134136
Self::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
135137
let fee = Self::calculate_staking_fee(
136-
netuid,
137-
&hotkey,
138+
Some((&hotkey, netuid)),
139+
&coldkey,
140+
None,
141+
&coldkey,
138142
I96F32::saturating_from_num(alpha_unstaked),
139143
);
140144

@@ -208,8 +212,10 @@ impl<T: Config> Pallet<T> {
208212
let alpha_unstaked =
209213
Self::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
210214
let fee = Self::calculate_staking_fee(
211-
netuid,
212-
&hotkey,
215+
Some((&hotkey, netuid)),
216+
&coldkey,
217+
None,
218+
&coldkey,
213219
I96F32::saturating_from_num(alpha_unstaked),
214220
);
215221

@@ -315,8 +321,10 @@ impl<T: Config> Pallet<T> {
315321

316322
// 4. Swap the alpha to tao and update counters for this subnet.
317323
let fee = Self::calculate_staking_fee(
318-
netuid,
319-
&hotkey,
324+
Some((&hotkey, netuid)),
325+
&coldkey,
326+
None,
327+
&coldkey,
320328
I96F32::saturating_from_num(alpha_unstaked),
321329
);
322330
let tao_unstaked =

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,24 +1073,48 @@ impl<T: Config> Pallet<T> {
10731073
}
10741074

10751075
pub(crate) fn calculate_staking_fee(
1076-
netuid: u16,
1077-
hotkey: &T::AccountId,
1076+
origin: Option<(&T::AccountId, u16)>,
1077+
_origin_coldkey: &T::AccountId,
1078+
destination: Option<(&T::AccountId, u16)>,
1079+
_destination_coldkey: &T::AccountId,
10781080
alpha_estimate: I96F32,
10791081
) -> u64 {
1080-
if (netuid == Self::get_root_netuid()) || (SubnetMechanism::<T>::get(netuid)) == 0 {
1081-
DefaultStakingFee::<T>::get()
1082-
} else {
1083-
let fee = alpha_estimate
1084-
.saturating_mul(
1085-
I96F32::saturating_from_num(AlphaDividendsPerSubnet::<T>::get(netuid, &hotkey))
1086-
.safe_div(I96F32::saturating_from_num(TotalHotkeyAlpha::<T>::get(
1087-
&hotkey, netuid,
1088-
))),
1089-
)
1090-
.saturating_mul(Self::get_alpha_price(netuid)) // fee needs to be in TAO
1091-
.saturating_to_num::<u64>();
1082+
match origin {
1083+
// If origin is defined, we are removing/moving stake
1084+
Some((origin_hotkey, origin_netuid)) => {
1085+
if let Some((_destination_hotkey, destination_netuid)) = destination {
1086+
// This is a stake move/swap/transfer
1087+
if destination_netuid == origin_netuid {
1088+
// If destination is on the same subnet, use the default fee
1089+
return DefaultStakingFee::<T>::get();
1090+
}
1091+
}
10921092

1093-
fee.max(DefaultStakingFee::<T>::get())
1093+
if origin_netuid == Self::get_root_netuid()
1094+
|| SubnetMechanism::<T>::get(origin_netuid) == 0
1095+
{
1096+
// If the origin netuid is root, or the subnet mechanism is 0, use the default fee
1097+
DefaultStakingFee::<T>::get()
1098+
} else {
1099+
// Otherwise, calculate the fee based on the alpha estimate
1100+
let fee = alpha_estimate
1101+
.saturating_mul(
1102+
I96F32::saturating_from_num(AlphaDividendsPerSubnet::<T>::get(
1103+
origin_netuid,
1104+
&origin_hotkey,
1105+
))
1106+
.safe_div(I96F32::saturating_from_num(
1107+
TotalHotkeyAlpha::<T>::get(&origin_hotkey, origin_netuid),
1108+
)),
1109+
)
1110+
.saturating_mul(Self::get_alpha_price(origin_netuid)) // fee needs to be in TAO
1111+
.saturating_to_num::<u64>();
1112+
1113+
fee.max(DefaultStakingFee::<T>::get())
1114+
}
1115+
}
1116+
// If origin is not defined, we are adding stake; use default fee
1117+
None => DefaultStakingFee::<T>::get(),
10941118
}
10951119
}
10961120
}

pallets/subtensor/src/tests/swap_coldkey.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,13 @@ fn test_swap_concurrent_modifications() {
540540
let additional_stake = 500_000_000_000;
541541
let initial_stake_alpha =
542542
I96F32::from(initial_stake).saturating_mul(SubtensorModule::get_alpha_price(netuid));
543-
let fee = SubtensorModule::calculate_staking_fee(netuid, &hotkey, initial_stake_alpha);
543+
let fee = SubtensorModule::calculate_staking_fee(
544+
None,
545+
&new_coldkey,
546+
Some((&hotkey, netuid)),
547+
&new_coldkey,
548+
initial_stake_alpha,
549+
);
544550

545551
// Setup initial state
546552
add_network(netuid, 1, 1);

0 commit comments

Comments
 (0)