Run a Liquidation Bot

Liquidation bots protect lenders by liquidating unhealthy loans and earn rewards in the process.

What is a Liquidation Bot?

Liquidation bots:

  1. Monitor active loans for health status

  2. Identify liquidatable positions (LTV breach or overdue)

  3. Execute liquidations to claim collateral + bonus

  4. Protect lenders from bad debt

Revenue Model

Liquidators earn a 5% bonus on collateral:

Example:
- Loan debt: $5,000 USDC
- Collateral: 2 ETH @ $3,000 = $6,000
- Liquidator pays: $5,000 (the debt)
- Liquidator receives: 2 ETH ($6,000)
- Profit: $1,000 (including 5% bonus)

Prerequisites

  • Node.js 18+

  • Base Mainnet RPC (Alchemy or Infura recommended)

  • Wallet with:

    • ETH for gas

    • USDC capital to pay off debts

  • Capital requirements scale with loan sizes you want to liquidate

Quick Setup

1. Clone the Repository

2. Configure Environment

Edit .env:

3. Fund Your Wallet

You need:

  • ETH for gas (~0.01 ETH minimum)

  • USDC to pay off loans (depends on loan sizes)

4. Run the Bot

Configuration Options

Variable
Description
Default

PRIVATE_KEY

Wallet private key

Required

RPC_URL

Base Mainnet RPC

Required

LOG_LEVEL

Logging verbosity

info

CHECK_INTERVAL_MS

Health check frequency

10000

MIN_PROFIT_USD

Minimum profit to execute

10

MAX_GAS_PRICE_GWEI

Gas price limit

100

MAX_LOAN_SIZE_USD

Largest loan to liquidate

100000

ENABLE_FLASHLOANS

Use flash loans for capital

false

Liquidation Conditions

A loan is liquidatable when either is true:

1. LTV Breach

Calculation:

2. Overdue

Profitability Check

Only execute if:

Architecture

Components

  1. Loan Monitor: Fetches active loans from indexer

  2. Health Checker: Evaluates LTV and overdue status

  3. Liquidation Executor: Executes profitable liquidations

Operating Strategies

Basic Strategy

Check all loans periodically:

Priority Strategy

Process highest-profit liquidations first:

Flash Loan Strategy

Use flash loans when you don't have capital:

Execution Flow

Step 1: Approve USDC (one-time)

Step 2: Execute Liquidation

Step 3: Verify Success

Monitoring

Logs

Metrics

Expose metrics at /metrics:

  • loans_checked_total: Loans evaluated

  • liquidations_executed_total: Successful liquidations

  • liquidations_failed_total: Failed attempts

  • profit_earned_total: Cumulative USDC profit

  • gas_spent_total: Gas in ETH

Health Checks

Risk Management

Capital Management

Strategy
Capital Required
Risk

Small loans only (< $5k)

$10,000

Low

Medium loans (< $25k)

$50,000

Medium

All loans

$100,000+

High

Competition Risks

  • Other bots may liquidate first

  • MEV bots can front-run your transactions

  • Consider using Flashbots for large liquidations

Market Risks

  • Flash crashes may make collateral worthless fast

  • Stale prices can cause circuit breaker activation

  • Network congestion can delay transactions

Troubleshooting

"Loan not liquidatable"

Possible causes:

  • LTV recovered (price moved favorably)

  • Another bot liquidated first

  • Circuit breaker is active

"Insufficient balance"

  • Add more USDC to your wallet

  • Lower MAX_LOAN_SIZE_USD

  • Enable flash loans if implemented

"Transaction reverted"

  • Loan was liquidated by competitor

  • Gas price too low (increase priority)

  • Contract paused (circuit breaker)

"Oracle price stale"

  • Wait for oracle update

  • Check L2 sequencer status

  • Verify RPC is synced

Advanced: Custom Liquidation Logic

Economics Example

Best Practices

  1. Monitor gas prices - Don't overpay during congestion

  2. Set appropriate limits - Don't liquidate loans larger than your capital

  3. Handle failures gracefully - Retry logic for transient errors

  4. Diversify strategies - Don't rely on one approach

  5. Keep logs - Debug issues and track performance

  6. Secure your keys - Use hardware wallet or KMS

Security Considerations

  1. Private Key Security

    • Never commit keys to version control

    • Use environment variables or secrets manager

    • Consider hardware wallet integration

  2. MEV Protection

    • Large liquidations attract MEV bots

    • Use private mempools (Flashbots, MEV Blocker)

    • Submit bundles when possible

  3. Capital Protection

    • Set maximum loan size limits

    • Monitor wallet balance

    • Implement circuit breakers

Next Steps

Last updated