refactor: updated the ETH references in getUserAccountData

This commit is contained in:
The3D 2021-07-16 18:21:33 +02:00
parent 76a141cd36
commit 4b9f282c9c
13 changed files with 89 additions and 81 deletions

View File

@ -366,9 +366,9 @@ interface ILendingPool {
/** /**
* @dev Returns the user account data across all the reserves * @dev Returns the user account data across all the reserves
* @param user The address of the user * @param user The address of the user
* @return totalCollateralETH the total collateral in ETH of the user * @return totalCollateralBase the total collateral of the user in the base currency used by the price feed
* @return totalDebtETH the total debt in ETH of the user * @return totalDebtBase the total debt of the user in the base currency used by the price feed
* @return availableBorrowsETH the borrowing power left of the user * @return availableBorrowsBase the borrowing power left of the user in the base currency used by the price feed
* @return currentLiquidationThreshold the liquidation threshold of the user * @return currentLiquidationThreshold the liquidation threshold of the user
* @return ltv the loan to value of the user * @return ltv the loan to value of the user
* @return healthFactor the current health factor of the user * @return healthFactor the current health factor of the user
@ -377,9 +377,9 @@ interface ILendingPool {
external external
view view
returns ( returns (
uint256 totalCollateralETH, uint256 totalCollateralBase,
uint256 totalDebtETH, uint256 totalDebtBase,
uint256 availableBorrowsETH, uint256 availableBorrowsBase,
uint256 currentLiquidationThreshold, uint256 currentLiquidationThreshold,
uint256 ltv, uint256 ltv,
uint256 healthFactor uint256 healthFactor

View File

@ -474,17 +474,17 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
view view
override override
returns ( returns (
uint256 totalCollateralETH, uint256 totalCollateralBase,
uint256 totalDebtETH, uint256 totalDebtBase,
uint256 availableBorrowsETH, uint256 availableBorrowsBase,
uint256 currentLiquidationThreshold, uint256 currentLiquidationThreshold,
uint256 ltv, uint256 ltv,
uint256 healthFactor uint256 healthFactor
) )
{ {
( (
totalCollateralETH, totalCollateralBase,
totalDebtETH, totalDebtBase,
ltv, ltv,
currentLiquidationThreshold, currentLiquidationThreshold,
healthFactor, healthFactor,
@ -498,9 +498,9 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
_addressesProvider.getPriceOracle() _addressesProvider.getPriceOracle()
); );
availableBorrowsETH = GenericLogic.calculateAvailableBorrowsETH( availableBorrowsBase = GenericLogic.calculateAvailableBorrows(
totalCollateralETH, totalCollateralBase,
totalDebtETH, totalDebtBase,
ltv ltv
); );
} }

View File

@ -32,17 +32,17 @@ library GenericLogic {
uint256 assetPrice; uint256 assetPrice;
uint256 assetUnit; uint256 assetUnit;
uint256 userBalance; uint256 userBalance;
uint256 userBalanceETH; uint256 userBalanceInBaseCurrency;
uint256 userDebt; uint256 userDebt;
uint256 userStableDebt; uint256 userStableDebt;
uint256 userDebtETH; uint256 userDebtInBaseCurrency;
uint256 decimals; uint256 decimals;
uint256 ltv; uint256 ltv;
uint256 liquidationThreshold; uint256 liquidationThreshold;
uint256 i; uint256 i;
uint256 healthFactor; uint256 healthFactor;
uint256 totalCollateralInETH; uint256 totalCollateralInBaseCurrency;
uint256 totalDebtInETH; uint256 totalDebtInBaseCurrency;
uint256 avgLtv; uint256 avgLtv;
uint256 avgUncappedLtv; uint256 avgUncappedLtv;
uint256 avgLiquidationThreshold; uint256 avgLiquidationThreshold;
@ -60,14 +60,15 @@ library GenericLogic {
/** /**
* @dev Calculates the user data across the reserves. * @dev Calculates the user data across the reserves.
* this includes the total liquidity/collateral/borrow balances in ETH, * this includes the total liquidity/collateral/borrow balances in the base currency used by the price feed,
* the average Loan To Value, the average Liquidation Ratio, and the Health factor. * the average Loan To Value, the average Liquidation Ratio, and the Health factor.
* @param user The address of the user * @param user The address of the user
* @param reservesData Data of all the reserves * @param reservesData Data of all the reserves
* @param userConfig The configuration of the user * @param userConfig The configuration of the user
* @param reserves The list of the available reserves * @param reserves The list of the available reserves
* @param oracle The price oracle address * @param oracle The price oracle address
* @return The total collateral and total debt of the user in ETH, the avg ltv, liquidation threshold, the HF and the uncapped avg ltv (without exposure ceiling) * @return The total collateral and total debt of the user in the base currency used by the price feed,
* the avg ltv, liquidation threshold, the HF and the uncapped avg ltv (without exposure ceiling)
**/ **/
function calculateUserAccountData( function calculateUserAccountData(
address user, address user,
@ -129,18 +130,20 @@ library GenericLogic {
vars.aTokenSupply = 0; vars.aTokenSupply = 0;
} }
vars.userBalanceETH = vars.assetPrice.mul(vars.userBalance).div(vars.assetUnit); vars.userBalanceInBaseCurrency = vars.assetPrice.mul(vars.userBalance).div(vars.assetUnit);
vars.totalCollateralInETH = vars.totalCollateralInETH.add(vars.userBalanceETH); vars.totalCollateralInBaseCurrency = vars.totalCollateralInBaseCurrency.add(
vars.userBalanceInBaseCurrency
);
vars.exposureCapCrossed = vars.exposureCapCrossed =
vars.exposureCap != 0 && vars.exposureCap != 0 &&
vars.aTokenSupply.div(10**vars.decimals) > vars.exposureCap; vars.aTokenSupply.div(10**vars.decimals) > vars.exposureCap;
vars.avgLtv = vars.avgLtv.add( vars.avgLtv = vars.avgLtv.add(
vars.exposureCapCrossed ? 0 : vars.userBalanceETH.mul(vars.ltv) vars.exposureCapCrossed ? 0 : vars.userBalanceInBaseCurrency.mul(vars.ltv)
); );
vars.avgUncappedLtv = vars.avgUncappedLtv.add(vars.userBalanceETH.mul(vars.ltv)); vars.avgUncappedLtv = vars.avgUncappedLtv.add(vars.userBalanceInBaseCurrency.mul(vars.ltv));
vars.avgLiquidationThreshold = vars.avgLiquidationThreshold.add( vars.avgLiquidationThreshold = vars.avgLiquidationThreshold.add(
vars.userBalanceETH.mul(vars.liquidationThreshold) vars.userBalanceInBaseCurrency.mul(vars.liquidationThreshold)
); );
} }
@ -154,27 +157,29 @@ library GenericLogic {
vars.userDebt = vars.userDebt.rayMul(vars.normalizedDebt); vars.userDebt = vars.userDebt.rayMul(vars.normalizedDebt);
} }
vars.userDebt = vars.userDebt.add(vars.userStableDebt); vars.userDebt = vars.userDebt.add(vars.userStableDebt);
vars.userDebtETH = vars.assetPrice.mul(vars.userDebt).div(vars.assetUnit); vars.userDebtInBaseCurrency = vars.assetPrice.mul(vars.userDebt).div(vars.assetUnit);
vars.totalDebtInETH = vars.totalDebtInETH.add(vars.userDebtETH); vars.totalDebtInBaseCurrency = vars.totalDebtInBaseCurrency.add(vars.userDebtInBaseCurrency);
} }
} }
vars.avgLtv = vars.totalCollateralInETH > 0 ? vars.avgLtv.div(vars.totalCollateralInETH) : 0; vars.avgLtv = vars.totalCollateralInBaseCurrency > 0
vars.avgUncappedLtv = vars.totalCollateralInETH > 0 ? vars.avgLtv.div(vars.totalCollateralInBaseCurrency)
? vars.avgUncappedLtv.div(vars.totalCollateralInETH)
: 0; : 0;
vars.avgLiquidationThreshold = vars.totalCollateralInETH > 0 vars.avgUncappedLtv = vars.totalCollateralInBaseCurrency > 0
? vars.avgLiquidationThreshold.div(vars.totalCollateralInETH) ? vars.avgUncappedLtv.div(vars.totalCollateralInBaseCurrency)
: 0;
vars.avgLiquidationThreshold = vars.totalCollateralInBaseCurrency > 0
? vars.avgLiquidationThreshold.div(vars.totalCollateralInBaseCurrency)
: 0; : 0;
vars.healthFactor = calculateHealthFactorFromBalances( vars.healthFactor = calculateHealthFactorFromBalances(
vars.totalCollateralInETH, vars.totalCollateralInBaseCurrency,
vars.totalDebtInETH, vars.totalDebtInBaseCurrency,
vars.avgLiquidationThreshold vars.avgLiquidationThreshold
); );
return ( return (
vars.totalCollateralInETH, vars.totalCollateralInBaseCurrency,
vars.totalDebtInETH, vars.totalDebtInBaseCurrency,
vars.avgLtv, vars.avgLtv,
vars.avgLiquidationThreshold, vars.avgLiquidationThreshold,
vars.healthFactor, vars.healthFactor,
@ -184,43 +189,46 @@ library GenericLogic {
/** /**
* @dev Calculates the health factor from the corresponding balances * @dev Calculates the health factor from the corresponding balances
* @param totalCollateralInETH The total collateral in ETH * @param totalCollateralInBaseCurrency The total collateral in the base currency used by the price feed
* @param totalDebtInETH The total debt in ETH * @param totalDebtInBaseCurrency The total debt in the base currency used by the price feed
* @param liquidationThreshold The avg liquidation threshold * @param liquidationThreshold The avg liquidation threshold
* @return The health factor calculated from the balances provided * @return The health factor calculated from the balances provided
**/ **/
function calculateHealthFactorFromBalances( function calculateHealthFactorFromBalances(
uint256 totalCollateralInETH, uint256 totalCollateralInBaseCurrency,
uint256 totalDebtInETH, uint256 totalDebtInBaseCurrency,
uint256 liquidationThreshold uint256 liquidationThreshold
) internal pure returns (uint256) { ) internal pure returns (uint256) {
if (totalDebtInETH == 0) return uint256(-1); if (totalDebtInBaseCurrency == 0) return uint256(-1);
return (totalCollateralInETH.percentMul(liquidationThreshold)).wadDiv(totalDebtInETH); return
(totalCollateralInBaseCurrency.percentMul(liquidationThreshold)).wadDiv(
totalDebtInBaseCurrency
);
} }
/** /**
* @dev Calculates the equivalent amount in ETH that an user can borrow, depending on the available collateral and the * @dev Calculates the maximum amount that can be borrowed depending on the available collateral, the total debt and the
* average Loan To Value * average Loan To Value
* @param totalCollateralInETH The total collateral in ETH * @param totalCollateralInBaseCurrency The total collateral in the base currency used by the price feed
* @param totalDebtInETH The total borrow balance * @param totalDebtInBaseCurrency The total borrow balance in the base currency used by the price feed
* @param ltv The average loan to value * @param ltv The average loan to value
* @return the amount available to borrow in ETH for the user * @return the amount available to borrow in the base currency of the used by the price feed
**/ **/
function calculateAvailableBorrowsETH( function calculateAvailableBorrows(
uint256 totalCollateralInETH, uint256 totalCollateralInBaseCurrency,
uint256 totalDebtInETH, uint256 totalDebtInBaseCurrency,
uint256 ltv uint256 ltv
) internal pure returns (uint256) { ) internal pure returns (uint256) {
uint256 availableBorrowsETH = totalCollateralInETH.percentMul(ltv); uint256 availableBorrowsInBaseCurrency = totalCollateralInBaseCurrency.percentMul(ltv);
if (availableBorrowsETH < totalDebtInETH) { if (availableBorrowsInBaseCurrency < totalDebtInBaseCurrency) {
return 0; return 0;
} }
availableBorrowsETH = availableBorrowsETH.sub(totalDebtInETH); availableBorrowsInBaseCurrency = availableBorrowsInBaseCurrency.sub(totalDebtInBaseCurrency);
return availableBorrowsETH; return availableBorrowsInBaseCurrency;
} }
/** /**

View File

@ -92,16 +92,16 @@ library ValidationLogic {
struct ValidateBorrowLocalVars { struct ValidateBorrowLocalVars {
uint256 currentLtv; uint256 currentLtv;
uint256 currentLiquidationThreshold; uint256 currentLiquidationThreshold;
uint256 amountOfCollateralNeededETH; uint256 collateralNeededInBaseCurrency;
uint256 userCollateralBalanceETH; uint256 userCollateralInBaseCurrency;
uint256 userBorrowBalanceETH; uint256 userDebtInBaseCurrency;
uint256 availableLiquidity; uint256 availableLiquidity;
uint256 healthFactor; uint256 healthFactor;
uint256 totalDebt; uint256 totalDebt;
uint256 totalSupplyVariableDebt; uint256 totalSupplyVariableDebt;
uint256 reserveDecimals; uint256 reserveDecimals;
uint256 borrowCap; uint256 borrowCap;
uint256 amountInETH; uint256 amountInBaseCurrency;
bool isActive; bool isActive;
bool isFrozen; bool isFrozen;
bool isPaused; bool isPaused;
@ -181,8 +181,8 @@ library ValidationLogic {
} }
( (
vars.userCollateralBalanceETH, vars.userCollateralInBaseCurrency,
vars.userBorrowBalanceETH, vars.userDebtInBaseCurrency,
vars.currentLtv, vars.currentLtv,
vars.currentLiquidationThreshold, vars.currentLiquidationThreshold,
vars.healthFactor, vars.healthFactor,
@ -196,23 +196,23 @@ library ValidationLogic {
oracle oracle
); );
require(vars.userCollateralBalanceETH > 0, Errors.VL_COLLATERAL_BALANCE_IS_0); require(vars.userCollateralInBaseCurrency > 0, Errors.VL_COLLATERAL_BALANCE_IS_0);
require( require(
vars.healthFactor > GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD, vars.healthFactor > GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD,
Errors.VL_HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD Errors.VL_HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD
); );
vars.amountInETH = IPriceOracleGetter(oracle).getAssetPrice(asset); vars.amountInBaseCurrency = IPriceOracleGetter(oracle).getAssetPrice(asset);
vars.amountInETH = vars.amountInETH.mul(amount).div(10**vars.reserveDecimals); vars.amountInBaseCurrency = vars.amountInBaseCurrency.mul(amount).div(10**vars.reserveDecimals);
//add the current already borrowed amount to the amount requested to calculate the total collateral needed. //add the current already borrowed amount to the amount requested to calculate the total collateral needed.
vars.amountOfCollateralNeededETH = vars.userBorrowBalanceETH.add(vars.amountInETH).percentDiv( vars.collateralNeededInBaseCurrency = vars.userDebtInBaseCurrency.add(vars.amountInBaseCurrency).percentDiv(
vars.currentLtv vars.currentLtv
); //LTV is calculated in percentage ); //LTV is calculated in percentage
require( require(
vars.amountOfCollateralNeededETH <= vars.userCollateralBalanceETH, vars.collateralNeededInBaseCurrency <= vars.userCollateralInBaseCurrency,
Errors.VL_COLLATERAL_CANNOT_COVER_NEW_BORROW Errors.VL_COLLATERAL_CANNOT_COVER_NEW_BORROW
); );

View File

@ -100,9 +100,9 @@ contract LendingPoolHarnessForVariableDebtToken is ILendingPool {
view view
override override
returns ( returns (
uint256 totalCollateralETH, uint256 totalCollateralBase,
uint256 totalDebtETH, uint256 totalDebtBase,
uint256 availableBorrowsETH, uint256 availableBorrowsBase,
uint256 currentLiquidationThreshold, uint256 currentLiquidationThreshold,
uint256 ltv, uint256 ltv,
uint256 healthFactor uint256 healthFactor

View File

@ -55,7 +55,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
const amountDAIToBorrow = await convertToCurrencyDecimals( const amountDAIToBorrow = await convertToCurrencyDecimals(
dai.address, dai.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(daiPrice.toString()) .div(daiPrice.toString())
.multipliedBy(0.95) .multipliedBy(0.95)
.toFixed(0) .toFixed(0)
@ -269,7 +269,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
const amountUSDCToBorrow = await convertToCurrencyDecimals( const amountUSDCToBorrow = await convertToCurrencyDecimals(
usdc.address, usdc.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(usdcPrice.toString()) .div(usdcPrice.toString())
.multipliedBy(0.9502) .multipliedBy(0.9502)
.toFixed(0) .toFixed(0)

View File

@ -83,7 +83,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset',
const amountDAIToBorrow = await convertToCurrencyDecimals( const amountDAIToBorrow = await convertToCurrencyDecimals(
dai.address, dai.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(daiPrice.toString()) .div(daiPrice.toString())
.multipliedBy(0.95) .multipliedBy(0.95)
.toFixed(0) .toFixed(0)
@ -267,7 +267,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset',
const amountUSDCToBorrow = await convertToCurrencyDecimals( const amountUSDCToBorrow = await convertToCurrencyDecimals(
usdc.address, usdc.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(usdcPrice.toString()) .div(usdcPrice.toString())
.multipliedBy(0.9502) .multipliedBy(0.9502)
.toFixed(0) .toFixed(0)

View File

@ -224,7 +224,7 @@ makeSuite('Pausable Pool', (testEnv: TestEnv) => {
const amountUSDCToBorrow = await convertToCurrencyDecimals( const amountUSDCToBorrow = await convertToCurrencyDecimals(
usdc.address, usdc.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(usdcPrice.toString()) .div(usdcPrice.toString())
.multipliedBy(0.9502) .multipliedBy(0.9502)
.toFixed(0) .toFixed(0)

View File

@ -224,7 +224,7 @@ makeSuite('Pause One Reserve', (testEnv: TestEnv) => {
const amountUSDCToBorrow = await convertToCurrencyDecimals( const amountUSDCToBorrow = await convertToCurrencyDecimals(
usdc.address, usdc.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(usdcPrice.toString()) .div(usdcPrice.toString())
.multipliedBy(0.9502) .multipliedBy(0.9502)
.toFixed(0) .toFixed(0)

View File

@ -61,7 +61,7 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
const amountDAIToBorrow = await convertToCurrencyDecimals( const amountDAIToBorrow = await convertToCurrencyDecimals(
dai.address, dai.address,
new BigNumber(userGlobalDataBefore.availableBorrowsETH.toString()) new BigNumber(userGlobalDataBefore.availableBorrowsBase.toString())
.div(daiPrice.toString()) .div(daiPrice.toString())
.multipliedBy(0.95) .multipliedBy(0.95)
.toFixed(0) .toFixed(0)
@ -128,7 +128,7 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
const amountDAIToBorrow = await convertToCurrencyDecimals( const amountDAIToBorrow = await convertToCurrencyDecimals(
dai.address, dai.address,
new BigNumber(userGlobalDataBefore.availableBorrowsETH.toString()) new BigNumber(userGlobalDataBefore.availableBorrowsBase.toString())
.div(daiPrice.toString()) .div(daiPrice.toString())
.multipliedBy(0.8) .multipliedBy(0.8)
.toFixed(0) .toFixed(0)
@ -141,7 +141,7 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
const userGlobalDataBefore2 = await pool.getUserAccountData(borrower.address); const userGlobalDataBefore2 = await pool.getUserAccountData(borrower.address);
const amountWETHToBorrow = new BigNumber(userGlobalDataBefore2.availableBorrowsETH.toString()) const amountWETHToBorrow = new BigNumber(userGlobalDataBefore2.availableBorrowsBase.toString())
.multipliedBy(0.8) .multipliedBy(0.8)
.toFixed(0); .toFixed(0);

View File

@ -55,7 +55,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
const amountDAIToBorrow = await convertToCurrencyDecimals( const amountDAIToBorrow = await convertToCurrencyDecimals(
dai.address, dai.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(daiPrice.toString()) .div(daiPrice.toString())
.multipliedBy(0.95) .multipliedBy(0.95)
.toFixed(0) .toFixed(0)
@ -269,7 +269,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
const amountUSDCToBorrow = await convertToCurrencyDecimals( const amountUSDCToBorrow = await convertToCurrencyDecimals(
usdc.address, usdc.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(usdcPrice.toString()) .div(usdcPrice.toString())
.multipliedBy(0.9502) .multipliedBy(0.9502)
.toFixed(0) .toFixed(0)

View File

@ -83,7 +83,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset',
const amountDAIToBorrow = await convertToCurrencyDecimals( const amountDAIToBorrow = await convertToCurrencyDecimals(
dai.address, dai.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(daiPrice.toString()) .div(daiPrice.toString())
.multipliedBy(0.95) .multipliedBy(0.95)
.toFixed(0) .toFixed(0)
@ -267,7 +267,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset',
const amountUSDCToBorrow = await convertToCurrencyDecimals( const amountUSDCToBorrow = await convertToCurrencyDecimals(
usdc.address, usdc.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(usdcPrice.toString()) .div(usdcPrice.toString())
.multipliedBy(0.9502) .multipliedBy(0.9502)
.toFixed(0) .toFixed(0)

View File

@ -224,7 +224,7 @@ makeSuite('Pausable Pool', (testEnv: TestEnv) => {
const amountUSDCToBorrow = await convertToCurrencyDecimals( const amountUSDCToBorrow = await convertToCurrencyDecimals(
usdc.address, usdc.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString()) new BigNumber(userGlobalData.availableBorrowsBase.toString())
.div(usdcPrice.toString()) .div(usdcPrice.toString())
.multipliedBy(0.9502) .multipliedBy(0.9502)
.toFixed(0) .toFixed(0)