Module 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::liquidation_logic

@title liquidation_logic module @author Aave @notice Implements actions involving management of collateral in the protocol, the main one being the liquidations

use 0x1::event;
use 0x1::object;
use 0x1::signer;
use 0xa888dd108ea636a234c973b8b72b9cadf0c7ae94d6f355c64dde9fdc275724ee::error_config;
use 0xa888dd108ea636a234c973b8b72b9cadf0c7ae94d6f355c64dde9fdc275724ee::reserve_config;
use 0xa888dd108ea636a234c973b8b72b9cadf0c7ae94d6f355c64dde9fdc275724ee::user_config;
use 0xc1b23ac8ce67df6361b241bfa30a22c9b177fe39c471334a514eea977e6673a9::oracle;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::a_token_factory;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::emode_logic;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::events;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::fungible_asset_manager;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::generic_logic;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::isolation_mode_logic;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::pool;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::pool_logic;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::validation_logic;
use 0xd5044d9f4b80910ef7b8910a68d2ac257d14319fb9315ddce1ca5190878bc8ea::variable_debt_token_factory;
use 0xd63343bff7977cb364b17dcdcdeb64e77b7bc9b4ec252f481ace51b6de3bbd96::math_utils;
use 0xd63343bff7977cb364b17dcdcdeb64e77b7bc9b4ec252f481ace51b6de3bbd96::wad_ray_math;

Struct DeficitCreated

@notice Emitted when a deficit is created in the protocol @param user The address of the user whose debt created the deficit @param debt_asset The address of the debt asset @param amount_created The amount of deficit created

#[event]
struct DeficitCreated has drop, store

Struct LiquidationCall

@dev Emitted when a borrower is liquidated. @param collateral_asset The address of the underlying asset used as collateral, to receive as result of the liquidation @param debt_asset The address of the underlying borrowed asset to be repaid with the liquidation @param user The address of the borrower getting liquidated @param debt_to_cover The debt amount of borrowed asset the liquidator wants to cover @param liquidated_collateral_amount The amount of collateral received by the liquidator @param liquidator The address of the liquidator @param receive_a_token True if the liquidators wants to receive the collateral aTokens, false if he wants to receive the underlying collateral asset directly

#[event]
struct LiquidationCall has drop, store

Struct LiquidationCallLocalVars

@notice Local variables for liquidation call function @dev Used to avoid stack too deep errors

struct LiquidationCallLocalVars has drop

Struct ExecuteLiquidationCallParams

@notice Parameters for execute liquidation call function @dev Used to pass parameters to helper functions

struct ExecuteLiquidationCallParams has drop

Struct AvailableCollateralToLiquidateLocalVars

@notice Local variables for calculate available collateral to liquidate function @dev Used to avoid stack too deep errors

struct AvailableCollateralToLiquidateLocalVars has drop

Constants

@dev This constant represents below which health factor value it is possible to liquidate an amount of debt corresponding to MAX_LIQUIDATION_CLOSE_FACTOR. A value of 0.95e18 results in 0.95 0.95 * 10 ** 18

const CLOSE_FACTOR_HF_THRESHOLD: u256 = 950000000000000000;

@dev Default percentage of borrower's debt to be repaid in a liquidation. @dev Percentage applied when the users health factor is above CLOSE_FACTOR_HF_THRESHOLD Expressed in bps, a value of 0.5e4 results in 50.00% 5 * 10 ** 3

const DEFAULT_LIQUIDATION_CLOSE_FACTOR: u256 = 5000;

@dev This constant represents a base value threshold. If the total collateral or debt on a position is below this threshold, the close factor is raised to 100%. @notice The default value assumes that the basePrice is usd denominated by 18 decimals and needs to be adjusted in a non USD-denominated pool. 2000 * 10 ** 18, since CL's price is always 18 decimals on Aptos.

const MIN_BASE_MAX_CLOSE_FACTOR_THRESHOLD: u256 = 500000000000000000000;

@dev This constant represents the minimum amount of assets in base currency that need to be leftover after a liquidation, if not clearing a position completely. This parameter is inferred from MIN_BASE_MAX_CLOSE_FACTOR_THRESHOLD as the logic is dependent. Assuming a MIN_BASE_MAX_CLOSE_FACTOR_THRESHOLD of n a liquidation of n+1 might result in n/2 leftover which is assumed to be still economically liquidatable. This mechanic was introduced to ensure liquidators don't optimize gas by leaving some wei on the liquidation.

const MIN_LEFTOVER_BASE: u256 = 250000000000000000000;

Function liquidation_call

@notice Function to liquidate a position if its Health Factor drops below 1. The caller (liquidator) @dev Emits the LiquidationCall() event, and the DeficitCreated() event if the liquidation results in bad debt @param account The account signer of the caller @param collateral_asset The address of the underlying asset used as collateral, to receive as result of the liquidation @param debt_asset The address of the underlying borrowed asset to be repaid with the liquidation @param user The address of the borrower getting liquidated @param debt_to_cover The debt amount of borrowed asset the liquidator wants to cover @param receive_a_token True if the liquidators wants to receive the collateral aTokens, false if he wants to receive the underlying collateral asset directly

public entry fun liquidation_call(account: &signer, collateral_asset: address, debt_asset: address, user: address, debt_to_cover: u256, receive_a_token: bool)