Skip to content

Commit ef055a7

Browse files
committed
fix another test
1 parent 742c4d8 commit ef055a7

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

pallets/transaction-fee/src/tests/mock.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,65 @@ pub fn setup_stake(
823823
));
824824
remove_stake_rate_limit_for_tests(hotkey, coldkey, netuid);
825825
}
826+
827+
pub(crate) fn quote_remove_stake_after_alpha_fee(
828+
coldkey: &U256,
829+
hotkey: &U256,
830+
netuid: NetUid,
831+
alpha: AlphaBalance,
832+
) -> (u64, u64) {
833+
if netuid.is_root() {
834+
return (alpha.into(), 0);
835+
}
836+
837+
let call: RuntimeCall = RuntimeCall::SubtensorModule(pallet_subtensor::Call::remove_stake {
838+
hotkey: *hotkey,
839+
netuid,
840+
amount_unstaked: alpha,
841+
});
842+
let info =
843+
<RuntimeCall as frame_support::dispatch::GetDispatchInfo>::get_dispatch_info(&call);
844+
let tao_fee = pallet_transaction_payment::Pallet::<Test>::compute_fee(0, &info, 0.into());
845+
846+
frame_support::storage::with_transaction(
847+
|| -> frame_support::storage::TransactionOutcome<
848+
Result<(u64, u64), sp_runtime::DispatchError>,
849+
> {
850+
let alpha_balance =
851+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid);
852+
853+
let mut alpha_fee =
854+
pallet_subtensor_swap::Pallet::<Test>::get_alpha_amount_for_tao(
855+
netuid,
856+
tao_fee.into(),
857+
);
858+
859+
if alpha_fee.is_zero() {
860+
alpha_fee = alpha_balance;
861+
}
862+
863+
let alpha_fee = alpha_fee.min(alpha_balance);
864+
865+
if !alpha_fee.is_zero() {
866+
assert_ok!(SubtensorModule::unstake_from_subnet(
867+
hotkey,
868+
coldkey,
869+
netuid,
870+
alpha_fee,
871+
0.into(),
872+
true,
873+
));
874+
}
875+
876+
let alpha_after_fee =
877+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
878+
hotkey, coldkey, netuid,
879+
);
880+
let quoted_alpha = alpha.min(alpha_after_fee);
881+
882+
let quote = swap_alpha_to_tao(netuid, quoted_alpha);
883+
frame_support::storage::TransactionOutcome::Rollback(Ok(quote))
884+
},
885+
)
886+
.expect("transactional quote should not fail")
887+
}

pallets/transaction-fee/src/tests/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,13 @@ fn test_remove_stake_fees_alpha() {
220220
);
221221

222222
// Simulate stake removal to get how much TAO should we get for unstaked Alpha
223-
let (expected_unstaked_tao, swap_fee) =
224-
mock::swap_alpha_to_tao(sn.subnets[0].netuid, unstake_amount);
223+
// after the alpha-fee pre-withdrawal has already moved the pool.
224+
let (expected_unstaked_tao, swap_fee) = mock::quote_remove_stake_after_alpha_fee(
225+
&sn.coldkey,
226+
&sn.hotkeys[0],
227+
sn.subnets[0].netuid,
228+
unstake_amount,
229+
);
225230

226231
// Forse-set signer balance to ED
227232
let current_balance = Balances::free_balance(sn.coldkey);

0 commit comments

Comments
 (0)