@@ -626,6 +626,7 @@ fn test_try_associate_hotkey() {
626
626
627
627
#[ test]
628
628
fn test_stake_fee_api ( ) {
629
+ // The API should match the calculation
629
630
new_test_ext ( 1 ) . execute_with ( || {
630
631
let hotkey1 = U256 :: from ( 1 ) ;
631
632
let coldkey1 = U256 :: from ( 2 ) ;
@@ -642,8 +643,6 @@ fn test_stake_fee_api() {
642
643
let reciprocal_price = 2 ; // 1 / price
643
644
let stake_amount = 100_000_000_000 ;
644
645
645
- let default_fee = DefaultStakingFee :: < Test > :: get ( ) ;
646
-
647
646
// Setup alpha out
648
647
SubnetAlphaOut :: < Test > :: insert ( netuid0, 100_000_000_000 ) ;
649
648
SubnetAlphaOut :: < Test > :: insert ( netuid1, 100_000_000_000 ) ;
@@ -668,8 +667,15 @@ fn test_stake_fee_api() {
668
667
Some ( ( hotkey1, netuid0) ) ,
669
668
coldkey1,
670
669
stake_amount,
671
- ) ; // Default for adding stake
672
- assert_eq ! ( stake_fee_0, default_fee) ;
670
+ ) ;
671
+ let dynamic_fee_0 = SubtensorModule :: calculate_staking_fee (
672
+ None ,
673
+ & coldkey1,
674
+ Some ( ( & hotkey1, netuid0) ) ,
675
+ & coldkey1,
676
+ I96F32 :: saturating_from_num ( stake_amount) ,
677
+ ) ;
678
+ assert_eq ! ( stake_fee_0, dynamic_fee_0) ;
673
679
674
680
// Test stake fee for remove on root
675
681
let stake_fee_1 = SubtensorModule :: get_stake_fee (
@@ -678,8 +684,15 @@ fn test_stake_fee_api() {
678
684
None ,
679
685
coldkey1,
680
686
stake_amount,
681
- ) ; // Default for removing stake from root
682
- assert_eq ! ( stake_fee_1, default_fee) ;
687
+ ) ;
688
+ let dynamic_fee_1 = SubtensorModule :: calculate_staking_fee (
689
+ Some ( ( & hotkey1, root_netuid) ) ,
690
+ & coldkey1,
691
+ None ,
692
+ & coldkey1,
693
+ I96F32 :: saturating_from_num ( stake_amount) ,
694
+ ) ;
695
+ assert_eq ! ( stake_fee_1, dynamic_fee_1) ;
683
696
684
697
// Test stake fee for move from root to non-root
685
698
let stake_fee_2 = SubtensorModule :: get_stake_fee (
@@ -688,8 +701,15 @@ fn test_stake_fee_api() {
688
701
Some ( ( hotkey1, netuid0) ) ,
689
702
coldkey1,
690
703
stake_amount,
691
- ) ; // Default for moving stake from root to non-root
692
- assert_eq ! ( stake_fee_2, default_fee) ;
704
+ ) ;
705
+ let dynamic_fee_2 = SubtensorModule :: calculate_staking_fee (
706
+ Some ( ( & hotkey1, root_netuid) ) ,
707
+ & coldkey1,
708
+ Some ( ( & hotkey1, netuid0) ) ,
709
+ & coldkey1,
710
+ I96F32 :: saturating_from_num ( stake_amount) ,
711
+ ) ;
712
+ assert_eq ! ( stake_fee_2, dynamic_fee_2) ;
693
713
694
714
// Test stake fee for move between hotkeys on root
695
715
let stake_fee_3 = SubtensorModule :: get_stake_fee (
@@ -698,8 +718,15 @@ fn test_stake_fee_api() {
698
718
Some ( ( hotkey2, root_netuid) ) ,
699
719
coldkey1,
700
720
stake_amount,
701
- ) ; // Default for moving stake between hotkeys on root
702
- assert_eq ! ( stake_fee_3, default_fee) ;
721
+ ) ;
722
+ let dynamic_fee_3 = SubtensorModule :: calculate_staking_fee (
723
+ Some ( ( & hotkey1, root_netuid) ) ,
724
+ & coldkey1,
725
+ Some ( ( & hotkey2, root_netuid) ) ,
726
+ & coldkey1,
727
+ I96F32 :: saturating_from_num ( stake_amount) ,
728
+ ) ;
729
+ assert_eq ! ( stake_fee_3, dynamic_fee_3) ;
703
730
704
731
// Test stake fee for move between coldkeys on root
705
732
let stake_fee_4 = SubtensorModule :: get_stake_fee (
@@ -708,8 +735,15 @@ fn test_stake_fee_api() {
708
735
Some ( ( hotkey1, root_netuid) ) ,
709
736
coldkey2,
710
737
stake_amount,
711
- ) ; // Default for moving stake between coldkeys on root
712
- assert_eq ! ( stake_fee_4, default_fee) ;
738
+ ) ;
739
+ let dynamic_fee_4 = SubtensorModule :: calculate_staking_fee (
740
+ Some ( ( & hotkey1, root_netuid) ) ,
741
+ & coldkey1,
742
+ Some ( ( & hotkey1, root_netuid) ) ,
743
+ & coldkey2,
744
+ I96F32 :: saturating_from_num ( stake_amount) ,
745
+ ) ;
746
+ assert_eq ! ( stake_fee_4, dynamic_fee_4) ;
713
747
714
748
// Test stake fee for *swap* from non-root to root
715
749
let stake_fee_5 = SubtensorModule :: get_stake_fee (
@@ -718,11 +752,13 @@ fn test_stake_fee_api() {
718
752
Some ( ( hotkey1, root_netuid) ) ,
719
753
coldkey1,
720
754
stake_amount,
721
- ) ; // Charged a dynamic fee
755
+ ) ;
722
756
let dynamic_fee_5 = SubtensorModule :: calculate_staking_fee (
723
- netuid0,
724
- & hotkey1,
725
- I96F32 :: from_num ( stake_amount) ,
757
+ Some ( ( & hotkey1, netuid0) ) ,
758
+ & coldkey1,
759
+ Some ( ( & hotkey1, root_netuid) ) ,
760
+ & coldkey1,
761
+ I96F32 :: saturating_from_num ( stake_amount) ,
726
762
) ;
727
763
assert_eq ! ( stake_fee_5, dynamic_fee_5) ;
728
764
@@ -733,11 +769,13 @@ fn test_stake_fee_api() {
733
769
Some ( ( hotkey2, netuid0) ) ,
734
770
coldkey1,
735
771
stake_amount,
736
- ) ; // Charged a dynamic fee
772
+ ) ;
737
773
let dynamic_fee_6 = SubtensorModule :: calculate_staking_fee (
738
- netuid0,
739
- & hotkey1,
740
- I96F32 :: from_num ( stake_amount) ,
774
+ Some ( ( & hotkey1, netuid0) ) ,
775
+ & coldkey1,
776
+ Some ( ( & hotkey2, netuid0) ) ,
777
+ & coldkey1,
778
+ I96F32 :: saturating_from_num ( stake_amount) ,
741
779
) ;
742
780
assert_eq ! ( stake_fee_6, dynamic_fee_6) ;
743
781
@@ -748,11 +786,13 @@ fn test_stake_fee_api() {
748
786
Some ( ( hotkey1, netuid0) ) ,
749
787
coldkey2,
750
788
stake_amount,
751
- ) ; // Charged a dynamic fee
789
+ ) ;
752
790
let dynamic_fee_7 = SubtensorModule :: calculate_staking_fee (
753
- netuid0,
754
- & hotkey1,
755
- I96F32 :: from_num ( stake_amount) ,
791
+ Some ( ( & hotkey1, netuid0) ) ,
792
+ & coldkey1,
793
+ Some ( ( & hotkey1, netuid0) ) ,
794
+ & coldkey2,
795
+ I96F32 :: saturating_from_num ( stake_amount) ,
756
796
) ;
757
797
assert_eq ! ( stake_fee_7, dynamic_fee_7) ;
758
798
@@ -763,12 +803,146 @@ fn test_stake_fee_api() {
763
803
Some ( ( hotkey1, netuid1) ) ,
764
804
coldkey1,
765
805
stake_amount,
766
- ) ; // Charged a dynamic fee
806
+ ) ;
767
807
let dynamic_fee_8 = SubtensorModule :: calculate_staking_fee (
768
- netuid0,
769
- & hotkey1,
770
- I96F32 :: from_num ( stake_amount) ,
808
+ Some ( ( & hotkey1, netuid0) ) ,
809
+ & coldkey1,
810
+ Some ( ( & hotkey1, netuid1) ) ,
811
+ & coldkey1,
812
+ I96F32 :: saturating_from_num ( stake_amount) ,
771
813
) ;
772
814
assert_eq ! ( stake_fee_8, dynamic_fee_8) ;
773
815
} ) ;
774
816
}
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