Skip to content

Commit 2633e88

Browse files
authored
fix tests to incl fee (#1186)
1 parent 4263d84 commit 2633e88

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

pallets/subtensor/src/tests/move_stake.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,8 @@ fn test_do_swap_success() {
10801080
let origin_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
10811081
let destination_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
10821082

1083+
let fee = DefaultMinStake::<Test>::get();
1084+
10831085
let coldkey = U256::from(1);
10841086
let hotkey = U256::from(2);
10851087
let stake_amount = DefaultMinStake::<Test>::get() * 10;
@@ -1114,7 +1116,11 @@ fn test_do_swap_success() {
11141116
&coldkey,
11151117
destination_netuid,
11161118
);
1117-
assert_abs_diff_eq!(alpha_after, stake_amount, epsilon = stake_amount / 1000);
1119+
assert_abs_diff_eq!(
1120+
alpha_after,
1121+
stake_amount - fee,
1122+
epsilon = stake_amount / 1000
1123+
);
11181124
});
11191125
}
11201126

@@ -1259,6 +1265,8 @@ fn test_do_swap_same_subnet() {
12591265
let subnet_owner_hotkey = U256::from(1101);
12601266
let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
12611267

1268+
let fee = DefaultMinStake::<Test>::get();
1269+
12621270
let coldkey = U256::from(1);
12631271
let hotkey = U256::from(2);
12641272
let stake_amount = DefaultMinStake::<Test>::get() * 10;
@@ -1268,6 +1276,7 @@ fn test_do_swap_same_subnet() {
12681276

12691277
let alpha_before =
12701278
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
1279+
let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(netuid, fee);
12711280

12721281
assert_ok!(SubtensorModule::do_swap_stake(
12731282
RuntimeOrigin::signed(coldkey),
@@ -1279,7 +1288,8 @@ fn test_do_swap_same_subnet() {
12791288

12801289
let alpha_after =
12811290
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
1282-
assert_abs_diff_eq!(alpha_after, alpha_before, epsilon = 5);
1291+
assert_abs_diff_eq!(alpha_after, alpha_before - fee_as_alpha, epsilon = 15);
1292+
// Some slippage due to fee on same subnet.
12831293
});
12841294
}
12851295

@@ -1291,13 +1301,16 @@ fn test_do_swap_partial_stake() {
12911301
let origin_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
12921302
let destination_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
12931303

1304+
let fee = DefaultMinStake::<Test>::get();
1305+
12941306
let coldkey = U256::from(1);
12951307
let hotkey = U256::from(2);
12961308
let total_stake = DefaultMinStake::<Test>::get() * 10;
12971309

12981310
SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
12991311
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, origin_netuid, total_stake, 0);
13001312

1313+
let fee_as_alpha2 = SubtensorModule::swap_tao_for_alpha(destination_netuid, fee);
13011314
let swap_amount = total_stake / 2;
13021315
assert_ok!(SubtensorModule::do_swap_stake(
13031316
RuntimeOrigin::signed(coldkey),
@@ -1322,7 +1335,7 @@ fn test_do_swap_partial_stake() {
13221335
&coldkey,
13231336
destination_netuid
13241337
),
1325-
swap_amount,
1338+
swap_amount - fee_as_alpha2,
13261339
epsilon = total_stake / 1000
13271340
);
13281341
});
@@ -1336,6 +1349,8 @@ fn test_do_swap_storage_updates() {
13361349
let origin_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
13371350
let destination_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
13381351

1352+
let fee = DefaultMinStake::<Test>::get();
1353+
13391354
let coldkey = U256::from(1);
13401355
let hotkey = U256::from(2);
13411356
let stake_amount = DefaultMinStake::<Test>::get() * 10;
@@ -1365,13 +1380,15 @@ fn test_do_swap_storage_updates() {
13651380
0
13661381
);
13671382

1383+
let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(destination_netuid, fee);
1384+
13681385
assert_abs_diff_eq!(
13691386
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
13701387
&hotkey,
13711388
&coldkey,
13721389
destination_netuid
13731390
),
1374-
alpha,
1391+
alpha - fee_as_alpha,
13751392
epsilon = 5
13761393
);
13771394
});
@@ -1385,13 +1402,16 @@ fn test_do_swap_multiple_times() {
13851402
let netuid1 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
13861403
let netuid2 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
13871404

1405+
let fee = DefaultMinStake::<Test>::get();
1406+
13881407
let coldkey = U256::from(1);
13891408
let hotkey = U256::from(2);
13901409
let initial_stake = DefaultMinStake::<Test>::get() * 10;
13911410

13921411
SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
13931412
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid1, initial_stake, 0);
13941413

1414+
let mut total_alpha1_fee = 0;
13951415
for _ in 0..3 {
13961416
let alpha1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
13971417
&hotkey, &coldkey, netuid1,
@@ -1404,6 +1424,9 @@ fn test_do_swap_multiple_times() {
14041424
netuid2,
14051425
alpha1
14061426
));
1427+
1428+
let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(netuid1, fee);
1429+
total_alpha1_fee += fee_as_alpha;
14071430
}
14081431
let alpha2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
14091432
&hotkey, &coldkey, netuid2,
@@ -1416,17 +1439,21 @@ fn test_do_swap_multiple_times() {
14161439
netuid1,
14171440
alpha2
14181441
));
1442+
1443+
let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(netuid1, fee);
1444+
total_alpha1_fee += fee_as_alpha;
14191445
}
14201446
}
14211447

1422-
let final_stake_netuid1 =
1448+
let final_stake_netuid1: u64 =
14231449
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid1);
14241450
let final_stake_netuid2 =
14251451
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid2);
1452+
let expected_stake = initial_stake - total_alpha1_fee;
14261453
assert_abs_diff_eq!(
14271454
final_stake_netuid1,
1428-
initial_stake,
1429-
epsilon = initial_stake / 1000
1455+
expected_stake,
1456+
epsilon = expected_stake / 1000
14301457
);
14311458
assert_eq!(final_stake_netuid2, 0);
14321459
});

0 commit comments

Comments
 (0)