How to Build a Flash Loan Liquidation Bot on Aave v3

Note: When the market crashes, millions of dollars in positions become liquidatable on lending protocols. Bots that act first earn the liquidation bonus—up to 8% of the collateral. Others either miss out or have their transactions reverted due to price increases. Developing a flash loan liquidation

Blockchain Development Services

Frequently Asked Questions

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1450
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1308
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    1003
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1269
  • image_logo-advance_0.webp
    B2B Advance company logo design
    717
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    1008

Note: When the market crashes, millions of dollars in positions become liquidatable on lending protocols. Bots that act first earn the liquidation bonus—up to 8% of the collateral. Others either miss out or have their transactions reverted due to price increases. Developing a flash loan liquidation bot for Aave v3 is a challenge of speed, precise calculation, and MEV strategy. Flash loans allow participation without capital: borrow an asset, liquidate, swap collateral to repay, and return the debt plus fee—all in one atomic transaction. Our engineers have over 7 years of blockchain development experience and have passed more than 100 smart contract audits. We guarantee stable operation and MEV protection. In this article, we dive into how Aave v3 liquidations work, design the bot architecture, and incorporate MEV protection.

How Liquidation Works on Aave v3

A position becomes liquidatable when the health factor drops below 1.0. Health factor = (collateral_value * liquidation_threshold) / debt_value. The liquidation threshold on Aave v3 is 82.5% for ETH and 75% for WBTC.

The liquidator calls liquidationCall(collateralAsset, debtAsset, user, debtToCover, receiveAToken). A maximum of 50% of the debt can be covered per transaction (close factor). In return, the liquidator receives collateral with a bonus—the liquidation bonus, which is 5% for ETH.

Profitability Check Before Sending a Transaction

profit = collateral_received * collateral_price - debt_covered * debt_price - flash_loan_fee (0.09% on Aave v3) - swap_slippage - gas_cost 

If profit ≤ 0, we do not send the transaction. This sounds obvious, but without accurate off-chain calculations using current oracle prices and slippage, the bot loses money on every unprofitable liquidation by only paying gas. For example, liquidating a $10,000 position with a $50 gas fee can become unprofitable if slippage exceeds 0.5%.

Bot Architecture

Smart Contract Executor

A single contract that receives liquidation parameters and performs the atomic sequence:

  1. flashLoan() from the Aave PoolAddressesProvider—borrow the debtAsset
  2. In the executeOperation() callback, call liquidationCall
  3. Receive the collateralAsset (or aToken)
  4. Swap collateralAssetdebtAsset via Uniswap v3 or 1inch
  5. Return debtAsset + fee to Aave
  6. The surplus—profit—is sent to the wallet

The contract should be non-upgradeable and have an onlyOwner modifier on the execution function. Upgradeability is not needed; simplicity and a minimal attack surface are key.

Off-Chain Monitoring

The source of position data. Three options:

  • getUserAccountData() via Multicall for a list of known borrowers—slow for many addresses
  • The Graph subgraphs for Aave—index all positions, updated per block
  • Direct event tracking: Borrow, Deposit, Repay events → local position state machine

The optimal setup: The Graph for initial state loading + WebSocket events for real-time updates. The list of positions with a health factor < 1.05 is checked at every new block.

Method Speed Scalability Cost
Multicall Low Limited Free (query)
The Graph High High Paid (subgraph query)
Event-driven Medium Medium Free

How to Protect Against MEV?

A liquidation bot lives in the MEV world. Sending a transaction to the public mempool with standard gasPrice allows searchers to front-run it: copy the calldata, resend with higher gas, and liquidate the position first.

Bundle simulation. Before sending, simulate the bundle via eth_callBundle—verify that by the time the block is mined, the position is still liquidatable and profit is positive. Sending bundles through Flashbots is three times more effective than public transactions on Ethereum mainnet in terms of liquidation speed.

Dynamic priority fee. On L2s (Arbitrum, Optimism, Base), MEV competition is lower due to the sequencer. Liquidations on Arbitrum are often profitable even without Flashbots. For example, on Arbitrum, liquidating a $50,000 position yields about $2,500 in fees with a gas cost of $5 — that's 500 times lower than Ethereum gas costs for a similar gain.

Typical Implementation Issues

Slippage when swapping large collateral. For large positions, swapping 1,000 ETH to USDC via a single Uniswap v3 pool causes significant price impact. Solution: split routing via the 1inch API or a custom multi-hop implementation across several pools. The optimal route calculation must be part of the off-chain profitability check.

Reorgs on L2s. Arbitrum and Optimism have finality lag. A transaction is confirmed on L2 but may later be reorged—rare, but it happens. For a liquidation bot, this is not critical (the position is either liquidated or not), but confirmations must be handled correctly.

Gas estimation. eth_estimateGas for a flash loan transaction can be off by 10-20%—external contracts behave differently. We add a 20% buffer to the estimate and verify that the transaction remains profitable after the buffer.

Stale health factor. Between calculating the health factor and sending the transaction, 1-2 blocks may pass. If the price rises during that time, the position is no longer liquidatable, and liquidationCall will revert. Add try/catch logic at the off-chain level and only pay gas for failed transactions.

What Is Included in the Work

  • Architecture and documentation: data flow diagrams, contract and off-chain service descriptions.
  • Smart contract: full Solidity code with Foundry tests.
  • Off-chain monitoring: Node.js service with The Graph, Redis, and viem.
  • MEV integration: Flashbots/MEV Blocker setup, simulation.
  • Testing: fork tests on mainnet, soft launch on real data.
  • Deployment and support: deploy to required chains, 2 weeks of post-launch support.
  • Deliverables: full access to code repository, comprehensive documentation, and training session.

Chain Comparison for Liquidations

Chain Average Gas Cost MEV Protection Liquidity
Ethereum $50-100 Flashbots High
Arbitrum $1-5 Sequencer High
Polygon $0.1-0.5 Low MEV Medium

Development Process

Smart contract development (3-4 days). Solidity + Foundry. Fork tests on Ethereum/Arbitrum mainnet: simulate specific liquidatable positions from historical data, verify calculation correctness and profit.

Off-chain monitoring (4-6 days). Node.js/TypeScript, viem for on-chain interaction, The Graph SDK, WebSocket block subscriptions, Redis for position state storage.

MEV integration (2-3 days). Flashbots SDK, bundle building, pre-submission simulation.

Testnet + mainnet soft launch (3-5 days). Start with a Sepolia fork, then mainnet with minimal capital—a few real liquidations to calibrate profitability parameters.

Timeline Estimates

A basic liquidation system for one protocol (Aave v3) on one chain: 1-2 weeks. Multi-protocol (Aave + Compound + Morpho) with multi-chain support: from 3-4 weeks. Timelines depend on MEV protection requirements and the number of target protocols. The cost to build a basic bot starts at $5,000, with potential earnings of thousands per liquidation. Contact us to discuss your project and get a preliminary estimate. Our team has over 5 years of market presence, 7+ years of blockchain experience, and 50+ DeFi projects completed. Order a turnkey bot with full documentation.