Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tests to incl fee #1186

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions pallets/subtensor/src/tests/move_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,8 @@ fn test_do_swap_success() {
let origin_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
let destination_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);

let fee = DefaultMinStake::<Test>::get();

let coldkey = U256::from(1);
let hotkey = U256::from(2);
let stake_amount = DefaultMinStake::<Test>::get() * 10;
Expand Down Expand Up @@ -1114,7 +1116,11 @@ fn test_do_swap_success() {
&coldkey,
destination_netuid,
);
assert_abs_diff_eq!(alpha_after, stake_amount, epsilon = stake_amount / 1000);
assert_abs_diff_eq!(
alpha_after,
stake_amount - fee,
epsilon = stake_amount / 1000
);
});
}

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

let fee = DefaultMinStake::<Test>::get();

let coldkey = U256::from(1);
let hotkey = U256::from(2);
let stake_amount = DefaultMinStake::<Test>::get() * 10;
Expand All @@ -1268,6 +1276,7 @@ fn test_do_swap_same_subnet() {

let alpha_before =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(netuid, fee);

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

let alpha_after =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
assert_abs_diff_eq!(alpha_after, alpha_before, epsilon = 5);
assert_abs_diff_eq!(alpha_after, alpha_before - fee_as_alpha, epsilon = 15);
// Some slippage due to fee on same subnet.
});
}

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

let fee = DefaultMinStake::<Test>::get();

let coldkey = U256::from(1);
let hotkey = U256::from(2);
let total_stake = DefaultMinStake::<Test>::get() * 10;

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

let fee_as_alpha2 = SubtensorModule::swap_tao_for_alpha(destination_netuid, fee);
let swap_amount = total_stake / 2;
assert_ok!(SubtensorModule::do_swap_stake(
RuntimeOrigin::signed(coldkey),
Expand All @@ -1322,7 +1335,7 @@ fn test_do_swap_partial_stake() {
&coldkey,
destination_netuid
),
swap_amount,
swap_amount - fee_as_alpha2,
epsilon = total_stake / 1000
);
});
Expand All @@ -1336,6 +1349,8 @@ fn test_do_swap_storage_updates() {
let origin_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
let destination_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);

let fee = DefaultMinStake::<Test>::get();

let coldkey = U256::from(1);
let hotkey = U256::from(2);
let stake_amount = DefaultMinStake::<Test>::get() * 10;
Expand Down Expand Up @@ -1365,13 +1380,15 @@ fn test_do_swap_storage_updates() {
0
);

let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(destination_netuid, fee);

assert_abs_diff_eq!(
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey,
&coldkey,
destination_netuid
),
alpha,
alpha - fee_as_alpha,
epsilon = 5
);
});
Expand All @@ -1385,13 +1402,16 @@ fn test_do_swap_multiple_times() {
let netuid1 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
let netuid2 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);

let fee = DefaultMinStake::<Test>::get();

let coldkey = U256::from(1);
let hotkey = U256::from(2);
let initial_stake = DefaultMinStake::<Test>::get() * 10;

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

let mut total_alpha1_fee = 0;
for _ in 0..3 {
let alpha1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey, &coldkey, netuid1,
Expand All @@ -1404,6 +1424,9 @@ fn test_do_swap_multiple_times() {
netuid2,
alpha1
));

let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(netuid1, fee);
total_alpha1_fee += fee_as_alpha;
}
let alpha2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey, &coldkey, netuid2,
Expand All @@ -1416,17 +1439,21 @@ fn test_do_swap_multiple_times() {
netuid1,
alpha2
));

let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(netuid1, fee);
total_alpha1_fee += fee_as_alpha;
}
}

let final_stake_netuid1 =
let final_stake_netuid1: u64 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid1);
let final_stake_netuid2 =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid2);
let expected_stake = initial_stake - total_alpha1_fee;
assert_abs_diff_eq!(
final_stake_netuid1,
initial_stake,
epsilon = initial_stake / 1000
expected_stake,
epsilon = expected_stake / 1000
);
assert_eq!(final_stake_netuid2, 0);
});
Expand Down
Loading