Skip to content

Commit

Permalink
better fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed May 22, 2024
1 parent 652c102 commit 78b83b8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
8 changes: 3 additions & 5 deletions pallets/loans/src/entities/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use sp_runtime::{
},
DispatchError,
};
use sp_std::{cmp::min, collections::btree_map::BTreeMap};
use sp_std::collections::btree_map::BTreeMap;

use crate::{
entities::{
Expand Down Expand Up @@ -359,8 +359,7 @@ impl<T: Config> ActiveLoan<T> {
inner.adjust(Adjustment::Increase(amount.balance()?))?
}
ActivePricing::External(inner) => {
let when = T::Time::now();
inner.adjust(Adjustment::Increase(amount.external()?), Zero::zero(), when)?
inner.adjust(Adjustment::Increase(amount.external()?), Zero::zero())?
}
}

Expand Down Expand Up @@ -434,8 +433,7 @@ impl<T: Config> ActiveLoan<T> {
}
ActivePricing::External(inner) => {
let principal = amount.principal.external()?;
let when = min(T::Time::now(), self.schedule.maturity.date());
inner.adjust(Adjustment::Decrease(principal), amount.interest, when)?;
inner.adjust(Adjustment::Decrease(principal), amount.interest)?;
}
}

Expand Down
9 changes: 4 additions & 5 deletions pallets/loans/src/entities/pricing/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sp_runtime::{
traits::{EnsureAdd, EnsureFixedPointNumber, EnsureSub, Zero},
ArithmeticError, DispatchError, DispatchResult, FixedPointNumber,
};
use sp_std::collections::btree_map::BTreeMap;
use sp_std::{cmp::min, collections::btree_map::BTreeMap};

use crate::{
entities::interest::ActiveInterestRate,
Expand Down Expand Up @@ -166,9 +166,9 @@ impl<T: Config> ExternalActivePricing<T> {
) -> Result<T::Balance, DispatchError> {
if self.info.with_linear_pricing {
Ok(cfg_utils::math::y_coord_in_rect(
(price_last_updated, price),
(min(price_last_updated, maturity), price),
(maturity, self.info.notional),
T::Time::now(),
min(T::Time::now(), maturity),
)?)
} else {
Ok(price)
Expand Down Expand Up @@ -302,7 +302,6 @@ impl<T: Config> ExternalActivePricing<T> {
&mut self,
amount_adj: Adjustment<ExternalAmount<T>>,
interest: T::Balance,
when: Seconds,
) -> DispatchResult {
self.outstanding_quantity = amount_adj
.clone()
Expand All @@ -318,7 +317,7 @@ impl<T: Config> ExternalActivePricing<T> {

self.interest.adjust_debt(interest_adj)?;
self.latest_settlement_price = amount_adj.abs().settlement_price;
self.settlement_price_updated = when;
self.settlement_price_updated = T::Time::now();

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/loans/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
entities::{
changes::{Change, InternalMutation, LoanMutation},
input::{PrincipalInput, RepaidInput},
loans::{ActiveLoan, LoanInfo},
loans::{ActiveLoan, ActiveLoanInfo, LoanInfo},
pricing::{
external::{ExternalAmount, ExternalPricing, MaxBorrowAmount as ExtMaxBorrowAmount},
internal::{InternalPricing, MaxBorrowAmount as IntMaxBorrowAmount},
Expand Down
18 changes: 8 additions & 10 deletions pallets/loans/src/tests/repay_loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ fn with_external_pricing_and_overdue() {
util::borrow_loan(loan_id, PrincipalInput::External(amount));

// The loan is overdue
advance_time(YEAR * 2);
advance_time(YEAR * DAY);

let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE);
config_mocks(amount.balance().unwrap());
Expand All @@ -839,14 +839,12 @@ fn with_external_pricing_and_overdue() {
},
));

let active_loan = util::get_loan(loan_id);

let settlement_price_updated = match active_loan.pricing() {
ActivePricing::External(inner) => inner.settlement_price_updated(),
_ => unreachable!(),
};

// We must never overpass madurity date
assert_eq!(active_loan.maturity_date(), settlement_price_updated);
assert_eq!(
ActiveLoanInfo::try_from((POOL_A, util::get_loan(loan_id)))
.unwrap()
.current_price
.unwrap(),
NOTIONAL
);
});
}

0 comments on commit 78b83b8

Please sign in to comment.