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

Feat: decrease debt #1857

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ pub mod pallet {
loan_id: T::LoanId,
amount: PrincipalInput<T>,
},
/// Debt of a loan has been decreased
DebtDecreased {
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: RepaidInput<T>,
},
}

#[pallet::error]
Expand Down Expand Up @@ -890,6 +896,34 @@ pub mod pallet {

Ok(())
}

/// Decrease debt for a loan. Similar to [`Pallet::repay()`] but
/// without transferring from the pool.
///
/// The origin must be the borrower of the loan.
/// The decrease debt action should fulfill the repay 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(14)]
pub fn decrease_debt(
Copy link
Contributor

@wischli wischli Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR: Add unit test(s) for this. We must not merge PRs which add/alter code without adding/updating tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not adding new logic IMO, just sparing the T::Pool::repay(..) which will be mocked. So pub fn repay(..) should already cover everything.

If you disagree, can still add a test.

Copy link
Contributor

@lemunozm lemunozm Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a super simple fn increase_debt_does_not_withdraw() test for the increase_debt() case, maybe we can create a similar one in tests/repay_loan.rs checking that nothing goes to the pool. Of course, paranoid mode-on just ensuring future refactors does not break the invariant here

origin: OriginFor<T>,
pool_id: T::PoolId,
loan_id: T::LoanId,
amount: RepaidInput<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

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

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

Ok(())
}
}

// Loan actions
Expand Down
Loading