Liquidation Health Factor
Make sure that liquidations can only happen if the position is unhealthy
Use Case
In DeFi lending protocols, positions can be liquidated when they become unhealthy due to market movements or other factors. The health factor is a critical security parameter that determines when a position can be liquidated. An incorrect health factor calculation or validation could lead to:
- Unauthorized liquidations of healthy positions
- Loss of user funds through forced liquidations
- Protocol insolvency if positions can’t be liquidated when needed
- Market manipulation through targeted liquidations
This assertion is particularly important for:
- Preventing liquidations of positions that are still healthy
- Ensuring liquidations only occur when necessary for protocol solvency
- Protecting users from unfair liquidations
- Maintaining protocol stability by enforcing proper liquidation thresholds
Applicable Protocols
- Lending protocols (e.g., Aave, Compound, Morpho) where positions can be liquidated
- Margin trading platforms that require position health monitoring
- Perpetual futures protocols with liquidation mechanisms
- Any DeFi protocol that uses health factors to determine position solvency
These protocols need this assertion because:
- Lending protocols rely on liquidations to maintain solvency
- Margin trading platforms need precise health factor calculations
- Perpetual futures protocols use liquidations for risk management
- Incorrect liquidation logic could lead to significant protocol losses
Explanation
The assertion implements a multi-layered approach to verify liquidation health factors:
-
Pre-liquidation Check:
- Uses
forkPreState()
to capture the health factor before liquidation - Verifies the position is actually unhealthy (below liquidation threshold)
- Prevents liquidations of healthy positions
- Uses
-
Post-liquidation Validation:
- Uses
forkPostState()
to verify the health factor after liquidation - Ensures the health factor improves after liquidation
- Confirms the position meets minimum health requirements
- Uses
-
Parameter Validation:
- Uses
getCallInputs()
to monitor liquidation function calls - Validates that seized assets and repaid shares are non-zero
- Enforces a maximum liquidation amount to prevent protocol destabilization
- Ensures liquidation parameters are within safe bounds
- Uses
The assertion uses the following cheatcodes:
forkPreState()
: Captures the health factor before liquidationforkPostState()
: Verifies the health factor after liquidationgetCallInputs()
: Monitors liquidation function callsregisterCallTrigger()
: Triggers on liquidation function calls
This approach ensures that:
- Only unhealthy positions can be liquidated
- Liquidations improve the position’s health
- The protocol maintains proper risk management
- Liquidation amounts are within safe limits
For more information about cheatcodes, see the Cheatcodes Documentation.
Code Example
Note: This code example is maintained in the Phylax Assertion Examples Repository. For the latest version and a test example, please refer to the repository.
Testing
To test this assertion:
- Deploy a test instance of the lending protocol
- Create positions with varying health factors
- Attempt liquidations on both healthy and unhealthy positions
- Verify the assertion correctly:
- Prevents liquidations of healthy positions
- Allows liquidations of unhealthy positions
- Ensures health factor improvement after liquidation
Assertion Best Practices
- Combine with other assertions like ERC20 Drain for comprehensive protection
- Use appropriate health factor thresholds based on protocol risk parameters