Liqudation modules: overview, code, comparing

Liqudation modules: overview, code, comparing

Lev Menchshikov(👈contacts)
Special for Oxorio

Contents:

📊 Infographics
💎 DYDX
💎 AAVE V3
💎 Compound
💎 MakerDAO
💎 Anchor
💎 BinanceUSD
📚 Additional useful resoucres

📊 Infographics

Liquidator Profits by Protocol by Month (MakerDAO, DYDX, Compound)

Monthly Active Liquidators by Protocol (iMakerDAO, DYDX, Compound)

💎DYDX

🔗 More info about open-source liquidation bot - link
🔗 Liquidator code (Github repo) - link
🔗 Exchange Whitepaper - link
🔗 Short Tokens & Leveraged Long Tokens Whitepaper - Link

◾ dYdX’s liquidation process is a bit more similar to Compound’s, but differs in that dYdX does not expose a tokenized interface to its lending protocol like how Compound does through its cTokens.

◾ Instead, dYdX creates a series of trading accounts for each address within its main Solo Margin contract, and tracks the credits and debts of each account on each market it supports.

◾ Instead of having explicit function signatures like how Maker has bite and Compound has liquidateBorrow , dYdX has a single operate function that takes in different ‘action types’, where action type number 6 liquidates a borrower’s account.

◾ The liquidator is able to purchase collateral from the borrower at 5% discount, earning the same healthy spread as Compound.

◾ dYdX provided its own proxy contract, which allows users to liquidate borrowers while keeping their accounts within a safe collateralization ratio. This has proven massively popular, with over 90% of liquidation volume going through this proxy.

◾ dYdX also differs from other protocols in that flash borrowing is built into the protocol, allowing liquidators to atomically borrow the required asset, liquidate, and repay the loan in one single transaction and without needing to use an external proxy contract.

DYDX liquidation sensitivity to price decline

💎AAVE V3

🔗 AAVE V3 Technical paper - Link (The main features and good explanation in 3.1)
🔗 AAVE V3 code (github repo) - Link

◾ In AAVE V3, the reset mechanism has been improved to be able to fully liquidate a dip when it approaches failure, i.e. HF < 0.95 (previously only half of the position could be liquidated at any time).

✨AAVE and DAI

◾ The formulas for calculating LTV and Liquidation Threshold are absolutely identical to the formulas from the StableUnit research.

◾ The main barrier to MakerDAO integration is that the liquidation module uses the Dutch auction model.

MakerDAO liquidation sensetivity to price dec;ine

✨Code overview

⚡ Contract ReserveConfiguration code can be found here - link

⚡ Contract UserConfiguration code can be found here - link



💎Compound

🔗 Whitepaper - link
🔗 Tech docs (about liquidation | borrow) - link

✨Project description

⚡ Compound offers one of the most straightforward borrowing and lending experiences in DeFi, and its liquidation process follows matches this simplicity.

⚡ Compound allows users to lend, borrow and earn interest in crypto. If you stake your tokens on the platform, you can earn lucrative interest-bearing returns (hence the name). However, instead of routing through banks, the system is completely P2P.

✨Liquidation description

◾ If a user’s borrowing balance exceeds their total collateral value (borrowing capacity) due to the value of collateral falling, or borrowed assets increasing in value, the public

can be called, which exchanges the invoking user’s asset for the borrower’s collateral, at a slightly better than market price.

Compound liquidation sensitivity to price decline

◾ Any Ethereum address that possesses the borrowed asset may invoke the liquidation function, exchanging their asset for the borrower’s cToken collateral.

◾ As both users, both assets, and prices are all contained within the Compound protocol, liquidation is frictionless and doesn't rely on any outside systems or order-books.

◾ Unlike in v1, liquidators must interact with each cToken contract in which they wish to repay a borrow and seize another asset as collateral.

◾ When collateral is seized, the liquidator is transferred cTokens, which they may redeem the same as if they had supplied the asset themselves.

◾ Users must approve each cToken contract before calling liquidate (i.e. on the borrowed asset which they are repaying), as they are transferring funds into the contract.


✨Deep explaining of liquidation process (can be skipped!)

⚡ Let’s take a look at a single liquidation in-depth.
⚡ This liquidation involves two actors, our liquidator (0x64a) who we’ll call “Alice” and our borrower (0xb5b) who we’ll call “Bob”.

◾ Bob took out a loan of USDC on Compound using ETH as collateral. This is often done to purchase more ETH using the lent USDC as a permissionless way to get leverage. Unfortunately for Bob, this loan period coincided with a large drop in the price of ETH, leaving the value of this borrower’s collateral below the required collateral ratio for ETH (133%).

◾ Because different assets have different levels of quality, price stability, and liquidity, Compound assigns a different collateralization ratio to each asset (up to 200% for REP at the moment).

◾ Alice notices that Bob is below the required collateralization ratio — presumably, by monitoring contract state or using Compound’s convenient liquidateBorrowAllowed function — and calling liquidateBorrow on Compound’s USDC market contract, triggering the liquidation process:

🚩 Compound first pays Bob any outstanding interest Bob has accrued on his collateral (which, after all, might put him over the require collateralization ratio).
🚩 Compound verifies that Bob is indeed in default using the market price from their oracles.
🚩 Compound transfers the required amount of lent asset (USDC) from Alice to the cUSDC market contract. In doing so, Alice is rewarded with Bob’s ETH collateral at a fixed discount to market price (currently, 5%). The ETH collateral is returned as cETH, allowing the liquidator to either keep earning interest on the borrower’s ETH, or redeem the cETH for ETH on Compound. In this scenario, Alice made a nice ~$7 in free ETH for her efforts.

◾ While this particular liquidator held onto the cETH, other liquidators use smart contracts to atomically redeem and sell their cTokens, locking in the 5% profit they accrue from this trade.

◾ At first glance, one might suspect that this liquidation was done manually, especially given that Compound has not released any open source liquidation bots and the mild popularity of liquidation tracking dashboards. However, when looking at the time-of-day distribution of this liquidator’s activity, we can clearly see that it’s active around the clock, and therefore, most likely a bot.

✨Comptroller

◾ Each function call is validated through a policy layer, referred to as the Comptroller; this contract validates collateral and liquidity, before allowing a user action to proceed.

◾ The Compound protocol does not support specific tokens by default; instead, markets must be whitelisted. This is accomplished with an admin function

that allows users to begin interacting with the asset. In order to borrow an asset, there must be a valid price from the Price Oracle; in order to use an asset as collateral, there must be a valid price and a collateralFactor.

✨Code review

⚡ Contract CToken code can be found here - link


ComptrollerG4 and ComptrollerG7 contracts code is here(G4) and here(G7)


💎MakerDAO

🔗 Maker's theme-related docs - here and here
🔗 The code of liquidation module is here

◾ Maker’s liquidation process occurs in two discrete steps: a bite and then a bust

◾ It’s similar to how a car might be liquidated — first it’s repossessed, and then it’s auctioned off to repay the owner’s debt. In the Maker system, repossession of a loan is triggered by calling bite and liquidation is triggered by calling bust on their set of smart contracts.

◾ You may also find the deep step-by-step analyze of liquidation process here

MakerDAO liquidation sensitivity to price decline


✨Schematics

◾ MakerDAO has a huge scheme described the hole process of liquidation with all participants, but it will take some extra time to understand it.


◾ We also need to take into account the following situation:
Liquidator(1) spent money on gas to bite the risky CDP and initiate liquidation, he actually made no profit, while Liquidator(2) was rewarded for his effort with a nice 3% discount on ETH! While there are a large number of bots that will atomically bite and bust CDPs to profit from their effort, only half of all bites involve the same bot also profiting from the liquidation.

◾ Most of the bust-less bites appear to be bots that are unable to find an appropriate price to flip the discounted ETH for upon liquidation.

◾ Here is a good GIF, step-by-step describing the liquidation in MakerDAO - link, that has been taken from this MakerDAO document.


💎Anchor

🔗Whitepaper - link
🔗Github repo - link
🔗Liquidation contract description - link
🔗Liquidation contract (github repo) - link
🔗Liquidation Queue Contract description - link
🔗Liquidation Queue Contract (github repo) - link

✨Introduction

⚡ Anchor is an algorithmic stablecoin project that is assigned to the GDP Index, which is coded as an MMU (Monetary Measurement Unit) indicator, calculated using a proprietary formula created by the CEO of the company.

⚡ If the Anchor price deviates more than 1% from the MMU value, the system issues new Anchor tokens and burns the coins if the price is too low to ensure MMU-pegged equilibrium.

⚡ The project uses a system of two tokens: the main token for ANCT settlements and the stabilization token DOCT, which cannot be transferred and used in settlements.

⚡ The liquidation module is based on instant settlements with a fixed spread. The device and operation are well described in this doc - link

✨Liquidation module description

◾ The Liquidation Contract enable users to submit Terra stablecoin bids for a Cw20-compliant token. Bidders can specify the rate of premium they will receive on bid execution. The maximum premium rate is set at 20%.

◾ Upon execution of a bid, the Cw20 tokens are sent to the bidder. The bidder's Terra stablecoins are sent to the repay address. If a repay address is not specified, the Terra stablecoins are sent sent to the message sender. The Oracle contract is responsible for providing the relevant Cw20 token prices.

◾ Additionally, the Liquidation contract serves as the point of calculation for partial collateral liquidations where a loan position is liquidated until it reaches a safe borrow_amount / borrow_limit ratio. The required liquidation amount for each collateral is calculated based on the fed-in loan position's attributes.

◾ Price data from the Oracle contract are only valid for 60 seconds price_timeframe. The Liquidation contract disables bid executions until new price data is fed into the Oracle contract.

✨Some Math formulas

◾ Partially liquidating loan positions are liquidated until the position reaches below the safe risk ratio of 0.8; loan positions with a risk ratio of 0.8 or below are considered safe from undercollateralization. Collaterals are liquidated proportionally to their locked amounts and the position's liquidation factor:

◾ Where a loan position's liquidation factor is determined as a function of the loan's total collateral value, borrow limit, and liability.

◾ The liquidation factor accounts for fees lost during bid execution (feeDeductor), such as the premium rate of bids, fees applied on bid execution, and taxes charged on native Terra transfers:

✨Multi-collateral liquidations

◾ Liquidation also applies for multi-collateral loans, which are loans that are backed with two or more collaterals. When liquidated, all of the locked collateral types are liquidated accordingly until the multi-collateral loan meets the safe risk ratio (if partial liquidation is applicable).

◾ During liquidation, collaterals in a multi-collateral loan are liquidated proportional to each collateral's collateral value, and inverse to the its maximum LTV. Collaterals with lower maximum LTVs are given a higher weight as they require a lesser amount of liquidated value to decrease the risk ratio.

✨Liquidation incentives

Due to the message-driven nature of smart contracts, liquidation can only occur when triggered by an external entity (liquidator). 1% of the liquidated value is set aside as incentives for liquidators to actively monitor risky loans and trigger liquidations.


💎BinanceUSD

Due to the stablecoin architecture can't be used as a good example. Just some options and indicators that may be used to form a general conception.

✨Indicators

Initial LTV = 65%

Margin Call = 75%

Liquidation LTV = 83%

Hourly & Daily Interest Rate = 0.001042% / 0.0250%

✨Available Collateral assets

ADA -APE - ASTR - AVAX - BCH - BNB - BTC - CAKE - CHR - CTSI - DAI - DAR -DODO - DOGE - DOT - DYDX - EOS - ETC - ETH - FIL - FTM - GALA - GMT - ILV - IOTX - JASMY - KNC - KSM - LINK - LTC - LUNA - MANA - MATIC - MBOX - MDX - NEAR- ROSE - RUNE - SAND - SLP - SOL - THETA - TLM - UNI - USDC - USDT - WAVES - XRP - ZIL

✨Loan terms

7 -14 - 30 -90 -180 days

📚Additional useful sources

🔗 "An Empirical Study of DeFi Liquidations" - link

🔗 "Liquidators: The Secret Whales Helping DeFi Function" - link

🔗 "An intro to liquidations on Anchor protocol" - link

🔗 "Anchor protocolo liquidations analysis" - link



Report Page