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

Fixed a bug to redeem with dust AvailableStakingBalance #1470

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion modules/homa-lite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ pub mod module {
Self::convert_staking_to_liquid(available_staking_balance)?,
);

let mut liquid_remaining = liquid_amount;
if Self::convert_liquid_to_staking(actual_liquid_amount)? > T::XcmUnbondFee::get() {
// Immediately redeem from the available_staking_balances
let actual_staking_amount = Self::convert_liquid_to_staking(actual_liquid_amount)?;
Expand All @@ -456,10 +457,10 @@ pub mod module {
actual_staking_amount,
actual_liquid_amount,
));
liquid_remaining = liquid_remaining.saturating_sub(actual_liquid_amount);
}

// Unredeemed requests are added to a queue.
let liquid_remaining = liquid_amount.saturating_sub(actual_liquid_amount);
if Self::liquid_amount_is_above_minimum_threshold(liquid_remaining) {
// Check if there's already a queued redeem request.
let (request_amount, _) = Self::redeem_requests(&who).unwrap_or((0, Permill::default()));
Expand Down
27 changes: 27 additions & 0 deletions modules/homa-lite/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,30 @@ fn staking_and_liquid_conversion_works() {
assert_eq!(HomaLite::convert_liquid_to_staking(5_000_000), Ok(1_000_000));
});
}

#[test]
fn redeem_can_handle_dust_available_staking_currency() {
ExtBuilder::default().build().execute_with(|| {
// If AvailableStakingBalance is not enough to pay for the unbonding fee, ignore it.
// pub XcmUnbondFee: Balance = dollar(1);
assert_ok!(HomaLite::schedule_unbond(Origin::root(), 999_000_000, 0));
MockRelayBlockNumberProvider::set(0);
HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 5_000_000_000);

assert_eq!(AvailableStakingBalance::<Runtime>::get(), 999_000_000);

// Ignore the dust AvailableStakingBalance and put the full amount onto the queue.
assert_ok!(HomaLite::request_redeem(
Origin::signed(ROOT),
dollar(1000),
Permill::zero()
));

assert_eq!(HomaLite::redeem_requests(ROOT), Some((dollar(1000), Permill::zero())));
System::assert_last_event(Event::HomaLite(crate::Event::RedeemRequested(
ROOT,
dollar(1000),
Permill::zero(),
)));
});
}