Liquidation Bots

Build bots that monitor credit accounts and execute profitable liquidations.

Overview

Liquidation bots need to:

  1. Find accounts with low health factors

  2. Filter for liquidatable accounts

  3. Compute optimal liquidation paths

  4. Execute liquidations profitably

This guide covers each step with verified SDK patterns.


Understanding Liquidation

WHY: Know what you're building before writing code.

When Accounts Become Liquidatable

An account becomes liquidatable when its health factor drops below 1.0:

Health Factor = Total Weighted Collateral Value / Total Debt

Where:
- Weighted Value = Sum of (Token Balance * Price * Liquidation Threshold)
- Total Debt = Principal + Accrued Interest + Quota Fees

Health factor is scaled by 10000, so healthFactor < 10000 means liquidatable.

The Liquidation Process

  1. Liquidator calls creditFacade.liquidateCreditAccount()

  2. Protocol converts collateral to underlying token

  3. Debt is repaid from converted collateral

  4. Liquidator receives premium (configured per Credit Manager)

  5. Remaining funds go to account owner (if any)

The liquidator provides the multicall that handles collateral conversion. This is where profit comes from - efficient routing means better conversion rates.


Finding Liquidatable Accounts

WHY: Efficiently scan all accounts to find opportunities.

Using CreditAccountCompressor

The CreditAccountCompressor has built-in health factor filtering:

Filter by isLiquidatable

The isLiquidatable field accounts for additional protocol checks:

Pagination for Large Result Sets

The compressor returns paginated results. Iterate through all pages:


Account Analysis

WHY: Understand an account's composition before liquidating.

Collateral Breakdown

Estimating Profit


Building the Liquidation Multicall

WHY: The multicall handles collateral conversion and determines profit.

Basic Structure

A liquidation multicall typically:

  1. Updates stale price feeds (if needed)

  2. Swaps collateral tokens to underlying

  3. Repays debt (handled by protocol)

Using Slippage Protection

Always protect against sandwich attacks:

See Controlling Slippage for details.


Executing Liquidation

WHY: Actually perform the liquidation and capture profit.

The liquidateCreditAccount Call

Handling Partial Liquidation

In some configurations, partial liquidation is possible. Check the Credit Manager configuration:


Bot Architecture

WHY: Production bots need proper design for reliability and competitiveness.

Monitoring Loop

Simulation Before Execution

Always simulate before sending transactions:

Competition Considerations

Liquidation is competitive. Other bots are scanning the same accounts.

Strategies:

  • Speed: Use faster RPC endpoints, optimize code paths

  • Gas: Pay higher gas for priority (use maxPriorityFeePerGas)

  • Efficiency: Better swap routing means higher profit, can afford more gas

  • Flashbots: Use MEV-protected submission to avoid frontrunning


Complete Example: Simple Liquidation Bot


Gotchas

Price Updates Must Come First

If any price feeds are stale, update them at the start of your multicall:

See Updating Price Feeds.

Account State Can Change

Between scanning and executing, another bot may liquidate the account:

Gas Estimation

Liquidation gas costs vary based on:

  • Number of collateral tokens

  • Complexity of swaps

  • Price feed updates needed

Always estimate gas before calculating profitability.


Next Steps

Last updated