While the LendingPool contract's getUserDebt function attempts to calculate a user's current debt by scaling up the stored debt balance with the current usage index, this linear scaling is fundamentally inaccurate for debts that accrue compound interest. Using the DebtToken's balanceOf function will provide the accurate debt amount.
The getUserDebt function calculates the user's debt by multiplying the stored scaledDebtBalance by the current reserve.usageIndex. This approach assumes a linear interest model. However, lending protocols typically use compound_ interest.
Linear vs. Compound Interest: Linear interest accrues only on the principal amount. Compound interest accrues on both the principal and the accumulated interest. The reserve.usageIndex reflects the accumulated interest, but using it in a linear calculation does not accurately capture the effect of compounding.
Inaccurate Debt Calculation: Because of the difference between linear and compound interest, the getUserDebt function will consistently underestimate the user's actual debt. The longer the debt is outstanding, the greater the discrepancy will be.
Liquidations: Liquidations may be triggered prematurely or delayed because the system relies on an incorrect debt value.
Borrowing Power: Users' borrowing power calculations, which are based on their debt, will be incorrect, potentially allowing them to borrow more than they should.
Accounting and Reporting: The protocol's overall accounting and reporting will be flawed.
Potential for Exploitation: This discrepancy can be exploited. Because the reported debt is lower than the actual debt, malicious users might be able to strategically interact with the protocol to their advantage. For instance, they might be able to avoid liquidation for longer than they should.
Alice borrows 100 units of an asset.
The loan accrues compound interest over time.
The reserve.usageIndex increases to reflect the accrued interest.
getUserDebt calculates Alice's debt using linear scaling, resulting in a value slightly lower than her actual debt.
This difference, while seemingly small initially, grows over time due to the compounding effect.
Use this guide to intergrate foundry into your project: foundry
Create a new file FortisAudits.t.sol in the test directory.
Add the following gist code to the file: Gist Code
Fix the wrong user debt storing in the LendingPool contract (separate report submitted for this issue)
5.Run the test using forge test --mt test_FortisAudits_WrongUserDebtReturned -vvvv.
Logs before the fix:
Logs after the fix:
The most accurate way to determine a user's current debt is to directly query the DebtToken contract's balanceOf function. This function already incorporates the compound interest calculation.By using the DebtToken's balanceOf function, the getUserDebt function will accurately reflect the user's current debt, including the effect of compound interest. This change is crucial for the stability and proper functioning of the lending protocol.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.