Submitted by MiloTruck, also found by deth, yumsec (1, 2), deepkin, and T1MOH
The collectFees() function calls _writeState() before transferring assets to the feeRecipient:
WildcatMarket.sol#L106-L107
However, _writeState() calls totalAssets() when checking if the market is delinquent:
WildcatMarketBase.sol#L448-L453
totalAssets() returns the current asset balance of the market:
WildcatMarketBase.sol#L238-L240
Since the transfer of assets is only performed after _writeState() is called, totalAssets() will be higher than it should be in _writeState(). This could cause collectFees() to incorrectly update state.isDelinquent to false when the market is still delinquent.
For example:
collectFees() will incorrectly update the market to non-delinquent when liquidityRequired() < totalAssets() + state.accuredProtocolFees.
Should this occur, the delinquency fee will not be added to scaleFactor until the markets state is updated later on, causing a loss of yield for lenders since the borrower gets to avoid paying the penalty fee for a period of time.
The following test demonstrates how collectFees() incorrectly updates state.isDelinquent to false when the market is still delinquent:
In collectFees(), consider calling _writeState() after assets have been transferred to the feeRecipient:
WildcatMarket.sol#L106-L107
laurenceday (Wildcat) confirmed and commented:
Note: For full discussion, see here.
