Skip to content
Crypto Blog

Crypto guides and market analysis

How to run a DeFi liquidation bot on Ethereum for next-block execution

How to run a DeFi liquidation bot on Ethereum: track health factors, simulate an Aave V3 liquidation and swap, then send a profitable bundle for the next block.

Crypto Blog Newsroom5 min read

How to run a DeFi liquidation bot on Ethereum for next-block execution

A DeFi liquidation bot goes from spotting a position to getting paid by tracking its health factor, simulating a liquidation and collateral sale, then submitting a bundle whose builder payment fits the expected profit.

  • Eligibility: Aave V3 health factor below 1.
  • Execution: simulate the liquidation and swap together.
  • Submission: target the next block.

Start with a synced node connection, a deployed liquidator contract, a funded signer key and an Ethereum RPC. The RPC supplies chain state and sends transactions; the contract performs the on-chain sequence. The signer authorizes the transaction and needs funds for any required gas or payment.

How do you find and price a liquidatable position?

Index borrowers from protocol events, then refresh their health factors each block. Aave V3 positions become eligible for liquidation when the health factor falls below 1. The value can move as collateral prices, debt and accrued interest change, so an old candidate is not enough: evaluate it against the state you expect the transaction to execute on.

  1. Find a position below the threshold. Use indexed events to maintain the borrower set, then check current health factors each block. A position above 1 is not eligible yet. When one drops below 1, fetch its collateral and debt assets and test whether a liquidation still makes economic sense.
  2. Simulate the liquidation and swap. Call Aave V3’s liquidationCall(collateralAsset, debtAsset, user, debtToCover, receiveAToken) with a debt amount the position can repay. Simulate the collateral sale as part of the same contract execution. That shows whether the proceeds can cover the debt and any flash loan repayment, while accounting for the swap’s slippage.

Easy to get wrong: A liquidation that succeeds in isolation may fail after the swap or loan repayment. Simulate the complete transaction against current state, including the chosen collateral output and the amount of debt covered.

A flash loan can supply the debt asset for the liquidation, so the bot does not need to hold the full repayment amount in advance. The contract borrows, repays the user’s debt, receives collateral, sells enough to repay the loan, then retains any remainder. The sequence must complete atomically: if the collateral cannot repay the loan, the transaction reverts.

Calculate net profit as the liquidation bonus minus swap slippage, gas and the builder payment. Use the expected sale proceeds, not the nominal bonus alone. A thin margin can disappear when the swap price moves or execution costs rise before inclusion.

How do you submit the bundle and collect the bonus?

Set the builder payment from expected profit, then submit the signed transaction for the next block using the Flashbots Auction method eth_sendBundle. A private bundle keeps the candidate out of the public mempool while it is considered for inclusion, reducing the chance another searcher copies the visible liquidation transaction.

  1. Choose a payment from the margin. In a contested liquidation, the winning bot can hand most of its profit to the builder. Estimate the value left after slippage and gas, then set a payment that remains competitive while leaving the contract positive expected profit. A fixed gas price ignores how much the specific opportunity is worth; recalculate the payment when the position, swap quote or costs change.
  2. Submit for the next block. Sign the transaction and send it with eth_sendBundle, targeting the next block. This is where private transactions ethereum fits as a concrete example: private transaction routing is the feature that keeps the candidate out of the public mempool. Flashbots’ bundle submission lets the searcher specify the block for which the transaction is valid.
  3. Verify the result on-chain. A bundle submission is an attempt, not proof of execution. If the opportunity disappears, the swap no longer clears the loan, or the transaction fails against the block’s state, it may not be included. After the target block, check the transaction receipt and the liquidator contract’s balances. The pipeline worked when the liquidation executed and the collateral remaining after repayment and costs appears in the contract.

This flow answers “how to run a defi liquidation bot on ethereum” at the pipeline level: discover, simulate, price, submit, and verify. Keep the transaction atomic so the loan repayment, liquidation and swap either complete together or revert together.

Related questions

Do I need capital to run a liquidation bot?

A flash loan can cover the liquidation debt asset, so the bot does not need to pre-fund that principal. It still needs a funded signer and a contract that can execute the sequence; the transaction must also leave enough value to cover the loan repayment, gas and builder payment.

Why do my liquidation bundles never land?

First check whether the candidate was still eligible and profitable against the target block’s state. A bundle can also lose because its builder payment is too small for the competition, or fail because the liquidation or collateral swap reverts. Re-simulate the whole transaction, including repayment and payment, and target the next block with updated state.