On-Chain Liquidation Bots
DeFi lending protocols pay anyone willing to repay an undercollateralized borrower's debt and seize their collateral at a discount — a bounty that bots race each other to collect the instant a position crosses the line.
Prerequisites: Margin Calls and Forced Liquidation, Flash Loans and Atomic Arbitrage
A DeFi lending protocol like Aave or Compound has no human loan officer to call a borrower when their collateral value falls too close to their debt. Instead the protocol builds the check into its code: every borrower must maintain a health factor — collateral value divided by debt, adjusted for a liquidation threshold — above 1. The moment it falls below 1, the position becomes eligible for liquidation, and the protocol pays whoever repays part of the debt a liquidation bonus, typically 5–15% of the debt repaid, funded by seizing that much extra of the borrower's collateral.
Because the reward goes to whoever submits the liquidation transaction first, and because it can be entirely financed with a flash loan, this is a race with no meaningful capital barrier — the entire strategy is built and run by bots.
A liquidation bounty is public and known the instant a position becomes eligible; the only thing that decides who collects it is who can detect the crossing and get a transaction included first, which is why on-chain liquidations are dominated by automated infrastructure, not manual traders.
The race, step by step
The mechanics inside the winning transaction usually look like: flash-borrow the debt asset, repay it on the borrower's behalf, receive the discounted collateral, swap that collateral back to the borrowed asset on a DEX, repay the flash loan, keep the spread. Because the whole thing is one atomic transaction, the bot never actually needs to hold the debt asset or the collateral asset itself.
Worked example
A borrower has posted 10 ETH (worth $30,000 at $3,000/ETH) as collateral against a 24,500 USDC loan on a protocol with an 80% liquidation threshold. Health factor is — below 1, so the position is liquidatable. The protocol allows liquidating up to 50% of the debt, with a 10% bonus, in one transaction.
A bot flash-borrows 12,250 USDC, repays half the debt, and in exchange receives collateral worth in ETH, or about 4.49 ETH. It swaps that ETH on a DEX for roughly 13,430 USDC (after slippage and fees), repays the 12,250 USDC flash loan plus a small fee (~$11), and keeps the remainder: 13{,}430 - 12{,}250 - 11 \approx \1{,}169 before gas, and perhaps \1,100–1,140 after — a meaningful profit that exists purely because the borrower's collateral fell below the required buffer.
The liquidation bonus is not free money handed out by the protocol — it comes directly out of the liquidated borrower's remaining collateral. A sharp, fast price drop can trigger cascading liquidations where each bot's sell of seized collateral pushes the market price down further, tipping more positions underwater and amplifying the very move that started it.
Related concepts
Practice in interviews
Further reading
- Aave Protocol Documentation, Liquidations
- Qin et al., An Empirical Study of DeFi Liquidations