which means pool.depositData.totalAmount = pool.depositData.totalAmount -(principalPaid - interestPaid); the issue is we can not make sure principalPaid larger than principalPaid.
LoanManager# repayWithCollateral will call updateWithRepayWithCollateral. Let's dive into function executeRepay
from the logic calculate interestPaid and principalPaid , when repay, the interest is first calculated and can not make sure interestPaid < principalPaid , in some situation, principalPaid equals 0 is possible.
it("Should successfully update pool with repay with collateral of stable borrow",async () => {const { admin,user,hubPool } =awaitloadFixture(deployHubPoolFixture);// deploy mock loan manager so can emit event with paramsconstloanManager=awaitnewHubPoolLogged__factory(user).deploy(hubPool);awaithubPool.connect(admin).grantRole(LOAN_MANAGER_ROLE, loanManager);// set pool dataconstdepositInterestIndex=BigInt(1.839232023893e18);constdepositTotalAmount=BigInt(10e18);conststableBorrowTotalAmount=BigInt(1.43543539e18);conststableInterestRate=BigInt(0.1420009e18);conststableAverageInterestRate=BigInt(0.19014e18);constfeeTotalRetainedAmount=BigInt(0.14e18);constpoolData=getInitialPoolData();poolData.depositData.interestIndex = depositInterestIndex;poolData.depositData.totalAmount = depositTotalAmount;poolData.stableBorrowData.totalAmount = stableBorrowTotalAmount;poolData.stableBorrowData.interestRate = stableInterestRate;poolData.stableBorrowData.averageInterestRate = stableAverageInterestRate;poolData.feeData.totalRetainedAmount = feeTotalRetainedAmount;awaithubPool.setPoolData(poolData);// calculate new stable average interest rate//@audit try to mididy principalPaid is 0 , this will revert() constprincipalPaid=BigInt(0);constloanStableRate=BigInt(0.125e18);constnewStableAverageInterestRate=calcDecreasingAverageStableBorrowInterestRate( principalPaid, loanStableRate, stableBorrowTotalAmount, stableAverageInterestRate );// update pool with repay with collateralconstinterestPaid=BigInt(0.09811e8);constupdatePoolWithRepayWithCollateral=awaitloanManager.updatePoolWithRepayWithCollateral( principalPaid, interestPaid, loanStableRate );});