The DeFi liquidation game

Gerhard Wagner
Coinmonks
12 min readApr 23, 2021

--

Not all competitive multiplayer games require an APM optimized keyboard and mouse. Some are played with full nodes and mempool services.

The MEV space is ablaze with activity and has seen tremendous interest over the last couple of months. Ethereum is a dark forest, Escaping the Dark Forest, salmonella and many other stories have underlined that extracting value from the blockchain is a highly competitive, 24/7 game that is played by various individuals who hunt for high score, and projects like KeeperDAO or Flashbots, that are attempting to organize the game in new ways.

The MEV space can be broadly divided into several subcategories according to the data presented in Quantifying Blockchain Extractable Value: How dark is the forest? and the most important two by far are:

Arbitrage

Describes the process of buying an asset in one LP pool and simultaneously selling it in another LP pool at a different price to make a profit. The transaction that creates the MEV opportunity can be anything that changes the asset price in the LP pool in a significant way. For example, it could be created by a user or bot trading or someone extracting value through a liquidation or by another arbitrage opportunity. Since Uniswap/Sushiswap and other AMMs are amongst some of the most used contracts on Ethereum it’s hardly a surprise that this type of MEV is fairly common.

Liquidation

Lending protocols such as Maker, Compound and Aave have seen tremendous growth since beginning of 2020. Along with the increase in TLV, liquidations have also seen more volume especially during sudden market downturns where liquidators pay back loans of users that have fallen under their health factor. While liquidators generally employ similar MEV capturing techniques as arbitragers, like front running and back-running, MEV discovery/creation/frequency of occurring and the degree of competition can differ quite a bit. This article sheds some deeper insight into the DeFi liquidation space. We will look at techniques that liquidators use to capture MEV. We will also discuss on how to measure latency, a key metric that is used in HFT and apply it to the MEV space.

The data that was used to generate the charts has been gathered from liquidations that occurred between January 2020 and April 2021 on v1 and v2 on Aave. Liquidations that resulted in low profit (less than 1 ETH) or negative profit have been removed from the data set. The profit threshold was chosen to capture only liquidations that had sufficient economic incentives to attract multiple liquidators for the same opportunity.

If you want to refresh your memories on how liquidations in DeFi work, I recommend reading Liquidators: The Secret Whales Helping DeFi Function before you continue.

Whales vs Flashboys

Before we dive into analyzing liquidations, let’s talk about flash loans. While nowadays it’s normal to be able to borrow millions worth of crypto without providing any collateral as long as you pay it back in the same transaction, this was ground breaking and revolutionary only a year ago. What everybody remembers is the hacks like bZx 1, bZx 2 or balancer. The hacks drastically sped up the natural selection of stable and secure DeFi protocols. Yet the flashloans also had other effects on the ecosystem. For instance, they leveled the playing field for anyone participating in the MEV space. It was no longer required to be a crypto whale to go after the bigger opportunities, no suddenly everyone could do it. This increased the number of players drastically and heated up competition.

The following chart shows liquidations and classifies them based on how the liquidators source their liquidity.

  • Whale-Aave: borrowed assets from Aave
  • Whale-Compound: borrowed assets from Compound
  • Whale-Dydx: borrowed assets from DyDx
  • Whale: only interacts with Aave during liquidation and ERC20 contracts. It’s assumed that the liquidator uses own funds for the liquidation
  • Flashboys: liquidation sources to pay back the loan are from Uniswap, Sushiswap or Kyber. None of the major lending platforms is used to borrow assets
  • Unknown: source of liquidity is undetected
Source of liquidity of all liquidations in the data set

We can derive the following general trends from the data set. The flash boys have really only seen growth after Uniswap v2 was launched in May 2020 as Aave v1 was flashloan enabled but prevented users from performing a liquidation while taking a flashloan at the same time. There was not really any other place where you could flashloan from that had decent liquidity back then. While there is fluctuation between different liquidity sources it’s pretty apparent that whales have generally dominated early and mid-2020 and after that the flashboys have taken over. Factors that could have contributed to this development are:

  • Liquidity that the liquidator can source or lend against might not be enough to go after bigger opportunities.
  • Lending rates for flashloans are cheap. Uniwap and Sushiswap take 0.3%.
  • Liquidation with your own funds might be risky and lead to a loss. The worst case for a flashloan sourced liquidation is that the transaction reverts.

Latency

First, let’s cover price oracles briefly, it’s important to understand the notion of latency that is later introduced. Aave has since its launch used Chainlink oracles for asset price updates. If you are not familiar with Chainlink, imagine a bunch of providers fetching asset prices from somewhere outside the blockchain and then submitting the prices to a smart contract when prices move either up or down. As soon as enough oracle update transactions are submitted to the oracle contract a new mid price is calculated from all the submissions and the asset price is updated. This happens a lot as you can imagine and Ethereum blocks are full of price oracle updates. For an Aave user, a price decrease of one of the collateral enabled assets or a price increase of one of the borrowed assets can lead to the user becoming unhealthy or, in other words, the value of assets borrowed is getting too close to the value of the collateral. Aave needs to ensure that all loans are over-collateralized at all times and if that is not the case anymore someone else can payback the loan for that user and get the same value of available collateral plus a bonus and perform the liquidation. So what enables the liquidation from being successful is a price update that makes a user under-collateralized and we will call this event the MEV creation.

Measuring the distance between the MEV creation and the MEV extraction event (in this case liquidations) allows us to determine the latency of any attempt regardless if successful or failed. Why is this important? Isn’t a liquidator just interested in how many successful liquidations go through? The answer is yes of course the amount extracted is the high score. But measuring latency lets you understand what the optimal strategy is and measure your bot against it with every attempt that is made. It also gives you more immediate feedback on how well your bot is performing and you understand if other players are starting to outcompete you when their latency outperforms yours by a significant margin or when there is a change in the ecosystem e.g. a blazing fast new mempool service.

Let’s discuss two liquidations and the techniques that are used and measure their latency to understand a way of how it could be approached.

Technique 1: Gas Auction

This transaction liquidates user 0x67e5ab5b573f01030a1076dda7a79ef0f31a1bfa on April 18th and the liquidator pays back some of the borrowed DAI from the user and receives Aave tokens in return, sells the Aave tokens for DAI, pays back the DAI borrowed from the Uniswap pool, and then swaps the remaining Aave tokens for ETH. It’s a gas auctioning transaction where multiple liquidators bid up the gas price to be the first one to liquidate the user. Making you the first means putting your transaction as close to the MEV creation event as possible. We can find the MEV creation event by simulating the liquidation which occurred at block 12261731 and transaction index 2 and trying to execute it in previous locations. The process is something like the following:

  1. Is the liquidation still successful at block 12261731 and transaction index 1? yes
  2. Is the liquidation still successful at block 12261731 and transaction index 0? yes. Oh then the MEV creation event must be in the previous block.
  3. Is the liquidation still successful at block 12261730 and transaction index 120 (this block has 121 transactions)? yes
  4. Is liquidation still successful at block 12261730 and transaction index 95? no. Is the transaction a price oracle update? yes. Then we have found the MEV creation event which in this case updates the price of DAI.

Now that we have found the MEV creation event we can calculate the latency of the liquidation by measuring the distance between the two events. Let’s assign a latency score comprised of block and transaction latency that reveals all relevant timing-based information from the block that is available. For block latency, the block number that contains the liquidation is subtracted from the one that contains the price oracle update. For transaction latency, we measure how many transactions are between the two events. So we flatten blocks into just a single list of transactions and count how many there are to get from the creation to the extraction event. This means that there is one block between MEV creation and extraction and 28 transactions from one event to the other. Therefore we conclude that the liquidation has a latency score of 1–28.

Latency measure for gas auction liquidation

There are two other competitors that tried to liquidate the same user. 0x5Abb repriced the liquidation 4 times and ended up paying 7.98 ETH in transaction fees. 0x2CA1 gave up earlier and paid 1.5 ETH.

List of liquidation competitors

What’s interesting in this block are the first two transactions which seem to arbitrage between different liquidity pools. They reveal that the block contains transactions that have been submitted through flashbots. So the arbitrager could have taken advantage of the liquidation as well. We will cover flashbots in more detail in the later part and explain why this is the case.

Technique 2: Back-running price oracle updates

This transaction liquidates user 0xb64943f4f26d837ceeac96cae86d1bab23a3414d on April 7th. The liquidator who sent the transaction does not compete on gas price with others but rather on who can back-run and get closest after the MEV creation event in the same block. Transactions in a block are typically ordered by gas price from highest to lowest by miners. Comparing the gas price of the MEV creation and extraction events we notice that they are the same 166.1 Gwei. When we measure the latency we find that liquidation comes literally after the oracle update. This is a latency score of 0–1 (same block and 1 transaction from MEV creation to the extraction event) and it is the highest latency score possible.

Latency measure for back-running oracle price update liquidation

Let’s see how the competition did in attempting to liquidate the same user and let’s measure their latency. The table below contains all the liquidation attempts for the same user in the same block with the same gas price as the successful liquidation.

List of liquidation competitors

There is a lot going on here. There is a total of 12 other liquidation attempts from 5 different liquidators. The price oracle update at transaction index 90 is fully enveloped by liquidation attempts. There is even 2 that came in too early as you can see in row 2 and 3. Negative transaction latency score means that MEV extraction happened before the creation. It’s also striking that the majority of liquidators chose to send multiple attempts. This technique somehow reminds me of Heap Spraying, a technique that was popular in the early 2000s. It was used to achieve code execution in web browsers by putting code in various places on the heap hoping that an overwritten function pointer would later reference one of the locations and execute the code. In this case, the liquidators spam liquidation attempts timed with the MEV creation event in order to maximize their chances to get as close after it as possible.

Who is winning?

In our previous examples, we have discussed the two dominant strategies for liquidations. Who is winning — the gas auctioneers or the price oracle back runners? Going back to the concept of latency we can see that back-running price oracles achieves the highest latency and it is, therefore the optimal technique as it occurs in the same block as the MEV creation event while the gas auction strictly occurs in the next block given that no one has exploited the opportunity yet. When we think about the techniques in this way it becomes apparent that the techniques can be combined, where a liquidator can first try to back-run with the initial attempt and then if the opportunity still persists after the MEV creation event has been included in the newest block fallback to the gas auction technique.

Let’s look at the entire liquidation data set again and measure latency. The below chart reveals the block latency of liquidations grouped by occurrence in the same, the next, or after more than 1 block. We notice that latency has gradually improved over time. It is also likely that flashloans have significantly increased the number of people participating in the liquidation game from mid-2020, which in turn lead to increased competition and contributed to lower latency across liquidations. We further see that in the last few months price oracle back-running has become by far the most successful liquidation technique.

Block latency for liquidations that were successfully measured

The game is ever changing

While these techniques are successful at the moment, it does not mean that they will be in the future. One project that can potentially make all of the above techniques eventually obsolete and put the liquidation game on its head is flashbots. For those who are not familiar with the project I highly recommend checking out their Github or joining their very active community. The basic idea of flashbots is to create an off-chain coordination mechanism to decide on who can order the transactions in the next block and so capture the MEV. Searchers (basically anybody who wants to extract MEV) can propose block alternatives to a vanilla block that includes their MEV extraction as well as MEV creation transactions and basically bribe miners to chose their block (or part of their block) with a direct ETH transfer to the coinbase address. Miners are incentivized to choose the block that makes them the biggest profit. When flashbots was started only one miner participated for a while but this seems to have changed drastically (see their march update) and quite a few big miners have started to mine flashbots blocks.

One of the irst flashbots blocks that contained an Aave liquidation was mined on April 7th, paying back a USDC loan and getting WBTC in return. Looking at the first few transactions in block 12192419 it becomes apparent that this is a non-standard block:

Transaction at index 0 contains the MEV creation event. It is the price oracle update for USDC with a set gas price of 171.250001823 Gwei.

Transaction at index 1 contains the MEV extraction event. It is the liquidation with a set gas price of 0. Instead of paying the miner through gas there is a transfer to the coinbase address of 0.94 ETH that is collected by the F2Pool in this case.

The following transactions in the block look standard based on their set gas prices and their position in the block. Also there are no further coinbase transfers. It looks like that the liquidation was the only MEV extraction in the block.

One coinbase transfer at block 12192419

TL;DR

Nothing stands still, things are moving at light speed in the MEV space it seems. New players entering the game, oracle mechanics changing and 3rd party mempool services getting faster all contribute to the fact that the grounds are constantly shifting and change on which players have an edge. flashbots certainly has the potential to fundamentally change on how the liquidation game is played. If so it will also hopefully have a positive impact for regular Ethereum users as the MEV extraction coordination will happen off-chain making gas auctions and tx spamming a thing of the past. If individuals will still be able to compete in the future in this highly competitive, 24/7 multiplayer game among miners and bigger players entering the space remains to be seen.

--

--

Coinmonks
Coinmonks

Published in Coinmonks

Coinmonks is a non-profit Crypto Educational Publication.

Gerhard Wagner
Gerhard Wagner

Written by Gerhard Wagner

Bounty Hunter @immunefi | Watson @sherlockdefi

No responses yet