From 3fb72364ef2c70a93200c266ce0e924ad86013d6 Mon Sep 17 00:00:00 2001 From: Antonio Guilherme Ferreira Viggiano Date: Wed, 3 Jul 2024 10:07:06 -0300 Subject: [PATCH] c4-012 getCreditPositionProRataAssignedCollateral can be simplified to totalCollateral * credit / totalDebt --- src/libraries/LoanLibrary.sol | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libraries/LoanLibrary.sol b/src/libraries/LoanLibrary.sol index 75bfba68..57b1b41c 100644 --- a/src/libraries/LoanLibrary.sol +++ b/src/libraries/LoanLibrary.sol @@ -161,9 +161,10 @@ library LoanLibrary { } /// @notice Get the pro-rata collateral assigned to a CreditPosition - /// The amount of collateral assigned to a CreditPosition is the amount of collateral assigned to the - /// DebtPosition pro-rata to the CreditPosition's credit and the DebtPosition's futureValue - /// @dev If the DebtPosition's futureValue is 0, the amount of collateral assigned to the CreditPosition is 0 + /// The amount of collateral assigned to a CreditPosition is equal to + /// the total borrower collateral pro-rata to the CreditPosition's credit + /// and the total borrower debt, because of the auto-assigned collateral to individual loans + /// @dev If the borrower's debt is 0, the amount of collateral assigned to the CreditPosition is 0 /// @param state The state struct /// @param creditPosition The CreditPosition /// @return The amount of collateral assigned to the CreditPosition @@ -174,12 +175,11 @@ library LoanLibrary { { DebtPosition storage debtPosition = getDebtPosition(state, creditPosition.debtPositionId); - uint256 debtPositionCollateral = getDebtPositionAssignedCollateral(state, debtPosition); - uint256 creditPositionCredit = creditPosition.credit; - uint256 debtPositionFutureValue = debtPosition.futureValue; + uint256 debt = state.data.debtToken.balanceOf(debtPosition.borrower); + uint256 collateral = state.data.collateralToken.balanceOf(debtPosition.borrower); - if (debtPositionFutureValue != 0) { - return Math.mulDivDown(debtPositionCollateral, creditPositionCredit, debtPositionFutureValue); + if (debt != 0) { + return Math.mulDivDown(collateral, creditPosition.credit, debt); } else { return 0; }