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

Loans: add outstanding principal & interest to runtime API #1482

Merged
merged 4 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 33 additions & 24 deletions pallets/loans/src/entities/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,25 +344,25 @@ impl<T: Config> ActiveLoan<T> {
&self,
mut amount: RepaidPricingAmount<T>,
) -> Result<RepaidPricingAmount<T>, DispatchError> {
let (interest_accrued, max_repay_principal) = match &self.pricing {
let (max_repay_principal, outstanding_interest) = match &self.pricing {
ActivePricing::Internal(inner) => {
amount.principal.internal()?;

let principal = self
.total_borrowed
.ensure_sub(self.total_repaid.principal)?;

(inner.current_interest(principal)?, principal)
(principal, inner.outstanding_interest(principal)?)
}
ActivePricing::External(inner) => {
let external_amount = amount.principal.external()?;
let max_repay_principal = inner.max_repay_principal(external_amount)?;

(inner.current_interest()?, max_repay_principal)
(max_repay_principal, inner.outstanding_interest()?)
}
};

amount.interest = amount.interest.min(interest_accrued);
amount.interest = amount.interest.min(outstanding_interest);

ensure!(
amount.principal.balance()? <= max_repay_principal,
Expand All @@ -374,7 +374,7 @@ impl<T: Config> ActiveLoan<T> {
RepayRestrictions::None => true,
RepayRestrictions::Full => {
amount.principal.balance()? == max_repay_principal
&& amount.interest == interest_accrued
&& amount.interest == outstanding_interest
}
},
Error::<T>::from(RepayLoanError::Restriction)
Expand Down Expand Up @@ -497,37 +497,46 @@ pub struct ActiveLoanInfo<T: Config> {
/// Related active loan
active_loan: ActiveLoan<T>,

/// Interest accrued for this loan
interest_accrued: T::Balance,

/// Present value of the loan
present_value: T::Balance,

/// Current outstanding principal of this loan
outstanding_principal: T::Balance,

/// Current outstanding interest of this loan
outstanding_interest: T::Balance,
}

impl<T: Config> TryFrom<ActiveLoan<T>> for ActiveLoanInfo<T> {
type Error = DispatchError;

fn try_from(active_loan: ActiveLoan<T>) -> Result<Self, Self::Error> {
let (interest_accrued, present_value) = match &active_loan.pricing {
ActivePricing::Internal(inner) => {
let principal = active_loan
.total_borrowed
.ensure_sub(active_loan.total_repaid.principal)?;

let maturity_date = active_loan.schedule.maturity.date();

(
inner.current_interest(principal)?,
inner.present_value(active_loan.origination_date, maturity_date)?,
)
}
ActivePricing::External(inner) => (inner.current_interest()?, inner.present_value()?),
};
let (present_value, outstanding_principal, outstanding_interest) =
match &active_loan.pricing {
ActivePricing::Internal(inner) => {
let principal = active_loan
.total_borrowed
.ensure_sub(active_loan.total_repaid.principal)?;
let maturity_date = active_loan.schedule.maturity.date();

(
inner.present_value(active_loan.origination_date, maturity_date)?,
principal,
inner.outstanding_interest(principal)?,
)
}
ActivePricing::External(inner) => (
inner.present_value()?,
inner.outstanding_principal()?,
inner.outstanding_interest()?,
),
};

Ok(Self {
active_loan,
interest_accrued,
present_value,
outstanding_principal,
outstanding_interest,
})
}
}
10 changes: 7 additions & 3 deletions pallets/loans/src/entities/pricing/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ impl<T: Config> ExternalActivePricing<T> {
Ok(T::PriceRegistry::get(&self.info.price_id)?.1)
}

pub fn current_interest(&self) -> Result<T::Balance, DispatchError> {
pub fn outstanding_principal(&self) -> Result<T::Balance, DispatchError> {
let price = self.current_price()?;
Ok(self.outstanding_quantity.ensure_mul_int(price)?)
}

pub fn outstanding_interest(&self) -> Result<T::Balance, DispatchError> {
let outstanding_notional = self
.outstanding_quantity
.ensure_mul_int(self.info.notional)?;
Expand All @@ -136,8 +141,7 @@ impl<T: Config> ExternalActivePricing<T> {
}

pub fn present_value(&self) -> Result<T::Balance, DispatchError> {
let price = self.current_price()?;
Ok(self.outstanding_quantity.ensure_mul_int(price)?)
self.outstanding_principal()
}

pub fn present_value_cached<Prices>(&self, cache: &Prices) -> Result<T::Balance, DispatchError>
Expand Down
6 changes: 3 additions & 3 deletions pallets/loans/src/entities/pricing/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ impl<T: Config> InternalActivePricing<T> {
self.compute_present_value(debt, origination_date, maturity_date)
}

pub fn current_interest(
pub fn outstanding_interest(
&self,
current_principal: T::Balance,
outstanding_principal: T::Balance,
) -> Result<T::Balance, DispatchError> {
let debt = self.interest.current_debt()?;
Ok(debt.ensure_sub(current_principal)?)
Ok(debt.ensure_sub(outstanding_principal)?)
}

pub fn max_borrow_amount(
Expand Down