Skip to content

Commit

Permalink
Fix: transfer debt event with updated repaid amount (#1837)
Browse files Browse the repository at this point in the history
* fix transfer debt event with updated repaid amount

* add tests for checking repay events
  • Loading branch information
lemunozm authored and gpmayorga committed May 17, 2024
1 parent 96bee3e commit 6f1daa6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
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

0 comments on commit 6f1daa6

Please sign in to comment.