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

Fix: transfer debt event with updated repaid amount #1837

Merged
merged 2 commits into from
May 17, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ pub mod pallet {
Err(Error::<T>::UnrelatedChangeId)?
};

let (_, _count) = Self::transfer_debt_action(
let (repaid_amount, _count) = Self::transfer_debt_action(
&who,
pool_id,
from_loan_id,
Expand Down Expand Up @@ -945,7 +945,7 @@ pub mod pallet {
repaid_amount: RepaidInput<T>,
borrow_amount: PrincipalInput<T>,
permissionless: bool,
) -> Result<(T::Balance, u32), DispatchError> {
) -> Result<(RepaidInput<T>, u32), DispatchError> {
ensure!(
from_loan_id != to_loan_id,
Error::<T>::TransferDebtToSameLoan
Expand All @@ -962,7 +962,7 @@ pub mod pallet {
let count =
Self::borrow_action(who, pool_id, to_loan_id, &borrow_amount, permissionless)?;

Ok((repaid_amount.repaid_amount()?.total()?, count))
Ok((repaid_amount, count))
}

/// Set the maturity date of the loan to this instant.
Expand Down
2 changes: 1 addition & 1 deletion pallets/loans/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::{
ActivePricing, Pricing,
},
},
pallet::{ActiveLoans, CreatedLoan, Error, LastLoanId, PortfolioValuation},
pallet::{ActiveLoans, CreatedLoan, Error, Event, LastLoanId, PortfolioValuation},
types::{
policy::{WriteOffRule, WriteOffStatus, WriteOffTrigger},
valuation::{DiscountedCashFlow, ValuationMethod},
Expand Down
21 changes: 16 additions & 5 deletions pallets/loans/src/tests/repay_loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,29 @@ fn with_success_half_amount() {
let loan_id = util::create_loan(util::base_internal_loan());
util::borrow_loan(loan_id, PrincipalInput::Internal(COLLATERAL_VALUE / 2));

let amount = RepaidInput {
principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2),
interest: 1234, /* Will not be used */
unscheduled: 0,
};

config_mocks(COLLATERAL_VALUE / 2);
assert_ok!(Loans::repay(
RuntimeOrigin::signed(BORROWER),
POOL_A,
loan_id,
RepaidInput {
principal: PrincipalInput::Internal(COLLATERAL_VALUE / 2),
interest: 0,
unscheduled: 0,
},
amount.clone()
));
assert_eq!(0, util::current_loan_debt(loan_id));

System::assert_last_event(RuntimeEvent::Loans(Event::Repaid {
pool_id: POOL_A,
loan_id: loan_id,
amount: RepaidInput {
interest: 0,
..amount
},
}));
});
}

Expand Down
17 changes: 14 additions & 3 deletions pallets/loans/src/tests/transfer_debt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ fn with_success_internals() {

let repay_amount = RepaidInput {
principal: PrincipalInput::Internal(COLLATERAL_VALUE),
interest: 0,
interest: 1234, /* Will not be used */
unscheduled: 0,
};
let borrow_amount = PrincipalInput::Internal(COLLATERAL_VALUE);
Expand All @@ -372,8 +372,8 @@ fn with_success_internals() {
POOL_A,
loan_1,
loan_2,
repay_amount,
borrow_amount,
repay_amount.clone(),
borrow_amount.clone(),
));

assert_ok!(Loans::apply_transfer_debt(
Expand All @@ -384,6 +384,17 @@ fn with_success_internals() {

assert_eq!(0, util::current_loan_debt(loan_1));
assert_eq!(COLLATERAL_VALUE, util::current_loan_debt(loan_2));

System::assert_last_event(RuntimeEvent::Loans(Event::DebtTransferred {
pool_id: POOL_A,
from_loan_id: loan_1,
to_loan_id: loan_2,
repaid_amount: RepaidInput {
interest: 0,
..repay_amount
},
borrow_amount,
}));
});
}

Expand Down
Loading