feat: prepared for liquidation protocol fee configuration

This commit is contained in:
The3D 2021-06-28 18:59:16 +02:00
parent 97c624e703
commit 1d67923ab5
4 changed files with 69 additions and 11 deletions

View File

@ -159,6 +159,13 @@ interface ILendingPoolConfigurator {
**/ **/
event SupplyCapChanged(address indexed asset, uint256 supplyCap); event SupplyCapChanged(address indexed asset, uint256 supplyCap);
/**
* @dev Emitted when the protocol fee on liquidation is updated
* @param asset The address of the underlying asset of the reserve
* @param fee The new fee
**/
event LiquidationProtocolFeeChanged(address indexed asset, uint256 fee);
/** /**
* @dev Emitted when the reserve decimals are updated * @dev Emitted when the reserve decimals are updated
* @param asset The address of the underlying asset of the reserve * @param asset The address of the underlying asset of the reserve
@ -387,6 +394,13 @@ interface ILendingPoolConfigurator {
**/ **/
function setSupplyCap(address asset, uint256 supplyCap) external; function setSupplyCap(address asset, uint256 supplyCap) external;
/**
* @dev Sets the protocol fee on liquidation
* @param asset The address of the underlying asset of the reserve
* @param fee The fee on liquidaton bonus
**/
function setReserveLiquidationProtocolFee(address asset, uint256 fee) external;
/** /**
* @dev Registers a new admin with rights on risk related configurations * @dev Registers a new admin with rights on risk related configurations
* @param admin The address of the admin to register * @param admin The address of the admin to register

View File

@ -40,10 +40,10 @@ contract LendingPoolCollateralManager is
using PercentageMath for uint256; using PercentageMath for uint256;
using ReserveLogic for DataTypes.ReserveCache; using ReserveLogic for DataTypes.ReserveCache;
uint256 public constant STD_LIQUIDATION_CLOSE_FACTOR = 5000; uint256 public constant DEFAULT_LIQUIDATION_CLOSE_FACTOR = 5000;
uint256 public constant MAX_LIQUIDATION_CLOSE_FACTOR = 10000; uint256 public constant MAX_LIQUIDATION_CLOSE_FACTOR = 10000;
uint256 public constant CLOSE_FACTOR_HF_THRESHOLD = 0.95 * 1e18; uint256 public constant CLOSE_FACTOR_HF_THRESHOLD = 0.98 * 1e18;
struct LiquidationCallLocalVars { struct LiquidationCallLocalVars {
uint256 userCollateralBalance; uint256 userCollateralBalance;
@ -59,6 +59,7 @@ contract LendingPoolCollateralManager is
uint256 healthFactor; uint256 healthFactor;
uint256 liquidatorPreviousATokenBalance; uint256 liquidatorPreviousATokenBalance;
uint256 closeFactor; uint256 closeFactor;
uint256 protocolFee;
IAToken collateralAtoken; IAToken collateralAtoken;
IPriceOracleGetter oracle; IPriceOracleGetter oracle;
bool isCollateralEnabled; bool isCollateralEnabled;
@ -130,7 +131,7 @@ contract LendingPoolCollateralManager is
vars.userCollateralBalance = vars.collateralAtoken.balanceOf(user); vars.userCollateralBalance = vars.collateralAtoken.balanceOf(user);
vars.closeFactor = vars.healthFactor > CLOSE_FACTOR_HF_THRESHOLD vars.closeFactor = vars.healthFactor > CLOSE_FACTOR_HF_THRESHOLD
? STD_LIQUIDATION_CLOSE_FACTOR ? DEFAULT_LIQUIDATION_CLOSE_FACTOR
: MAX_LIQUIDATION_CLOSE_FACTOR; : MAX_LIQUIDATION_CLOSE_FACTOR;
vars.maxLiquidatableDebt = vars.userStableDebt.add(vars.userVariableDebt).percentMul( vars.maxLiquidatableDebt = vars.userStableDebt.add(vars.userVariableDebt).percentMul(
@ -143,7 +144,8 @@ contract LendingPoolCollateralManager is
( (
vars.maxCollateralToLiquidate, vars.maxCollateralToLiquidate,
vars.debtAmountNeeded vars.debtAmountNeeded,
vars.protocolFee
) = _calculateAvailableCollateralToLiquidate( ) = _calculateAvailableCollateralToLiquidate(
collateralReserve, collateralReserve,
debtReserveCache, debtReserveCache,
@ -271,6 +273,9 @@ contract LendingPoolCollateralManager is
uint256 maxAmountCollateralToLiquidate; uint256 maxAmountCollateralToLiquidate;
uint256 debtAssetDecimals; uint256 debtAssetDecimals;
uint256 collateralDecimals; uint256 collateralDecimals;
uint256 collateralProtocolFee;
uint256 collateralAmount;
uint256 debtAmountNeeded;
} }
/** /**
@ -297,9 +302,7 @@ contract LendingPoolCollateralManager is
uint256 userCollateralBalance, uint256 userCollateralBalance,
IPriceOracleGetter oracle IPriceOracleGetter oracle
) internal view returns (uint256, uint256) { ) internal view returns (uint256, uint256) {
uint256 collateralAmount = 0;
uint256 debtAmountNeeded = 0;
AvailableCollateralToLiquidateLocalVars memory vars; AvailableCollateralToLiquidateLocalVars memory vars;
vars.collateralPrice = oracle.getAssetPrice(collateralAsset); vars.collateralPrice = oracle.getAssetPrice(collateralAsset);
@ -308,8 +311,10 @@ contract LendingPoolCollateralManager is
(, , vars.liquidationBonus, vars.collateralDecimals, ) = collateralReserve (, , vars.liquidationBonus, vars.collateralDecimals, ) = collateralReserve
.configuration .configuration
.getParams(); .getParams();
vars.debtAssetDecimals = debtReserveCache.reserveConfiguration.getDecimalsMemory(); vars.debtAssetDecimals = debtReserveCache.reserveConfiguration.getDecimalsMemory();
// This is the maximum possible amount of the selected collateral that can be liquidated, given the // This is the maximum possible amount of the selected collateral that can be liquidated, given the
// max amount of liquidatable debt // max amount of liquidatable debt
vars.maxAmountCollateralToLiquidate = vars vars.maxAmountCollateralToLiquidate = vars
@ -320,16 +325,16 @@ contract LendingPoolCollateralManager is
.div(vars.collateralPrice.mul(10**vars.debtAssetDecimals)); .div(vars.collateralPrice.mul(10**vars.debtAssetDecimals));
if (vars.maxAmountCollateralToLiquidate > userCollateralBalance) { if (vars.maxAmountCollateralToLiquidate > userCollateralBalance) {
collateralAmount = userCollateralBalance; vars.collateralAmount = userCollateralBalance;
debtAmountNeeded = vars vars.debtAmountNeeded = vars
.collateralPrice .collateralPrice
.mul(collateralAmount) .mul(collateralAmount)
.mul(10**vars.debtAssetDecimals) .mul(10**vars.debtAssetDecimals)
.div(vars.debtAssetPrice.mul(10**vars.collateralDecimals)) .div(vars.debtAssetPrice.mul(10**vars.collateralDecimals))
.percentDiv(vars.liquidationBonus); .percentDiv(vars.liquidationBonus);
} else { } else {
collateralAmount = vars.maxAmountCollateralToLiquidate; vars.collateralAmount = vars.maxAmountCollateralToLiquidate;
debtAmountNeeded = debtToCover; vars.debtAmountNeeded = debtToCover;
} }
return (collateralAmount, debtAmountNeeded); return (collateralAmount, debtAmountNeeded);
} }

View File

@ -442,6 +442,21 @@ contract LendingPoolConfigurator is VersionedInitializable, ILendingPoolConfigur
emit ReserveFactorChanged(asset, reserveFactor); emit ReserveFactorChanged(asset, reserveFactor);
} }
/// @inheritdoc ILendingPoolConfigurator
function setReserveLiquidationProtocolFee(address asset, uint256 fee)
external
override
onlyRiskOrPoolAdmins
{
DataTypes.ReserveConfigurationMap memory currentConfig = _pool.getConfiguration(asset);
currentConfig.setLiquidationProtocolFee(fee);
_pool.setConfiguration(asset, currentConfig.data);
emit LiquidationProtocolFeeChanged(asset, fee);
}
///@inheritdoc ILendingPoolConfigurator ///@inheritdoc ILendingPoolConfigurator
function setBorrowCap(address asset, uint256 borrowCap) external override onlyRiskOrPoolAdmins { function setBorrowCap(address asset, uint256 borrowCap) external override onlyRiskOrPoolAdmins {
DataTypes.ReserveConfigurationMap memory currentConfig = _pool.getConfiguration(asset); DataTypes.ReserveConfigurationMap memory currentConfig = _pool.getConfiguration(asset);

View File

@ -356,6 +356,30 @@ library ReserveConfiguration {
return (self.data & ~BORROW_CAP_MASK) >> BORROW_CAP_START_BIT_POSITION; return (self.data & ~BORROW_CAP_MASK) >> BORROW_CAP_START_BIT_POSITION;
} }
/**
* @dev Sets the borrow cap of the reserve
* @param self The reserve configuration
* @param borrowCap The borrow cap
**/
function setLiquidationProtocolFee(DataTypes.ReserveConfigurationMap memory self, uint256 borrowCap)
internal
pure
{
}
/**
* @dev Gets the liquidation protocol fee
* @param self The reserve configuration
* @return The liquidation protocol fee
**/
function getLiquidationProtocolFee(DataTypes.ReserveConfigurationMap storage self)
internal
view
returns (uint256)
{
return 0;
}
/** /**
* @dev Sets the supply cap of the reserve * @dev Sets the supply cap of the reserve
* @param self The reserve configuration * @param self The reserve configuration