Skip to content

Commit a6a39a5

Browse files
committed
Fix limit swap
1 parent 69e3a43 commit a6a39a5

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

pallets/subtensor/src/staking/move_stake.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,20 @@ impl<T: Config> Pallet<T> {
330330
check_transfer_toggle,
331331
)?;
332332

333+
// Calculate the amount that should be moved in this operation
334+
let move_amount = if alpha_amount < max_amount {
335+
alpha_amount
336+
} else {
337+
max_amount
338+
};
339+
333340
// Unstake from the origin subnet, returning TAO (or a 1:1 equivalent).
334341
let fee = DefaultStakingFee::<T>::get().safe_div(2);
335342
let tao_unstaked = Self::unstake_from_subnet(
336343
origin_hotkey,
337344
origin_coldkey,
338345
origin_netuid,
339-
alpha_amount,
346+
move_amount,
340347
fee,
341348
);
342349

pallets/subtensor/src/tests/staking.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3885,3 +3885,65 @@ fn test_remove_99_9989_per_cent_stake_leaves_a_little() {
38853885
assert_abs_diff_eq!(new_alpha, (alpha as f64 * 0.01) as u64, epsilon = 10);
38863886
});
38873887
}
3888+
3889+
#[test]
3890+
fn test_move_stake_limit_partial() {
3891+
new_test_ext(1).execute_with(|| {
3892+
let subnet_owner_coldkey = U256::from(1001);
3893+
let subnet_owner_hotkey = U256::from(1002);
3894+
let coldkey = U256::from(1);
3895+
let hotkey = U256::from(2);
3896+
let stake_amount = 150_000_000_000;
3897+
let move_amount = 150_000_000_000;
3898+
3899+
// add network
3900+
let origin_netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
3901+
let destination_netuid: u16 =
3902+
add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
3903+
register_ok_neuron(origin_netuid, hotkey, coldkey, 192213123);
3904+
register_ok_neuron(destination_netuid, hotkey, coldkey, 192213123);
3905+
3906+
// Give the neuron some stake to remove
3907+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
3908+
&hotkey,
3909+
&coldkey,
3910+
origin_netuid,
3911+
stake_amount,
3912+
);
3913+
3914+
// Forse-set alpha in and tao reserve to make price equal 1.5 on both origin and destination,
3915+
// but there's much more liquidity on destination, so its price wouldn't go up when restaked
3916+
let tao_reserve: U96F32 = U96F32::from_num(150_000_000_000_u64);
3917+
let alpha_in: U96F32 = U96F32::from_num(100_000_000_000_u64);
3918+
SubnetTAO::<Test>::insert(origin_netuid, tao_reserve.to_num::<u64>());
3919+
SubnetAlphaIn::<Test>::insert(origin_netuid, alpha_in.to_num::<u64>());
3920+
SubnetTAO::<Test>::insert(destination_netuid, (tao_reserve * 100_000).to_num::<u64>());
3921+
SubnetAlphaIn::<Test>::insert(destination_netuid, (alpha_in * 100_000).to_num::<u64>());
3922+
let current_price: U96F32 =
3923+
U96F32::from_num(SubtensorModule::get_alpha_price(origin_netuid));
3924+
assert_eq!(current_price, U96F32::from_num(1.5));
3925+
3926+
// The relative price between origin and destination subnets is 1.
3927+
// Setup limit relative price so that it doesn't drop by more than 1% from current price
3928+
let limit_price = 990_000_000;
3929+
3930+
// Move stake with slippage safety - executes partially
3931+
assert_ok!(SubtensorModule::swap_stake_limit(
3932+
RuntimeOrigin::signed(coldkey),
3933+
hotkey,
3934+
origin_netuid,
3935+
destination_netuid,
3936+
move_amount,
3937+
limit_price,
3938+
true,
3939+
));
3940+
3941+
let new_alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
3942+
&hotkey,
3943+
&coldkey,
3944+
origin_netuid,
3945+
);
3946+
3947+
assert_abs_diff_eq!(new_alpha, 149_000_000_000, epsilon = 100_000_000,);
3948+
});
3949+
}

0 commit comments

Comments
 (0)