Liquidations
Last updated
Last updated
The health of the Phantazm 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 to the LendingPool
contract, paying back part of the debt owed and receiving discounted collateral in return (also known as the liquidation bonus as ).
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:
By calling the directly in the LendingPool contract.
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 '' section for more details.
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 , 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()
.
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'.
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.
Similarly to the sections above, you will need to gather user account data and keep an index of the user data locally.
Since GraphQL does not provide real time calculated user data such as the healthFactor
, you will need to compute this yourself.
Once you have the account(s) to liquidate, you will need to calculate the amount of collateral that can be liquidated:
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
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
Below is an example contract. When making the liquidationCall()
to the LendingPool contract, your contract must already have at least debtToCover
of debt
.
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
.
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.
One way to calculate the profitability is the following:
Store and retrieve each collateral's relevant details such as address, decimals used, and liquidation bonus.
Get the user's collateral balance (aTokenBalance
).
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.
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.
Your approximate profit will be the value of the collateral bonus (4) minus the cost of your transaction (5).
At the moment, liquidation bonuses are evaluated and determined by the risk team based on liquidity risk.
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.
Events are emitted each time a user interacts with the protocol (deposit, repay, borrow, etc). See the for relevant events.
When you have the user's address, you can simply call to read the user's current healthFactor
. If the healthFactor
is below 1, then the account can be liquidated.
The data you will need to pass into the Phantazm.js method's can be fetched from , namely the UserReserve
objects.
Use on the contract (for Solidity) or the UserReserve
object (for GraphQL) with the relevant parameters.
You can also pass in type(uint).max
as the debtToCover
in to liquidate the maximum amount allowed.
Get the asset's price according to the (getAssetPrice()
).
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). .
This can be both calculated off-chain and on-chain, see and the GenericLogic Library contract, respectively.
Phantazm Protocol uses Chainlink as a price oracle, with a backup oracle in case of a Chainlink malfunction. See our section for more details.