Skip to content

Commit

Permalink
not overpassing maturity
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed May 22, 2024
1 parent e2dd3ca commit 652c102
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
8 changes: 5 additions & 3 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::collections::btree_map::BTreeMap;
use sp_std::{cmp::min, collections::btree_map::BTreeMap};

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

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

Expand Down
7 changes: 6 additions & 1 deletion pallets/loans/src/entities/pricing/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ impl<T: Config> ExternalActivePricing<T> {
self.info.price_id
}

pub fn settlement_price_updated(&self) -> Seconds {
self.settlement_price_updated
}

pub fn has_registered_price(&self, pool_id: T::PoolId) -> bool {
T::PriceRegistry::get(&self.info.price_id, &pool_id).is_ok()
}
Expand Down Expand Up @@ -298,6 +302,7 @@ 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 @@ -313,7 +318,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 = T::Time::now();
self.settlement_price_updated = when;

Ok(())
}
Expand Down
38 changes: 38 additions & 0 deletions pallets/loans/src/tests/repay_loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,3 +812,41 @@ fn with_unregister_price_id_and_oracle_not_required() {
);
});
}

#[test]
fn with_external_pricing_and_overdue() {
new_test_ext().execute_with(|| {
let loan_id = util::create_loan(LoanInfo {
..util::base_external_loan()
});

let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE);
util::borrow_loan(loan_id, PrincipalInput::External(amount));

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

let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE);
config_mocks(amount.balance().unwrap());
assert_ok!(Loans::repay(
RuntimeOrigin::signed(BORROWER),
POOL_A,
loan_id,
RepaidInput {
principal: PrincipalInput::External(amount),
interest: 0,
unscheduled: 0,
},
));

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);
});
}

0 comments on commit 652c102

Please sign in to comment.