From 78b83b80e226aaec0b3867c633af450533a31388 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Wed, 22 May 2024 12:13:26 +0200 Subject: [PATCH] better fix --- pallets/loans/src/entities/loans.rs | 8 +++----- pallets/loans/src/entities/pricing/external.rs | 9 ++++----- pallets/loans/src/tests/mod.rs | 2 +- pallets/loans/src/tests/repay_loan.rs | 18 ++++++++---------- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/pallets/loans/src/entities/loans.rs b/pallets/loans/src/entities/loans.rs index a6659da785..20344370f6 100644 --- a/pallets/loans/src/entities/loans.rs +++ b/pallets/loans/src/entities/loans.rs @@ -14,7 +14,7 @@ use sp_runtime::{ }, DispatchError, }; -use sp_std::{cmp::min, collections::btree_map::BTreeMap}; +use sp_std::collections::btree_map::BTreeMap; use crate::{ entities::{ @@ -359,8 +359,7 @@ impl ActiveLoan { inner.adjust(Adjustment::Increase(amount.balance()?))? } ActivePricing::External(inner) => { - let when = T::Time::now(); - inner.adjust(Adjustment::Increase(amount.external()?), Zero::zero(), when)? + inner.adjust(Adjustment::Increase(amount.external()?), Zero::zero())? } } @@ -434,8 +433,7 @@ impl ActiveLoan { } ActivePricing::External(inner) => { let principal = amount.principal.external()?; - let when = min(T::Time::now(), self.schedule.maturity.date()); - inner.adjust(Adjustment::Decrease(principal), amount.interest, when)?; + inner.adjust(Adjustment::Decrease(principal), amount.interest)?; } } diff --git a/pallets/loans/src/entities/pricing/external.rs b/pallets/loans/src/entities/pricing/external.rs index c3d10d7b0d..6bb654fa23 100644 --- a/pallets/loans/src/entities/pricing/external.rs +++ b/pallets/loans/src/entities/pricing/external.rs @@ -9,7 +9,7 @@ use sp_runtime::{ traits::{EnsureAdd, EnsureFixedPointNumber, EnsureSub, Zero}, ArithmeticError, DispatchError, DispatchResult, FixedPointNumber, }; -use sp_std::collections::btree_map::BTreeMap; +use sp_std::{cmp::min, collections::btree_map::BTreeMap}; use crate::{ entities::interest::ActiveInterestRate, @@ -166,9 +166,9 @@ impl ExternalActivePricing { ) -> Result { if self.info.with_linear_pricing { Ok(cfg_utils::math::y_coord_in_rect( - (price_last_updated, price), + (min(price_last_updated, maturity), price), (maturity, self.info.notional), - T::Time::now(), + min(T::Time::now(), maturity), )?) } else { Ok(price) @@ -302,7 +302,6 @@ impl ExternalActivePricing { &mut self, amount_adj: Adjustment>, interest: T::Balance, - when: Seconds, ) -> DispatchResult { self.outstanding_quantity = amount_adj .clone() @@ -318,7 +317,7 @@ impl ExternalActivePricing { self.interest.adjust_debt(interest_adj)?; self.latest_settlement_price = amount_adj.abs().settlement_price; - self.settlement_price_updated = when; + self.settlement_price_updated = T::Time::now(); Ok(()) } diff --git a/pallets/loans/src/tests/mod.rs b/pallets/loans/src/tests/mod.rs index ac8dd0011c..83a1783de3 100644 --- a/pallets/loans/src/tests/mod.rs +++ b/pallets/loans/src/tests/mod.rs @@ -11,7 +11,7 @@ use super::{ entities::{ changes::{Change, InternalMutation, LoanMutation}, input::{PrincipalInput, RepaidInput}, - loans::{ActiveLoan, LoanInfo}, + loans::{ActiveLoan, ActiveLoanInfo, LoanInfo}, pricing::{ external::{ExternalAmount, ExternalPricing, MaxBorrowAmount as ExtMaxBorrowAmount}, internal::{InternalPricing, MaxBorrowAmount as IntMaxBorrowAmount}, diff --git a/pallets/loans/src/tests/repay_loan.rs b/pallets/loans/src/tests/repay_loan.rs index d72d4f1d58..13970a0d31 100644 --- a/pallets/loans/src/tests/repay_loan.rs +++ b/pallets/loans/src/tests/repay_loan.rs @@ -824,7 +824,7 @@ fn with_external_pricing_and_overdue() { util::borrow_loan(loan_id, PrincipalInput::External(amount)); // The loan is overdue - advance_time(YEAR * 2); + advance_time(YEAR * DAY); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); config_mocks(amount.balance().unwrap()); @@ -839,14 +839,12 @@ fn with_external_pricing_and_overdue() { }, )); - 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); + assert_eq!( + ActiveLoanInfo::try_from((POOL_A, util::get_loan(loan_id))) + .unwrap() + .current_price + .unwrap(), + NOTIONAL + ); }); }