Skip to content

Commit 5b659e4

Browse files
authored
Merge pull request #1410 from opentensor/feat/fix-fee-calc-args-and-test
Feat/fix fee calc args and test
2 parents ad43ed6 + 2b2a02e commit 5b659e4

File tree

2 files changed

+225
-42
lines changed

2 files changed

+225
-42
lines changed

pallets/subtensor/src/rpc_info/stake_info.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,30 @@ impl<T: Config> Pallet<T> {
116116

117117
pub fn get_stake_fee(
118118
origin: Option<(T::AccountId, u16)>,
119-
_origin_coldkey_account: T::AccountId,
120-
_destination: Option<(T::AccountId, u16)>,
121-
_destination_coldkey_account: T::AccountId,
119+
origin_coldkey_account: T::AccountId,
120+
destination: Option<(T::AccountId, u16)>,
121+
destination_coldkey_account: T::AccountId,
122122
amount: u64,
123123
) -> u64 {
124-
match origin {
125-
Some((origin_hotkey_account, origin_netuid)) => Self::calculate_staking_fee(
126-
origin_netuid,
127-
&origin_hotkey_account,
128-
I96F32::saturating_from_num(amount),
129-
),
130-
None => {
131-
// Adding stake (comes from no netuid)
132-
DefaultStakingFee::<T>::get()
133-
}
134-
}
124+
let origin_: Option<(&T::AccountId, u16)> =
125+
if let Some((ref origin_hotkey, origin_netuid)) = origin {
126+
Some((origin_hotkey, origin_netuid))
127+
} else {
128+
None
129+
};
130+
131+
let destination_ = if let Some((ref destination_hotkey, destination_netuid)) = destination {
132+
Some((destination_hotkey, destination_netuid))
133+
} else {
134+
None
135+
};
136+
137+
Self::calculate_staking_fee(
138+
origin_,
139+
&origin_coldkey_account,
140+
destination_,
141+
&destination_coldkey_account,
142+
I96F32::saturating_from_num(amount),
143+
)
135144
}
136145
}

pallets/subtensor/src/tests/staking2.rs

Lines changed: 202 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ fn test_try_associate_hotkey() {
626626

627627
#[test]
628628
fn test_stake_fee_api() {
629+
// The API should match the calculation
629630
new_test_ext(1).execute_with(|| {
630631
let hotkey1 = U256::from(1);
631632
let coldkey1 = U256::from(2);
@@ -642,8 +643,6 @@ fn test_stake_fee_api() {
642643
let reciprocal_price = 2; // 1 / price
643644
let stake_amount = 100_000_000_000;
644645

645-
let default_fee = DefaultStakingFee::<Test>::get();
646-
647646
// Setup alpha out
648647
SubnetAlphaOut::<Test>::insert(netuid0, 100_000_000_000);
649648
SubnetAlphaOut::<Test>::insert(netuid1, 100_000_000_000);
@@ -668,8 +667,15 @@ fn test_stake_fee_api() {
668667
Some((hotkey1, netuid0)),
669668
coldkey1,
670669
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);
673679

674680
// Test stake fee for remove on root
675681
let stake_fee_1 = SubtensorModule::get_stake_fee(
@@ -678,8 +684,15 @@ fn test_stake_fee_api() {
678684
None,
679685
coldkey1,
680686
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);
683696

684697
// Test stake fee for move from root to non-root
685698
let stake_fee_2 = SubtensorModule::get_stake_fee(
@@ -688,8 +701,15 @@ fn test_stake_fee_api() {
688701
Some((hotkey1, netuid0)),
689702
coldkey1,
690703
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);
693713

694714
// Test stake fee for move between hotkeys on root
695715
let stake_fee_3 = SubtensorModule::get_stake_fee(
@@ -698,8 +718,15 @@ fn test_stake_fee_api() {
698718
Some((hotkey2, root_netuid)),
699719
coldkey1,
700720
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);
703730

704731
// Test stake fee for move between coldkeys on root
705732
let stake_fee_4 = SubtensorModule::get_stake_fee(
@@ -708,8 +735,15 @@ fn test_stake_fee_api() {
708735
Some((hotkey1, root_netuid)),
709736
coldkey2,
710737
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);
713747

714748
// Test stake fee for *swap* from non-root to root
715749
let stake_fee_5 = SubtensorModule::get_stake_fee(
@@ -718,11 +752,13 @@ fn test_stake_fee_api() {
718752
Some((hotkey1, root_netuid)),
719753
coldkey1,
720754
stake_amount,
721-
); // Charged a dynamic fee
755+
);
722756
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),
726762
);
727763
assert_eq!(stake_fee_5, dynamic_fee_5);
728764

@@ -733,11 +769,13 @@ fn test_stake_fee_api() {
733769
Some((hotkey2, netuid0)),
734770
coldkey1,
735771
stake_amount,
736-
); // Charged a dynamic fee
772+
);
737773
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),
741779
);
742780
assert_eq!(stake_fee_6, dynamic_fee_6);
743781

@@ -748,11 +786,13 @@ fn test_stake_fee_api() {
748786
Some((hotkey1, netuid0)),
749787
coldkey2,
750788
stake_amount,
751-
); // Charged a dynamic fee
789+
);
752790
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),
756796
);
757797
assert_eq!(stake_fee_7, dynamic_fee_7);
758798

@@ -763,12 +803,146 @@ fn test_stake_fee_api() {
763803
Some((hotkey1, netuid1)),
764804
coldkey1,
765805
stake_amount,
766-
); // Charged a dynamic fee
806+
);
767807
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),
771813
);
772814
assert_eq!(stake_fee_8, dynamic_fee_8);
773815
});
774816
}
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)