The newest version of the Compound protocol has a different liquidation system than its predecessor. To keep the system solvent, the protocol absorbs borrower accounts that violate the collateral requirements.
Underwater borrows are repaid using the protocol’s reserves. However, if the protocol reserves are not above a governance chosen threshold, searchers can extract value after an account is absorbed.
Like most DeFi projects, Compound III requires that borrowers have a greater value of collateral locked in the protocol than the total value of their borrow at all times. This can be referred to as over-collateralization.
If a borrower accrues too much interest on their borrow, or the USD value of their collateral reduces too much, or the USD value of their borrow increases too much, the account becomes liquidatable.
Value can be extracted by any account during the liquidation process though an arbitrage opportunity. When the protocol absorbs an account, it repays its open borrow of the base asset using the protocol reserves. It also seizes the entirety of the account’s collateral assets.
By using the protocol price feed, a discounted price of the seized collateral is determined, and it is sold publicly. Any account can buy the discounted collateral using the base asset.
A liquidator can buy this discounted collateral asset, exchange it on a DEX at a higher price, and pocket the difference, in a single EVM transaction.
The following is the series of events in an on-chain liquidation:
- A block is mined such that a protocol price feed update occurs or an interest accrual occurs that renders a borrower account underwater. This can be checked at any time programmatically using the Comet isLiquidatable function, which returns a boolean.
- A searcher that detects this change creates an EVM transaction that calls functions on multiple contracts to execute the liquidation and capture value through arbitrage.
- The first function that is called is the Comet absorb function. This moves the borrower’s collateral to the protocol balance sheet and reduces reserves to cover the entire borrow.
- It is important to note that arbitrage is only possible after this point if the protocol’s reserves have been reduced below the aforementioned threshold as a result of the absorption. The values can be checked at any time using the Comet getReserves and targetReserves functions.
- Next the searcher determines the amount of the base asset that can be exchanged for seized collateral. To determine the base asset payment, the searcher can check the collateralBalanceOf the Comet contract address for each collateral asset, get its price using the price feed, and use the quoteCollateral function to find the discounted rate.
- The searcher can pay the base asset out of their own pocket or open a flash loan using another protocol. The example code opens a Uniswap flashswap of the base asset. The searcher then calls buyCollateral to purchase as much of the discounted collateral as they can.
- Once the searcher holds the discounted collateral, they can then sell it at market price using a DEX like Uniswap.
- After selling the collateral, the searcher should now hold more of the base asset than their flashswap’s required repayment. The searcher then repays the flashswap.
- The remaining balance of the base asset in the searcher account can now be sent to their hot wallet or contract.
Each and every blockchain where Compound III is deployed will require liquidators to ensure that the protocol instance remains solvent. The protocol is written in Solidity and can be deployed to any EVM chain that has a price feed, sufficient liquidity, and a means for on-chain governance.
Do not run the example code.
Disclaimer: The liquidator bot contract example in the Comet repository is simply an education tool. Running automation software that executes this contract by no means guarantees successful absorption or arbitrage.
It is highly likely that running it will result in reverted transactions and EVM gas wasted as a result of losing the battle to better running searchers (more on front-running).
Public blockchains like Ethereum are being monitored by countless searchers that are competing with one another for arbitrage opportunities. If you are not yet familiar with this concept, I recommend reading the Paradigm team’s blog post: Ethereum is a Dark Forest.
The following are the steps for running the liquidator for strictly educational purposes:
If you haven’t already, clone the Comet repository code to your local machine.
git clone https://github.com/compound-finance/comet.git cd comet/
We’ll need to install all of the dependencies using Yarn and compile the contracts to continue.
It is important to consider that this contract uses Uniswap pools for flash loans and selling of collateral. The contract will not work unless the network that it is deployed to has Uniswap pools and sufficient liquidity.
Once the contract is deployed, we’ll run a script in perpetuity that monitors the blockchain; specifically Comet accounts. When a liquidation opportunity occurs, the script will automatically call the contract’s initFlash function, which executes the liquidation.
To run the liquidation_bot script, use the command that is noted in the root directory’s package.json file. The network flag at the end of the command refers to the EVM based blockchain network that Compound III, Uniswap, and the Liquidator contract are all deployed to.
The names of networks that Comet is deployed to can be found in the deployments folder of the repository.
cd comet/ npm install -g npx LIQUIDATOR_ADDRESS="[insert liquidator contract address]" \ npx hardhat run scripts/liquidation_bot/index.ts --network mainnet
Liquidators are integral to the health of the Compound protocol. They incentivize borrowers to keep their borrows solvent. Running a successful liquidator is not an easy task. This is due to the competition of other searchers chasing after the same rewards.
Running the example code above using the public mempool and without any further code optimizations is a fool’s errand.
To rise to the level of competition that has been displayed on Ethereum, it is important to take time to research all of the most powerful tools available.
A successful searcher will run their own Ethereum node to monitor the mempool and have access to block data as soon as it becomes available. They’ll run highly optimized off and on-chain code that generates valid transactions as quickly as possible, that are able to beat out the other competing transactions in being mined.
They’ll use private mempools like the Flashbots RPC to submit their transaction directly to miners and also protect themselves from the loss of reverted transactions.
Deep explanations of MEV tools are out of the scope of this guide, but are necessary to be explored. They can be found publicly online.
Thanks for reading and be sure to get in touch in the #development room of the very active Compound Discord server. There are community developers there and they are happy to answer technical questions and share their knowledge of liquidations.