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: linear accrual not overpassing maturity #1842

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess maturity not being passed already is checked by the ensure_can_borrw()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But not in repay()

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also check, that the linear pricing actually clamps with giving the notional baack?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still like to test the case where

last_updated_price and T::Time::now() are both equal to maturity. I think that is an edge case worth testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tested currently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it is repaid with one DAY of overdue, and the expected value is notional

Loading