Renamed debt fetching function, updated liquidationManager

This commit is contained in:
The3D 2020-07-07 17:14:44 +02:00
parent 5b840dd577
commit 6b2764f760
10 changed files with 132 additions and 214 deletions

View File

@ -376,11 +376,6 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
//if we reached this point, we can transfer
IERC20(_reserve).universalTransfer(msg.sender, _amount);
(uint256 stableBalance, uint256 variableBalance) = UserLogic.getUserBorrowBalances(
msg.sender,
reserve
);
emit Borrow(
_reserve,
msg.sender,
@ -405,13 +400,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
**/
struct RepayLocalVars {
uint256 stableBorrowBalance;
uint256 variableBorrowBalance;
uint256 borrowBalanceIncrease;
uint256 stableDebt;
uint256 variableDebt;
uint256 paybackAmount;
uint256 paybackAmountMinusFees;
uint256 currentStableRate;
uint256 originationFee;
}
function repay(
@ -424,7 +416,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
CoreLibrary.UserReserveData storage user = usersReserveData[_onBehalfOf][_reserve];
(vars.stableBorrowBalance, vars.variableBorrowBalance) = UserLogic.getUserBorrowBalances(
(vars.stableDebt, vars.variableDebt) = UserLogic.getUserCurrentDebt(
_onBehalfOf,
reserve
);
@ -433,8 +425,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
//default to max amount
vars.paybackAmount = rateMode == CoreLibrary.InterestRateMode.STABLE
? vars.stableBorrowBalance
: vars.variableBorrowBalance;
? vars.stableDebt
: vars.variableDebt;
if (_amount != UINT_MAX_VALUE && _amount < vars.paybackAmount) {
vars.paybackAmount = _amount;
@ -446,8 +438,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
_amount,
rateMode,
_onBehalfOf,
vars.stableBorrowBalance,
vars.variableBorrowBalance,
vars.stableDebt,
vars.variableDebt,
vars.paybackAmount,
msg.value
);
@ -493,7 +485,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
CoreLibrary.UserReserveData storage user = usersReserveData[msg.sender][_reserve];
(uint256 stableBorrowBalance, uint256 variableBorrowBalance) = UserLogic.getUserBorrowBalances(
(uint256 stableDebt, uint256 variableDebt) = UserLogic.getUserCurrentDebt(
msg.sender,
reserve
);
@ -503,8 +495,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
ValidationLogic.validateSwapRateMode(
reserve,
user,
stableBorrowBalance,
variableBorrowBalance,
stableDebt,
variableDebt,
rateMode
);
@ -512,14 +504,14 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
if (rateMode == CoreLibrary.InterestRateMode.STABLE) {
//burn stable rate tokens, mint variable rate tokens
IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender,stableBorrowBalance);
IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, stableBorrowBalance);
IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender,stableDebt);
IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, stableDebt);
} else {
//do the opposite
IVariableDebtToken(reserve.variableDebtTokenAddress).burn(msg.sender, variableBorrowBalance);
IVariableDebtToken(reserve.variableDebtTokenAddress).burn(msg.sender, variableDebt);
IStableDebtToken(reserve.stableDebtTokenAddress).mint(
msg.sender,
variableBorrowBalance,
variableDebt,
reserve.currentStableBorrowRate
);
}
@ -840,10 +832,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
view
returns (
uint256 currentATokenBalance,
uint256 currentStableBorrowBalance,
uint256 currentVariableBorrowBalance,
uint256 principalStableBorrowBalance,
uint256 principalVariableBorrowBalance,
uint256 currentStableDebt,
uint256 currentVariableDebt,
uint256 principalStableDebt,
uint256 principalVariableDebt,
uint256 stableBorrowRate,
uint256 liquidityRate,
uint256 variableBorrowIndex,
@ -854,12 +846,12 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(_user);
(currentStableBorrowBalance, currentVariableBorrowBalance) = UserLogic.getUserBorrowBalances(
(currentStableDebt, currentVariableDebt) = UserLogic.getUserCurrentDebt(
_user,
reserve
);
(principalStableBorrowBalance, principalVariableBorrowBalance) = UserLogic
.getUserPrincipalBorrowBalances(_user, reserve);
(principalStableDebt, principalVariableDebt) = UserLogic
.getUserPrincipalDebt(_user, reserve);
liquidityRate = reserve.currentLiquidityRate;
stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(_user);
stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated(

View File

@ -43,24 +43,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
uint256 constant LIQUIDATION_CLOSE_FACTOR_PERCENT = 50;
/**
* @dev emitted when a borrow fee is liquidated
* @param _collateral the address of the collateral being liquidated
* @param _reserve the address of the reserve
* @param _user the address of the user being liquidated
* @param _feeLiquidated the total fee liquidated
* @param _liquidatedCollateralForFee the amount of collateral received by the protocol in exchange for the fee
* @param _timestamp the timestamp of the action
**/
event OriginationFeeLiquidated(
address indexed _collateral,
address indexed _reserve,
address indexed _user,
uint256 _feeLiquidated,
uint256 _liquidatedCollateralForFee,
uint256 _timestamp
);
/**
* @dev emitted when a borrower is liquidated
* @param _collateral the address of the collateral being liquidated
@ -68,7 +50,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
* @param _user the address of the user being liquidated
* @param _purchaseAmount the total amount liquidated
* @param _liquidatedCollateralAmount the amount of collateral being liquidated
* @param _accruedBorrowInterest the amount of interest accrued by the borrower since the last action
* @param _liquidator the address of the liquidator
* @param _receiveAToken true if the liquidator wants to receive aTokens, false otherwise
* @param _timestamp the timestamp of the action
@ -79,7 +60,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
address indexed _user,
uint256 _purchaseAmount,
uint256 _liquidatedCollateralAmount,
uint256 _accruedBorrowInterest,
address _liquidator,
bool _receiveAToken,
uint256 _timestamp
@ -96,8 +76,8 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
struct LiquidationCallLocalVars {
uint256 userCollateralBalance;
uint256 userCompoundedBorrowBalance;
uint256 borrowBalanceIncrease;
uint256 userStableDebt;
uint256 userVariableDebt;
uint256 maxPrincipalAmountToLiquidate;
uint256 actualAmountToLiquidate;
uint256 liquidationRatio;
@ -140,7 +120,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
) external payable returns (uint256, string memory) {
CoreLibrary.ReserveData storage principalReserve = reserves[_reserve];
CoreLibrary.ReserveData storage collateralReserve = reserves[_collateral];
CoreLibrary.UserReserveData storage userPrincipal = usersReserveData[msg.sender][_reserve];
CoreLibrary.UserReserveData storage userCollateral = usersReserveData[msg
.sender][_collateral];
@ -184,9 +163,9 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
}
//if the user hasn't borrowed the specific currency defined by _reserve, it cannot be liquidated
(,vars.userCompoundedBorrowBalance) = UserLogic.getUserBorrowBalances(_user, principalReserve);
(vars.userStableDebt,vars.userVariableDebt) = UserLogic.getUserCurrentDebt(_user, principalReserve);
if (vars.userCompoundedBorrowBalance == 0) {
if (vars.userStableDebt == 0 && vars.userVariableDebt == 0) {
return (
uint256(LiquidationErrors.CURRRENCY_NOT_BORROWED),
"User did not borrow the specified currency"
@ -194,8 +173,8 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
}
//all clear - calculate the max principal amount that can be liquidated
vars.maxPrincipalAmountToLiquidate = vars
.userCompoundedBorrowBalance
vars.maxPrincipalAmountToLiquidate = vars.userStableDebt
.add(vars.userVariableDebt)
.mul(LIQUIDATION_CLOSE_FACTOR_PERCENT)
.div(100);
@ -215,7 +194,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
vars.userCollateralBalance
);
//if principalAmountNeeded < vars.ActualAmountToLiquidate, there isn't enough
//of _collateral to cover the actual amount that is being liquidated, hence we liquidate
//a smaller amount
@ -235,11 +213,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
}
}
collateralReserve.updateStateOnLiquidationAsCollateral(
_collateral,
vars.maxCollateralToLiquidate,
_receiveAToken
);
//TODO Burn debt tokens
vars.collateralAtoken = AToken(collateralReserve.aTokenAddress);
@ -260,35 +234,13 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
//transfers the principal currency to the pool
IERC20(_reserve).universalTransferFromSenderToThis(vars.actualAmountToLiquidate, true);
if (vars.feeLiquidated > 0) {
//if there is enough collateral to liquidate the fee, first transfer burn an equivalent amount of
//aTokens of the user
vars.collateralAtoken.burnOnLiquidation(_user, vars.liquidatedCollateralForFee);
//then liquidate the fee by transferring it to the fee collection address
IERC20(_collateral).universalTransfer(
addressesProvider.getTokenDistributor(),
vars.liquidatedCollateralForFee
);
emit OriginationFeeLiquidated(
_collateral,
_reserve,
_user,
vars.feeLiquidated,
vars.liquidatedCollateralForFee,
//solium-disable-next-line
block.timestamp
);
}
emit LiquidationCall(
_collateral,
_reserve,
_user,
vars.actualAmountToLiquidate,
vars.maxCollateralToLiquidate,
vars.borrowBalanceIncrease,
msg.sender,
_receiveAToken,
//solium-disable-next-line

View File

@ -41,7 +41,7 @@ library UserLogic {
_amount > IERC20(_reserve.aTokenAddress).balanceOf(_userAddress);
}
function getUserBorrowBalances(address _user,CoreLibrary.ReserveData storage _reserve)
function getUserCurrentDebt(address _user,CoreLibrary.ReserveData storage _reserve)
internal
view
returns (uint256, uint256)
@ -52,7 +52,7 @@ library UserLogic {
);
}
function getUserPrincipalBorrowBalances(address _user,CoreLibrary.ReserveData storage _reserve)
function getUserPrincipalDebt(address _user,CoreLibrary.ReserveData storage _reserve)
internal
view
returns (uint256, uint256)

View File

@ -417,8 +417,8 @@ export const repay = async (
if (sendValue) {
const valueToSend =
rateMode == RateMode.Stable
? userDataBefore.currentStableBorrowBalance
: userDataBefore.currentVariableBorrowBalance;
? userDataBefore.currentStableDebt
: userDataBefore.currentVariableDebt;
if (sendValue !== '-1') {
const valueToSend = await convertToCurrencyDecimals(reserve, sendValue);

View File

@ -31,20 +31,20 @@ export const calcExpectedUserDataAfterDeposit = (
): UserReserveData => {
const expectedUserData = <UserReserveData>{};
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedStableDebtTokenBalance(
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
userDataBeforeAction,
txTimestamp
);
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedVariableDebtTokenBalance(
expectedUserData.currentVariableDebt = expectedUserData.principalStableDebt = calcExpectedVariableDebtTokenBalance(
reserveDataBeforeAction,
userDataBeforeAction,
txTimestamp
);
expectedUserData.principalATokenBalance = userDataBeforeAction.principalStableBorrowBalance;
expectedUserData.principalVariableBorrowBalance =
userDataBeforeAction.principalVariableBorrowBalance;
expectedUserData.principalATokenBalance = userDataBeforeAction.principalStableDebt;
expectedUserData.principalVariableDebt =
userDataBeforeAction.principalVariableDebt;
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
@ -94,12 +94,12 @@ export const calcExpectedUserDataAfterDeposit = (
txTimestamp
);
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedStableDebtTokenBalance(
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
userDataBeforeAction,
txTimestamp
);
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedVariableDebtTokenBalance(
expectedUserData.currentVariableDebt = expectedUserData.principalStableDebt = calcExpectedVariableDebtTokenBalance(
reserveDataBeforeAction,
userDataBeforeAction,
txTimestamp
@ -141,20 +141,20 @@ export const calcExpectedUserDataAfterRedeem = (
amountRedeemed
);
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedStableDebtTokenBalance(
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
userDataBeforeAction,
txTimestamp
);
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedVariableDebtTokenBalance(
expectedUserData.currentVariableDebt = expectedUserData.principalStableDebt = calcExpectedVariableDebtTokenBalance(
reserveDataBeforeAction,
userDataBeforeAction,
txTimestamp
);
expectedUserData.principalStableBorrowBalance = userDataBeforeAction.principalStableBorrowBalance;
expectedUserData.principalVariableBorrowBalance =
userDataBeforeAction.principalVariableBorrowBalance;
expectedUserData.principalStableDebt = userDataBeforeAction.principalStableDebt;
expectedUserData.principalVariableDebt =
userDataBeforeAction.principalVariableDebt;
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
@ -345,7 +345,7 @@ export const calcExpectedReserveDataAfterBorrow = (
if (borrowRateMode == RateMode.Stable) {
const debtAccrued = userStableBorrowBalance.minus(
userDataBeforeAction.principalStableBorrowBalance
userDataBeforeAction.principalStableDebt
);
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
@ -363,7 +363,7 @@ export const calcExpectedReserveDataAfterBorrow = (
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable;
} else {
const debtAccrued = userVariableBorrowBalance.minus(
userDataBeforeAction.principalVariableBorrowBalance
userDataBeforeAction.principalVariableDebt
);
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable
@ -449,7 +449,7 @@ export const calcExpectedReserveDataAfterRepay = (
if (borrowRateMode == RateMode.Stable) {
const debtAccrued = userStableBorrowBalance.minus(
userDataBeforeAction.principalStableBorrowBalance
userDataBeforeAction.principalStableDebt
);
console.log(
@ -476,7 +476,7 @@ export const calcExpectedReserveDataAfterRepay = (
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable;
} else {
const debtAccrued = userVariableBorrowBalance.minus(
userDataBeforeAction.principalVariableBorrowBalance
userDataBeforeAction.principalVariableDebt
);
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
@ -543,62 +543,62 @@ export const calcExpectedUserDataAfterBorrow = (
): UserReserveData => {
const expectedUserData = <UserReserveData>{};
const currentStableBorrowBalance = calcExpectedStableDebtTokenBalance(
const currentStableDebt = calcExpectedStableDebtTokenBalance(
userDataBeforeAction,
txTimestamp
);
const currentVariableBorrowBalance = calcExpectedVariableDebtTokenBalance(
const currentVariableDebt = calcExpectedVariableDebtTokenBalance(
reserveDataBeforeAction,
userDataBeforeAction,
txTimestamp
);
if (interestRateMode == RateMode.Stable) {
const debtAccrued = currentStableBorrowBalance.minus(
userDataBeforeAction.principalStableBorrowBalance
const debtAccrued = currentStableDebt.minus(
userDataBeforeAction.principalStableDebt
);
expectedUserData.principalStableBorrowBalance = currentStableBorrowBalance.plus(amountBorrowed);
expectedUserData.principalVariableBorrowBalance =
userDataBeforeAction.principalVariableBorrowBalance;
expectedUserData.principalStableDebt = currentStableDebt.plus(amountBorrowed);
expectedUserData.principalVariableDebt =
userDataBeforeAction.principalVariableDebt;
expectedUserData.stableBorrowRate = calcExpectedUserStableRate(
userDataBeforeAction.principalStableBorrowBalance.plus(debtAccrued),
userDataBeforeAction.principalStableDebt.plus(debtAccrued),
userDataBeforeAction.stableBorrowRate,
new BigNumber(amountBorrowed),
reserveDataBeforeAction.stableBorrowRate
);
expectedUserData.stableRateLastUpdated = txTimestamp;
} else {
expectedUserData.principalVariableBorrowBalance = currentVariableBorrowBalance.plus(
expectedUserData.principalVariableDebt = currentVariableDebt.plus(
amountBorrowed
);
expectedUserData.principalStableBorrowBalance =
userDataBeforeAction.principalStableBorrowBalance;
expectedUserData.principalStableDebt =
userDataBeforeAction.principalStableDebt;
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
}
expectedUserData.currentStableBorrowBalance = calcExpectedStableDebtTokenBalance(
expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance(
{
...userDataBeforeAction,
currentStableBorrowBalance: expectedUserData.principalStableBorrowBalance,
principalStableBorrowBalance: expectedUserData.principalStableBorrowBalance,
currentStableDebt: expectedUserData.principalStableDebt,
principalStableDebt: expectedUserData.principalStableDebt,
stableBorrowRate: expectedUserData.stableBorrowRate,
stableRateLastUpdated: expectedUserData.stableRateLastUpdated,
},
currentTimestamp
);
expectedUserData.currentVariableBorrowBalance = calcExpectedVariableDebtTokenBalance(
expectedUserData.currentVariableDebt = calcExpectedVariableDebtTokenBalance(
expectedDataAfterAction,
{
...userDataBeforeAction,
currentVariableBorrowBalance: expectedUserData.principalVariableBorrowBalance,
principalVariableBorrowBalance: expectedUserData.principalVariableBorrowBalance,
currentVariableDebt: expectedUserData.principalVariableDebt,
principalVariableDebt: expectedUserData.principalVariableDebt,
variableBorrowIndex:
interestRateMode == RateMode.Variable
? expectedDataAfterAction.variableBorrowIndex
@ -607,7 +607,7 @@ export const calcExpectedUserDataAfterBorrow = (
currentTimestamp
);
if (expectedUserData.principalVariableBorrowBalance.eq(0)) {
if (expectedUserData.principalVariableDebt.eq(0)) {
expectedUserData.variableBorrowIndex = new BigNumber(0);
} else {
expectedUserData.variableBorrowIndex =
@ -676,33 +676,33 @@ export const calcExpectedUserDataAfterRepay = (
}
if (rateMode == RateMode.Stable) {
expectedUserData.principalVariableBorrowBalance =
userDataBeforeAction.principalVariableBorrowBalance;
expectedUserData.currentVariableBorrowBalance = variableBorrowBalance;
expectedUserData.principalVariableDebt =
userDataBeforeAction.principalVariableDebt;
expectedUserData.currentVariableDebt = variableBorrowBalance;
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = stableBorrowBalance.minus(
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = stableBorrowBalance.minus(
totalRepaid
);
if (expectedUserData.currentStableBorrowBalance.eq('0')) {
if (expectedUserData.currentStableDebt.eq('0')) {
//user repaid everything
expectedUserData.stableBorrowRate = expectedUserData.stableRateLastUpdated = new BigNumber(
'0'
);
}
} else {
expectedUserData.currentStableBorrowBalance = stableBorrowBalance;
expectedUserData.principalStableBorrowBalance =
userDataBeforeAction.principalStableBorrowBalance;
expectedUserData.currentStableDebt = stableBorrowBalance;
expectedUserData.principalStableDebt =
userDataBeforeAction.principalStableDebt;
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalVariableBorrowBalance = variableBorrowBalance.minus(
expectedUserData.currentVariableDebt = expectedUserData.principalVariableDebt = variableBorrowBalance.minus(
totalRepaid
);
if (expectedUserData.currentVariableBorrowBalance.eq('0')) {
if (expectedUserData.currentVariableDebt.eq('0')) {
//user repaid everything
expectedUserData.variableBorrowIndex = new BigNumber('0');
} else {
@ -785,7 +785,7 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
if (rateMode === RateMode.Stable) {
//swap user stable debt to variable
const debtAccrued = stableBorrowBalance.minus(
userDataBeforeAction.principalStableBorrowBalance
userDataBeforeAction.principalStableDebt
);
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
@ -802,16 +802,16 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
);
expectedReserveData.totalBorrowsStable = reserveDataBeforeAction.totalBorrowsStable.minus(
userDataBeforeAction.principalStableBorrowBalance
userDataBeforeAction.principalStableDebt
);
} else {
const debtAccrued = variableBorrowBalance.minus(
userDataBeforeAction.principalVariableBorrowBalance
userDataBeforeAction.principalVariableDebt
);
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable.minus(
userDataBeforeAction.principalVariableBorrowBalance
userDataBeforeAction.principalVariableDebt
);
expectedReserveData.totalBorrowsStable = reserveDataBeforeAction.totalBorrowsStable.plus(
@ -895,25 +895,25 @@ export const calcExpectedUserDataAfterSwapRateMode = (
if (rateMode === RateMode.Stable) {
// swap to variable
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = new BigNumber(
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = new BigNumber(
0
);
expectedUserData.stableBorrowRate = new BigNumber(0);
expectedUserData.principalVariableBorrowBalance = expectedUserData.currentVariableBorrowBalance = userDataBeforeAction.currentVariableBorrowBalance.plus(
expectedUserData.principalVariableDebt = expectedUserData.currentVariableDebt = userDataBeforeAction.currentVariableDebt.plus(
stableBorrowBalance
);
expectedUserData.variableBorrowIndex = expectedDataAfterAction.variableBorrowIndex;
expectedUserData.stableRateLastUpdated = new BigNumber(0);
} else {
expectedUserData.principalStableBorrowBalance = expectedUserData.currentStableBorrowBalance = userDataBeforeAction.currentStableBorrowBalance.plus(
expectedUserData.principalStableDebt = expectedUserData.currentStableDebt = userDataBeforeAction.currentStableDebt.plus(
variableBorrowBalance
);
//weighted average of the previous and the current
expectedUserData.stableBorrowRate = calcExpectedUserStableRate(
userDataBeforeAction.principalStableBorrowBalance,
userDataBeforeAction.principalStableDebt,
userDataBeforeAction.stableBorrowRate,
variableBorrowBalance,
reserveDataBeforeAction.stableBorrowRate
@ -921,7 +921,7 @@ export const calcExpectedUserDataAfterSwapRateMode = (
expectedUserData.stableRateLastUpdated = txTimestamp;
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalVariableBorrowBalance = new BigNumber(
expectedUserData.currentVariableDebt = expectedUserData.principalVariableDebt = new BigNumber(
0
);
@ -947,7 +947,7 @@ export const calcExpectedReserveDataAfterStableRateRebalance = (
const stableBorrowBalance = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp);
const debtAccrued = stableBorrowBalance.minus(userDataBeforeAction.principalStableBorrowBalance);
const debtAccrued = stableBorrowBalance.minus(userDataBeforeAction.principalStableDebt);
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
@ -965,7 +965,7 @@ export const calcExpectedReserveDataAfterStableRateRebalance = (
expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate(
avgRateBefore,
reserveDataBeforeAction.totalBorrowsStable.minus(userDataBeforeAction.principalStableBorrowBalance),
reserveDataBeforeAction.totalBorrowsStable.minus(userDataBeforeAction.principalStableDebt),
stableBorrowBalance,
reserveDataBeforeAction.stableBorrowRate
);
@ -1014,23 +1014,23 @@ export const calcExpectedUserDataAfterStableRateRebalance = (
): UserReserveData => {
const expectedUserData = {...userDataBeforeAction};
expectedUserData.principalVariableBorrowBalance = calcExpectedVariableDebtTokenBalance(
expectedUserData.principalVariableDebt = calcExpectedVariableDebtTokenBalance(
reserveDataBeforeAction,
userDataBeforeAction,
txTimestamp
);
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedStableDebtTokenBalance(
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
userDataBeforeAction,
txTimestamp
);
expectedUserData.stableRateLastUpdated = txTimestamp;
expectedUserData.principalVariableBorrowBalance =
userDataBeforeAction.principalVariableBorrowBalance;
expectedUserData.principalVariableDebt =
userDataBeforeAction.principalVariableDebt;
const debtAccrued = expectedUserData.currentStableBorrowBalance.minus(
userDataBeforeAction.principalStableBorrowBalance
const debtAccrued = expectedUserData.currentStableDebt.minus(
userDataBeforeAction.principalStableDebt
);
expectedUserData.stableBorrowRate = reserveDataBeforeAction.stableBorrowRate;
@ -1077,12 +1077,12 @@ export const calcExpectedUsersDataAfterRedirectInterest = (
const expectedFromData = {...fromDataBeforeAction};
const expectedToData = {...toDataBeforeAction};
expectedFromData.currentStableBorrowBalance = calcExpectedStableDebtTokenBalance(
expectedFromData.currentStableDebt = calcExpectedStableDebtTokenBalance(
fromDataBeforeAction,
txTimestamp
);
expectedToData.currentVariableBorrowBalance = calcExpectedVariableDebtTokenBalance(
expectedToData.currentVariableDebt = calcExpectedVariableDebtTokenBalance(
reserveDataBeforeAction,
toDataBeforeAction,
txTimestamp
@ -1253,13 +1253,13 @@ const calcExpectedVariableDebtTokenBalance = (
) => {
const debt = calcExpectedReserveNormalizedDebt(reserveDataBeforeAction, currentTimestamp);
const {principalVariableBorrowBalance, variableBorrowIndex} = userDataBeforeAction;
const {principalVariableDebt, variableBorrowIndex} = userDataBeforeAction;
if (variableBorrowIndex.eq(0)) {
return principalVariableBorrowBalance;
return principalVariableDebt;
}
return principalVariableBorrowBalance
return principalVariableDebt
.wadToRay()
.rayMul(debt)
.rayDiv(variableBorrowIndex)
@ -1271,7 +1271,7 @@ const calcExpectedStableDebtTokenBalance = (
currentTimestamp: BigNumber
) => {
const {
principalStableBorrowBalance,
principalStableDebt,
stableBorrowRate,
stableRateLastUpdated,
} = userDataBeforeAction;
@ -1281,7 +1281,7 @@ const calcExpectedStableDebtTokenBalance = (
currentTimestamp.eq(stableRateLastUpdated) ||
stableRateLastUpdated.eq(0)
) {
return principalStableBorrowBalance;
return principalStableDebt;
}
const cumulatedInterest = calcCompoundedInterest(
@ -1290,7 +1290,7 @@ const calcExpectedStableDebtTokenBalance = (
stableRateLastUpdated
);
return principalStableBorrowBalance.wadToRay().rayMul(cumulatedInterest).rayToWad();
return principalStableDebt.wadToRay().rayMul(cumulatedInterest).rayToWad();
};
const calcLinearInterest = (

View File

@ -93,10 +93,10 @@ export const getUserData = async (
redirectedBalance: new BigNumber(redirectedBalance),
currentATokenUserIndex: new BigNumber(userIndex),
currentATokenBalance: new BigNumber(userData.currentATokenBalance.toString()),
currentStableBorrowBalance: new BigNumber(userData.currentStableBorrowBalance.toString()),
currentVariableBorrowBalance: new BigNumber(userData.currentVariableBorrowBalance.toString()),
principalStableBorrowBalance: new BigNumber(userData.principalStableBorrowBalance.toString()),
principalVariableBorrowBalance: new BigNumber(userData.principalVariableBorrowBalance.toString()),
currentStableDebt: new BigNumber(userData.currentStableDebt.toString()),
currentVariableDebt: new BigNumber(userData.currentVariableDebt.toString()),
principalStableDebt: new BigNumber(userData.principalStableDebt.toString()),
principalVariableDebt: new BigNumber(userData.principalVariableDebt.toString()),
variableBorrowIndex: new BigNumber(userData.variableBorrowIndex.toString()),
stableBorrowRate: new BigNumber(userData.stableBorrowRate.toString()),
liquidityRate: new BigNumber(userData.liquidityRate.toString()),

View File

@ -7,10 +7,10 @@ export interface UserReserveData {
interestRedirectionAddress: string
redirectionAddressRedirectedBalance: BigNumber
redirectedBalance: BigNumber
currentStableBorrowBalance: BigNumber
currentVariableBorrowBalance: BigNumber
principalStableBorrowBalance: BigNumber
principalVariableBorrowBalance: BigNumber
currentStableDebt: BigNumber
currentVariableDebt: BigNumber
principalStableDebt: BigNumber
principalVariableDebt: BigNumber
variableBorrowIndex: BigNumber
liquidityRate: BigNumber
stableBorrowRate: BigNumber

View File

@ -17,7 +17,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
} = ProtocolErrors;
it('LIQUIDATION - Deposits ETH, borrows DAI/Check liquidation fails because health factor is above 1', async () => {
const {dai, users, pool, oracle} = testEnv;
const {dai, users, pool, oracle} = testEnv;
const depositor = users[0];
const borrower = users[1];
@ -120,7 +120,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
);
it('LIQUIDATION - Liquidates the borrow', async () => {
const {pool, dai, users, addressesProvider, oracle} = testEnv;
const {pool, dai, users, addressesProvider, oracle} = testEnv;
const borrower = users[1];
//mints dai to the caller
@ -135,7 +135,8 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
const daiReserveDataBefore = await pool.getReserveData(dai.address);
const ethReserveDataBefore = await pool.getReserveData(MOCK_ETH_ADDRESS);
const amountToLiquidate = new BigNumber(userReserveDataBefore.currentBorrowBalance.toString())
const amountToLiquidate = new BigNumber(userReserveDataBefore.currentStableDebt.toString())
.plus(userReserveDataBefore.currentVariableDebt.toString())
.div(2)
.toFixed(0);
@ -170,27 +171,15 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
.div(new BigNumber(collateralPrice).times(new BigNumber(10).pow(principalDecimals)))
.decimalPlaces(0, BigNumber.ROUND_DOWN);
const expectedFeeLiquidated = new BigNumber(principalPrice)
.times(new BigNumber(userReserveDataBefore.originationFee.toString()).times(105))
.times(new BigNumber(10).pow(collateralDecimals))
.div(new BigNumber(collateralPrice).times(new BigNumber(10).pow(principalDecimals)))
.div(100)
.decimalPlaces(0, BigNumber.ROUND_DOWN);
expect(userGlobalDataAfter.healthFactor).to.be.bignumber.gt(
oneEther.toFixed(0),
'Invalid health factor'
);
expect(userReserveDataAfter.originationFee).to.be.bignumber.eq(
'0',
'Origination fee should be repaid'
);
expect(feeAddressBalance).to.be.bignumber.gt('0');
expect(userReserveDataAfter.principalBorrowBalance).to.be.bignumber.almostEqual(
new BigNumber(userReserveDataBefore.currentBorrowBalance.toString())
expect(userReserveDataAfter.currentVariableDebt).to.be.bignumber.almostEqual(
new BigNumber(userReserveDataBefore.currentVariableDebt.toString())
.minus(amountToLiquidate)
.toFixed(0),
'Invalid user borrow balance after liquidation'
@ -204,9 +193,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
);
expect(ethReserveDataAfter.availableLiquidity).to.be.bignumber.almostEqual(
new BigNumber(ethReserveDataBefore.availableLiquidity.toString())
.minus(expectedFeeLiquidated)
.toFixed(0),
new BigNumber(ethReserveDataBefore.availableLiquidity.toString()).toFixed(0),
'Invalid collateral available liquidity'
);
});
@ -215,7 +202,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
'User 3 deposits 1000 USDC, user 4 1 ETH,' +
' user 4 borrows - drops HF, liquidates the borrow',
async () => {
const {users, pool, usdc, oracle, addressesProvider} = testEnv;
const {users, pool, usdc, oracle, addressesProvider} = testEnv;
const depositor = users[3];
const borrower = users[4];
//mints USDC to depositor
@ -274,7 +261,8 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
const usdcReserveDataBefore = await pool.getReserveData(usdc.address);
const ethReserveDataBefore = await pool.getReserveData(MOCK_ETH_ADDRESS);
const amountToLiquidate = new BigNumber(userReserveDataBefore.currentBorrowBalance.toString())
const amountToLiquidate = new BigNumber(userReserveDataBefore.currentStableDebt.toString())
.plus(userReserveDataBefore.currentStableDebt.toString())
.div(2)
.toFixed(0);
@ -309,23 +297,11 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
.div(new BigNumber(collateralPrice).times(new BigNumber(10).pow(principalDecimals)))
.decimalPlaces(0, BigNumber.ROUND_DOWN);
const expectedFeeLiquidated = new BigNumber(principalPrice)
.times(new BigNumber(userReserveDataBefore.originationFee.toString()).times(105))
.times(new BigNumber(10).pow(collateralDecimals))
.div(new BigNumber(collateralPrice).times(new BigNumber(10).pow(principalDecimals)))
.div(100)
.decimalPlaces(0, BigNumber.ROUND_DOWN);
expect(userGlobalDataAfter.healthFactor).to.be.bignumber.gt(
oneEther.toFixed(0),
'Invalid health factor'
);
expect(userReserveDataAfter.originationFee).to.be.bignumber.eq(
'0',
'Origination fee should be repaid'
);
expect(feeAddressBalance).to.be.bignumber.gt('0');
expect(userReserveDataAfter.principalBorrowBalance).to.be.bignumber.almostEqual(
@ -343,9 +319,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
);
expect(ethReserveDataAfter.availableLiquidity).to.be.bignumber.almostEqual(
new BigNumber(ethReserveDataBefore.availableLiquidity.toString())
.minus(expectedFeeLiquidated)
.toFixed(0),
new BigNumber(ethReserveDataBefore.availableLiquidity.toString()).toFixed(0),
'Invalid collateral available liquidity'
);
}

View File

@ -462,10 +462,10 @@ export class LendingPool extends Contract {
_user: string
): Promise<{
currentATokenBalance: BigNumber;
currentStableBorrowBalance: BigNumber;
currentVariableBorrowBalance: BigNumber;
principalStableBorrowBalance: BigNumber;
principalVariableBorrowBalance: BigNumber;
currentStableDebt: BigNumber;
currentVariableDebt: BigNumber;
principalStableDebt: BigNumber;
principalVariableDebt: BigNumber;
stableBorrowRate: BigNumber;
liquidityRate: BigNumber;
variableBorrowIndex: BigNumber;
@ -727,10 +727,10 @@ export class LendingPool extends Contract {
_user: string
): Promise<{
currentATokenBalance: BigNumber;
currentStableBorrowBalance: BigNumber;
currentVariableBorrowBalance: BigNumber;
principalStableBorrowBalance: BigNumber;
principalVariableBorrowBalance: BigNumber;
currentStableDebt: BigNumber;
currentVariableDebt: BigNumber;
principalStableDebt: BigNumber;
principalVariableDebt: BigNumber;
stableBorrowRate: BigNumber;
liquidityRate: BigNumber;
variableBorrowIndex: BigNumber;

View File

@ -900,22 +900,22 @@ const _abi = [
},
{
internalType: "uint256",
name: "currentStableBorrowBalance",
name: "currentStableDebt",
type: "uint256"
},
{
internalType: "uint256",
name: "currentVariableBorrowBalance",
name: "currentVariableDebt",
type: "uint256"
},
{
internalType: "uint256",
name: "principalStableBorrowBalance",
name: "principalStableDebt",
type: "uint256"
},
{
internalType: "uint256",
name: "principalVariableBorrowBalance",
name: "principalVariableDebt",
type: "uint256"
},
{