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 calculation #1935

Merged
merged 2 commits into from
Mar 3, 2022
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
25 changes: 18 additions & 7 deletions modules/cdp-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,19 +1038,30 @@ impl<T: Config> Pallet<T> {
// update CDP state
let collateral_adjustment = <LoansOf<T>>::amount_try_from_balance(decrease_collateral)?.saturating_neg();
let previous_debit_value = Self::get_debit_value(currency_id, debit);
let decrease_debit_value = actual_stable_amount.min(previous_debit_value);
let refund_stable = actual_stable_amount.saturating_sub(decrease_debit_value);
let decrease_debit_balance = Self::try_convert_to_debit_balance(currency_id, decrease_debit_value)
.ok_or(Error::<T>::ConvertDebitBalanceFailed)?;
let (decrease_debit_value, decrease_debit_balance) = if actual_stable_amount >= previous_debit_value {
// refund extra stable coin to the CDP owner
<T as Config>::Currency::transfer(
stable_currency_id,
&loans_module_account,
who,
actual_stable_amount.saturating_sub(previous_debit_value),
)?;

(previous_debit_value, debit)
} else {
(
actual_stable_amount,
Self::try_convert_to_debit_balance(currency_id, actual_stable_amount)
.ok_or(Error::<T>::ConvertDebitBalanceFailed)?,
)
};

let debit_adjustment = <LoansOf<T>>::amount_try_from_balance(decrease_debit_balance)?.saturating_neg();
<LoansOf<T>>::update_loan(who, currency_id, collateral_adjustment, debit_adjustment)?;

// repay the debit of CDP
<T as Config>::CDPTreasury::burn_debit(&loans_module_account, decrease_debit_value)?;

// refund extra stable coin to the CDP owner
<T as Config>::Currency::transfer(stable_currency_id, &loans_module_account, who, refund_stable)?;

// check the CDP if is still at valid risk.
Self::check_position_valid(
currency_id,
Expand Down