Skip to content

Commit

Permalink
Loans: add increase_debt() action (#1691)
Browse files Browse the repository at this point in the history
* base implementation

* add event

* add testing & benchmarks

* fix docs
  • Loading branch information
lemunozm authored Jan 15, 2024
1 parent 6bdbf67 commit 19ea6b5
Showing 7 changed files with 91 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pallets/loans/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -472,6 +472,14 @@ benchmarks! {

}: _(RawOrigin::Signed(any), pool_id, change_id)

increase_debt {
let n in 1..Helper::<T>::max_active_loans() - 1;

let borrower = account("borrower", 0, 0);
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, PrincipalInput::Internal(10.into()))
}

impl_benchmark_test_suite!(
35 changes: 34 additions & 1 deletion pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
@@ -346,6 +346,12 @@ pub mod pallet {
repaid_amount: RepaidInput<T>,
borrow_amount: PrincipalInput<T>,
},
/// Debt of a loan has been increased
DebtIncreased {
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: PrincipalInput<T>,
},
}

#[pallet::error]
@@ -469,7 +475,6 @@ pub mod pallet {
/// at [`types::LoanRestrictions`]. The `amount` will be transferred
/// from pool reserve to borrower. The portfolio valuation of the pool
/// is updated to reflect the new present value of the loan.
/// Rate accumulation will start after the first borrow.
#[pallet::weight(T::WeightInfo::borrow(T::MaxActiveLoansPerPool::get()))]
#[pallet::call_index(1)]
pub fn borrow(
@@ -847,6 +852,34 @@ pub mod pallet {

Ok(())
}

/// Increase debt for a loan. Similar to [`Pallet::borrow()`] but
/// without transferring from the pool.
///
/// The origin must be the borrower of the loan.
/// The increase debt action should fulfill the borrow restrictions
/// configured at [`types::LoanRestrictions`]. The portfolio valuation
/// of the pool is updated to reflect the new present value of the loan.
#[pallet::weight(T::WeightInfo::increase_debt(T::MaxActiveLoansPerPool::get()))]
#[pallet::call_index(13)]
pub fn increase_debt(
origin: OriginFor<T>,
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: PrincipalInput<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

let _count = Self::borrow_action(&who, pool_id, loan_id, &amount, false)?;

Self::deposit_event(Event::<T>::DebtIncreased {
pool_id,
loan_id,
amount,
});

Ok(())
}
}

// Loan actions
29 changes: 29 additions & 0 deletions pallets/loans/src/tests/borrow_loan.rs
Original file line number Diff line number Diff line change
@@ -582,3 +582,32 @@ fn twice_with_elapsed_time() {
);
});
}

#[test]
fn increase_debt_does_not_withdraw() {
new_test_ext().execute_with(|| {
MockPools::mock_withdraw(|_, _, _| {
unreachable!("increase debt must not withdraw funds from the pool");
});

let loan = LoanInfo {
pricing: Pricing::External(ExternalPricing {
max_borrow_amount: ExtMaxBorrowAmount::NoLimit,
..util::base_external_pricing()
}),
..util::base_external_loan()
};

let loan_id = util::create_loan(loan);

let amount = ExternalAmount::new(QUANTITY, PRICE_VALUE);
config_mocks(amount.balance().unwrap());

assert_ok!(Loans::borrow(
RuntimeOrigin::signed(BORROWER),
POOL_A,
loan_id,
PrincipalInput::External(amount)
));
});
}
5 changes: 5 additions & 0 deletions pallets/loans/src/weights.rs
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ pub trait WeightInfo {
fn update_portfolio_valuation(n: u32) -> Weight;
fn propose_transfer_debt(n: u32) -> Weight;
fn apply_transfer_debt(n: u32) -> Weight;
fn increase_debt(n: u32) -> Weight;
}

impl WeightInfo for () {
@@ -81,4 +82,8 @@ impl WeightInfo for () {
fn apply_transfer_debt(_: u32) -> Weight {
Weight::zero()
}

fn increase_debt(_: u32) -> Weight {
Weight::zero()
}
}
5 changes: 5 additions & 0 deletions runtime/altair/src/weights/pallet_loans.rs
Original file line number Diff line number Diff line change
@@ -372,4 +372,9 @@ impl<T: frame_system::Config> pallet_loans::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().writes(5))
}

fn increase_debt(_: u32) -> Weight {
// Pending to generate
Weight::zero()
}
}
5 changes: 5 additions & 0 deletions runtime/centrifuge/src/weights/pallet_loans.rs
Original file line number Diff line number Diff line change
@@ -372,4 +372,9 @@ impl<T: frame_system::Config> pallet_loans::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().writes(5))
}

fn increase_debt(_: u32) -> Weight {
// Pending to generate
Weight::zero()
}
}
5 changes: 5 additions & 0 deletions runtime/development/src/weights/pallet_loans.rs
Original file line number Diff line number Diff line change
@@ -372,4 +372,9 @@ impl<T: frame_system::Config> pallet_loans::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().writes(5))
}

fn increase_debt(_: u32) -> Weight {
// Pending to generate
Weight::zero()
}
}

0 comments on commit 19ea6b5

Please sign in to comment.