Skip to content

Commit 8456061

Browse files
committed
Add test for fee amount
1 parent 8fc45ba commit 8456061

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

pallets/subtensor/src/staking/add_stake.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use super::*;
2-
use safe_math::SafeDiv;
3-
use sp_core::Get;
42
use substrate_fixed::types::I96F32;
53

64
impl<T: Config> Pallet<T> {

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::*;
2-
use sp_core::Get;
32
use substrate_fixed::types::I96F32;
43

54
impl<T: Config> Pallet<T> {

pallets/subtensor/src/tests/staking.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,67 @@ fn test_remove_stake_fee_goes_to_subnet_tao() {
22192219
});
22202220
}
22212221

2222+
// cargo test --package pallet-subtensor --lib -- tests::staking::test_add_stake_fee_equals_dividends --exact --show-output --nocapture
2223+
#[test]
2224+
fn test_add_stake_fee_equals_dividends() {
2225+
new_test_ext(1).execute_with(|| {
2226+
let subnet_owner_coldkey = U256::from(1001);
2227+
let subnet_owner_hotkey = U256::from(1002);
2228+
let hotkey = U256::from(2);
2229+
let coldkey = U256::from(3);
2230+
let tao_to_stake = DefaultMinStake::<Test>::get() * 10_000;
2231+
2232+
let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
2233+
SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
2234+
2235+
// Add stake first time to init TotalHotkeyAlpha
2236+
SubtensorModule::add_balance_to_coldkey_account(
2237+
&coldkey,
2238+
2 * (tao_to_stake + ExistentialDeposit::get()),
2239+
);
2240+
assert_ok!(SubtensorModule::add_stake(
2241+
RuntimeOrigin::signed(coldkey),
2242+
hotkey,
2243+
netuid,
2244+
tao_to_stake
2245+
));
2246+
2247+
// Mock 0.001 per alpha dividends
2248+
AlphaDividendsPerSubnet::<Test>::insert(
2249+
netuid,
2250+
hotkey,
2251+
TotalHotkeyAlpha::<Test>::get(hotkey, netuid) / 1000,
2252+
);
2253+
2254+
// Mock price to be 1 and a lot of liquidity
2255+
let tao_reserve: U96F32 = U96F32::from_num(100_000_000_000_000_u64);
2256+
let alpha_in: U96F32 = U96F32::from_num(100_000_000_000_000_u64);
2257+
SubnetTAO::<Test>::insert(netuid, tao_reserve.to_num::<u64>());
2258+
SubnetAlphaIn::<Test>::insert(netuid, alpha_in.to_num::<u64>());
2259+
2260+
// Add stake again to measure fee
2261+
let alpha_before =
2262+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
2263+
assert_ok!(SubtensorModule::add_stake(
2264+
RuntimeOrigin::signed(coldkey),
2265+
hotkey,
2266+
netuid,
2267+
tao_to_stake
2268+
));
2269+
2270+
// Calculate expected fee
2271+
let expected_alpha_no_fee = tao_to_stake;
2272+
let alpha_after =
2273+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
2274+
2275+
let actual_fee = expected_alpha_no_fee - (alpha_after - alpha_before);
2276+
let expected_fee =
2277+
(expected_alpha_no_fee as f64 * 0.001 * Tempo::<Test>::get(netuid) as f64) as u64;
2278+
2279+
assert_abs_diff_eq!(actual_fee, expected_fee, epsilon = expected_fee / 1000);
2280+
});
2281+
}
2282+
22222283
#[test]
22232284
fn test_stake_below_min_validate() {
22242285
// Testing the signed extension validate function

pallets/subtensor/src/tests/swap_coldkey.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,15 +591,14 @@ fn test_swap_concurrent_modifications() {
591591
&mut weight
592592
));
593593

594-
let eps = 500; // RAO
595594
assert_abs_diff_eq!(
596595
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
597596
&hotkey,
598597
&new_coldkey,
599598
netuid
600599
),
601600
stake_before_swap + additional_stake - fee,
602-
epsilon = eps
601+
epsilon = (stake_before_swap + additional_stake - fee) / 1000
603602
);
604603
assert!(!Alpha::<Test>::contains_key((hotkey, old_coldkey, netuid)));
605604
});

0 commit comments

Comments
 (0)