Liquity V2 and How Liquidation Bots Work

Blog Post

DeFi lending protocols allow anyone borrow against volatile assets, keeping exposure while unlocking liquidity. Modern systems like Bold or Orki (which is a fork of Liquity V2) even allow users to set their own collateral ratio and interest rates (after a certain threshold). Users are incentivized to keep their positions healthy, but the liquidation bots are the ones that ensure positions are closed before they create bad debt that could destabilize the entire platform.

At its core, a liquidation bot is a program that monitors troves ("positions" or "vaults" in other contexts) and liquidates them when they become undercollateralized. In volatile crypto markets, prices can swing dramatically within minutes or even seconds. Manual liquidations simply aren't fast enough to protect protocols during rapid market movements.

Troves And Collateralization Ratio

Troves are the core of Liquity V2 and they have 2 relevant parameters for liquidation bots:

  • collateral: The amount of collateral deposited
  • debt: The amount borrowed

Each trove's collateralization ratio (CR) can be calculated like this:

Collateralization Ratio = (Collateral Amount * Collateral Price) / Debt

The Minimum Collateralization Ratio (MCR) is set by the protocol and determines if a position can be liquidated. This represents the lowest acceptable ratio before a position becomes eligible for liquidation. Each protocol will set the MCR according to some parameters, such as the volatility of the collateral. For instance, if you borrow against ETH in Orki the MCR is 110%, and positions below that become eligible for liquidation. For example:

  • Day 1: You deposit 1,500 USD of ETH, borrow 1,000 USDK → CR = 150%
  • Day 2: ETH price drops 20%, your collateral is now worth 1,200 USD → CR = 120%
  • Day 3: ETH drops another 10%, collateral worth 1,080 USD → CR = 108%

Your position is now below the MCR and can be liquidated. That means you get to keep your 1,000 USDK, but you lose the collateral. A liquidation bot will be monitoring your trove and call the liquidate function. In return, it receives a small compensation for keeping the system healthy.

Monitoring Troves

Liquidation bots need to track two things: troves and collateral prices. Both can be monitored via on-chain events.

Whenever a trove changes, a TroveOperation event is emitted from the trove manager contract. The bot should maintain a local copy of trove data to quickly determine which ones are liquidatable.

Each supported collateral has one or more price oracles. These need to be monitored to detect price updates. Common oracles include Chainlink, RedStone, and Pyth. While their ABIs differ, interacting with them is conceptually similar.

In theory, the bot also needs to track interest rates for each trove because as debt increases with interest, some troves may fall below the MCR. However, in practice, most liquidations are triggered by price drops, so interest rate monitoring is less important.

Liquidations

Once we've identified one or more troves to liquidate, the bot calls the batchLiquidateTroves in the trove manager with the ids of the troves and that's it.

I mean, that's where the easy part ends. If you want your call to happen before other bots there's a lot more work to do:

Optimizing gas fees

Miners are incentivized to process transactions with higher gas fees first, so you can increase the gas fee to make sure your transaction is included before other bots, but you also need to be careful and ensure you still make a profit when paying higher fees.

Finding faster RPC APIs

Not all RPCs are alike, and being first to receive an event will make a huge difference in the time it takes for your bot to react. You should try a few. Free ones will likely not be competitive. Also make sure you use the eth_subscribe method to list for events, otherwise you may be polling and that will lead to you getting rate-limited or spending a lot of money.

Avoiding front runners

Other bots will be monitoring the mempool and seeing if they can mimic your transaction to make a profit instead of you. It's very annoying. When you can, use Flashbots.

Fine-tuning performance

I'm not sure whether language choice matters, but I'm pretty sure software design does. For instance, if your code receives an event for a drop in the price of a collateral, goes through all the troves to see which ones are liquidatable, and then liquidates the ones it finds, you're not optimizing for being the first to react. You should consider liquidating the troves you find immediately.

I used Node.js to build the bot, which allowed me to try different things very quickly, and I designed the code around streams.

There's a lot more you can do. If you are interested in diving deeper or just want to chat, do reach out on X https://x.com/gabrielgpoca.

Reply via email