The BGD Labs team shared their vision of possible root causes of the invariant deviation. Different assumptions were made about what might have gone wrong in the past. For example, there were markets with very high historical interest rates, implementation updates, or even updated
accruedToTreasury logic. They pointed out that there is a market with a deviation in the invariant, and assets from this market haven't been borrowed. We decided to start from this specific market to simplify the investigation. This simplification reduced the variables in the virtual balance formula, narrowing down possible causes. A notable case was the
aArbAave market. Preliminary analysis revealed that a seemingly routine flash loan
transaction had caused the delta to spike by 5*10^7.
Initial reviews of the flash loan logic did not reveal any obvious errors. While minor rounding errors could account for slight increases in delta, they could not explain the significant spike observed. To isolate the problematic transactions, a binary search method was applied, revealing that the last significant delta increase occurred over a year prior, suggesting an issue in an older implementation. A review of the
relevant update following the last transaction revealed the origin of the issue.
By examining the code differences in
FlashLoanLogic.sol between the affected versions, it was discovered that the calculation of the
reserve.liquidityIndex during flash loan operations did not account for the
accruedToTreasury variable. This variable represents the treasury's share of the accrued interest, which is not included in the total supply of
aTokens but still accumulates interest over time.
Specifically, the function
cumulativeToLiquidityIndex, responsible for adjusting the
liquidityIndex based on the premium collected from flash loans, was using the
totalSupply of
aTokens without adding the
accruedToTreasury. As a result, the premium was effectively being distributed over a smaller number of tokens than it should have been, causing the
liquidityIndex to increase more than appropriate.
This miscalculation led to an overestimation of the virtual balance
Bv, pushing it beyond the actual balance
Bu.