Keepers are automated agents that perform critical, time-sensitive tasks in DeFi lending protocols, ensuring system health, security, and efficiency without manual intervention.
The Role of Keepers in DeFi Lending Ecosystems
Core Keeper Functions in Lending
Liquidation Execution
Automated liquidation is the primary keeper function, protecting the protocol from undercollateralized loans.
- Monitors loan-to-value (LTV) ratios in real-time against oracle prices.
- Executes liquidation auctions when collateral value falls below a set threshold, like 85% LTV.
- This function safeguards lender funds and maintains overall system solvency, preventing bad debt accumulation.
Debt Auction Management
Surplus and debt auctions rebalance the protocol's capital after liquidations.
- Sells excess collateral (surplus) from liquidations to recoup bad debt.
- Mints and auctions protocol tokens to cover any remaining system shortfalls.
- This ensures the lending pool remains fully collateralized and stable for all users, as seen in MakerDAO's system.
Interest Rate Updates
Dynamic rate adjustment uses keepers to algorithmically manage supply and demand.
- Updates borrowing and lending rates based on real-time utilization ratios of asset pools.
- For example, Aave's rates increase when pool utilization is high to attract more lenders.
- This optimizes capital efficiency and ensures competitive yields for depositors.
Health Check & Keeper Incentives
System monitoring and incentive design ensures keeper networks remain active and profitable.
- Continuously checks the health of all vaults/positions across the protocol.
- Offers liquidation bonuses (e.g., 5-10% of collateral) to incentivize timely execution.
- A robust keeper ecosystem prevents failed liquidations during high volatility, protecting user assets.
Debt Position Management
Position maintenance involves automated upkeep for complex loan types.
- Handles the accrual of interest and fees on borrowed positions over time.
- Manages the closing or adjusting of leveraged positions in protocols like Euler.
- This automation reduces user effort and ensures accurate, up-to-date accounting for all debts.
Anatomy of a Liquidation
A detailed breakdown of the automated process where Keepers liquidate undercollateralized positions in DeFi lending protocols.
Step 1: Monitoring for Under-Collateralization
Keepers continuously scan the blockchain for positions that have fallen below the required health factor.
Detailed Instructions
Keepers run specialized software or bots that poll the public mempool and query smart contracts to identify risky positions. The key metric is the Health Factor (HF), calculated as (Collateral Value * Liquidation Threshold) / Borrowed Value. A position becomes eligible for liquidation when its HF drops below 1.0 (or a protocol-specific threshold like 1.05).
- Sub-step 1: Query Protocol Contracts: The keeper calls the
getUserAccountDatafunction on a protocol like Aave to fetch a user's health factor. - Sub-step 2: Monitor Oracle Prices: The keeper simultaneously checks the latest price feeds from oracles (e.g., Chainlink's
latestAnswerfor ETH/USD) to ensure the collateral valuation is current. - Sub-step 3: Identify Targets: The bot filters all positions, flagging those where
healthFactor < liquidationThreshold.
Tip: Efficient keepers use event listeners (like
HealthFactorUpdated) instead of constant polling to save on RPC calls and gas costs.
javascript// Example query to Aave V3 on Ethereum Mainnet const userData = await aaveContract.getUserAccountData('0xUserAddress'); // userData.healthFactor is returned as a BigNumber (e.g., 1.1e18 for 1.1) if (userData.healthFactor.lt(ethers.utils.parseUnits('1.0', 18))) { // Position is undercollateralized }
Step 2: Calculating the Liquidation Bonus
Determining the profitable discount (liquidation bonus) for purchasing the collateral and the exact amount to liquidate.
Detailed Instructions
Upon finding a target, the keeper calculates the optimal liquidation amount. Protocols offer a liquidation bonus (or penalty) as an incentive, typically 5-15%, allowing the keeper to buy collateral at a discount. The calculation ensures the user's health factor is restored above the safe threshold (e.g., back to 1.1) without over-liquidating.
- Sub-step 1: Determine Close Factor: Protocols like Compound have a close factor (e.g., 50%) limiting the max borrow amount that can be liquidated in one transaction.
- Sub-step 2: Compute Debt to Cover: The keeper calculates the
debtToCoverneeded to raise the HF above the threshold, often using the formula:debtToCover = (borrowedValue * (1 - targetHF / currentHF)). - Sub-step 3: Apply Bonus: The keeper determines the
collateralSeizedamount using the bonus:collateralSeized = debtToCover / (oraclePrice * (1 - liquidationBonus)).
Tip: The liquidation bonus must outweigh the gas cost and any slippage for the transaction to be profitable.
solidity// Simplified logic from a typical liquidation function function calculateLiquidationAmount( uint256 debtValue, uint256 collateralValue, uint256 liquidationBonus // e.g., 1100 for a 10% bonus (1100/1000 = 1.1) ) internal pure returns (uint256 debtToCover, uint256 collateralSeized) { // ... calculation logic here }
Step 3: Executing the Liquidation Transaction
The keeper submits a transaction to the protocol's liquidation function, paying the debt and seizing collateral.
Detailed Instructions
The keeper constructs and broadcasts a transaction calling the protocol's specific liquidation function. On Aave V3, this is the liquidationCall() function. The transaction must be submitted with sufficient gas and a competitive gas price to outbid other keepers in the same block—a process known as MEV (Maximal Extractable Value) searcher behavior.
- Sub-step 1: Build Transaction: The keeper encodes the call with parameters: collateral asset address (e.g.,
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2for WETH), debt asset address, user address, debt to cover, andreceiveATokenflag. - Sub-step 2: Estimate Gas & Profit: The keeper simulates the transaction using
eth_estimateGasto ensure it won't revert and calculates net profit:(Collateral Discount Value) - (Gas Cost in ETH * ETH Price). - Sub-step 3: Submit to Network: The keeper sends the signed transaction, often via a private RPC or flashbots bundle to avoid front-running.
Tip: Using a private transaction relay or a service like Flashbots Protect can prevent costly failed transactions from being seen and copied by competitors.
javascript// Example transaction call to Aave V3 Pool contract const tx = await poolContract.liquidationCall( '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // WETH collateral '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC debt '0xUnsafeUserAddress', 5000000000, // debtToCover in USDC units (e.g., $5000) false // do not receive aToken );
Step 4: Post-Liquidation Settlement & Arbitrage
Finalizing the liquidation by selling seized collateral and ensuring protocol stability.
Detailed Instructions
After a successful liquidation, the keeper receives the discounted collateral tokens. The final step involves converting this collateral into the debt asset (or stablecoin) to realize the profit and repay any flash loans used. This often triggers a decentralized exchange (DEX) arbitrage loop.
- Sub-step 1: Swap Collateral for Debt Asset: The keeper immediately swaps the seized WETH for USDC on a DEX like Uniswap V3 to lock in the discount and hedge price risk. A common command is to call
swapExactTokensForTokenson a router contract. - Sub-step 2: Repay Flash Loan (if used): If the liquidation was funded via a flash loan from a protocol like Balancer or Dydx, the keeper must repay the loan plus fees within the same transaction.
- Sub-step 3: Verify Protocol Health: The keeper (or a separate monitoring service) confirms the liquidated user's health factor is now above the threshold (e.g., >1.0), restoring safety to the protocol.
Tip: The entire process—from flash loan to swap—is often bundled into a single atomic transaction using a smart contract to eliminate capital risk.
solidity// Example flow within a keeper's contract function executeLiquidation(address user) external { // 1. Take flash loan of USDC // 2. Call pool.liquidationCall(...) // 3. Swap received WETH for USDC on Uniswap // 4. Repay flash loan // 5. Send profit to keeper }
Keeper Network Models and Trade-offs
Comparison overview of keeper models for managing liquidations and health in DeFi lending protocols.
| Feature | Centralized Keeper Pool | Permissioned Keeper Network | Permissionless Open Bidding |
|---|---|---|---|
Liquidation Execution Speed | ~1-2 seconds | ~3-5 seconds | ~5-30 seconds |
Network Security Model | Trusted operator with multisig | Staked validator set (e.g., 21 nodes) | Economic bonding (e.g., 50 ETH min bond) |
Typical Fee Model | Fixed 0.5% of liquidation bonus | Dynamic auction, avg 0.3% | Open auction, often <0.1% |
Protocol Integration Complexity | Low (single API endpoint) | Medium (oracle & network consensus) | High (gas auction & MEV protection) |
Example Implementation | Aave V2 Guardian | MakerDAO Keeper Network | Compound v2 Open System |
Primary Trade-off | Speed vs. centralization risk | Reliability vs. validator collusion risk | Censorship resistance vs. latency |
Average Cost per Liquidation | $150 | $85 | $40 |
Failure Rate (Missed Liquidations) | <0.01% | <0.5% | ~2-5% |
Stakeholder Perspectives on Keepers
Understanding the Keeper's Role
Keepers are automated bots or scripts that perform essential maintenance tasks in DeFi protocols. Think of them as the diligent janitors and security guards of the digital finance world, working behind the scenes to keep everything running smoothly and safely.
Why Keepers Are Essential
- Liquidation Protection: They monitor loans on platforms like Aave and Compound. If a borrower's collateral value falls below a required threshold, a keeper instantly liquidates it to protect the lenders' funds.
- System Efficiency: They trigger time-based actions, such as harvesting yield or rebalancing investment pools in protocols like Yearn Finance, without requiring manual user intervention.
- Arbitrage Opportunities: Keepers help maintain price stability by exploiting small price differences between decentralized exchanges like Uniswap and Curve, ensuring you get fair market rates.
Real-World Example
When you deposit ETH as collateral to borrow DAI on MakerDAO, a network of keepers constantly watches the value of your ETH. If its price drops sharply, a keeper will automatically auction off your collateral to repay the loan, preventing systemic losses for the protocol and its users.
Building and Operating a Keeper
A technical guide to creating and running an automated bot to perform critical maintenance tasks in DeFi lending protocols.
Step 1: Understand the Keeper's Role and Mechanics
Learn the core functions a Keeper performs and the economic incentives.
Detailed Instructions
A Keeper is an off-chain bot that monitors on-chain conditions and executes transactions to maintain the health of a lending protocol. Its primary roles are liquidations (repaying undercollateralized loans to seize collateral) and interest rate updates (applying accrued interest to user accounts). The keeper earns a liquidation bonus or gas reimbursement for successful transactions. To be profitable, the keeper's gas costs must be lower than its rewards. You must understand the specific protocol's smart contracts, such as the Comptroller in Compound or the LendingPool in Aave, which emit events when a loan becomes eligible for liquidation (e.g., when the Health Factor drops below 1.0).
- Sub-step 1: Study the Protocol Documentation: Review the official docs for the target protocol (e.g., Aave V3, Compound V3) to identify the exact functions for liquidations and updates.
- Sub-step 2: Analyze the Smart Contracts: Use a block explorer like Etherscan to examine the contract ABI and identify key functions like
liquidateBorrow(address borrower, uint repayAmount, address cTokenCollateral). - Sub-step 3: Calculate Profitability: Model gas costs against potential rewards. A loan might have a collateral factor of 75% and a liquidation incentive of 8%, meaning you can seize $108,000 worth of collateral for repaying a $100,000 debt.
Tip: Start by monitoring protocols on testnets (like Goerli or Sepolia) to understand the event flow without financial risk.
Step 2: Set Up Your Development Environment and Infrastructure
Configure the necessary tools, nodes, and services for keeper operation.
Detailed Instructions
You need a reliable connection to the blockchain and a framework for building your bot. The core infrastructure includes an Ethereum node provider (e.g., Alchemy, Infura, or a self-hosted node), a wallet with funded ETH for gas, and a scripting environment. Most keepers are built using TypeScript/JavaScript with Ethers.js or Python with Web3.py. You will also need an event listener to monitor the blockchain for specific log emissions. For production, consider using a Keeper network like Chainlink Keepers or Gelato, which can automate transaction execution based on predefined conditions, handling gas optimization and reliability.
- Sub-step 1: Choose a Node Provider: Sign up for a service like Alchemy and obtain your HTTP WebSocket endpoint (e.g.,
wss://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY). - Sub-step 2: Set Up a Wallet: Create a new wallet using ethers and fund it. Securely store your private key in an environment variable.
javascriptconst { Wallet } = require('ethers'); const provider = new ethers.providers.WebSocketProvider(ALCHEMY_WSS); const wallet = new Wallet(process.env.PRIVATE_KEY, provider);
- Sub-step 3: Install Dependencies: Initialize a Node.js project and install
ethersanddotenv.
Tip: Use a service with high uptime and archive data access to ensure you don't miss any events. Consider setting up alerting for node health.
Step 3: Develop the Event Monitoring and Liquidation Logic
Write the code to listen for liquidation events and calculate profitable opportunities.
Detailed Instructions
The core of your keeper is the logic that filters blockchain events and decides when to act. You must listen for events like LiquidationCall (Aave) or LiquidateBorrow (Compound). When an event is detected, your bot must fetch the current state of the loan by calling view functions on the protocol contracts to verify it is still undercollateralized. Calculate the maximum repayable amount and the liquidation bonus. A critical check is the close factor, which may limit the percentage of a loan you can liquidate in one transaction (e.g., 50% in Compound). Your code must also simulate the transaction using eth_call to estimate gas and ensure profitability before broadcasting.
- Sub-step 1: Create the Event Filter: Define the filter for the specific event from the protocol's contract address.
javascriptconst liquidationFilter = { address: '0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9', // Aave V2 LendingPool topics: [ethers.utils.id('LiquidationCall(address,address,address,uint256,address,bool)')] };
- Sub-step 2: Fetch Loan Health Data: Call
getUserAccountData(userAddress)on the Aave protocol to get the current health factor. - Sub-step 3: Build and Simulate the Transaction: Construct the liquidation transaction data and use
provider.call()to simulate it, checking for errors and estimating gas.
Tip: Implement robust error handling for failed simulations and revert reasons. Use a gas estimation multiplier (e.g., 1.2x) to account for volatility.
Step 4: Implement Transaction Execution and Gas Optimization
Send the liquidation transaction with strategies to outbid competitors and minimize costs.
Detailed Instructions
Execution in a competitive keeper environment requires speed and gas optimization. You must broadcast your transaction with a competitive gas price to be included in a block before others. Use a gas station API or the provider's getFeeData() to get current base fee and priority fee (tip). Consider using a flashbots bundle or similar MEV-protected relay to submit transactions privately, preventing front-running. After sending, monitor the transaction receipt for status (1 for success, 0 for failure). If it fails, analyze the revert reason and adjust your logic. Keep detailed logs of all attempts, profits, and losses for accounting and strategy refinement.
- Sub-step 1: Set Dynamic Gas Parameters: Fetch network conditions and set a priority fee that is 10-20% above the estimated market rate to increase speed.
javascriptconst feeData = await provider.getFeeData(); const tx = { to: LENDING_POOL_ADDRESS, data: liquidateCalldata, gasLimit: 500000, maxPriorityFeePerGas: feeData.maxPriorityFeePerGas.mul(110).div(100), // +10% maxFeePerGas: feeData.maxFeePerGas };
- Sub-step 2: Submit the Transaction: Send the signed transaction using your wallet and capture the transaction hash.
- Sub-step 3: Monitor and Confirm: Wait for at least 6 block confirmations on Ethereum mainnet before considering the transaction final and logging the profit.
Tip: For time-sensitive liquidations, consider deploying your keeper logic as a smart contract on a Layer 2 or sidechain with lower latency and fees, such as Arbitrum or Optimism.
Step 5: Deploy, Monitor, and Maintain the Keeper System
Run the keeper in a production environment with ongoing monitoring and updates.
Detailed Instructions
Deploy your keeper script to a reliable server (e.g., AWS EC2, Google Cloud) or use a serverless function with a cron trigger. Ensure it runs continuously with a process manager like PM2. Implement comprehensive logging (using Winston or similar) to record all monitored events, transaction attempts, and balance changes. Set up alerting (via Discord, Telegram, or PagerDuty) for critical failures, such as the bot wallet running low on ETH for gas or the node connection dropping. Regularly update your bot to accommodate protocol upgrades (new contract deployments) and changes to economic parameters like liquidation bonuses. Periodically review your profitability and adjust your gas bidding strategy based on network congestion.
- Sub-step 1: Containerize the Application: Use Docker to create a consistent deployment environment. Define the necessary environment variables in a
docker-compose.ymlfile. - Sub-step 2: Set Up Logging and Metrics: Log all actions to a file and a service like Datadog. Track key metrics:
liquidation_opportunities_found,transactions_sent,total_profit_eth. - Sub-step 3: Create Health Checks: Implement an HTTP endpoint that returns the bot's status (wallet balance, last block processed, uptime) and integrate it with a monitoring service.
Tip: Maintain a separate 'treasury' wallet to periodically sweep profits from your operational wallet, minimizing the exposure of your capital.
Risks, Challenges, and Failure Modes
Further Reading and Tools
Ready to Start Building?
Let's bring your Web3 vision to life.
From concept to deployment, ChainScore helps you architect, build, and scale secure blockchain solutions.