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: unlimit external pricing borrows #1428

Merged
merged 4 commits into from
Jun 29, 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
2 changes: 1 addition & 1 deletion pallets/loans/src/entities/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl<T: Config> ActiveLoan<T> {
fn ensure_can_borrow(&self, amount: T::Balance) -> DispatchResult {
let max_borrow_amount = match &self.pricing {
ActivePricing::Internal(inner) => inner.max_borrow_amount(self.total_borrowed)?,
ActivePricing::External(inner) => inner.max_borrow_amount()?,
ActivePricing::External(inner) => inner.max_borrow_amount(amount)?,
};

ensure!(
Expand Down
35 changes: 25 additions & 10 deletions pallets/loans/src/entities/pricing/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cfg_traits::{
};
use cfg_types::adjustments::Adjustment;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{self, ensure, RuntimeDebugNoBound};
use frame_support::{self, ensure, RuntimeDebug, RuntimeDebugNoBound};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{
Expand All @@ -16,15 +16,25 @@ use sp_runtime::{

use crate::pallet::{Config, Error, PoolIdOf, PriceOf};

/// Define the max borrow amount of a loan
#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebug, MaxEncodedLen)]
pub enum MaxBorrowAmount<Balance> {
hieronx marked this conversation as resolved.
Show resolved Hide resolved
/// You can borrow until the pool reserve
NoLimit,

/// Maximum number of items associated with the loan of the pricing.
Quantity(Balance),
}

/// External pricing method
#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct ExternalPricing<T: Config> {
/// Id of an external price
pub price_id: T::PriceId,

/// Maximum number of items associated with the loan of the pricing.
pub max_borrow_quantity: T::Balance,
/// Maximum amount that can be borrowed.
pub max_borrow_amount: MaxBorrowAmount<T::Balance>,
}

impl<T: Config> ExternalPricing<T> {
Expand Down Expand Up @@ -78,13 +88,18 @@ impl<T: Config> ExternalActivePricing<T> {
Ok(prices.get(&self.info.price_id)?.0)
}

pub fn max_borrow_amount(&self) -> Result<T::Balance, DispatchError> {
let price = self.calculate_price()?;
let available = self
.info
.max_borrow_quantity
.ensure_sub(self.outstanding_quantity)?;
Ok(price.ensure_mul_int(available)?)
pub fn max_borrow_amount(
&self,
desired_amount: T::Balance,
) -> Result<T::Balance, DispatchError> {
match self.info.max_borrow_amount {
MaxBorrowAmount::Quantity(quantity) => {
let price = self.calculate_price()?;
let available = quantity.ensure_sub(self.outstanding_quantity)?;
Ok(price.ensure_mul_int(available)?)
}
MaxBorrowAmount::NoLimit => Ok(desired_amount),
}
}

pub fn last_updated(&self) -> Result<Moment, DispatchError> {
Expand Down
27 changes: 26 additions & 1 deletion pallets/loans/src/tests/borrow_loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn with_unregister_price_id() {
let loan = LoanInfo {
pricing: Pricing::External(ExternalPricing {
price_id: UNREGISTER_PRICE_ID,
max_borrow_quantity: QUANTITY,
max_borrow_amount: ExtMaxBorrowAmount::Quantity(QUANTITY),
}),
..util::base_external_loan()
};
Expand Down Expand Up @@ -274,6 +274,31 @@ fn with_correct_amount_external_pricing() {
});
}

#[test]
fn with_unlimited_amount_external_pricing() {
new_test_ext().execute_with(|| {
let loan = LoanInfo {
pricing: Pricing::External(ExternalPricing {
price_id: REGISTER_PRICE_ID,
max_borrow_amount: ExtMaxBorrowAmount::NoLimit,
}),
..util::base_external_loan()
};

let loan_id = util::create_loan(loan);

let amount = PRICE_VALUE.saturating_mul_int(2 /* Could be any value */);
config_mocks(amount);

assert_ok!(Loans::borrow(
RuntimeOrigin::signed(BORROWER),
POOL_A,
loan_id,
amount
));
});
}

#[test]
fn twice() {
new_test_ext().execute_with(|| {
Expand Down
2 changes: 1 addition & 1 deletion pallets/loans/src/tests/create_loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn with_unregister_price_id() {
let loan = LoanInfo {
pricing: Pricing::External(ExternalPricing {
price_id: UNREGISTER_PRICE_ID,
max_borrow_quantity: QUANTITY,
max_borrow_amount: ExtMaxBorrowAmount::Quantity(QUANTITY),
}),
..util::base_external_loan()
};
Expand Down
4 changes: 2 additions & 2 deletions pallets/loans/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use super::{
entities::{
loans::{ActiveLoan, LoanInfo},
pricing::{
external::ExternalPricing,
internal::{InternalPricing, MaxBorrowAmount},
external::{ExternalPricing, MaxBorrowAmount as ExtMaxBorrowAmount},
internal::{InternalPricing, MaxBorrowAmount as IntMaxBorrowAmount},
ActivePricing, Pricing,
},
},
Expand Down
10 changes: 5 additions & 5 deletions pallets/loans/src/tests/util.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::*;

pub fn total_borrowed_rate(value: f64) -> MaxBorrowAmount<Rate> {
MaxBorrowAmount::UpToTotalBorrowed {
pub fn total_borrowed_rate(value: f64) -> IntMaxBorrowAmount<Rate> {
IntMaxBorrowAmount::UpToTotalBorrowed {
advance_rate: Rate::from_float(value),
}
}

pub fn outstanding_debt_rate(value: f64) -> MaxBorrowAmount<Rate> {
MaxBorrowAmount::UpToOutstandingDebt {
pub fn outstanding_debt_rate(value: f64) -> IntMaxBorrowAmount<Rate> {
IntMaxBorrowAmount::UpToOutstandingDebt {
advance_rate: Rate::from_float(value),
}
}
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn base_external_loan() -> LoanInfo<Runtime> {
collateral: ASSET_AA,
pricing: Pricing::External(ExternalPricing {
price_id: REGISTER_PRICE_ID,
max_borrow_quantity: QUANTITY,
max_borrow_amount: ExtMaxBorrowAmount::Quantity(QUANTITY),
}),
restrictions: LoanRestrictions {
borrows: BorrowRestrictions::NotWrittenOff,
Expand Down