Skip to content

Commit 2b2a02e

Browse files
committed
add fee calc test
1 parent d24410e commit 2b2a02e

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

pallets/subtensor/src/tests/staking2.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,135 @@ fn test_stake_fee_api() {
814814
assert_eq!(stake_fee_8, dynamic_fee_8);
815815
});
816816
}
817+
818+
#[test]
819+
fn test_stake_fee_calculation() {
820+
new_test_ext(1).execute_with(|| {
821+
let hotkey1 = U256::from(1);
822+
let coldkey1 = U256::from(2);
823+
let hotkey2 = U256::from(3);
824+
let coldkey2 = U256::from(4);
825+
826+
let netuid0 = 1;
827+
let netuid1 = 2;
828+
let root_netuid = SubtensorModule::get_root_netuid();
829+
// Set SubnetMechanism to 1 (Dynamic)
830+
SubnetMechanism::<Test>::insert(netuid0, 1);
831+
SubnetMechanism::<Test>::insert(netuid1, 1);
832+
833+
let alpha_divs = 100_000_000_000;
834+
let total_hotkey_alpha = 100_000_000_000;
835+
let tao_in = 100_000_000_000; // 100 TAO
836+
let reciprocal_price = 2; // 1 / price
837+
let stake_amount = 100_000_000_000_u64;
838+
839+
let default_fee = DefaultStakingFee::<Test>::get();
840+
841+
// Setup alpha out
842+
SubnetAlphaOut::<Test>::insert(netuid0, 100_000_000_000);
843+
SubnetAlphaOut::<Test>::insert(netuid1, 100_000_000_000);
844+
// Set pools using price
845+
SubnetAlphaIn::<Test>::insert(netuid0, tao_in * reciprocal_price);
846+
SubnetTAO::<Test>::insert(netuid0, tao_in);
847+
SubnetAlphaIn::<Test>::insert(netuid1, tao_in * reciprocal_price);
848+
SubnetTAO::<Test>::insert(netuid1, tao_in);
849+
850+
// Setup alpha divs for hotkey1
851+
AlphaDividendsPerSubnet::<Test>::insert(netuid0, hotkey1, alpha_divs);
852+
AlphaDividendsPerSubnet::<Test>::insert(netuid1, hotkey1, alpha_divs);
853+
854+
// Setup total hotkey alpha for hotkey1
855+
TotalHotkeyAlpha::<Test>::insert(hotkey1, netuid0, total_hotkey_alpha);
856+
TotalHotkeyAlpha::<Test>::insert(hotkey1, netuid1, total_hotkey_alpha);
857+
858+
// Test stake fee for add_stake
859+
let stake_fee_0 = SubtensorModule::calculate_staking_fee(
860+
None,
861+
&coldkey1,
862+
Some((&hotkey1, netuid0)),
863+
&coldkey1,
864+
I96F32::from_num(stake_amount),
865+
); // Default for adding stake
866+
assert_eq!(stake_fee_0, default_fee);
867+
868+
// Test stake fee for remove on root
869+
let stake_fee_1 = SubtensorModule::calculate_staking_fee(
870+
Some((&hotkey1, root_netuid)),
871+
&coldkey1,
872+
None,
873+
&coldkey1,
874+
I96F32::from_num(stake_amount),
875+
); // Default for removing stake from root
876+
assert_eq!(stake_fee_1, default_fee);
877+
878+
// Test stake fee for move from root to non-root
879+
let stake_fee_2 = SubtensorModule::calculate_staking_fee(
880+
Some((&hotkey1, root_netuid)),
881+
&coldkey1,
882+
Some((&hotkey1, netuid0)),
883+
&coldkey1,
884+
I96F32::from_num(stake_amount),
885+
); // Default for moving stake from root to non-root
886+
assert_eq!(stake_fee_2, default_fee);
887+
888+
// Test stake fee for move between hotkeys on root
889+
let stake_fee_3 = SubtensorModule::calculate_staking_fee(
890+
Some((&hotkey1, root_netuid)),
891+
&coldkey1,
892+
Some((&hotkey2, root_netuid)),
893+
&coldkey1,
894+
I96F32::from_num(stake_amount),
895+
); // Default for moving stake between hotkeys on root
896+
assert_eq!(stake_fee_3, default_fee);
897+
898+
// Test stake fee for move between coldkeys on root
899+
let stake_fee_4 = SubtensorModule::calculate_staking_fee(
900+
Some((&hotkey1, root_netuid)),
901+
&coldkey1,
902+
Some((&hotkey1, root_netuid)),
903+
&coldkey2,
904+
I96F32::from_num(stake_amount),
905+
); // Default for moving stake between coldkeys on root
906+
assert_eq!(stake_fee_4, default_fee);
907+
908+
// Test stake fee for *swap* from non-root to root
909+
let stake_fee_5 = SubtensorModule::calculate_staking_fee(
910+
Some((&hotkey1, netuid0)),
911+
&coldkey1,
912+
Some((&hotkey1, root_netuid)),
913+
&coldkey1,
914+
I96F32::from_num(stake_amount),
915+
); // Charged a dynamic fee
916+
assert_ne!(stake_fee_5, default_fee);
917+
918+
// Test stake fee for move between hotkeys on non-root
919+
let stake_fee_6 = SubtensorModule::calculate_staking_fee(
920+
Some((&hotkey1, netuid0)),
921+
&coldkey1,
922+
Some((&hotkey2, netuid0)),
923+
&coldkey1,
924+
I96F32::from_num(stake_amount),
925+
); // Charge the default fee
926+
assert_eq!(stake_fee_6, default_fee);
927+
928+
// Test stake fee for move between coldkeys on non-root
929+
let stake_fee_7 = SubtensorModule::calculate_staking_fee(
930+
Some((&hotkey1, netuid0)),
931+
&coldkey1,
932+
Some((&hotkey1, netuid0)),
933+
&coldkey2,
934+
I96F32::from_num(stake_amount),
935+
); // Charge the default fee; stake did not leave the subnet.
936+
assert_eq!(stake_fee_7, default_fee);
937+
938+
// Test stake fee for *swap* from non-root to non-root
939+
let stake_fee_8 = SubtensorModule::calculate_staking_fee(
940+
Some((&hotkey1, netuid0)),
941+
&coldkey1,
942+
Some((&hotkey1, netuid1)),
943+
&coldkey1,
944+
I96F32::from_num(stake_amount),
945+
); // Charged a dynamic fee
946+
assert_ne!(stake_fee_8, default_fee);
947+
});
948+
}

0 commit comments

Comments
 (0)