diff --git a/pallets/loans-ref/src/lib.rs b/pallets/loans-ref/src/lib.rs index b8d5b17aee..edec690835 100644 --- a/pallets/loans-ref/src/lib.rs +++ b/pallets/loans-ref/src/lib.rs @@ -247,7 +247,7 @@ pub mod pallet { _, Blake2_128Concat, PoolIdOf, - BoundedVec, T::MaxActiveLoansPerPool>, + BoundedVec<(T::LoanId, ActiveLoan), T::MaxActiveLoansPerPool>, ValueQuery, >; @@ -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| { @@ -754,7 +754,7 @@ pub mod pallet { let loans = ActiveLoans::::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::, DispatchError>>()?; let value = PortfolioValuation::::try_mutate(pool_id, |portfolio| { @@ -772,10 +772,11 @@ pub mod pallet { fn insert_active_loan( pool_id: PoolIdOf, + loan_id: T::LoanId, loan: ActiveLoan, ) -> Result { PortfolioValuation::::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::::PortfolioValuationUpdated { pool_id, @@ -785,7 +786,7 @@ pub mod pallet { ActiveLoans::::try_mutate(pool_id, |active_loans| { active_loans - .try_push(loan) + .try_push((loan_id, loan)) .map_err(|_| Error::::MaxActiveLoansReached)?; Ok(active_loans.len().ensure_into()?) @@ -803,9 +804,9 @@ pub mod pallet { { PortfolioValuation::::try_mutate(pool_id, |portfolio| { ActiveLoans::::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::::LoanNotActiveOrNotFound)?; let result = f(loan)?; @@ -830,11 +831,11 @@ pub mod pallet { ActiveLoans::::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::::LoanNotActiveOrNotFound)?; Ok(( - active_loans.swap_remove(index), + active_loans.swap_remove(index).1, active_loans.len().ensure_into()?, )) }) diff --git a/pallets/loans-ref/src/loan.rs b/pallets/loans-ref/src/loan.rs index 224c1edb30..89e007b0ad 100644 --- a/pallets/loans-ref/src/loan.rs +++ b/pallets/loans-ref/src/loan.rs @@ -87,18 +87,8 @@ impl CreatedLoan { &self.borrower } - pub fn activate( - self, - pool_id: PoolIdOf, - loan_id: T::LoanId, - ) -> Result, DispatchError> { - ActiveLoan::new( - pool_id, - loan_id, - self.info, - self.borrower, - T::Time::now().as_secs(), - ) + pub fn activate(self, pool_id: PoolIdOf) -> Result, DispatchError> { + ActiveLoan::new(pool_id, self.info, self.borrower, T::Time::now().as_secs()) } pub fn close(self) -> Result<(ClosedLoan, T::AccountId), DispatchError> { @@ -140,9 +130,6 @@ impl ClosedLoan { #[derive(Encode, Decode, Clone, TypeInfo, MaxEncodedLen)] #[scale_info(skip_type_params(T))] pub struct ActiveLoan { - /// Id of this loan - loan_id: T::LoanId, - /// Specify the repayments schedule of the loan schedule: RepaymentSchedule, @@ -174,13 +161,11 @@ pub struct ActiveLoan { impl ActiveLoan { pub fn new( pool_id: PoolIdOf, - loan_id: T::LoanId, info: LoanInfo, borrower: T::AccountId, now: Moment, ) -> Result { Ok(ActiveLoan { - loan_id, schedule: info.schedule, collateral: info.collateral, restrictions: info.restrictions, @@ -200,10 +185,6 @@ impl ActiveLoan { }) } - pub fn loan_id(&self) -> T::LoanId { - self.loan_id - } - pub fn borrower(&self) -> &T::AccountId { &self.borrower } diff --git a/pallets/loans-ref/src/tests.rs b/pallets/loans-ref/src/tests.rs index 4b014bbe3f..0360035d06 100644 --- a/pallets/loans-ref/src/tests.rs +++ b/pallets/loans-ref/src/tests.rs @@ -53,8 +53,9 @@ mod util { pub fn get_loan(loan_id: LoanId) -> ActiveLoan { ActiveLoans::::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 {