From b117c25e9d49aad5934f6fbc1e5f41195d02996d Mon Sep 17 00:00:00 2001 From: lemunozm Date: Mon, 25 Sep 2023 11:25:39 +0200 Subject: [PATCH] rename input types --- pallets/loans/src/benchmarking.rs | 27 +-- pallets/loans/src/entities/changes.rs | 9 +- pallets/loans/src/entities/input.rs | 58 ++++++ pallets/loans/src/entities/loans.rs | 19 +- pallets/loans/src/entities/pricing.rs | 54 +---- pallets/loans/src/lib.rs | 25 +-- pallets/loans/src/tests/borrow_loan.rs | 58 +++--- pallets/loans/src/tests/close_loan.rs | 24 +-- pallets/loans/src/tests/mod.rs | 3 +- pallets/loans/src/tests/mutate_loan.rs | 16 +- pallets/loans/src/tests/policy.rs | 4 +- .../loans/src/tests/portfolio_valuation.rs | 28 +-- pallets/loans/src/tests/repay_loan.rs | 188 +++++++++--------- pallets/loans/src/tests/transfer_debt.rs | 106 +++++----- pallets/loans/src/tests/util.rs | 6 +- pallets/loans/src/tests/write_off_loan.rs | 26 +-- runtime/integration-tests/src/utils/loans.rs | 11 +- 17 files changed, 332 insertions(+), 330 deletions(-) create mode 100644 pallets/loans/src/entities/input.rs diff --git a/pallets/loans/src/benchmarking.rs b/pallets/loans/src/benchmarking.rs index 2173581727..9dd7c3bc24 100644 --- a/pallets/loans/src/benchmarking.rs +++ b/pallets/loans/src/benchmarking.rs @@ -36,10 +36,11 @@ use sp_std::time::Duration; use crate::{ entities::{ changes::{Change, LoanMutation}, + input::{PrincipalInput, RepaidInput}, loans::LoanInfo, pricing::{ internal::{InternalPricing, MaxBorrowAmount}, - Pricing, PricingAmount, RepaidPricingAmount, + Pricing, }, }, pallet::*, @@ -185,7 +186,7 @@ where RawOrigin::Signed(borrower).into(), pool_id, loan_id, - PricingAmount::Internal(10.into()), + PrincipalInput::Internal(10.into()), ) .unwrap(); } @@ -196,8 +197,8 @@ where RawOrigin::Signed(borrower).into(), pool_id, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(10.into()), + RepaidInput { + principal: PrincipalInput::Internal(10.into()), interest: T::Balance::max_value(), unscheduled: 0.into(), }, @@ -252,12 +253,12 @@ where Helper::::borrow_loan(pool_id, loan_1); let loan_2 = Helper::::create_loan(pool_id, (u16::MAX - 1).into()); - let repaid_amount = RepaidPricingAmount { - principal: PricingAmount::Internal(10.into()), + let repaid_amount = RepaidInput { + principal: PrincipalInput::Internal(10.into()), interest: 0.into(), unscheduled: 0.into(), }; - let borrow_amount = PricingAmount::Internal(10.into()); + let borrow_amount = PrincipalInput::Internal(10.into()); Pallet::::propose_transfer_debt( RawOrigin::Signed(borrower).into(), @@ -356,7 +357,7 @@ benchmarks! { let pool_id = Helper::::initialize_active_state(n); let loan_id = Helper::::create_loan(pool_id, u16::MAX.into()); - }: _(RawOrigin::Signed(borrower), pool_id, loan_id, PricingAmount::Internal(10.into())) + }: _(RawOrigin::Signed(borrower), pool_id, loan_id, PrincipalInput::Internal(10.into())) repay { let n in 1..Helper::::max_active_loans() - 1; @@ -366,8 +367,8 @@ benchmarks! { let loan_id = Helper::::create_loan(pool_id, u16::MAX.into()); Helper::::borrow_loan(pool_id, loan_id); - let repaid = RepaidPricingAmount { - principal: PricingAmount::Internal(10.into()), + let repaid = RepaidInput { + principal: PrincipalInput::Internal(10.into()), interest: 0.into(), unscheduled: 0.into() }; @@ -466,12 +467,12 @@ benchmarks! { Helper::::borrow_loan(pool_id, loan_1); let loan_2 = Helper::::create_loan(pool_id, (u16::MAX - 1).into()); - let repaid_amount = RepaidPricingAmount { - principal: PricingAmount::Internal(10.into()), + let repaid_amount = RepaidInput { + principal: PrincipalInput::Internal(10.into()), interest: 0.into(), unscheduled: 0.into() }; - let borrow_amount = PricingAmount::Internal(10.into()); + let borrow_amount = PrincipalInput::Internal(10.into()); }: _(RawOrigin::Signed(borrower), pool_id, loan_1, loan_2, repaid_amount, borrow_amount) diff --git a/pallets/loans/src/entities/changes.rs b/pallets/loans/src/entities/changes.rs index aada91f8b3..4f7cfdfc1d 100644 --- a/pallets/loans/src/entities/changes.rs +++ b/pallets/loans/src/entities/changes.rs @@ -5,7 +5,7 @@ use frame_support::{storage::bounded_vec::BoundedVec, RuntimeDebug}; use scale_info::TypeInfo; use crate::{ - entities::pricing::{PricingAmount, RepaidPricingAmount}, + entities::input::{PrincipalInput, RepaidInput}, pallet::Config, types::{ policy::WriteOffRule, valuation::ValuationMethod, InterestPayments, Maturity, @@ -39,10 +39,5 @@ pub enum LoanMutation { pub enum Change { Loan(T::LoanId, LoanMutation), Policy(BoundedVec, T::MaxWriteOffPolicySize>), - TransferDebt( - T::LoanId, - T::LoanId, - RepaidPricingAmount, - PricingAmount, - ), + TransferDebt(T::LoanId, T::LoanId, RepaidInput, PrincipalInput), } diff --git a/pallets/loans/src/entities/input.rs b/pallets/loans/src/entities/input.rs new file mode 100644 index 0000000000..9fba7e0ec3 --- /dev/null +++ b/pallets/loans/src/entities/input.rs @@ -0,0 +1,58 @@ +use codec::{Decode, Encode, MaxEncodedLen}; +use frame_support::RuntimeDebugNoBound; +use scale_info::TypeInfo; +use sp_runtime::{ArithmeticError, DispatchError}; + +use crate::{ + entities::pricing::external::ExternalAmount, + pallet::{Config, Error}, + types::RepaidAmount, +}; + +#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)] +#[scale_info(skip_type_params(T))] +pub enum PrincipalInput { + Internal(T::Balance), + External(ExternalAmount), +} + +impl PrincipalInput { + pub fn balance(&self) -> Result { + match self { + Self::Internal(amount) => Ok(*amount), + Self::External(external) => external.balance(), + } + } + + pub fn internal(&self) -> Result { + match self { + Self::Internal(amount) => Ok(*amount), + Self::External(_) => Err(Error::::MismatchedPricingMethod.into()), + } + } + + pub fn external(&self) -> Result, DispatchError> { + match self { + Self::Internal(_) => Err(Error::::MismatchedPricingMethod.into()), + Self::External(principal) => Ok(principal.clone()), + } + } +} + +#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)] +#[scale_info(skip_type_params(T))] +pub struct RepaidInput { + pub principal: PrincipalInput, + pub interest: T::Balance, + pub unscheduled: T::Balance, +} + +impl RepaidInput { + pub fn repaid_amount(&self) -> Result, ArithmeticError> { + Ok(RepaidAmount { + principal: self.principal.balance()?, + interest: self.interest, + unscheduled: self.unscheduled, + }) + } +} diff --git a/pallets/loans/src/entities/loans.rs b/pallets/loans/src/entities/loans.rs index 70884de352..4702b221b4 100644 --- a/pallets/loans/src/entities/loans.rs +++ b/pallets/loans/src/entities/loans.rs @@ -20,9 +20,10 @@ use sp_runtime::{ use crate::{ entities::{ changes::LoanMutation, + input::{PrincipalInput, RepaidInput}, pricing::{ external::ExternalActivePricing, internal::InternalActivePricing, ActivePricing, - Pricing, PricingAmount, RepaidPricingAmount, + Pricing, }, }, pallet::{AssetOf, Config, Error, PriceOf}, @@ -101,7 +102,7 @@ impl CreatedLoan { pub fn activate( self, pool_id: T::PoolId, - initial_amount: PricingAmount, + initial_amount: PrincipalInput, ) -> Result, DispatchError> { ActiveLoan::new( pool_id, @@ -188,7 +189,7 @@ impl ActiveLoan { pool_id: T::PoolId, info: LoanInfo, borrower: T::AccountId, - initial_amount: PricingAmount, + initial_amount: PrincipalInput, now: Moment, ) -> Result { Ok(ActiveLoan { @@ -300,7 +301,7 @@ impl ActiveLoan { self.write_down(value) } - fn ensure_can_borrow(&self, amount: &PricingAmount, pool_id: T::PoolId) -> DispatchResult { + fn ensure_can_borrow(&self, amount: &PrincipalInput, pool_id: T::PoolId) -> DispatchResult { let max_borrow_amount = match &self.pricing { ActivePricing::Internal(inner) => { amount.internal()?; @@ -342,7 +343,7 @@ impl ActiveLoan { Ok(()) } - pub fn borrow(&mut self, amount: &PricingAmount, pool_id: T::PoolId) -> DispatchResult { + pub fn borrow(&mut self, amount: &PrincipalInput, pool_id: T::PoolId) -> DispatchResult { self.ensure_can_borrow(amount, pool_id)?; self.total_borrowed.ensure_add_assign(amount.balance()?)?; @@ -367,9 +368,9 @@ impl ActiveLoan { /// - Checking repay restrictions fn prepare_repayment( &self, - mut amount: RepaidPricingAmount, + mut amount: RepaidInput, pool_id: T::PoolId, - ) -> Result, DispatchError> { + ) -> Result, DispatchError> { let (max_repay_principal, outstanding_interest) = match &self.pricing { ActivePricing::Internal(inner) => { amount.principal.internal()?; @@ -411,9 +412,9 @@ impl ActiveLoan { pub fn repay( &mut self, - amount: RepaidPricingAmount, + amount: RepaidInput, pool_id: T::PoolId, - ) -> Result, DispatchError> { + ) -> Result, DispatchError> { let amount = self.prepare_repayment(amount, pool_id)?; self.total_repaid diff --git a/pallets/loans/src/entities/pricing.rs b/pallets/loans/src/entities/pricing.rs index 0764e1dd48..1607a24a1e 100644 --- a/pallets/loans/src/entities/pricing.rs +++ b/pallets/loans/src/entities/pricing.rs @@ -1,12 +1,8 @@ use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::RuntimeDebugNoBound; use scale_info::TypeInfo; -use sp_runtime::{ArithmeticError, DispatchError}; -use crate::{ - pallet::{Config, Error}, - types::RepaidAmount, -}; +use crate::pallet::Config; pub mod external; pub mod internal; @@ -32,51 +28,3 @@ pub enum ActivePricing { /// Internal attributes External(external::ExternalActivePricing), } - -#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)] -#[scale_info(skip_type_params(T))] -pub enum PricingAmount { - Internal(T::Balance), - External(external::ExternalAmount), -} - -impl PricingAmount { - pub fn balance(&self) -> Result { - match self { - Self::Internal(amount) => Ok(*amount), - Self::External(external) => external.balance(), - } - } - - pub fn internal(&self) -> Result { - match self { - Self::Internal(amount) => Ok(*amount), - Self::External(_) => Err(Error::::MismatchedPricingMethod.into()), - } - } - - pub fn external(&self) -> Result, DispatchError> { - match self { - Self::Internal(_) => Err(Error::::MismatchedPricingMethod.into()), - Self::External(principal) => Ok(principal.clone()), - } - } -} - -#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)] -#[scale_info(skip_type_params(T))] -pub struct RepaidPricingAmount { - pub principal: PricingAmount, - pub interest: T::Balance, - pub unscheduled: T::Balance, -} - -impl RepaidPricingAmount { - pub fn repaid_amount(&self) -> Result, ArithmeticError> { - Ok(RepaidAmount { - principal: self.principal.balance()?, - interest: self.interest, - unscheduled: self.unscheduled, - }) - } -} diff --git a/pallets/loans/src/lib.rs b/pallets/loans/src/lib.rs index e9617a8e86..d9b7ae5f45 100644 --- a/pallets/loans/src/lib.rs +++ b/pallets/loans/src/lib.rs @@ -45,6 +45,7 @@ /// High level types that uses `pallet::Config` pub mod entities { pub mod changes; + pub mod input; pub mod interest; pub mod loans; pub mod pricing; @@ -81,8 +82,8 @@ pub mod pallet { use codec::HasCompact; use entities::{ changes::{Change, LoanMutation}, + input::{PrincipalInput, RepaidInput}, loans::{self, ActiveLoan, ActiveLoanInfo, LoanInfo}, - pricing::{PricingAmount, RepaidPricingAmount}, }; use frame_support::{ pallet_prelude::*, @@ -303,13 +304,13 @@ pub mod pallet { Borrowed { pool_id: T::PoolId, loan_id: T::LoanId, - amount: PricingAmount, + amount: PrincipalInput, }, /// An amount was repaid for a loan Repaid { pool_id: T::PoolId, loan_id: T::LoanId, - amount: RepaidPricingAmount, + amount: RepaidInput, }, /// A loan was written off WrittenOff { @@ -477,7 +478,7 @@ pub mod pallet { origin: OriginFor, pool_id: T::PoolId, loan_id: T::LoanId, - amount: PricingAmount, + amount: PrincipalInput, ) -> DispatchResult { let who = ensure_signed(origin)?; @@ -510,7 +511,7 @@ pub mod pallet { origin: OriginFor, pool_id: T::PoolId, loan_id: T::LoanId, - amount: RepaidPricingAmount, + amount: RepaidInput, ) -> DispatchResult { let who = ensure_signed(origin)?; @@ -778,8 +779,8 @@ pub mod pallet { pool_id: T::PoolId, from_loan_id: T::LoanId, to_loan_id: T::LoanId, - repaid_amount: RepaidPricingAmount, - borrow_amount: PricingAmount, + repaid_amount: RepaidInput, + borrow_amount: PrincipalInput, ) -> DispatchResult { let who = ensure_signed(origin)?; @@ -853,7 +854,7 @@ pub mod pallet { who: &T::AccountId, pool_id: T::PoolId, loan_id: T::LoanId, - amount: &PricingAmount, + amount: &PrincipalInput, permissionless: bool, ) -> Result { Ok(match CreatedLoan::::take(pool_id, loan_id) { @@ -884,9 +885,9 @@ pub mod pallet { who: &T::AccountId, pool_id: T::PoolId, loan_id: T::LoanId, - amount: &RepaidPricingAmount, + amount: &RepaidInput, permissionless: bool, - ) -> Result<(RepaidPricingAmount, u32), DispatchError> { + ) -> Result<(RepaidInput, u32), DispatchError> { Self::update_active_loan(pool_id, loan_id, |loan| { if !permissionless { Self::ensure_loan_borrower(who, loan.borrower())?; @@ -901,8 +902,8 @@ pub mod pallet { pool_id: T::PoolId, from_loan_id: T::LoanId, to_loan_id: T::LoanId, - repaid_amount: RepaidPricingAmount, - borrow_amount: PricingAmount, + repaid_amount: RepaidInput, + borrow_amount: PrincipalInput, permissionless: bool, ) -> Result<(T::Balance, u32), DispatchError> { ensure!( diff --git a/pallets/loans/src/tests/borrow_loan.rs b/pallets/loans/src/tests/borrow_loan.rs index 4e2bd70f5d..5421e8edfb 100644 --- a/pallets/loans/src/tests/borrow_loan.rs +++ b/pallets/loans/src/tests/borrow_loan.rs @@ -34,7 +34,7 @@ fn with_wrong_loan_id() { RuntimeOrigin::signed(BORROWER), POOL_A, 0, - PricingAmount::Internal(COLLATERAL_VALUE) + PrincipalInput::Internal(COLLATERAL_VALUE) ), Error::::LoanNotActiveOrNotFound ); @@ -53,7 +53,7 @@ fn from_other_borrower() { RuntimeOrigin::signed(OTHER_BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE) + PrincipalInput::Internal(COLLATERAL_VALUE) ), Error::::NotLoanBorrower ); @@ -70,7 +70,7 @@ fn with_restriction_no_written_off() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE / 2) + PrincipalInput::Internal(COLLATERAL_VALUE / 2) )); advance_time(YEAR + DAY); @@ -81,7 +81,7 @@ fn with_restriction_no_written_off() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE / 2) + PrincipalInput::Internal(COLLATERAL_VALUE / 2) ), Error::::from(BorrowLoanError::Restriction) ); @@ -105,7 +105,7 @@ fn with_restriction_full_once() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE / 2) // Must be full value + PrincipalInput::Internal(COLLATERAL_VALUE / 2) // Must be full value ), Error::::from(BorrowLoanError::Restriction) ); @@ -115,7 +115,7 @@ fn with_restriction_full_once() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE) + PrincipalInput::Internal(COLLATERAL_VALUE) )); // Borrow was already done @@ -124,7 +124,7 @@ fn with_restriction_full_once() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(0) + PrincipalInput::Internal(0) ), Error::::from(BorrowLoanError::Restriction) ); @@ -144,7 +144,7 @@ fn with_maturity_passed() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE) + PrincipalInput::Internal(COLLATERAL_VALUE) ), Error::::from(BorrowLoanError::MaturityDatePassed) ); @@ -162,7 +162,7 @@ fn with_wrong_internal_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(0) + PrincipalInput::Internal(0) ), Error::::MismatchedPricingMethod ); @@ -180,7 +180,7 @@ fn with_wrong_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(ExternalAmount::empty()) + PrincipalInput::External(ExternalAmount::empty()) ), Error::::MismatchedPricingMethod ); @@ -214,7 +214,7 @@ fn with_wrong_big_amount_internal_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(amount) + PrincipalInput::Internal(amount) ), Error::::from(BorrowLoanError::MaxAmountExceeded) ); @@ -248,7 +248,7 @@ fn with_correct_amount_internal_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(amount) + PrincipalInput::Internal(amount) )); assert_eq!(amount, util::current_loan_debt(loan_id)); }); @@ -280,7 +280,7 @@ fn with_unregister_price_id_and_oracle_required() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) ), PRICE_ID_NO_FOUND ); @@ -308,7 +308,7 @@ fn with_unregister_price_id_and_oracle_not_required() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) )); assert_eq!( @@ -324,7 +324,7 @@ fn with_unregister_price_id_and_oracle_not_required() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) )); assert_eq!( @@ -355,7 +355,7 @@ fn with_wrong_big_amount_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) ), Error::::from(BorrowLoanError::MaxAmountExceeded) ); @@ -375,7 +375,7 @@ fn with_incorrect_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) ), Error::::SettlementPriceExceedsVariation ); @@ -391,7 +391,7 @@ fn with_incorrect_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) ), Error::::SettlementPriceExceedsVariation ); @@ -407,7 +407,7 @@ fn with_incorrect_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) ), Error::::SettlementPriceExceedsVariation ); @@ -430,7 +430,7 @@ fn with_correct_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) )); assert_eq!( @@ -446,7 +446,7 @@ fn with_correct_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) )); assert_eq!( @@ -465,7 +465,7 @@ fn with_correct_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) )); assert_eq!( @@ -495,7 +495,7 @@ fn with_unlimited_amount_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::External(amount) + PrincipalInput::External(amount) )); }); } @@ -511,7 +511,7 @@ fn twice() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE / 2) + PrincipalInput::Internal(COLLATERAL_VALUE / 2) )); assert_eq!(COLLATERAL_VALUE / 2, util::current_loan_debt(loan_id)); @@ -519,7 +519,7 @@ fn twice() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE / 2) + PrincipalInput::Internal(COLLATERAL_VALUE / 2) )); assert_eq!(COLLATERAL_VALUE, util::current_loan_debt(loan_id)); @@ -530,7 +530,7 @@ fn twice() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(extra) + PrincipalInput::Internal(extra) ), Error::::from(BorrowLoanError::MaxAmountExceeded) ); @@ -548,7 +548,7 @@ fn twice_with_elapsed_time() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE / 2) + PrincipalInput::Internal(COLLATERAL_VALUE / 2) )); assert_eq!(COLLATERAL_VALUE / 2, util::current_loan_debt(loan_id)); @@ -566,7 +566,7 @@ fn twice_with_elapsed_time() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(COLLATERAL_VALUE / 2) + PrincipalInput::Internal(COLLATERAL_VALUE / 2) )); // At this point the loan has been fully borrowed. @@ -576,7 +576,7 @@ fn twice_with_elapsed_time() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - PricingAmount::Internal(extra) + PrincipalInput::Internal(extra) ), Error::::from(BorrowLoanError::MaxAmountExceeded) ); diff --git a/pallets/loans/src/tests/close_loan.rs b/pallets/loans/src/tests/close_loan.rs index 5acd2a46ac..307603e4a0 100644 --- a/pallets/loans/src/tests/close_loan.rs +++ b/pallets/loans/src/tests/close_loan.rs @@ -25,8 +25,8 @@ fn with_wrong_borrower() { ); // Make the loan active and ready to be closed - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); - util::repay_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); + util::repay_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); assert_noop!( Loans::close(RuntimeOrigin::signed(OTHER_BORROWER), POOL_A, loan_id), @@ -39,8 +39,8 @@ fn with_wrong_borrower() { fn without_fully_repaid_internal() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); - util::repay_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE / 2)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); + util::repay_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE / 2)); assert_noop!( Loans::close(RuntimeOrigin::signed(BORROWER), POOL_A, loan_id), @@ -54,9 +54,9 @@ fn without_fully_repaid_external() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); let amount = ExternalAmount::new(QUANTITY / 2.into(), PRICE_VALUE); - util::repay_loan(loan_id, PricingAmount::External(amount)); + util::repay_loan(loan_id, PrincipalInput::External(amount)); assert_noop!( Loans::close(RuntimeOrigin::signed(BORROWER), POOL_A, loan_id), @@ -69,8 +69,8 @@ fn without_fully_repaid_external() { fn with_time_after_fully_repaid_internal() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); - util::repay_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); + util::repay_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR); @@ -88,8 +88,8 @@ fn with_time_after_fully_repaid_internal() { fn with_fully_repaid_internal() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); - util::repay_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); + util::repay_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); assert_ok!(Loans::close( RuntimeOrigin::signed(BORROWER), @@ -106,8 +106,8 @@ fn with_fully_repaid_external() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount.clone())); - util::repay_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount.clone())); + util::repay_loan(loan_id, PrincipalInput::External(amount)); config_mocks(); assert_ok!(Loans::close( diff --git a/pallets/loans/src/tests/mod.rs b/pallets/loans/src/tests/mod.rs index 558399f6ce..ac8dd0011c 100644 --- a/pallets/loans/src/tests/mod.rs +++ b/pallets/loans/src/tests/mod.rs @@ -10,11 +10,12 @@ use sp_runtime::{traits::BadOrigin, DispatchError, FixedPointNumber}; use super::{ entities::{ changes::{Change, InternalMutation, LoanMutation}, + input::{PrincipalInput, RepaidInput}, loans::{ActiveLoan, LoanInfo}, pricing::{ external::{ExternalAmount, ExternalPricing, MaxBorrowAmount as ExtMaxBorrowAmount}, internal::{InternalPricing, MaxBorrowAmount as IntMaxBorrowAmount}, - ActivePricing, Pricing, PricingAmount, RepaidPricingAmount, + ActivePricing, Pricing, }, }, pallet::{ActiveLoans, CreatedLoan, Error, LastLoanId, PortfolioValuation}, diff --git a/pallets/loans/src/tests/mutate_loan.rs b/pallets/loans/src/tests/mutate_loan.rs index 9fba088e5c..b85964c263 100644 --- a/pallets/loans/src/tests/mutate_loan.rs +++ b/pallets/loans/src/tests/mutate_loan.rs @@ -50,7 +50,7 @@ fn without_active_loan() { fn with_wrong_policy_change() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(0)); + util::borrow_loan(loan_id, PrincipalInput::Internal(0)); config_mocks(loan_id, &DEFAULT_MUTATION); MockChangeGuard::mock_released(|_, _| Ok(Change::Policy(vec![].try_into().unwrap()))); @@ -66,7 +66,7 @@ fn with_wrong_policy_change() { fn with_wrong_permissions() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(0)); + util::borrow_loan(loan_id, PrincipalInput::Internal(0)); config_mocks(loan_id, &DEFAULT_MUTATION); assert_noop!( @@ -98,7 +98,7 @@ mod wrong_mutation { fn with_dcf() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(0)); + util::borrow_loan(loan_id, PrincipalInput::Internal(0)); let mutation = LoanMutation::Internal(InternalMutation::ProbabilityOfDefault( Rate::from_float(0.5), @@ -121,7 +121,7 @@ mod wrong_mutation { fn with_internal() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); - util::borrow_loan(loan_id, PricingAmount::External(ExternalAmount::empty())); + util::borrow_loan(loan_id, PrincipalInput::External(ExternalAmount::empty())); let mutation = LoanMutation::Internal(InternalMutation::ProbabilityOfDefault( Rate::from_float(0.5), @@ -144,7 +144,7 @@ mod wrong_mutation { fn with_maturity_extension() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(0)); + util::borrow_loan(loan_id, PrincipalInput::Internal(0)); let mutation = LoanMutation::MaturityExtension(YEAR.as_secs()); @@ -165,7 +165,7 @@ mod wrong_mutation { fn with_interest_rate() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(0)); + util::borrow_loan(loan_id, PrincipalInput::Internal(0)); // Too high let mutation = LoanMutation::InterestRate(InterestRate::Fixed { @@ -191,7 +191,7 @@ mod wrong_mutation { fn with_successful_proposal() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(0)); + util::borrow_loan(loan_id, PrincipalInput::Internal(0)); config_mocks(loan_id, &DEFAULT_MUTATION); @@ -235,7 +235,7 @@ fn with_successful_mutation_application() { }; let loan_id = util::create_loan(loan); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE / 2)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE / 2)); let mutations = vec![ // LoanMutation::InterestPayments(..), No changes, only one variant diff --git a/pallets/loans/src/tests/policy.rs b/pallets/loans/src/tests/policy.rs index 3614f249c5..f6d1b8f2b4 100644 --- a/pallets/loans/src/tests/policy.rs +++ b/pallets/loans/src/tests/policy.rs @@ -128,7 +128,7 @@ fn with_price_outdated() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); let policy: BoundedVec<_, _> = vec![WriteOffRule::new( [WriteOffTrigger::PriceOutdated(10)], @@ -177,7 +177,7 @@ fn with_price_outdated() { fn with_success() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); let policy: BoundedVec<_, _> = vec![ WriteOffRule::new( diff --git a/pallets/loans/src/tests/portfolio_valuation.rs b/pallets/loans/src/tests/portfolio_valuation.rs index 2308b07f95..f394e58497 100644 --- a/pallets/loans/src/tests/portfolio_valuation.rs +++ b/pallets/loans/src/tests/portfolio_valuation.rs @@ -72,14 +72,14 @@ fn with_active_loans() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_1, PricingAmount::External(amount.clone())); + util::borrow_loan(loan_1, PrincipalInput::External(amount.clone())); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, ..util::base_internal_loan() }); - util::borrow_loan(loan_2, PricingAmount::Internal(COLLATERAL_VALUE)); - util::repay_loan(loan_2, PricingAmount::Internal(COLLATERAL_VALUE / 4)); + util::borrow_loan(loan_2, PrincipalInput::Internal(COLLATERAL_VALUE)); + util::repay_loan(loan_2, PrincipalInput::Internal(COLLATERAL_VALUE / 4)); let valuation = amount.balance().unwrap() + COLLATERAL_VALUE - COLLATERAL_VALUE / 4; @@ -100,14 +100,14 @@ fn with_active_written_off_loans() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_1, PricingAmount::External(amount)); + util::borrow_loan(loan_1, PrincipalInput::External(amount)); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, ..util::base_internal_loan() }); - util::borrow_loan(loan_2, PricingAmount::Internal(COLLATERAL_VALUE)); - util::repay_loan(loan_2, PricingAmount::Internal(COLLATERAL_VALUE / 4)); + util::borrow_loan(loan_2, PrincipalInput::Internal(COLLATERAL_VALUE)); + util::repay_loan(loan_2, PrincipalInput::Internal(COLLATERAL_VALUE / 4)); advance_time(YEAR + DAY); @@ -125,14 +125,14 @@ fn filled_and_cleaned() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_1, PricingAmount::External(amount.clone())); + util::borrow_loan(loan_1, PrincipalInput::External(amount.clone())); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, ..util::base_internal_loan() }); - util::borrow_loan(loan_2, PricingAmount::Internal(COLLATERAL_VALUE)); - util::repay_loan(loan_2, PricingAmount::Internal(COLLATERAL_VALUE / 2)); + util::borrow_loan(loan_2, PrincipalInput::Internal(COLLATERAL_VALUE)); + util::repay_loan(loan_2, PrincipalInput::Internal(COLLATERAL_VALUE / 2)); advance_time(YEAR + DAY); @@ -140,8 +140,8 @@ fn filled_and_cleaned() { advance_time(YEAR / 2); - util::repay_loan(loan_1, PricingAmount::External(amount)); - util::repay_loan(loan_2, PricingAmount::Internal(COLLATERAL_VALUE / 2)); + util::repay_loan(loan_1, PrincipalInput::External(amount)); + util::repay_loan(loan_2, PrincipalInput::Internal(COLLATERAL_VALUE / 2)); advance_time(YEAR / 2); @@ -160,7 +160,7 @@ fn filled_and_cleaned() { fn exact_and_inexact_matches() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_1, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_1, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR / 2); config_mocks(); @@ -168,7 +168,7 @@ fn exact_and_inexact_matches() { // repay_loan() should affect to the portfolio valuation with the same value as // the absolute valuation of the loan - util::repay_loan(loan_1, PricingAmount::Internal(COLLATERAL_VALUE / 2)); + util::repay_loan(loan_1, PrincipalInput::Internal(COLLATERAL_VALUE / 2)); expected_portfolio(util::current_loan_pv(loan_1)); }); } @@ -186,7 +186,7 @@ fn with_unregister_price_id_and_oracle_not_required() { let loan_1 = util::create_loan(loan); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_1, PricingAmount::External(amount.clone())); + util::borrow_loan(loan_1, PrincipalInput::External(amount.clone())); advance_time(YEAR / 2); config_mocks(); diff --git a/pallets/loans/src/tests/repay_loan.rs b/pallets/loans/src/tests/repay_loan.rs index 05a5d095ec..ae3ef974e9 100644 --- a/pallets/loans/src/tests/repay_loan.rs +++ b/pallets/loans/src/tests/repay_loan.rs @@ -31,8 +31,8 @@ fn without_borrow_first() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: u128::MAX, unscheduled: 0, }, @@ -52,8 +52,8 @@ fn with_wrong_loan_id() { RuntimeOrigin::signed(BORROWER), POOL_A, 0, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: u128::MAX, unscheduled: 0, }, @@ -67,7 +67,7 @@ fn with_wrong_loan_id() { fn from_other_borrower() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); config_mocks(COLLATERAL_VALUE); assert_noop!( @@ -75,8 +75,8 @@ fn from_other_borrower() { RuntimeOrigin::signed(OTHER_BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: u128::MAX, unscheduled: 0, }, @@ -90,7 +90,7 @@ fn from_other_borrower() { fn has_been_written_off() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + DAY); util::write_off_loan(loan_id); @@ -100,8 +100,8 @@ fn has_been_written_off() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: u128::MAX, unscheduled: 0, }, @@ -113,7 +113,7 @@ fn has_been_written_off() { fn with_wrong_external_pricing() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); config_mocks(0); assert_noop!( @@ -121,8 +121,8 @@ fn with_wrong_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(ExternalAmount::empty()), + RepaidInput { + principal: PrincipalInput::External(ExternalAmount::empty()), interest: 0, unscheduled: 0, }, @@ -137,7 +137,7 @@ fn with_wrong_internal_pricing() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); config_mocks(0); assert_noop!( @@ -145,8 +145,8 @@ fn with_wrong_internal_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(0), + RepaidInput { + principal: PrincipalInput::Internal(0), interest: 0, unscheduled: 0, }, @@ -160,15 +160,15 @@ fn with_wrong_internal_pricing() { fn with_success_half_amount() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE / 2)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE / 2)); config_mocks(COLLATERAL_VALUE / 2); assert_ok!(Loans::repay( RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE / 2), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2), interest: 0, unscheduled: 0, }, @@ -181,15 +181,15 @@ fn with_success_half_amount() { fn with_success_total_amount() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); config_mocks(COLLATERAL_VALUE); assert_ok!(Loans::repay( RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }, @@ -202,7 +202,7 @@ fn with_success_total_amount() { fn with_more_than_required() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); config_mocks(COLLATERAL_VALUE); @@ -211,8 +211,8 @@ fn with_more_than_required() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE * 2), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE * 2), interest: 0, unscheduled: 0, }, @@ -224,8 +224,8 @@ fn with_more_than_required() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: u128::MAX, // Here there is no limit unscheduled: 0, }, @@ -238,8 +238,8 @@ fn with_more_than_required() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(1), // All was already repaid + RepaidInput { + principal: PrincipalInput::Internal(1), // All was already repaid interest: 0, unscheduled: 0, } @@ -250,8 +250,8 @@ fn with_more_than_required() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(0), + RepaidInput { + principal: PrincipalInput::Internal(0), interest: u128::MAX, //Discarded unscheduled: 0, }, @@ -269,7 +269,7 @@ fn with_restriction_full_once() { }, ..util::base_internal_loan() }); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); config_mocks(COLLATERAL_VALUE / 2); assert_noop!( @@ -277,8 +277,8 @@ fn with_restriction_full_once() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE / 2), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2), interest: 0, unscheduled: 0, }, @@ -291,8 +291,8 @@ fn with_restriction_full_once() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }, @@ -303,8 +303,8 @@ fn with_restriction_full_once() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(0), + RepaidInput { + principal: PrincipalInput::Internal(0), interest: 0, unscheduled: 0, } @@ -316,15 +316,15 @@ fn with_restriction_full_once() { fn twice_internal() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); config_mocks(COLLATERAL_VALUE / 2); assert_ok!(Loans::repay( RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE / 2), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2), interest: 0, unscheduled: 0, }, @@ -335,8 +335,8 @@ fn twice_internal() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE / 2), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2), interest: 0, unscheduled: 0, }, @@ -350,7 +350,7 @@ fn twice_external() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); let amount = ExternalAmount::new(QUANTITY / 2.into(), PRICE_VALUE); config_mocks(amount.balance().unwrap()); @@ -358,8 +358,8 @@ fn twice_external() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -377,8 +377,8 @@ fn twice_external() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(remaining), + RepaidInput { + principal: PrincipalInput::External(remaining), interest: 0, unscheduled: 0, }, @@ -391,15 +391,15 @@ fn twice_external() { fn twice_internal_with_elapsed_time() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); config_mocks(COLLATERAL_VALUE / 2); assert_ok!(Loans::repay( RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE / 2), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2), interest: 0, unscheduled: 0, }, @@ -419,8 +419,8 @@ fn twice_internal_with_elapsed_time() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE / 2), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2), interest: 0, unscheduled: 0, }, @@ -435,8 +435,8 @@ fn twice_internal_with_elapsed_time() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(0), + RepaidInput { + principal: PrincipalInput::Internal(0), interest: still_to_pay, unscheduled: 0, }, @@ -451,7 +451,7 @@ fn twice_external_with_elapsed_time() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); let amount = ExternalAmount::new(QUANTITY / 2.into(), PRICE_VALUE); config_mocks(amount.balance().unwrap()); @@ -459,8 +459,8 @@ fn twice_external_with_elapsed_time() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -483,8 +483,8 @@ fn twice_external_with_elapsed_time() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(remaining), + RepaidInput { + principal: PrincipalInput::External(remaining), interest: 0, unscheduled: 0, }, @@ -499,8 +499,8 @@ fn twice_external_with_elapsed_time() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(ExternalAmount::empty()), + RepaidInput { + principal: PrincipalInput::External(ExternalAmount::empty()), interest: still_to_pay, unscheduled: 0, }, @@ -520,7 +520,7 @@ fn current_debt_rate_no_increase_if_fully_repaid() { }), ..util::base_internal_loan() }); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR / 2); @@ -529,8 +529,8 @@ fn current_debt_rate_no_increase_if_fully_repaid() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: u128::MAX, unscheduled: 0, }, @@ -547,7 +547,7 @@ fn external_pricing_goes_up() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE * 2); config_mocks_with_price(amount.balance().unwrap(), PRICE_VALUE * 2); @@ -555,8 +555,8 @@ fn external_pricing_goes_up() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -571,7 +571,7 @@ fn external_pricing_goes_down() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE / 2); config_mocks_with_price(amount.balance().unwrap(), PRICE_VALUE / 2); @@ -579,8 +579,8 @@ fn external_pricing_goes_down() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -594,15 +594,15 @@ fn external_pricing_goes_down() { fn with_unscheduled_repayment_internal() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); config_mocks(1234); assert_ok!(Loans::repay( RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(0), + RepaidInput { + principal: PrincipalInput::Internal(0), interest: 0, unscheduled: 1234, }, @@ -619,15 +619,15 @@ fn with_unscheduled_repayment_external() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); config_mocks(1234); assert_ok!(Loans::repay( RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(ExternalAmount::empty()), + RepaidInput { + principal: PrincipalInput::External(ExternalAmount::empty()), interest: 0, unscheduled: 1234, }, @@ -647,7 +647,7 @@ fn with_incorrect_settlement_price_external_pricing() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); // Much higher let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE + PRICE_VALUE + 1); @@ -657,8 +657,8 @@ fn with_incorrect_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -677,8 +677,8 @@ fn with_incorrect_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -697,8 +697,8 @@ fn with_incorrect_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -713,7 +713,7 @@ fn with_correct_settlement_price_external_pricing() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); // Higher let amount = ExternalAmount::new( @@ -725,8 +725,8 @@ fn with_correct_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -744,8 +744,8 @@ fn with_correct_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -766,8 +766,8 @@ fn with_correct_settlement_price_external_pricing() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, @@ -791,7 +791,7 @@ fn with_unregister_price_id_and_oracle_not_required() { let loan_id = util::create_loan(loan); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); let amount = ExternalAmount::new(QUANTITY / 2.into(), PRICE_VALUE * 2); config_mocks_with_price(amount.balance().unwrap(), 0 /* unused */); @@ -799,8 +799,8 @@ fn with_unregister_price_id_and_oracle_not_required() { RuntimeOrigin::signed(BORROWER), POOL_A, loan_id, - RepaidPricingAmount { - principal: PricingAmount::External(amount), + RepaidInput { + principal: PrincipalInput::External(amount), interest: 0, unscheduled: 0, }, diff --git a/pallets/loans/src/tests/transfer_debt.rs b/pallets/loans/src/tests/transfer_debt.rs index 5f0a4e1ffe..ee7668bd12 100644 --- a/pallets/loans/src/tests/transfer_debt.rs +++ b/pallets/loans/src/tests/transfer_debt.rs @@ -4,8 +4,8 @@ fn config_mocks( transfer: ( LoanId, LoanId, - RepaidPricingAmount, - PricingAmount, + RepaidInput, + PrincipalInput, ), ) { MockPrices::mock_get(|id, pool_id| { @@ -48,7 +48,7 @@ fn config_mocks( fn with_wrong_borrower() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_1, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_1, PrincipalInput::Internal(COLLATERAL_VALUE)); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, @@ -61,12 +61,12 @@ fn with_wrong_borrower() { POOL_A, loan_1, loan_2, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }, - PricingAmount::Internal(COLLATERAL_VALUE), + PrincipalInput::Internal(COLLATERAL_VALUE), ), Error::::NotLoanBorrower ); @@ -83,7 +83,7 @@ fn with_wrong_borrower_for_repaid_loan() { }, OTHER_BORROWER, ); - util::borrow_loan(loan_1, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_1, PrincipalInput::Internal(COLLATERAL_VALUE)); let loan_2 = util::create_loan(util::base_internal_loan()); @@ -93,12 +93,12 @@ fn with_wrong_borrower_for_repaid_loan() { POOL_A, loan_1, loan_2, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }, - PricingAmount::Internal(COLLATERAL_VALUE), + PrincipalInput::Internal(COLLATERAL_VALUE), ), Error::::NotLoanBorrower ); @@ -109,7 +109,7 @@ fn with_wrong_borrower_for_repaid_loan() { fn with_wrong_borrower_for_borrowed_loan() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_1, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_1, PrincipalInput::Internal(COLLATERAL_VALUE)); let loan_2 = util::create_loan_by( LoanInfo { @@ -125,12 +125,12 @@ fn with_wrong_borrower_for_borrowed_loan() { POOL_A, loan_1, loan_2, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }, - PricingAmount::Internal(COLLATERAL_VALUE), + PrincipalInput::Internal(COLLATERAL_VALUE), ), Error::::NotLoanBorrower ); @@ -141,7 +141,7 @@ fn with_wrong_borrower_for_borrowed_loan() { fn with_wrong_loans() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); assert_noop!( Loans::propose_transfer_debt( @@ -149,12 +149,12 @@ fn with_wrong_loans() { POOL_A, 0, // Does not exists loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }, - PricingAmount::Internal(COLLATERAL_VALUE), + PrincipalInput::Internal(COLLATERAL_VALUE), ), Error::::LoanNotActiveOrNotFound ); @@ -165,12 +165,12 @@ fn with_wrong_loans() { POOL_A, loan_id, 0, // Does not exists - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }, - PricingAmount::Internal(COLLATERAL_VALUE), + PrincipalInput::Internal(COLLATERAL_VALUE), ), Error::::LoanNotActiveOrNotFound ); @@ -181,7 +181,7 @@ fn with_wrong_loans() { fn with_same_loan() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); assert_noop!( Loans::propose_transfer_debt( @@ -189,12 +189,12 @@ fn with_same_loan() { POOL_A, loan_id, loan_id, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }, - PricingAmount::Internal(COLLATERAL_VALUE), + PrincipalInput::Internal(COLLATERAL_VALUE), ), Error::::TransferDebtToSameLoan ); @@ -205,7 +205,7 @@ fn with_same_loan() { fn with_mismatch_internal_internal_amounts() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_1, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_1, PrincipalInput::Internal(COLLATERAL_VALUE)); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, @@ -218,12 +218,12 @@ fn with_mismatch_internal_internal_amounts() { POOL_A, loan_1, loan_2, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE / 2), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2), interest: 0, unscheduled: 0, }, - PricingAmount::Internal(COLLATERAL_VALUE / 3), + PrincipalInput::Internal(COLLATERAL_VALUE / 3), ), Error::::TransferDebtAmountMismatched ); @@ -235,7 +235,7 @@ fn with_mismatch_external_internal_amounts() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_1, PricingAmount::External(amount)); + util::borrow_loan(loan_1, PrincipalInput::External(amount)); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, @@ -255,12 +255,12 @@ fn with_mismatch_external_internal_amounts() { POOL_A, loan_1, loan_2, - RepaidPricingAmount { - principal: PricingAmount::External(repay_amount), + RepaidInput { + principal: PrincipalInput::External(repay_amount), interest: 0, unscheduled: 0, }, - PricingAmount::Internal(COLLATERAL_VALUE), + PrincipalInput::Internal(COLLATERAL_VALUE), ), Error::::TransferDebtAmountMismatched ); @@ -271,7 +271,7 @@ fn with_mismatch_external_internal_amounts() { fn with_mismatch_internal_external_amounts() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_1, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_1, PrincipalInput::Internal(COLLATERAL_VALUE)); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, @@ -286,12 +286,12 @@ fn with_mismatch_internal_external_amounts() { POOL_A, loan_1, loan_2, - RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }, - PricingAmount::External(borrow_amount), + PrincipalInput::External(borrow_amount), ), Error::::TransferDebtAmountMismatched ); @@ -303,7 +303,7 @@ fn with_mismatch_external_external_amounts() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_1, PricingAmount::External(amount)); + util::borrow_loan(loan_1, PrincipalInput::External(amount)); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, @@ -324,12 +324,12 @@ fn with_mismatch_external_external_amounts() { POOL_A, loan_1, loan_2, - RepaidPricingAmount { - principal: PricingAmount::External(repay_amount), + RepaidInput { + principal: PrincipalInput::External(repay_amount), interest: 0, unscheduled: 0, }, - PricingAmount::External(borrow_amount), + PrincipalInput::External(borrow_amount), ), Error::::TransferDebtAmountMismatched ); @@ -352,19 +352,19 @@ fn apply_without_released() { fn with_success_internals() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_1, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_1, PrincipalInput::Internal(COLLATERAL_VALUE)); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, ..util::base_internal_loan() }); - let repay_amount = RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE), + let repay_amount = RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE), interest: 0, unscheduled: 0, }; - let borrow_amount = PricingAmount::Internal(COLLATERAL_VALUE); + let borrow_amount = PrincipalInput::Internal(COLLATERAL_VALUE); config_mocks((loan_1, loan_2, repay_amount.clone(), borrow_amount.clone())); assert_ok!(Loans::propose_transfer_debt( @@ -392,19 +392,19 @@ fn with_success_externals() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_1, PricingAmount::External(amount)); + util::borrow_loan(loan_1, PrincipalInput::External(amount)); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, ..util::base_external_loan() }); - let repay_amount = RepaidPricingAmount { - principal: PricingAmount::External(ExternalAmount::new(QUANTITY, PRICE_VALUE)), + let repay_amount = RepaidInput { + principal: PrincipalInput::External(ExternalAmount::new(QUANTITY, PRICE_VALUE)), interest: 0, unscheduled: 0, }; - let borrow_amount = PricingAmount::External(ExternalAmount::new(QUANTITY, PRICE_VALUE)); + let borrow_amount = PrincipalInput::External(ExternalAmount::new(QUANTITY, PRICE_VALUE)); config_mocks((loan_1, loan_2, repay_amount.clone(), borrow_amount.clone())); assert_ok!(Loans::propose_transfer_debt( @@ -434,19 +434,19 @@ fn with_success_externals() { fn with_transfer_roundtrip() { new_test_ext().execute_with(|| { let loan_1 = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_1, PricingAmount::Internal(COLLATERAL_VALUE / 2)); + util::borrow_loan(loan_1, PrincipalInput::Internal(COLLATERAL_VALUE / 2)); let loan_2 = util::create_loan(LoanInfo { collateral: ASSET_BA, ..util::base_internal_loan() }); - let repay_amount = RepaidPricingAmount { - principal: PricingAmount::Internal(COLLATERAL_VALUE / 2), + let repay_amount = RepaidInput { + principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2), interest: 0, unscheduled: 0, }; - let borrow_amount = PricingAmount::Internal(COLLATERAL_VALUE / 2); + let borrow_amount = PrincipalInput::Internal(COLLATERAL_VALUE / 2); config_mocks((loan_1, loan_2, repay_amount.clone(), borrow_amount.clone())); assert_ok!(Loans::propose_transfer_debt( diff --git a/pallets/loans/src/tests/util.rs b/pallets/loans/src/tests/util.rs index 4ec2750897..1880799772 100644 --- a/pallets/loans/src/tests/util.rs +++ b/pallets/loans/src/tests/util.rs @@ -153,7 +153,7 @@ pub fn create_loan_by(loan: LoanInfo, borrower: AccountId) -> LoanId { LastLoanId::::get(POOL_A) } -pub fn borrow_loan(loan_id: LoanId, borrow_amount: PricingAmount) { +pub fn borrow_loan(loan_id: LoanId, borrow_amount: PrincipalInput) { MockPools::mock_withdraw(|_, _, _| Ok(())); MockPrices::mock_get(|_, _| Ok((PRICE_VALUE, BLOCK_TIME.as_secs()))); MockPrices::mock_register_id(|_, _| Ok(())); @@ -171,7 +171,7 @@ pub fn borrow_loan(loan_id: LoanId, borrow_amount: PricingAmount) { MockPrices::mock_register_id(|_, _| panic!("no register_id() mock")); } -pub fn repay_loan(loan_id: LoanId, repay_amount: PricingAmount) { +pub fn repay_loan(loan_id: LoanId, repay_amount: PrincingType) { MockPools::mock_deposit(|_, _, _| Ok(())); MockPrices::mock_get(|_, _| Ok((PRICE_VALUE, BLOCK_TIME.as_secs()))); @@ -179,7 +179,7 @@ pub fn repay_loan(loan_id: LoanId, repay_amount: PricingAmount) { RuntimeOrigin::signed(borrower(loan_id)), POOL_A, loan_id, - RepaidPricingAmount { + RepaidInput { principal: repay_amount, interest: u128::MAX, unscheduled: 0, diff --git a/pallets/loans/src/tests/write_off_loan.rs b/pallets/loans/src/tests/write_off_loan.rs index 5ce25de621..e9028ad112 100644 --- a/pallets/loans/src/tests/write_off_loan.rs +++ b/pallets/loans/src/tests/write_off_loan.rs @@ -12,7 +12,7 @@ fn config_mocks() { fn without_policy() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); assert_noop!( Loans::write_off(RuntimeOrigin::signed(ANY), POOL_A, loan_id), @@ -36,7 +36,7 @@ fn with_policy_but_not_overdue() { util::set_up_policy(POLICY_PERCENTAGE, POLICY_PENALTY); let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + BLOCK_TIME); @@ -54,7 +54,7 @@ fn with_valid_maturity() { util::set_up_policy(POLICY_PERCENTAGE, POLICY_PENALTY); let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR / 2); @@ -121,7 +121,7 @@ fn with_wrong_permission() { util::set_up_policy(POLICY_PERCENTAGE, POLICY_PENALTY); let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + DAY); @@ -145,7 +145,7 @@ fn with_success() { util::set_up_policy(POLICY_PERCENTAGE, POLICY_PENALTY); let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + DAY); @@ -163,7 +163,7 @@ fn with_admin_success() { util::set_up_policy(POLICY_PERCENTAGE, POLICY_PENALTY); let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + DAY); @@ -213,7 +213,7 @@ fn with_admin_less_than_policy() { util::set_up_policy(POLICY_PERCENTAGE, POLICY_PENALTY); let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + DAY); @@ -251,7 +251,7 @@ fn with_policy_change_after() { util::set_up_policy(POLICY_PERCENTAGE, POLICY_PENALTY); let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + DAY); @@ -283,7 +283,7 @@ fn with_policy_change_after() { fn with_policy_change_after_admin() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); config_mocks(); assert_ok!(Loans::admin_write_off( @@ -320,7 +320,7 @@ fn with_percentage_applied_internal() { util::set_up_policy(POLICY_PERCENTAGE, 0.0); let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + DAY); @@ -347,7 +347,7 @@ fn with_percentage_applied_external() { let loan_id = util::create_loan(util::base_external_loan()); let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE); - util::borrow_loan(loan_id, PricingAmount::External(amount)); + util::borrow_loan(loan_id, PrincipalInput::External(amount)); advance_time(YEAR + DAY); @@ -378,7 +378,7 @@ fn with_penalty_applied() { util::set_up_policy(0.0, POLICY_PENALTY); let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + DAY); @@ -418,7 +418,7 @@ fn with_penalty_applied() { fn fully() { new_test_ext().execute_with(|| { let loan_id = util::create_loan(util::base_internal_loan()); - util::borrow_loan(loan_id, PricingAmount::Internal(COLLATERAL_VALUE)); + util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE)); advance_time(YEAR + DAY); diff --git a/runtime/integration-tests/src/utils/loans.rs b/runtime/integration-tests/src/utils/loans.rs index 8d10d65f07..9a4628b40c 100644 --- a/runtime/integration-tests/src/utils/loans.rs +++ b/runtime/integration-tests/src/utils/loans.rs @@ -20,10 +20,11 @@ use cfg_traits::interest::{CompoundingSchedule, InterestRate}; use cfg_types::fixed_point::Rate; use pallet_loans::{ entities::{ + input::{PrincipalInput, RepaidInput}, loans::LoanInfo, pricing::{ internal::{InternalPricing, MaxBorrowAmount}, - Pricing, PricingAmount, RepaidPricingAmount, + Pricing, }, }, types::{ @@ -185,7 +186,7 @@ pub fn create_loan_call(pool_id: PoolId, info: LoanInfo) -> RuntimeCall pub fn borrow_call( pool_id: PoolId, loan_id: LoanId, - amount: PricingAmount, + amount: PrincipalInput, ) -> RuntimeCall { RuntimeCall::Loans(LoansCall::borrow { pool_id, @@ -194,11 +195,7 @@ pub fn borrow_call( }) } -pub fn repay_call( - pool_id: PoolId, - loan_id: LoanId, - amount: RepaidPricingAmount, -) -> RuntimeCall { +pub fn repay_call(pool_id: PoolId, loan_id: LoanId, amount: RepaidInput) -> RuntimeCall { RuntimeCall::Loans(LoansCall::repay { pool_id, loan_id,