Liquidations
The health of the Aave Protocol is dependant on the 'health' of the loans within the system, also known as the 'health factor'. When the 'health factor' of an account's total loans is below 1, anyone can make a liquidationCall() to the LendingPool contract, paying back part of the debt owed and receiving discounted collateral in return (also known as the liquidation bonus as listed here).
This incentivises third parties to participate in the health of the overall protocol, by acting in their own interest (to receive the discounted collateral) and as a result, ensure loans are sufficiently collateralised.
There are multiple ways to participate in liquidations:
    1.
    By calling the liquidationCall() directly in the LendingPool contract.
    2.
    By creating your own automated bot or system to liquidate loans.
For liquidation calls to be profitable, you must take into account the gas cost involved in liquidating the loan. If a high gas price is used, then the liquidation may be unprofitable for you. See the 'Calculating profitability vs gas cost' section for more details.

0. Prerequisites

When making a liquidationCall(), you must:
    Know the account (i.e. the ethereum address: user) whose health factor is below 1.
    Know the valid debt amount (debtToCover) and debt asset (debt) that can be paid.
      The close factor is 0.5, which means that only a maximum of 50% of the debt can be liquidated per valid liquidationCall().
      As mentioned here, you can set the debtToCover to uint(-1) and the protocol will proceed with the highest possible liquidation allowed by the close factor.
      You must already have a sufficient balance of the debt asset, which will be used by the liquidationCall() to pay back the debts.
    Know the collateral asset (collateral) you are closing. I.e. the collateral asset that the user has 'backing' their outstanding loan that you will partly receive as a 'bonus'.
    Whether you want to receive aTokens or the underlying asset (receiveAToken) after a successful liquidationCall().

1. Getting accounts to liquidate

Only user accounts that have a health factor below 1 can be liquidated. There are multiple ways you can get the health factor, with most of them involving 'user account data'.
"Users" in the Aave Protocol refer to a single ethereum address that has interacted with the protocol. This can be an externally owned account or contract.

On-chain

    1.
    To gather user account data from on-chain data, one way would be to monitor emitted events from the protocol and keep an up to date index of user data locally.
      1.
      Events are emitted each time a user interacts with the protocol (deposit, repay, borrow, etc). See the contract source code for relevant events.
    2.
    When you have the user's address, you can simply call getUserAccountData() to read the user's current healthFactor. If the healthFactor is below 1, then the account can be liquidated.

GraphQL

    1.
    Similarly to the sections above, you will need to gather user account data and keep an index of the user data locally.
    2.
    Since GraphQL does not provide real time calculated user data such as the healthFactor, you will need to compute this yourself. The easiest way is to use the Aave.js package, which has methods to compute summary user data.
      1.
      The data you will need to pass into the Aave.js method's can be fetched from our subgraph, namely the UserReserve objects.

2. Executing the liquidation call

Once you have the account(s) to liquidate, you will need to calculate the amount of collateral that can be liquidated:
    1.
    Use getUserReserveData() on the Protocol Data Provider contract (for Solidity) or the UserReserve object (for GraphQL) with the relevant parameters.
    2.
    Max debt that can be cleared by single liquidation call is given by the current close factor (which is 0.5 currently).
    debtToCover = (userStableDebt + userVariableDebt) * LiquidationCloseFactorPercent
      1.
      You can also pass in type(uint).max as the debtToCover in liquidationCall() to liquidate the maximum amount allowed.
    3.
    For reserves that have usageAsCollateralEnabled as true, the current liquidation bonus gives the max amount of collateral that need to be liquidated to cover debt πŸ‘‡πŸ»
    maxAmountOfCollateralToLiquidate = (debtAssetPrice * debtToCover * liquidationBonus)/ collateralPrice

Solidity

Below is an example contract. When making the liquidationCall() to the LendingPool contract, your contract must already have at least debtToCover of debt.
Liquidator.sol
ILendingPoolAddressesProvider.sol
ILendingPool.sol
1
pragma solidity ^0.6.6;
2
​
3
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
4
import "./ILendingPoolAddressesProvider.sol";
5
import "./ILendingPool.sol";
6
​
7
​
8
contract Liquidator {
9
​
10
address constant lendingPoolAddressProvider = INSERT_LENDING_POOL_ADDRESS
11
​
12
function myLiquidationFunction(
13
address _collateral,
14
address _reserve,
15
address _user,
16
uint256 _purchaseAmount,
17
bool _receiveaToken
18
)
19
external
20
{
21
ILendingPoolAddressesProvider addressProvider = ILendingPoolAddressesProvider(lendingPoolAddressProvider);
22
23
ILendingPool lendingPool = ILendingPool(addressProvider.getLendingPool());
24
25
require(IERC20(_reserve).approve(address(lendingPool), _purchaseAmount), "Approval error");
26
​
27
// Assumes this contract already has `_purchaseAmount` of `_reserve`.
28
lendingPool.liquidationCall(_collateral, _reserve, _user, _purchaseAmount, _receiveaToken);
29
}
30
}
Copied!
1
pragma solidity ^0.6.6;
2
​
3
interface ILendingPoolAddressesProvider {
4
function getLendingPool() external view returns (address);
5
}
Copied!
1
pragma solidity ^0.6.6;
2
​
3
interface ILendingPool {
4
function liquidationCall ( address _collateral, address _reserve, address _user, uint256 _purchaseAmount, bool _receiveAToken ) external payable;
5
}
Copied!

Javascript/Python

A similar call can be made with a package such as Web3.js/web.py. The account making the call must already have at least the debtToCover of debt.
JavaScript
Python
1
// Import the ABIs, see: https://docs.aave.com/developers/developing-on-aave/deployed-contract-instances
2
import DaiTokenABI from "./DAItoken.json"
3
import LendingPoolAddressesProviderABI from "./LendingPoolAddressesProvider.json"
4
import LendingPoolABI from "./LendingPool.json"
5
​
6
// ... The rest of your code ...
7
​
8
// Input variables
9
const collateralAddress = 'THE_COLLATERAL_ASSET_ADDRESS'
10
const daiAmountInWei = web3.utils.toWei("1000", "ether").toString()
11
const daiAddress = '0x6B175474E89094C44Da98b954EedeAC495271d0F' // mainnet DAI
12
const user = 'USER_ACCOUNT'
13
const receiveATokens = true
14
​
15
const lpAddressProviderAddress = '0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5' // mainnet
16
const lpAddressProviderContract = new web3.eth.Contract(LendingPoolAddressesProviderABI, lpAddressProviderAddress)
17
​
18
// Get the latest LendingPool contract address
19
const lpAddress = await lpAddressProviderContract.methods
20
.getLendingPool()
21
.call()
22
.catch((e) => {
23
throw Error(`Error getting lendingPool address: ${e.message}`)
24
})
25
​
26
// Approve the LendingPool address with the DAI contract
27
const daiContract = new web3.eth.Contract(DAITokenABI, daiAddress)
28
await daiContract.methods
29
.approve(
30
lpAddress,
31
daiAmountInWei
32
)
33
.send()
34
.catch((e) => {
35
throw Error(`Error approving DAI allowance: ${e.message}`)
36
})
37
​
38
// Make the deposit transaction via LendingPool contract
39
const lpContract = new web3.eth.Contract(LendingPoolABI, lpAddress)
40
await lpContract.methods
41
.liquidationCall(
42
collateralAddress,
43
daiAddress,
44
user,
45
daiAmountInWei,
46
receiveATokens,
47
)
48
.send()
49
.catch((e) => {
50
throw Error(`Error liquidating user with error: ${e.message}`)
51
})
Copied!
1
from web3 import Web3
2
import json
3
​
4
w3 = Web3(Web3.HTTPProvider(PROVIDER_URL))
5
​
6
def loadAbi(abi):
7
return json.load(open("./abis/%s"%(abi)))
8
9
def getContractInstance(address, abiFile):
10
return w3.eth.contract(address, abi=loadAbi(abiFile))
11
12
def liquidate(user, liquidator, amount):
13
​
14
allowance = dai.functions.allowance(user, lendingPool.address).call()
15
​
16
# Approve lendingPool to spend liquidator's funds
17
if allowance <= 0:
18
tx = dai.functions.approve(lendingPool.address, amount).transact({
19
"from": liquidator,
20
})
21
​
22
# Liquidation Call, collateral: weth and debt: dai
23
lendingPool.functions.liquidationCall(
24
weth.address,
25
dai.address,
26
user,
27
amount,
28
True
29
).transact({"from": liquidator})
30
31
dai = getContractInstance("0x6B175474E89094C44Da98b954EedeAC495271d0F", "DAI.json")
32
weth = getContractInstance("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", "WETH.json")
33
lendingPoolAddressProvider = getContractInstance("0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5", "LENDING_POOL_PROVIDER.json")
34
lendingPool = getContractInstance(
35
# Get address of latest lendingPool from lendingPoolAddressProvider
36
lendingPoolAddressProvider.functions.getLendingPool().call(),
37
"LENDING_POOL.json"
38
)
39
​
40
liquidate(alice, bob, amount)
Copied!

3. Setting up a bot

Depending on your environment, preferred programming tools and languages, your bot should:
    Ensure it has enough (or access to enough) funds when liquidating.
    Calculate the profitability of liquidating loans vs gas costs, taking into account the most lucrative collateral to liquidate.
    Ensure it has access to the latest protocol user data.
    Have the usual fail safes and security you'd expect for any production service.

Calculating profitability vs gas cost

One way to calculate the profitability is the following:
    1.
    Store and retrieve each collateral's relevant details such as address, decimals used, and liquidation bonus as listed here.
    2.
    Get the user's collateral balance (aTokenBalance).
    3.
    Get the asset's price according to the Aave's oracle contract (getAssetPrice()).
    4.
    The maximum collateral bonus you can receive will be the collateral balance (2) multiplied by the liquidation bonus (1) multiplied by the collateral asset's price in ETH (3). Note that for assets such as USDC, the number of decimals are different from other assets.
    5.
    The maximum cost of your transaction will be your gas price multiplied by the amount of gas used. You should be able to get a good estimation of the gas amount used by calling estimateGas via your web3 provider.
    6.
    Your approximate profit will be the value of the collateral bonus (4) minus the cost of your transaction (5).

Appendix

How is health factor calculated?

The health factor is calculated from: the user's collateral balance (in ETH) multiplied by the current liquidation threshold for all the user's outstanding assets, divided by 100, divided by the user's borrow balance and fees (in ETH). More info here.
This can be both calculated off-chain and on-chain, see Aave.js and the GenericLogic library contract, respectively.

How is liquidation bonus determined?

At the moment, liquidation bonuses are evaluated and determined by the risk team based on liquidity risk and updated here.
This will change in the future with Aave Protocol Governance.

Price oracles

Aave Protocol uses Chainlink as a price oracle, with a backup oracle in case of a Chainlink malfunction. See our Price Oracle section for more details.
The health factor of accounts is determined by the user's account data and the price of relevant assets, as last updated by the Price Oracle.
​
Last modified 26d ago