Skip to content

Commit

Permalink
rename input types
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Sep 25, 2023
1 parent aa4560a commit b117c25
Show file tree
Hide file tree
Showing 17 changed files with 332 additions and 330 deletions.
27 changes: 14 additions & 13 deletions pallets/loans/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
Expand Down Expand Up @@ -185,7 +186,7 @@ where
RawOrigin::Signed(borrower).into(),
pool_id,
loan_id,
PricingAmount::Internal(10.into()),
PrincipalInput::Internal(10.into()),
)
.unwrap();
}
Expand All @@ -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(),
},
Expand Down Expand Up @@ -252,12 +253,12 @@ where
Helper::<T>::borrow_loan(pool_id, loan_1);
let loan_2 = Helper::<T>::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::<T>::propose_transfer_debt(
RawOrigin::Signed(borrower).into(),
Expand Down Expand Up @@ -356,7 +357,7 @@ benchmarks! {
let pool_id = Helper::<T>::initialize_active_state(n);
let loan_id = Helper::<T>::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::<T>::max_active_loans() - 1;
Expand All @@ -366,8 +367,8 @@ benchmarks! {
let loan_id = Helper::<T>::create_loan(pool_id, u16::MAX.into());
Helper::<T>::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()
};
Expand Down Expand Up @@ -466,12 +467,12 @@ benchmarks! {
Helper::<T>::borrow_loan(pool_id, loan_1);
let loan_2 = Helper::<T>::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)

Expand Down
9 changes: 2 additions & 7 deletions pallets/loans/src/entities/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -39,10 +39,5 @@ pub enum LoanMutation<Rate> {
pub enum Change<T: Config> {
Loan(T::LoanId, LoanMutation<T::Rate>),
Policy(BoundedVec<WriteOffRule<T::Rate>, T::MaxWriteOffPolicySize>),
TransferDebt(
T::LoanId,
T::LoanId,
RepaidPricingAmount<T>,
PricingAmount<T>,
),
TransferDebt(T::LoanId, T::LoanId, RepaidInput<T>, PrincipalInput<T>),
}
58 changes: 58 additions & 0 deletions pallets/loans/src/entities/input.rs
Original file line number Diff line number Diff line change
@@ -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<T: Config> {
Internal(T::Balance),
External(ExternalAmount<T>),
}

impl<T: Config> PrincipalInput<T> {
pub fn balance(&self) -> Result<T::Balance, ArithmeticError> {
match self {
Self::Internal(amount) => Ok(*amount),
Self::External(external) => external.balance(),
}
}

pub fn internal(&self) -> Result<T::Balance, DispatchError> {
match self {
Self::Internal(amount) => Ok(*amount),
Self::External(_) => Err(Error::<T>::MismatchedPricingMethod.into()),
}
}

pub fn external(&self) -> Result<ExternalAmount<T>, DispatchError> {
match self {
Self::Internal(_) => Err(Error::<T>::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<T: Config> {
pub principal: PrincipalInput<T>,
pub interest: T::Balance,
pub unscheduled: T::Balance,
}

impl<T: Config> RepaidInput<T> {
pub fn repaid_amount(&self) -> Result<RepaidAmount<T::Balance>, ArithmeticError> {
Ok(RepaidAmount {
principal: self.principal.balance()?,
interest: self.interest,
unscheduled: self.unscheduled,
})
}
}
19 changes: 10 additions & 9 deletions pallets/loans/src/entities/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -101,7 +102,7 @@ impl<T: Config> CreatedLoan<T> {
pub fn activate(
self,
pool_id: T::PoolId,
initial_amount: PricingAmount<T>,
initial_amount: PrincipalInput<T>,
) -> Result<ActiveLoan<T>, DispatchError> {
ActiveLoan::new(
pool_id,
Expand Down Expand Up @@ -188,7 +189,7 @@ impl<T: Config> ActiveLoan<T> {
pool_id: T::PoolId,
info: LoanInfo<T>,
borrower: T::AccountId,
initial_amount: PricingAmount<T>,
initial_amount: PrincipalInput<T>,
now: Moment,
) -> Result<Self, DispatchError> {
Ok(ActiveLoan {
Expand Down Expand Up @@ -300,7 +301,7 @@ impl<T: Config> ActiveLoan<T> {
self.write_down(value)
}

fn ensure_can_borrow(&self, amount: &PricingAmount<T>, pool_id: T::PoolId) -> DispatchResult {
fn ensure_can_borrow(&self, amount: &PrincipalInput<T>, pool_id: T::PoolId) -> DispatchResult {
let max_borrow_amount = match &self.pricing {
ActivePricing::Internal(inner) => {
amount.internal()?;
Expand Down Expand Up @@ -342,7 +343,7 @@ impl<T: Config> ActiveLoan<T> {
Ok(())
}

pub fn borrow(&mut self, amount: &PricingAmount<T>, pool_id: T::PoolId) -> DispatchResult {
pub fn borrow(&mut self, amount: &PrincipalInput<T>, pool_id: T::PoolId) -> DispatchResult {
self.ensure_can_borrow(amount, pool_id)?;

self.total_borrowed.ensure_add_assign(amount.balance()?)?;
Expand All @@ -367,9 +368,9 @@ impl<T: Config> ActiveLoan<T> {
/// - Checking repay restrictions
fn prepare_repayment(
&self,
mut amount: RepaidPricingAmount<T>,
mut amount: RepaidInput<T>,
pool_id: T::PoolId,
) -> Result<RepaidPricingAmount<T>, DispatchError> {
) -> Result<RepaidInput<T>, DispatchError> {
let (max_repay_principal, outstanding_interest) = match &self.pricing {
ActivePricing::Internal(inner) => {
amount.principal.internal()?;
Expand Down Expand Up @@ -411,9 +412,9 @@ impl<T: Config> ActiveLoan<T> {

pub fn repay(
&mut self,
amount: RepaidPricingAmount<T>,
amount: RepaidInput<T>,
pool_id: T::PoolId,
) -> Result<RepaidPricingAmount<T>, DispatchError> {
) -> Result<RepaidInput<T>, DispatchError> {
let amount = self.prepare_repayment(amount, pool_id)?;

self.total_repaid
Expand Down
54 changes: 1 addition & 53 deletions pallets/loans/src/entities/pricing.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -32,51 +28,3 @@ pub enum ActivePricing<T: Config> {
/// Internal attributes
External(external::ExternalActivePricing<T>),
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub enum PricingAmount<T: Config> {
Internal(T::Balance),
External(external::ExternalAmount<T>),
}

impl<T: Config> PricingAmount<T> {
pub fn balance(&self) -> Result<T::Balance, ArithmeticError> {
match self {
Self::Internal(amount) => Ok(*amount),
Self::External(external) => external.balance(),
}
}

pub fn internal(&self) -> Result<T::Balance, DispatchError> {
match self {
Self::Internal(amount) => Ok(*amount),
Self::External(_) => Err(Error::<T>::MismatchedPricingMethod.into()),
}
}

pub fn external(&self) -> Result<external::ExternalAmount<T>, DispatchError> {
match self {
Self::Internal(_) => Err(Error::<T>::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<T: Config> {
pub principal: PricingAmount<T>,
pub interest: T::Balance,
pub unscheduled: T::Balance,
}

impl<T: Config> RepaidPricingAmount<T> {
pub fn repaid_amount(&self) -> Result<RepaidAmount<T::Balance>, ArithmeticError> {
Ok(RepaidAmount {
principal: self.principal.balance()?,
interest: self.interest,
unscheduled: self.unscheduled,
})
}
}
25 changes: 13 additions & 12 deletions pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::*,
Expand Down Expand Up @@ -303,13 +304,13 @@ pub mod pallet {
Borrowed {
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: PricingAmount<T>,
amount: PrincipalInput<T>,
},
/// An amount was repaid for a loan
Repaid {
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: RepaidPricingAmount<T>,
amount: RepaidInput<T>,
},
/// A loan was written off
WrittenOff {
Expand Down Expand Up @@ -477,7 +478,7 @@ pub mod pallet {
origin: OriginFor<T>,
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: PricingAmount<T>,
amount: PrincipalInput<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand Down Expand Up @@ -510,7 +511,7 @@ pub mod pallet {
origin: OriginFor<T>,
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: RepaidPricingAmount<T>,
amount: RepaidInput<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand Down Expand Up @@ -778,8 +779,8 @@ pub mod pallet {
pool_id: T::PoolId,
from_loan_id: T::LoanId,
to_loan_id: T::LoanId,
repaid_amount: RepaidPricingAmount<T>,
borrow_amount: PricingAmount<T>,
repaid_amount: RepaidInput<T>,
borrow_amount: PrincipalInput<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand Down Expand Up @@ -853,7 +854,7 @@ pub mod pallet {
who: &T::AccountId,
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: &PricingAmount<T>,
amount: &PrincipalInput<T>,
permissionless: bool,
) -> Result<u32, DispatchError> {
Ok(match CreatedLoan::<T>::take(pool_id, loan_id) {
Expand Down Expand Up @@ -884,9 +885,9 @@ pub mod pallet {
who: &T::AccountId,
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: &RepaidPricingAmount<T>,
amount: &RepaidInput<T>,
permissionless: bool,
) -> Result<(RepaidPricingAmount<T>, u32), DispatchError> {
) -> Result<(RepaidInput<T>, u32), DispatchError> {
Self::update_active_loan(pool_id, loan_id, |loan| {
if !permissionless {
Self::ensure_loan_borrower(who, loan.borrower())?;
Expand All @@ -901,8 +902,8 @@ pub mod pallet {
pool_id: T::PoolId,
from_loan_id: T::LoanId,
to_loan_id: T::LoanId,
repaid_amount: RepaidPricingAmount<T>,
borrow_amount: PricingAmount<T>,
repaid_amount: RepaidInput<T>,
borrow_amount: PrincipalInput<T>,
permissionless: bool,
) -> Result<(T::Balance, u32), DispatchError> {
ensure!(
Expand Down
Loading

0 comments on commit b117c25

Please sign in to comment.