Flash Loan Liquidation Strategies
Liquidation Opportunity Identification
The Anatomy of a Loan
In DeFi lending, every loan is a balancing act. On one side, you have the collateral a user has supplied. On the other, you have the assets they have borrowed. Protocols like Aave and Compound constantly watch this balance to make sure the collateral is always worth significantly more than the debt.
This safety margin is defined by two key parameters for each asset: the Collateral Factor (or Loan-to-Value ratio) and the The Collateral Factor determines how much you can borrow against your collateral. For example, if ETH has a Collateral Factor of 80%, you can borrow up to $80 worth of other assets for every $100 of ETH you supply.
The Liquidation Threshold is the trigger point. It's a higher percentage that represents the maximum value your debt can reach relative to your collateral before your position is considered undercollateralized and eligible for liquidation. If ETH's Liquidation Threshold is 85%, your loan is safe as long as your debt value is below 85% of your collateral value. The moment it crosses that line, the race begins.
Calculating the Health Factor
To standardize risk assessment across all user accounts, Aave uses a concept called the Health Factor. It’s a single number that represents the safety of a user's loan. If the Health Factor is above 1, the position is safe. If it drops below 1, it's open season for liquidators.
Compound uses a similar concept, often referred to as a collateralization ratio, but the principle is identical. The goal is to monitor a user's total borrowing power against their total liabilities. When liabilities exceed the adjusted value of their collateral, the account becomes liquidatable.
| Protocol | Key Metric | Liquidation Trigger |
|---|---|---|
| Aave V3 | Health Factor | Drops below 1.0 |
| Compound V2 | Collateralization Ratio | Drops below 1.0 |
| Compound V3 | N/A (Directly check isLiquidatable) | Function returns true |
Finding Your Targets
Now for the important part: how do you find these vulnerable accounts? You can't just scroll through a list. You need to query the blockchain data directly. There are two primary methods for this: direct node access and subgraphs.
Direct node access using a service like Infura or Alchemy gives you raw, real-time access to the blockchain. You can call functions on smart contracts directly. This is the fastest and most up-to-date method, but it can be complex and data-intensive.
For example, to check a user's health factor on Aave, you would use a JSON-RPC call to query the getUserAccountData function on the Pool contract. This function returns several values, including the user's health factor.
// Using ethers.js to interact with Aave's Pool contract
const aavePoolAddress = "0x87870Bca3F3fD6036531474E0457175d5F28656d"; // on Polygon
const aavePoolContract = new ethers.Contract(aavePoolAddress, AAVE_POOL_ABI, provider);
async function checkHealthFactor(userAddress) {
const userData = await aavePoolContract.getUserAccountData(userAddress);
// The healthFactor is returned as a BigNumber with 18 decimals
const healthFactor = ethers.utils.formatEther(userData.healthFactor);
console.log(`User's Health Factor: ${healthFactor}`);
if (parseFloat(healthFactor) < 1.0) {
console.log("This account is liquidatable!");
}
}
The second method is using a subgraph. Subgraphs are indexed, organized versions of blockchain data that are much easier to query. You use the GraphQL query language to request specific information, like a list of all users with a health factor below a certain threshold.
# Query for Aave V3 subgraph
{
users(where: {healthFactor_lt: "1000000000000000000"}) {
id
healthFactor
totalBorrowsUSD
totalCollateralUSD
}
}
Subgraphs are simpler but might have a slight delay compared to direct node access. In the world of liquidation bots, where milliseconds matter, this delay can be the difference between success and failure.
Choosing Your Prize
Once you've identified a liquidatable account, you don't just get to take all their collateral. You can only liquidate a portion of their debt, and in return, you get to seize a corresponding amount of their collateral plus a bonus. This is the —your reward for helping to keep the protocol solvent.
An account might have multiple types of collateral, like ETH, WBTC, and LINK. Each asset will have its own liquidation bonus. As a liquidator, your goal is to find the combination of debt to repay and collateral to seize that offers the highest profit. This often means targeting the collateral with the largest bonus, but you must also consider the liquidity of that asset on decentralized exchanges. A large bonus on an illiquid token isn't worth much if you can't sell it without crashing the price.
What is the primary purpose of the Collateral Factor (or Loan-to-Value ratio) in a DeFi lending protocol?
A user's position in Aave becomes eligible for liquidation when their Health Factor drops below 1.
Identifying an opportunity is the first critical step. Now you understand the mechanics of how loans become unhealthy and how to find them before anyone else.