Liquidation Bots

Build on-chain liquidation contracts that can be triggered by keepers or automation services.

For SDK-based liquidation bots (recommended for most use cases), see Liquidation Bots (SDK).

Overview

On-chain liquidation contracts are useful when you need:

  • Atomicity with flash loans or other on-chain operations

  • Integration with existing keeper infrastructure (Gelato, Chainlink Automation)

  • Custom liquidation logic that must execute trustlessly

  • Protocol-owned liquidation capability

Most liquidation bots use the SDK for monitoring and multicall building, then submit transactions off-chain. This guide covers the less common but important pattern of on-chain liquidation contracts.


Understanding On-Chain Liquidation

WHY: Know when to build a contract vs. use the SDK.

When to Use On-Chain Contracts

Approach
Best For

SDK bot

Most liquidators - flexible routing, off-chain simulation, rapid iteration

On-chain contract

Flash loan liquidations, keeper automation, protocol-owned backstop

The Liquidation Entry Point

The liquidator provides:

  • creditAccount - the account to liquidate

  • to - where remaining funds go after debt repayment

  • calls - multicall array that converts collateral to underlying

  • lossPolicyData - custom data for loss handling


Checking Liquidatability

WHY: Don't waste gas on accounts that can't be liquidated.

Health Factor Check

Expiration Check


Building Liquidation Multicalls

WHY: The multicall converts collateral tokens to underlying. Efficient routing means higher profit.

Basic Swap Pattern

For a single collateral token, swap it to underlying via the adapter:

Multi-Collateral Pattern

When an account has multiple collateral tokens:


Simple Liquidation Contract

WHY: A complete working example you can deploy and test.


Flash Loan Liquidation

WHY: Flash loans let you liquidate without upfront capital.

For partial liquidations, the liquidator must provide underlying tokens. Flash loans make this capital-free:


Gotchas

Approve to Credit Manager, Not Facade

For partial liquidations where you provide underlying:

Gas Costs Scale with Token Count

Liquidation gas depends on:

  • Number of collateral tokens to swap

  • Complexity of DEX routes

  • Price feed updates needed

Estimate gas before submitting to ensure profitability.

Race Conditions

Multiple liquidators compete for the same accounts. On-chain contracts are at a disadvantage vs. off-chain bots that can use Flashbots/MEV protection. Consider:

  • Using higher priority fees for competitive scenarios

  • Targeting accounts that off-chain bots may skip (complex collateral compositions)

  • Bundling with Flashbots Protect for MEV protection

exactAllInputSingle vs exactInputSingle

Use exactAllInputSingle (the "diff" pattern) for liquidation swaps. It swaps the entire balance minus dust, which is what you want when converting all collateral:


Next Steps

Last updated