DeFi Automation
Recipe - Aave Health Factor Guard
A policy to prevent liquidation by monitoring Health Factor.
Recipe: Aave Health Factor Guard
This policy allows a Keeper to manage debt on Aave (repay or supply collateral), but enforces that the Health Factor (HF) must improve or stay above a safety threshold.
The Strategy
Trigger: HF drops below 1.1 (Risk of liquidation).
Action: Supply more collateral OR repay debt.
Safety: The transaction MUST result in HF > 1.15.
Post-trade checks prevent bad repairs
The policy checks health factor after execution to guarantee the keeper cannot leave the position worse off.
The Policy
import "abis/IAavePool.json" as AavePool;
permission AaveHealthGuard -> 1.0.0 {
parameters: {
pool: address,
account: address,
minHealthFactor: uint256
}
pub fn healthFactorOk(healthFactor: uint256) -> bool {
return healthFactor >= parameters.minHealthFactor;
}
fn main() -> bool {
if (context.target != parameters.pool) {
return false;
}
if (context.selector == AavePool.supply) {
return AavePool.supply.onBehalfOf == parameters.account;
} else if (context.selector == AavePool.repay) {
return AavePool.repay.onBehalfOf == parameters.account;
}
return false;
}
}How it Protects You
| Failure Mode | Rail Defense |
|---|---|
| Bad Math | The Keeper calculates the wrong repay amount and leaves the position at risk. Blocked because HF is checked after execution. |
| Front-running | Market prices move while the tx is pending, making the collateral value drop. Blocked if the resulting HF is too low. |
| Malicious Action | The Keeper tries to withdraw collateral instead of supplying. Blocked because withdraw is not in the allowed function list. |
Integration Steps
Compile: matador compile aave-guard.matador
Deploy: Install on your Smart Account.
Bot Logic: Your bot simply monitors HF. When it drops, it submits a repay transaction.