How to Set Up an AAVE Liquidation Bot

·

Decentralized Finance (DeFi) continues to reshape the financial landscape, offering trustless lending and borrowing through smart contracts. Among the leading protocols in this space, AAVE stands out as a robust and innovative platform enabling users to borrow against a wide range of crypto assets like ETH, DAI, USDT, and LINK. However, maintaining system stability is critical—this is where liquidations play a vital role.

When borrowers fail to maintain sufficient collateral, their positions become vulnerable. To protect the protocol, AAVE allows liquidators to step in and repay part of a loan in exchange for discounted collateral—typically earning a 5% to 15% reward. While manual liquidation is possible via the AAVE app, many participants are turning to automation. This guide walks you through how to build your own AAVE liquidation bot, leveraging smart contracts and real-time data.


Understanding AAVE Liquidations

At the core of AAVE’s risk management is the Health Factor (HF). This metric determines whether a borrower’s position is safe or eligible for liquidation.

The Health Factor is calculated as:
HF = (Σ Collateral Value × Liquidation Threshold) / (Total Borrowed + Fees)
All values are converted into ETH equivalents using Chainlink price oracles.

Only accounts with a health factor below 1 are subject to liquidation. Liquidators must repay part of the debt and receive collateral at a discount—this incentive ensures rapid response times and system solvency.

👉 Discover how blockchain automation can boost your DeFi strategy today.


Three Ways to Participate in Liquidations

There are three primary methods for executing liquidations on AAVE:

  1. Using the AAVE Liquidations Dashboard
    Accessible at app.aave.com/liquidations, this user-friendly interface allows anyone to manually trigger liquidations.
  2. Calling liquidationCall() via Smart Contract
    Developers interact directly with AAVE’s lending pool contract using the liquidationCall() function.
  3. Building an Automated Liquidation Bot
    The most advanced approach—automating detection and execution using scripts, APIs, or custom bots.

This article focuses on methods 2 and 3, with an emphasis on building a reliable, profitable bot.


Prerequisites for Triggering a Liquidation

Before initiating a liquidation, several conditions must be met:

The liquidationCall() function requires:

liquidationCall(
    address collateralAsset,
    address debtAsset,
    address user,
    uint256 debtToCover,
    bool receiveAToken
)

Successful calls return collateral at a discount, determined by the asset’s liquidation bonus—higher for volatile assets like ETH, lower for stablecoins like DAI.


Finding At-Risk Accounts

To automate liquidations, your bot must first detect vulnerable positions. Here are two effective approaches:

1. On-Chain Event Monitoring

AAVE emits events whenever users deposit, borrow, or repay. By listening to these events (e.g., Borrow, Repay, LiquidationCall), you can maintain a real-time snapshot of user positions.

Once you detect activity from a user, use the AAVE protocol’s getUserReserveData() function to fetch their current health factor and exposure.

This method offers low latency but requires running a node or using services like Alchemy or Infura for event streaming.

2. Using AAVE’s Public API

AAVE provides a public endpoint to fetch at-risk accounts:

https://protocol-api.aave.com/liquidations?get=proto

While convenient, this method may have slight delays compared to direct blockchain monitoring. For high-frequency bots, combining both methods ensures accuracy and speed.

👉 Learn how real-time data access can improve your DeFi automation performance.


Executing a Liquidation via Web3.js

Once you’ve identified a target account, use Web3.js to execute the liquidation programmatically.

Here’s a simplified example:

// Import required ABIs
import DaiTokenABI from "./DAItoken.json";
import LendingPoolAddressesProviderABI from "./LendingPoolAddressesProvider.json";
import LendingPoolABI from "./LendingPool.json";

// Configuration
const collateralAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; // WETH
const daiAmountInWei = web3.utils.toWei("1000", "ether");
const daiAddress = '0x6B175474E89094C44Da98b954EedeAC495271d0F'; // DAI
const user = '0xUserAddress';
const receiveATokens = true;
const lpAddressProviderAddress = '0x24a42fD28C976A61Df5D00D0599C34c4f90748c8';

// Initialize contracts
const lpAddressProviderContract = new web3.eth.Contract(LendingPoolAddressesProviderABI, lpAddressProviderAddress);

// Fetch latest LendingPool and Core addresses
const lpCoreAddress = await lpAddressProviderContract.methods.getLendingPoolCore().call();
const lpAddress = await lpAddressProviderContract.methods.getLendingPool().call();

// Approve DAI spending
const daiContract = new web3.eth.Contract(DaiTokenABI, daiAddress);
await daiContract.methods.approve(lpCoreAddress, daiAmountInWei).send({ from: yourAddress });

// Execute liquidation
const lpContract = new web3.eth.Contract(LendingPoolABI, lpAddress);
await lpContract.methods.liquidationCall(
    collateralAddress,
    daiAddress,
    user,
    daiAmountInWei,
    receiveATokens
).send({ from: yourAddress });

This script automates approval and liquidation execution—critical for minimizing delays during volatile market conditions.


Building a Profitable Liquidation Bot

Creating a bot involves more than just calling functions—it requires strategic design for reliability and profitability.

Key Considerations:


Calculating Net Profit

To determine if a liquidation is worth executing:

  1. Fetch the collateral value using Chainlink oracles.
  2. Multiply by the liquidation bonus (e.g., 5–15%) to get maximum reward.
  3. Estimate gas fees in the target asset.
  4. Subtract gas cost from reward:
    Net Profit = (Collateral Value × Bonus) – Gas Cost
Example: Liquidating $10,000 worth of ETH with a 10% bonus yields $1,000 in rewards. If gas costs $150, net profit is $850.

Stablecoins usually offer lower bonuses due to lower volatility, while assets like AAVE or UNI may offer higher incentives.


Frequently Asked Questions (FAQ)

Q: Can anyone become an AAVE liquidator?
A: Yes—any wallet with sufficient funds can liquidate undercollateralized positions by repaying debt and claiming discounted collateral.

Q: What happens if multiple bots try to liquidate the same position?
A: The first successful transaction wins. This creates a competitive environment where speed and low latency are crucial.

Q: How often do liquidation opportunities occur?
A: Frequency depends on market volatility. During sharp price drops (e.g., BTC or ETH plunges), dozens of positions may become eligible within minutes.

Q: Is running a liquidation bot profitable?
A: It can be highly profitable during volatile markets, but requires technical skill, capital, and efficient infrastructure to outperform competitors.

Q: Are there risks involved in liquidation?
A: Yes—impermanent losses, oracle delays, or flash crashes can lead to losses if not properly managed. Always test your bot on testnets first.

Q: Does AAVE charge fees for liquidations?
A: No direct fees—liquidators keep the full reward minus gas costs. However, network congestion can increase transaction expenses significantly.


Final Thoughts

Setting up an AAVE liquidation bot is a powerful way to participate in DeFi beyond passive staking or lending. By combining real-time monitoring, smart contract interaction, and profit calculation logic, you can create an autonomous system that generates returns during market downturns.

Whether you're a developer exploring automation or an investor seeking yield opportunities, understanding the mechanics of liquidations gives you an edge in the fast-moving world of decentralized finance.

👉 Start building smarter DeFi strategies with advanced tools and insights.