Skip to content

Commit

Permalink
updated from #1408
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Apr 15, 2024
1 parent 58045f4 commit 8ca7dc2
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 97 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ rev_slice = { version = "0.1.5", default-features = false }
impl-trait-for-tuples = "0.2.1"
num-traits = { version = "0.2", default-features = false }
num_enum = { version = "0.5.3", default-features = false }
chrono = { version = "0.4", default-features = false }
chronoutil = "0.2"

# Cumulus
cumulus-pallet-aura-ext = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.1.0" }
Expand Down
2 changes: 2 additions & 0 deletions pallets/loans/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
chrono = { workspace = true }
chronoutil = { workspace = true }

frame-support = { workspace = true }
frame-system = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions pallets/loans/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ use crate::{
},
pallet::*,
types::{
cashflow::{InterestPayments, Maturity, PayDownSchedule, RepaymentSchedule},
valuation::{DiscountedCashFlow, ValuationMethod},
BorrowRestrictions, InterestPayments, LoanRestrictions, Maturity, PayDownSchedule,
RepayRestrictions, RepaymentSchedule,
BorrowRestrictions, LoanRestrictions, RepayRestrictions,
},
};

Expand Down
5 changes: 3 additions & 2 deletions pallets/loans/src/entities/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use crate::{
entities::input::{PrincipalInput, RepaidInput},
pallet::Config,
types::{
policy::WriteOffRule, valuation::ValuationMethod, InterestPayments, Maturity,
PayDownSchedule,
cashflow::{InterestPayments, Maturity, PayDownSchedule},
policy::WriteOffRule,
valuation::ValuationMethod,
},
};

Expand Down
36 changes: 26 additions & 10 deletions pallets/loans/src/entities/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ use crate::{
},
pallet::{AssetOf, Config, Error},
types::{
cashflow::RepaymentSchedule,
policy::{WriteOffStatus, WriteOffTrigger},
BorrowLoanError, BorrowRestrictions, CloseLoanError, CreateLoanError, LoanRestrictions,
MutationError, RepaidAmount, RepayLoanError, RepayRestrictions, RepaymentSchedule,
MutationError, RepaidAmount, RepayLoanError, RepayRestrictions,
},
};

Expand Down Expand Up @@ -237,6 +238,12 @@ impl<T: Config> ActiveLoan<T> {
}
}

pub fn principal(&self) -> Result<T::Balance, DispatchError> {
Ok(self
.total_borrowed
.ensure_sub(self.total_repaid.principal)?)
}

pub fn write_off_status(&self) -> WriteOffStatus<T::Rate> {
WriteOffStatus {
percentage: self.write_off_percentage,
Expand Down Expand Up @@ -345,6 +352,20 @@ impl<T: Config> ActiveLoan<T> {
Error::<T>::from(BorrowLoanError::MaturityDatePassed)
);

// TODO
// If the loan has an interest or pay down schedule other than None,
// then we should only allow borrowing more if no interest or principal
// payments are overdue.
//
// This is required because after borrowing more, it is not possible
// to validate anymore whether previous cashflows matched the repayment
// schedule, as we don't store historic data of the principal.
//
// Therefore, in `borrow()` we set repayments_on_schedule_until to now.
//
// TODO: check total_repaid_interest >= total_expected_interest
// and total_repaid_principal >= total_expected_principal

Ok(())
}

Expand Down Expand Up @@ -378,11 +399,8 @@ impl<T: Config> ActiveLoan<T> {
) -> Result<RepaidInput<T>, DispatchError> {
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)?;
let _ = amount.principal.internal()?;
let principal = self.principal()?;

(principal, inner.outstanding_interest(principal)?)
}
Expand Down Expand Up @@ -519,7 +537,7 @@ impl<T: Config> ActiveLoan<T> {

#[cfg(feature = "runtime-benchmarks")]
pub fn set_maturity(&mut self, duration: Seconds) {
self.schedule.maturity = crate::types::Maturity::fixed(duration);
self.schedule.maturity = crate::types::cashflow::Maturity::fixed(duration);
}
}

Expand Down Expand Up @@ -555,9 +573,7 @@ impl<T: Config> TryFrom<(T::PoolId, ActiveLoan<T>)> for ActiveLoanInfo<T> {

Ok(match &active_loan.pricing {
ActivePricing::Internal(inner) => {
let principal = active_loan
.total_borrowed
.ensure_sub(active_loan.total_repaid.principal)?;
let principal = active_loan.principal()?;

Self {
present_value,
Expand Down
6 changes: 3 additions & 3 deletions pallets/loans/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ use super::{
},
pallet::{ActiveLoans, CreatedLoan, Error, LastLoanId, PortfolioValuation},
types::{
cashflow::{InterestPayments, Maturity, PayDownSchedule, RepaymentSchedule},
policy::{WriteOffRule, WriteOffStatus, WriteOffTrigger},
valuation::{DiscountedCashFlow, ValuationMethod},
BorrowLoanError, BorrowRestrictions, CloseLoanError, CreateLoanError, InterestPayments,
LoanRestrictions, Maturity, MutationError, PayDownSchedule, RepayLoanError,
RepayRestrictions, RepaymentSchedule, WrittenOffError,
BorrowLoanError, BorrowRestrictions, CloseLoanError, CreateLoanError, LoanRestrictions,
MutationError, RepayLoanError, RepayRestrictions, WrittenOffError,
},
};

Expand Down
Loading

0 comments on commit 8ca7dc2

Please sign in to comment.