Skip to content

Commit

Permalink
fix duplicated fee issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Oct 26, 2021
1 parent 6544ca8 commit 9fa7d42
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
43 changes: 28 additions & 15 deletions modules/homa-lite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,26 @@ pub mod module {

Self::deposit_event(Event::<T>::RedeemRequestCancelled(who, request_amount));
}
} else {
// Redeem amount must be above a certain limit.
ensure!(
Self::liquid_amount_is_above_minimum_threshold(liquid_amount),
Error::<T>::AmountBelowMinimumThreshold
);
return Ok(());
}

// Redeem amount must be above a certain limit.
ensure!(
Self::liquid_amount_is_above_minimum_threshold(liquid_amount),
Error::<T>::AmountBelowMinimumThreshold
);

RedeemRequests::<T>::try_mutate(&who, |request| -> DispatchResult {
let old_amount = request.take().map(|(amount, _)| amount).unwrap_or_default();

// Withdraw fee is burned.
let base_withdraw_fee = T::BaseWithdrawFee::get().mul(liquid_amount);
let slash_amount = T::Currency::slash(T::LiquidCurrencyId::get(), &who, base_withdraw_fee);
ensure!(slash_amount.is_zero(), Error::<T>::InsufficientLiquidBalance);
let diff_amount = liquid_amount.saturating_sub(old_amount);

let base_withdraw_fee = T::BaseWithdrawFee::get().mul(diff_amount);
if !base_withdraw_fee.is_zero() {
// Burn withdraw fee for increased amount
let slash_amount = T::Currency::slash(T::LiquidCurrencyId::get(), &who, base_withdraw_fee);
ensure!(slash_amount.is_zero(), Error::<T>::InsufficientLiquidBalance);
}

// Deduct BaseWithdrawFee from the liquid amount.
let liquid_amount = liquid_amount.saturating_sub(base_withdraw_fee);
Expand Down Expand Up @@ -492,12 +501,17 @@ pub mod module {
}?;

// Insert/replace the new redeem request into storage.
RedeemRequests::<T>::insert(&who, (liquid_remaining, additional_fee));
*request = Some((liquid_remaining, additional_fee));

Self::deposit_event(Event::<T>::RedeemRequested(who, liquid_remaining, additional_fee));
Self::deposit_event(Event::<T>::RedeemRequested(
who.clone(),
liquid_remaining,
additional_fee,
));
}
}
Ok(())

Ok(())
})
}

/// Request staking currencies to be unbonded from the RelayChain.
Expand Down Expand Up @@ -850,7 +864,6 @@ pub mod module {

fn liquid_amount_is_above_minimum_threshold(liquid_amount: Balance) -> bool {
liquid_amount > T::MinimumRedeemThreshold::get()
&& Self::convert_liquid_to_staking(liquid_amount).unwrap_or_default() > T::XcmUnbondFee::get()
}

/// Construct a XCM message
Expand Down
52 changes: 52 additions & 0 deletions modules/homa-lite/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,3 +894,55 @@ fn process_scheduled_unbond_with_multiple_requests() {
assert_eq!(Currencies::reserved_balance(LKSM, &CHARLIE), 99899999999996);
});
}

#[test]
fn not_overcharge_redeem_fee() {
ExtBuilder::empty().build().execute_with(|| {
assert_ok!(Currencies::update_balance(
Origin::root(),
ALICE,
LKSM,
dollar(100) as i128
));

assert_ok!(HomaLite::set_total_staking_currency(Origin::root(), dollar(10)));

assert_ok!(HomaLite::request_redeem(
Origin::signed(ALICE),
dollar(50),
Permill::zero()
));

let fee = dollar(50) / 1000;

assert_eq!(Currencies::free_balance(LKSM, &ALICE), dollar(50));
assert_eq!(Currencies::reserved_balance(LKSM, &ALICE), dollar(50) - fee);

assert_ok!(HomaLite::request_redeem(
Origin::signed(ALICE),
dollar(50) - fee,
Permill::zero()
));

assert_eq!(Currencies::free_balance(LKSM, &ALICE), dollar(50));
assert_eq!(Currencies::reserved_balance(LKSM, &ALICE), dollar(50) - fee);

assert_ok!(HomaLite::request_redeem(
Origin::signed(ALICE),
dollar(100) - fee,
Permill::zero()
));

assert_eq!(Currencies::free_balance(LKSM, &ALICE), 0);
assert_eq!(Currencies::reserved_balance(LKSM, &ALICE), dollar(100) - fee * 2);

assert_ok!(HomaLite::request_redeem(
Origin::signed(ALICE),
dollar(10) - fee * 2,
Permill::zero()
));

assert_eq!(Currencies::free_balance(LKSM, &ALICE), dollar(90));
assert_eq!(Currencies::reserved_balance(LKSM, &ALICE), dollar(10) - fee * 2);
});
}
2 changes: 1 addition & 1 deletion runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("karura"),
impl_name: create_runtime_str!("karura"),
authoring_version: 1,
spec_version: 1017,
spec_version: 1018,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down

0 comments on commit 9fa7d42

Please sign in to comment.