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: LoanId out from ActiveLoan #1367

Merged
merged 2 commits into from
May 26, 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
21 changes: 11 additions & 10 deletions pallets/loans-ref/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub mod pallet {
_,
Blake2_128Concat,
PoolIdOf<T>,
BoundedVec<ActiveLoan<T>, T::MaxActiveLoansPerPool>,
BoundedVec<(T::LoanId, ActiveLoan<T>), T::MaxActiveLoansPerPool>,
ValueQuery,
>;

Expand Down Expand Up @@ -453,10 +453,10 @@ pub mod pallet {
Some(created_loan) => {
Self::ensure_loan_borrower(&who, created_loan.borrower())?;

let mut active_loan = created_loan.activate(pool_id, loan_id)?;
let mut active_loan = created_loan.activate(pool_id)?;
active_loan.borrow(amount)?;

Self::insert_active_loan(pool_id, active_loan)?
Self::insert_active_loan(pool_id, loan_id, active_loan)?
}
None => {
Self::update_active_loan(pool_id, loan_id, |loan| {
Expand Down Expand Up @@ -754,7 +754,7 @@ pub mod pallet {
let loans = ActiveLoans::<T>::get(pool_id);
let values = loans
.iter()
.map(|loan| Ok((loan.loan_id(), loan.present_value_by(&rates, &prices)?)))
.map(|(loan_id, loan)| Ok((*loan_id, loan.present_value_by(&rates, &prices)?)))
.collect::<Result<Vec<_>, DispatchError>>()?;

let value = PortfolioValuation::<T>::try_mutate(pool_id, |portfolio| {
Expand All @@ -772,10 +772,11 @@ pub mod pallet {

fn insert_active_loan(
pool_id: PoolIdOf<T>,
loan_id: T::LoanId,
loan: ActiveLoan<T>,
) -> Result<u32, DispatchError> {
PortfolioValuation::<T>::try_mutate(pool_id, |portfolio| {
portfolio.insert_elem(loan.loan_id(), loan.present_value()?)?;
portfolio.insert_elem(loan_id, loan.present_value()?)?;

Self::deposit_event(Event::<T>::PortfolioValuationUpdated {
pool_id,
Expand All @@ -785,7 +786,7 @@ pub mod pallet {

ActiveLoans::<T>::try_mutate(pool_id, |active_loans| {
active_loans
.try_push(loan)
.try_push((loan_id, loan))
.map_err(|_| Error::<T>::MaxActiveLoansReached)?;

Ok(active_loans.len().ensure_into()?)
Expand All @@ -803,9 +804,9 @@ pub mod pallet {
{
PortfolioValuation::<T>::try_mutate(pool_id, |portfolio| {
ActiveLoans::<T>::try_mutate(pool_id, |active_loans| {
let loan = active_loans
let (_, loan) = active_loans
.iter_mut()
.find(|loan| loan.loan_id() == loan_id)
.find(|(id, _)| *id == loan_id)
.ok_or(Error::<T>::LoanNotActiveOrNotFound)?;

let result = f(loan)?;
Expand All @@ -830,11 +831,11 @@ pub mod pallet {
ActiveLoans::<T>::try_mutate(pool_id, |active_loans| {
let index = active_loans
.iter()
.position(|loan| loan.loan_id() == loan_id)
.position(|(id, _)| *id == loan_id)
.ok_or(Error::<T>::LoanNotActiveOrNotFound)?;

Ok((
active_loans.swap_remove(index),
active_loans.swap_remove(index).1,
active_loans.len().ensure_into()?,
))
})
Expand Down
23 changes: 2 additions & 21 deletions pallets/loans-ref/src/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,8 @@ impl<T: Config> CreatedLoan<T> {
&self.borrower
}

pub fn activate(
self,
pool_id: PoolIdOf<T>,
loan_id: T::LoanId,
) -> Result<ActiveLoan<T>, DispatchError> {
ActiveLoan::new(
pool_id,
loan_id,
self.info,
self.borrower,
T::Time::now().as_secs(),
)
pub fn activate(self, pool_id: PoolIdOf<T>) -> Result<ActiveLoan<T>, DispatchError> {
ActiveLoan::new(pool_id, self.info, self.borrower, T::Time::now().as_secs())
}

pub fn close(self) -> Result<(ClosedLoan<T>, T::AccountId), DispatchError> {
Expand Down Expand Up @@ -140,9 +130,6 @@ impl<T: Config> ClosedLoan<T> {
#[derive(Encode, Decode, Clone, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct ActiveLoan<T: Config> {
/// Id of this loan
loan_id: T::LoanId,

/// Specify the repayments schedule of the loan
schedule: RepaymentSchedule,

Expand Down Expand Up @@ -174,13 +161,11 @@ pub struct ActiveLoan<T: Config> {
impl<T: Config> ActiveLoan<T> {
pub fn new(
pool_id: PoolIdOf<T>,
loan_id: T::LoanId,
info: LoanInfo<T>,
borrower: T::AccountId,
now: Moment,
) -> Result<Self, DispatchError> {
Ok(ActiveLoan {
loan_id,
schedule: info.schedule,
collateral: info.collateral,
restrictions: info.restrictions,
Expand All @@ -200,10 +185,6 @@ impl<T: Config> ActiveLoan<T> {
})
}

pub fn loan_id(&self) -> T::LoanId {
self.loan_id
}

pub fn borrower(&self) -> &T::AccountId {
&self.borrower
}
Expand Down
3 changes: 2 additions & 1 deletion pallets/loans-ref/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ mod util {
pub fn get_loan(loan_id: LoanId) -> ActiveLoan<Runtime> {
ActiveLoans::<Runtime>::get(POOL_A)
.into_iter()
.find(|loan| loan.loan_id() == loan_id)
.find(|(id, _)| *id == loan_id)
.unwrap()
.1
}

pub fn current_loan_debt(loan_id: LoanId) -> Balance {
Expand Down