Skip to content

Commit 8770079

Browse files
committed
adjust move stake tests to convert stake to alpha
1 parent 4dca324 commit 8770079

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

pallets/subtensor/src/tests/move_stake.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::*;
33
use approx::assert_abs_diff_eq;
44
use frame_support::{assert_err, assert_noop, assert_ok};
55
use sp_core::{Get, U256};
6+
use substrate_fixed::types::I96F32;
67

78
// 1. test_do_move_success
89
// Description: Test a successful move of stake between two hotkeys in the same subnet
@@ -111,14 +112,19 @@ fn test_do_move_different_subnets() {
111112
),
112113
0
113114
);
115+
let alpha_fee: I96F32 =
116+
I96F32::from_num(fee) / SubtensorModule::get_alpha_price(destination_netuid);
117+
let expected_value = I96F32::from_num(alpha)
118+
* SubtensorModule::get_alpha_price(origin_netuid)
119+
/ SubtensorModule::get_alpha_price(destination_netuid);
114120
assert_abs_diff_eq!(
115121
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
116122
&destination_hotkey,
117123
&coldkey,
118124
destination_netuid
119125
),
120-
stake_amount - 2 * fee,
121-
epsilon = stake_amount / 1000
126+
(expected_value - alpha_fee).to_num::<u64>(),
127+
epsilon = (expected_value / 1000).to_num::<u64>()
122128
);
123129
});
124130
}
@@ -700,13 +706,17 @@ fn test_do_move_storage_updates() {
700706
),
701707
0
702708
);
709+
let alpha_fee =
710+
I96F32::from_num(fee) / SubtensorModule::get_alpha_price(destination_netuid);
711+
let alpha2 = I96F32::from_num(alpha) * SubtensorModule::get_alpha_price(origin_netuid)
712+
/ SubtensorModule::get_alpha_price(destination_netuid);
703713
assert_abs_diff_eq!(
704714
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
705715
&destination_hotkey,
706716
&coldkey,
707717
destination_netuid
708718
),
709-
alpha - fee,
719+
(alpha2 - alpha_fee).to_num::<u64>(),
710720
epsilon = alpha / 1000
711721
);
712722
});
@@ -1063,10 +1073,12 @@ fn test_do_transfer_different_subnets() {
10631073
&destination_coldkey,
10641074
destination_netuid,
10651075
);
1076+
let expected_value = I96F32::from_num(stake_amount - fee)
1077+
/ SubtensorModule::get_alpha_price(destination_netuid);
10661078
assert_abs_diff_eq!(
10671079
dest_stake,
1068-
stake_amount - fee,
1069-
epsilon = stake_amount / 1000
1080+
expected_value.to_num::<u64>(),
1081+
epsilon = (expected_value / 1000).to_num::<u64>()
10701082
);
10711083
});
10721084
}
@@ -1114,10 +1126,15 @@ fn test_do_swap_success() {
11141126
&coldkey,
11151127
destination_netuid,
11161128
);
1129+
let alpha_fee =
1130+
I96F32::from_num(fee) / SubtensorModule::get_alpha_price(destination_netuid);
1131+
let expected_value = I96F32::from_num(alpha_before)
1132+
* SubtensorModule::get_alpha_price(origin_netuid)
1133+
/ SubtensorModule::get_alpha_price(destination_netuid);
11171134
assert_abs_diff_eq!(
11181135
alpha_after,
1119-
stake_amount - fee,
1120-
epsilon = stake_amount / 1000
1136+
(expected_value - alpha_fee).to_num::<u64>(),
1137+
epsilon = (expected_value / 1000).to_num::<u64>()
11211138
);
11221139
});
11231140
}
@@ -1309,7 +1326,6 @@ fn test_do_swap_partial_stake() {
13091326
SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
13101327
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, origin_netuid, total_stake, 0);
13111328

1312-
let fee_as_alpha2 = SubtensorModule::swap_tao_for_alpha(destination_netuid, fee);
13131329
let swap_amount = total_stake / 2;
13141330
assert_ok!(SubtensorModule::do_swap_stake(
13151331
RuntimeOrigin::signed(coldkey),
@@ -1328,14 +1344,20 @@ fn test_do_swap_partial_stake() {
13281344
total_stake - swap_amount,
13291345
epsilon = total_stake / 1000
13301346
);
1347+
1348+
let alpha_fee =
1349+
I96F32::from_num(fee) / SubtensorModule::get_alpha_price(destination_netuid);
1350+
let expected_value = I96F32::from_num(swap_amount)
1351+
* SubtensorModule::get_alpha_price(origin_netuid)
1352+
/ SubtensorModule::get_alpha_price(destination_netuid);
13311353
assert_abs_diff_eq!(
13321354
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
13331355
&hotkey,
13341356
&coldkey,
13351357
destination_netuid
13361358
),
1337-
swap_amount - fee_as_alpha2,
1338-
epsilon = total_stake / 1000
1359+
(expected_value - alpha_fee).to_num::<u64>(),
1360+
epsilon = (expected_value / 1000).to_num::<u64>()
13391361
);
13401362
});
13411363
}
@@ -1378,15 +1400,15 @@ fn test_do_swap_storage_updates() {
13781400
0
13791401
);
13801402

1381-
let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(destination_netuid, fee);
1382-
1403+
let alpha_fee =
1404+
SubtensorModule::get_alpha_price(destination_netuid) * I96F32::from_num(fee);
13831405
assert_abs_diff_eq!(
13841406
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
13851407
&hotkey,
13861408
&coldkey,
13871409
destination_netuid
13881410
),
1389-
alpha - fee_as_alpha,
1411+
alpha - alpha_fee.to_num::<u64>(),
13901412
epsilon = 5
13911413
);
13921414
});

0 commit comments

Comments
 (0)