Skip to content

Commit d5e97c3

Browse files
authored
Merge pull request #1445 from opentensor/fix/tx-fees
Adjust staking fees
2 parents ea8d0e3 + 589f9f2 commit d5e97c3

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ impl<T: Config> Pallet<T> {
10971097
DefaultStakingFee::<T>::get()
10981098
} else {
10991099
// Otherwise, calculate the fee based on the alpha estimate
1100-
let fee = alpha_estimate
1100+
let mut fee = alpha_estimate
11011101
.saturating_mul(
11021102
I96F32::saturating_from_num(AlphaDividendsPerSubnet::<T>::get(
11031103
origin_netuid,
@@ -1110,6 +1110,16 @@ impl<T: Config> Pallet<T> {
11101110
.saturating_mul(Self::get_alpha_price(origin_netuid)) // fee needs to be in TAO
11111111
.saturating_to_num::<u64>();
11121112

1113+
// 0.005% per epoch matches to 44% annual in compound interest. Do not allow the fee
1114+
// to be lower than that. (1.00005^(365*20) ~= 1.44)
1115+
let apr_20_percent = I96F32::saturating_from_num(0.00005);
1116+
fee = fee.max(
1117+
alpha_estimate
1118+
.saturating_mul(apr_20_percent)
1119+
.saturating_to_num::<u64>(),
1120+
);
1121+
1122+
// We should at least get DefaultStakingFee anyway
11131123
fee.max(DefaultStakingFee::<T>::get())
11141124
}
11151125
}

pallets/subtensor/src/tests/staking.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,10 @@ fn test_remove_stake_fee_realistic_values() {
23112311
);
23122312

23132313
// Estimate fees
2314-
let expected_fee: f64 = current_price * alpha_divs as f64;
2314+
let mut expected_fee: f64 = current_price * alpha_divs as f64;
2315+
if expected_fee < alpha_to_unstake as f64 * 0.00005 {
2316+
expected_fee = alpha_to_unstake as f64 * 0.00005;
2317+
}
23152318

23162319
// Remove stake to measure fee
23172320
let balance_before = SubtensorModule::get_coldkey_balance(&coldkey);
@@ -3903,7 +3906,7 @@ fn test_remove_99_9991_per_cent_stake_removes_all() {
39033906
let coldkey_account_id = U256::from(81337);
39043907
let amount = 10_000_000_000;
39053908
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
3906-
let fee = DefaultStakingFee::<Test>::get();
3909+
let mut fee = DefaultStakingFee::<Test>::get();
39073910
register_ok_neuron(netuid, hotkey_account_id, coldkey_account_id, 192213123);
39083911

39093912
// Give it some $$$ in his coldkey balance
@@ -3923,17 +3926,19 @@ fn test_remove_99_9991_per_cent_stake_removes_all() {
39233926
&coldkey_account_id,
39243927
netuid,
39253928
);
3929+
let remove_amount = (U64F64::from_num(alpha) * U64F64::from_num(0.999991)).to_num::<u64>();
39263930
assert_ok!(SubtensorModule::remove_stake(
39273931
RuntimeOrigin::signed(coldkey_account_id),
39283932
hotkey_account_id,
39293933
netuid,
3930-
(U64F64::from_num(alpha) * U64F64::from_num(0.999991)).to_num::<u64>()
3934+
remove_amount,
39313935
));
39323936

39333937
// Check that all alpha was unstaked and all TAO balance was returned (less fees)
3938+
fee = fee + fee.max((remove_amount as f64 * 0.00005) as u64);
39343939
assert_abs_diff_eq!(
39353940
SubtensorModule::get_coldkey_balance(&coldkey_account_id),
3936-
amount - fee * 2,
3941+
amount - fee,
39373942
epsilon = 10000,
39383943
);
39393944
assert_eq!(

0 commit comments

Comments
 (0)