mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
Renamed debt fetching function, updated liquidationManager
This commit is contained in:
parent
5b840dd577
commit
6b2764f760
|
@ -376,11 +376,6 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
//if we reached this point, we can transfer
|
//if we reached this point, we can transfer
|
||||||
IERC20(_reserve).universalTransfer(msg.sender, _amount);
|
IERC20(_reserve).universalTransfer(msg.sender, _amount);
|
||||||
|
|
||||||
(uint256 stableBalance, uint256 variableBalance) = UserLogic.getUserBorrowBalances(
|
|
||||||
msg.sender,
|
|
||||||
reserve
|
|
||||||
);
|
|
||||||
|
|
||||||
emit Borrow(
|
emit Borrow(
|
||||||
_reserve,
|
_reserve,
|
||||||
msg.sender,
|
msg.sender,
|
||||||
|
@ -405,13 +400,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
**/
|
**/
|
||||||
|
|
||||||
struct RepayLocalVars {
|
struct RepayLocalVars {
|
||||||
uint256 stableBorrowBalance;
|
uint256 stableDebt;
|
||||||
uint256 variableBorrowBalance;
|
uint256 variableDebt;
|
||||||
uint256 borrowBalanceIncrease;
|
|
||||||
uint256 paybackAmount;
|
uint256 paybackAmount;
|
||||||
uint256 paybackAmountMinusFees;
|
|
||||||
uint256 currentStableRate;
|
uint256 currentStableRate;
|
||||||
uint256 originationFee;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function repay(
|
function repay(
|
||||||
|
@ -424,7 +416,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
|
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
|
||||||
CoreLibrary.UserReserveData storage user = usersReserveData[_onBehalfOf][_reserve];
|
CoreLibrary.UserReserveData storage user = usersReserveData[_onBehalfOf][_reserve];
|
||||||
|
|
||||||
(vars.stableBorrowBalance, vars.variableBorrowBalance) = UserLogic.getUserBorrowBalances(
|
(vars.stableDebt, vars.variableDebt) = UserLogic.getUserCurrentDebt(
|
||||||
_onBehalfOf,
|
_onBehalfOf,
|
||||||
reserve
|
reserve
|
||||||
);
|
);
|
||||||
|
@ -433,8 +425,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
|
|
||||||
//default to max amount
|
//default to max amount
|
||||||
vars.paybackAmount = rateMode == CoreLibrary.InterestRateMode.STABLE
|
vars.paybackAmount = rateMode == CoreLibrary.InterestRateMode.STABLE
|
||||||
? vars.stableBorrowBalance
|
? vars.stableDebt
|
||||||
: vars.variableBorrowBalance;
|
: vars.variableDebt;
|
||||||
|
|
||||||
if (_amount != UINT_MAX_VALUE && _amount < vars.paybackAmount) {
|
if (_amount != UINT_MAX_VALUE && _amount < vars.paybackAmount) {
|
||||||
vars.paybackAmount = _amount;
|
vars.paybackAmount = _amount;
|
||||||
|
@ -446,8 +438,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
_amount,
|
_amount,
|
||||||
rateMode,
|
rateMode,
|
||||||
_onBehalfOf,
|
_onBehalfOf,
|
||||||
vars.stableBorrowBalance,
|
vars.stableDebt,
|
||||||
vars.variableBorrowBalance,
|
vars.variableDebt,
|
||||||
vars.paybackAmount,
|
vars.paybackAmount,
|
||||||
msg.value
|
msg.value
|
||||||
);
|
);
|
||||||
|
@ -493,7 +485,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
|
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
|
||||||
CoreLibrary.UserReserveData storage user = usersReserveData[msg.sender][_reserve];
|
CoreLibrary.UserReserveData storage user = usersReserveData[msg.sender][_reserve];
|
||||||
|
|
||||||
(uint256 stableBorrowBalance, uint256 variableBorrowBalance) = UserLogic.getUserBorrowBalances(
|
(uint256 stableDebt, uint256 variableDebt) = UserLogic.getUserCurrentDebt(
|
||||||
msg.sender,
|
msg.sender,
|
||||||
reserve
|
reserve
|
||||||
);
|
);
|
||||||
|
@ -503,8 +495,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
ValidationLogic.validateSwapRateMode(
|
ValidationLogic.validateSwapRateMode(
|
||||||
reserve,
|
reserve,
|
||||||
user,
|
user,
|
||||||
stableBorrowBalance,
|
stableDebt,
|
||||||
variableBorrowBalance,
|
variableDebt,
|
||||||
rateMode
|
rateMode
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -512,14 +504,14 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
|
|
||||||
if (rateMode == CoreLibrary.InterestRateMode.STABLE) {
|
if (rateMode == CoreLibrary.InterestRateMode.STABLE) {
|
||||||
//burn stable rate tokens, mint variable rate tokens
|
//burn stable rate tokens, mint variable rate tokens
|
||||||
IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender,stableBorrowBalance);
|
IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender,stableDebt);
|
||||||
IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, stableBorrowBalance);
|
IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, stableDebt);
|
||||||
} else {
|
} else {
|
||||||
//do the opposite
|
//do the opposite
|
||||||
IVariableDebtToken(reserve.variableDebtTokenAddress).burn(msg.sender, variableBorrowBalance);
|
IVariableDebtToken(reserve.variableDebtTokenAddress).burn(msg.sender, variableDebt);
|
||||||
IStableDebtToken(reserve.stableDebtTokenAddress).mint(
|
IStableDebtToken(reserve.stableDebtTokenAddress).mint(
|
||||||
msg.sender,
|
msg.sender,
|
||||||
variableBorrowBalance,
|
variableDebt,
|
||||||
reserve.currentStableBorrowRate
|
reserve.currentStableBorrowRate
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -840,10 +832,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
view
|
view
|
||||||
returns (
|
returns (
|
||||||
uint256 currentATokenBalance,
|
uint256 currentATokenBalance,
|
||||||
uint256 currentStableBorrowBalance,
|
uint256 currentStableDebt,
|
||||||
uint256 currentVariableBorrowBalance,
|
uint256 currentVariableDebt,
|
||||||
uint256 principalStableBorrowBalance,
|
uint256 principalStableDebt,
|
||||||
uint256 principalVariableBorrowBalance,
|
uint256 principalVariableDebt,
|
||||||
uint256 stableBorrowRate,
|
uint256 stableBorrowRate,
|
||||||
uint256 liquidityRate,
|
uint256 liquidityRate,
|
||||||
uint256 variableBorrowIndex,
|
uint256 variableBorrowIndex,
|
||||||
|
@ -854,12 +846,12 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
|
||||||
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
|
CoreLibrary.ReserveData storage reserve = reserves[_reserve];
|
||||||
|
|
||||||
currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(_user);
|
currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(_user);
|
||||||
(currentStableBorrowBalance, currentVariableBorrowBalance) = UserLogic.getUserBorrowBalances(
|
(currentStableDebt, currentVariableDebt) = UserLogic.getUserCurrentDebt(
|
||||||
_user,
|
_user,
|
||||||
reserve
|
reserve
|
||||||
);
|
);
|
||||||
(principalStableBorrowBalance, principalVariableBorrowBalance) = UserLogic
|
(principalStableDebt, principalVariableDebt) = UserLogic
|
||||||
.getUserPrincipalBorrowBalances(_user, reserve);
|
.getUserPrincipalDebt(_user, reserve);
|
||||||
liquidityRate = reserve.currentLiquidityRate;
|
liquidityRate = reserve.currentLiquidityRate;
|
||||||
stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(_user);
|
stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(_user);
|
||||||
stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated(
|
stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated(
|
||||||
|
|
|
@ -43,24 +43,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
||||||
|
|
||||||
uint256 constant LIQUIDATION_CLOSE_FACTOR_PERCENT = 50;
|
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
|
* @dev emitted when a borrower is liquidated
|
||||||
* @param _collateral the address of the collateral being 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 _user the address of the user being liquidated
|
||||||
* @param _purchaseAmount the total amount liquidated
|
* @param _purchaseAmount the total amount liquidated
|
||||||
* @param _liquidatedCollateralAmount the amount of collateral being 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 _liquidator the address of the liquidator
|
||||||
* @param _receiveAToken true if the liquidator wants to receive aTokens, false otherwise
|
* @param _receiveAToken true if the liquidator wants to receive aTokens, false otherwise
|
||||||
* @param _timestamp the timestamp of the action
|
* @param _timestamp the timestamp of the action
|
||||||
|
@ -79,7 +60,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
||||||
address indexed _user,
|
address indexed _user,
|
||||||
uint256 _purchaseAmount,
|
uint256 _purchaseAmount,
|
||||||
uint256 _liquidatedCollateralAmount,
|
uint256 _liquidatedCollateralAmount,
|
||||||
uint256 _accruedBorrowInterest,
|
|
||||||
address _liquidator,
|
address _liquidator,
|
||||||
bool _receiveAToken,
|
bool _receiveAToken,
|
||||||
uint256 _timestamp
|
uint256 _timestamp
|
||||||
|
@ -96,8 +76,8 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
||||||
|
|
||||||
struct LiquidationCallLocalVars {
|
struct LiquidationCallLocalVars {
|
||||||
uint256 userCollateralBalance;
|
uint256 userCollateralBalance;
|
||||||
uint256 userCompoundedBorrowBalance;
|
uint256 userStableDebt;
|
||||||
uint256 borrowBalanceIncrease;
|
uint256 userVariableDebt;
|
||||||
uint256 maxPrincipalAmountToLiquidate;
|
uint256 maxPrincipalAmountToLiquidate;
|
||||||
uint256 actualAmountToLiquidate;
|
uint256 actualAmountToLiquidate;
|
||||||
uint256 liquidationRatio;
|
uint256 liquidationRatio;
|
||||||
|
@ -140,7 +120,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
||||||
) external payable returns (uint256, string memory) {
|
) external payable returns (uint256, string memory) {
|
||||||
CoreLibrary.ReserveData storage principalReserve = reserves[_reserve];
|
CoreLibrary.ReserveData storage principalReserve = reserves[_reserve];
|
||||||
CoreLibrary.ReserveData storage collateralReserve = reserves[_collateral];
|
CoreLibrary.ReserveData storage collateralReserve = reserves[_collateral];
|
||||||
CoreLibrary.UserReserveData storage userPrincipal = usersReserveData[msg.sender][_reserve];
|
|
||||||
CoreLibrary.UserReserveData storage userCollateral = usersReserveData[msg
|
CoreLibrary.UserReserveData storage userCollateral = usersReserveData[msg
|
||||||
.sender][_collateral];
|
.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
|
//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 (
|
return (
|
||||||
uint256(LiquidationErrors.CURRRENCY_NOT_BORROWED),
|
uint256(LiquidationErrors.CURRRENCY_NOT_BORROWED),
|
||||||
"User did not borrow the specified currency"
|
"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
|
//all clear - calculate the max principal amount that can be liquidated
|
||||||
vars.maxPrincipalAmountToLiquidate = vars
|
vars.maxPrincipalAmountToLiquidate = vars.userStableDebt
|
||||||
.userCompoundedBorrowBalance
|
.add(vars.userVariableDebt)
|
||||||
.mul(LIQUIDATION_CLOSE_FACTOR_PERCENT)
|
.mul(LIQUIDATION_CLOSE_FACTOR_PERCENT)
|
||||||
.div(100);
|
.div(100);
|
||||||
|
|
||||||
|
@ -215,7 +194,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
||||||
vars.userCollateralBalance
|
vars.userCollateralBalance
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
//if principalAmountNeeded < vars.ActualAmountToLiquidate, there isn't enough
|
//if principalAmountNeeded < vars.ActualAmountToLiquidate, there isn't enough
|
||||||
//of _collateral to cover the actual amount that is being liquidated, hence we liquidate
|
//of _collateral to cover the actual amount that is being liquidated, hence we liquidate
|
||||||
//a smaller amount
|
//a smaller amount
|
||||||
|
@ -235,11 +213,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
collateralReserve.updateStateOnLiquidationAsCollateral(
|
//TODO Burn debt tokens
|
||||||
_collateral,
|
|
||||||
vars.maxCollateralToLiquidate,
|
|
||||||
_receiveAToken
|
|
||||||
);
|
|
||||||
|
|
||||||
vars.collateralAtoken = AToken(collateralReserve.aTokenAddress);
|
vars.collateralAtoken = AToken(collateralReserve.aTokenAddress);
|
||||||
|
|
||||||
|
@ -260,35 +234,13 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
||||||
//transfers the principal currency to the pool
|
//transfers the principal currency to the pool
|
||||||
IERC20(_reserve).universalTransferFromSenderToThis(vars.actualAmountToLiquidate, true);
|
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(
|
emit LiquidationCall(
|
||||||
_collateral,
|
_collateral,
|
||||||
_reserve,
|
_reserve,
|
||||||
_user,
|
_user,
|
||||||
vars.actualAmountToLiquidate,
|
vars.actualAmountToLiquidate,
|
||||||
vars.maxCollateralToLiquidate,
|
vars.maxCollateralToLiquidate,
|
||||||
vars.borrowBalanceIncrease,
|
|
||||||
msg.sender,
|
msg.sender,
|
||||||
_receiveAToken,
|
_receiveAToken,
|
||||||
//solium-disable-next-line
|
//solium-disable-next-line
|
||||||
|
|
|
@ -41,7 +41,7 @@ library UserLogic {
|
||||||
_amount > IERC20(_reserve.aTokenAddress).balanceOf(_userAddress);
|
_amount > IERC20(_reserve.aTokenAddress).balanceOf(_userAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUserBorrowBalances(address _user,CoreLibrary.ReserveData storage _reserve)
|
function getUserCurrentDebt(address _user,CoreLibrary.ReserveData storage _reserve)
|
||||||
internal
|
internal
|
||||||
view
|
view
|
||||||
returns (uint256, uint256)
|
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
|
internal
|
||||||
view
|
view
|
||||||
returns (uint256, uint256)
|
returns (uint256, uint256)
|
||||||
|
|
|
@ -417,8 +417,8 @@ export const repay = async (
|
||||||
if (sendValue) {
|
if (sendValue) {
|
||||||
const valueToSend =
|
const valueToSend =
|
||||||
rateMode == RateMode.Stable
|
rateMode == RateMode.Stable
|
||||||
? userDataBefore.currentStableBorrowBalance
|
? userDataBefore.currentStableDebt
|
||||||
: userDataBefore.currentVariableBorrowBalance;
|
: userDataBefore.currentVariableDebt;
|
||||||
|
|
||||||
if (sendValue !== '-1') {
|
if (sendValue !== '-1') {
|
||||||
const valueToSend = await convertToCurrencyDecimals(reserve, sendValue);
|
const valueToSend = await convertToCurrencyDecimals(reserve, sendValue);
|
||||||
|
|
|
@ -31,20 +31,20 @@ export const calcExpectedUserDataAfterDeposit = (
|
||||||
): UserReserveData => {
|
): UserReserveData => {
|
||||||
const expectedUserData = <UserReserveData>{};
|
const expectedUserData = <UserReserveData>{};
|
||||||
|
|
||||||
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedVariableDebtTokenBalance(
|
expectedUserData.currentVariableDebt = expectedUserData.principalStableDebt = calcExpectedVariableDebtTokenBalance(
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.principalATokenBalance = userDataBeforeAction.principalStableBorrowBalance;
|
expectedUserData.principalATokenBalance = userDataBeforeAction.principalStableDebt;
|
||||||
expectedUserData.principalVariableBorrowBalance =
|
expectedUserData.principalVariableDebt =
|
||||||
userDataBeforeAction.principalVariableBorrowBalance;
|
userDataBeforeAction.principalVariableDebt;
|
||||||
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
|
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
|
||||||
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
||||||
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
|
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
|
||||||
|
@ -94,12 +94,12 @@ export const calcExpectedUserDataAfterDeposit = (
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedVariableDebtTokenBalance(
|
expectedUserData.currentVariableDebt = expectedUserData.principalStableDebt = calcExpectedVariableDebtTokenBalance(
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
|
@ -141,20 +141,20 @@ export const calcExpectedUserDataAfterRedeem = (
|
||||||
amountRedeemed
|
amountRedeemed
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedVariableDebtTokenBalance(
|
expectedUserData.currentVariableDebt = expectedUserData.principalStableDebt = calcExpectedVariableDebtTokenBalance(
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.principalStableBorrowBalance = userDataBeforeAction.principalStableBorrowBalance;
|
expectedUserData.principalStableDebt = userDataBeforeAction.principalStableDebt;
|
||||||
expectedUserData.principalVariableBorrowBalance =
|
expectedUserData.principalVariableDebt =
|
||||||
userDataBeforeAction.principalVariableBorrowBalance;
|
userDataBeforeAction.principalVariableDebt;
|
||||||
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
|
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
|
||||||
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
||||||
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
|
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
|
||||||
|
@ -345,7 +345,7 @@ export const calcExpectedReserveDataAfterBorrow = (
|
||||||
|
|
||||||
if (borrowRateMode == RateMode.Stable) {
|
if (borrowRateMode == RateMode.Stable) {
|
||||||
const debtAccrued = userStableBorrowBalance.minus(
|
const debtAccrued = userStableBorrowBalance.minus(
|
||||||
userDataBeforeAction.principalStableBorrowBalance
|
userDataBeforeAction.principalStableDebt
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
||||||
|
@ -363,7 +363,7 @@ export const calcExpectedReserveDataAfterBorrow = (
|
||||||
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable;
|
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable;
|
||||||
} else {
|
} else {
|
||||||
const debtAccrued = userVariableBorrowBalance.minus(
|
const debtAccrued = userVariableBorrowBalance.minus(
|
||||||
userDataBeforeAction.principalVariableBorrowBalance
|
userDataBeforeAction.principalVariableDebt
|
||||||
);
|
);
|
||||||
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
||||||
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable
|
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable
|
||||||
|
@ -449,7 +449,7 @@ export const calcExpectedReserveDataAfterRepay = (
|
||||||
|
|
||||||
if (borrowRateMode == RateMode.Stable) {
|
if (borrowRateMode == RateMode.Stable) {
|
||||||
const debtAccrued = userStableBorrowBalance.minus(
|
const debtAccrued = userStableBorrowBalance.minus(
|
||||||
userDataBeforeAction.principalStableBorrowBalance
|
userDataBeforeAction.principalStableDebt
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
|
@ -476,7 +476,7 @@ export const calcExpectedReserveDataAfterRepay = (
|
||||||
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable;
|
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable;
|
||||||
} else {
|
} else {
|
||||||
const debtAccrued = userVariableBorrowBalance.minus(
|
const debtAccrued = userVariableBorrowBalance.minus(
|
||||||
userDataBeforeAction.principalVariableBorrowBalance
|
userDataBeforeAction.principalVariableDebt
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
||||||
|
@ -543,62 +543,62 @@ export const calcExpectedUserDataAfterBorrow = (
|
||||||
): UserReserveData => {
|
): UserReserveData => {
|
||||||
const expectedUserData = <UserReserveData>{};
|
const expectedUserData = <UserReserveData>{};
|
||||||
|
|
||||||
const currentStableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
const currentStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
const currentVariableBorrowBalance = calcExpectedVariableDebtTokenBalance(
|
const currentVariableDebt = calcExpectedVariableDebtTokenBalance(
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
if (interestRateMode == RateMode.Stable) {
|
if (interestRateMode == RateMode.Stable) {
|
||||||
const debtAccrued = currentStableBorrowBalance.minus(
|
const debtAccrued = currentStableDebt.minus(
|
||||||
userDataBeforeAction.principalStableBorrowBalance
|
userDataBeforeAction.principalStableDebt
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.principalStableBorrowBalance = currentStableBorrowBalance.plus(amountBorrowed);
|
expectedUserData.principalStableDebt = currentStableDebt.plus(amountBorrowed);
|
||||||
expectedUserData.principalVariableBorrowBalance =
|
expectedUserData.principalVariableDebt =
|
||||||
userDataBeforeAction.principalVariableBorrowBalance;
|
userDataBeforeAction.principalVariableDebt;
|
||||||
|
|
||||||
expectedUserData.stableBorrowRate = calcExpectedUserStableRate(
|
expectedUserData.stableBorrowRate = calcExpectedUserStableRate(
|
||||||
userDataBeforeAction.principalStableBorrowBalance.plus(debtAccrued),
|
userDataBeforeAction.principalStableDebt.plus(debtAccrued),
|
||||||
userDataBeforeAction.stableBorrowRate,
|
userDataBeforeAction.stableBorrowRate,
|
||||||
new BigNumber(amountBorrowed),
|
new BigNumber(amountBorrowed),
|
||||||
reserveDataBeforeAction.stableBorrowRate
|
reserveDataBeforeAction.stableBorrowRate
|
||||||
);
|
);
|
||||||
expectedUserData.stableRateLastUpdated = txTimestamp;
|
expectedUserData.stableRateLastUpdated = txTimestamp;
|
||||||
} else {
|
} else {
|
||||||
expectedUserData.principalVariableBorrowBalance = currentVariableBorrowBalance.plus(
|
expectedUserData.principalVariableDebt = currentVariableDebt.plus(
|
||||||
amountBorrowed
|
amountBorrowed
|
||||||
);
|
);
|
||||||
expectedUserData.principalStableBorrowBalance =
|
expectedUserData.principalStableDebt =
|
||||||
userDataBeforeAction.principalStableBorrowBalance;
|
userDataBeforeAction.principalStableDebt;
|
||||||
|
|
||||||
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
||||||
|
|
||||||
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
|
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedUserData.currentStableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
{
|
{
|
||||||
...userDataBeforeAction,
|
...userDataBeforeAction,
|
||||||
currentStableBorrowBalance: expectedUserData.principalStableBorrowBalance,
|
currentStableDebt: expectedUserData.principalStableDebt,
|
||||||
principalStableBorrowBalance: expectedUserData.principalStableBorrowBalance,
|
principalStableDebt: expectedUserData.principalStableDebt,
|
||||||
stableBorrowRate: expectedUserData.stableBorrowRate,
|
stableBorrowRate: expectedUserData.stableBorrowRate,
|
||||||
stableRateLastUpdated: expectedUserData.stableRateLastUpdated,
|
stableRateLastUpdated: expectedUserData.stableRateLastUpdated,
|
||||||
},
|
},
|
||||||
currentTimestamp
|
currentTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.currentVariableBorrowBalance = calcExpectedVariableDebtTokenBalance(
|
expectedUserData.currentVariableDebt = calcExpectedVariableDebtTokenBalance(
|
||||||
expectedDataAfterAction,
|
expectedDataAfterAction,
|
||||||
{
|
{
|
||||||
...userDataBeforeAction,
|
...userDataBeforeAction,
|
||||||
currentVariableBorrowBalance: expectedUserData.principalVariableBorrowBalance,
|
currentVariableDebt: expectedUserData.principalVariableDebt,
|
||||||
principalVariableBorrowBalance: expectedUserData.principalVariableBorrowBalance,
|
principalVariableDebt: expectedUserData.principalVariableDebt,
|
||||||
variableBorrowIndex:
|
variableBorrowIndex:
|
||||||
interestRateMode == RateMode.Variable
|
interestRateMode == RateMode.Variable
|
||||||
? expectedDataAfterAction.variableBorrowIndex
|
? expectedDataAfterAction.variableBorrowIndex
|
||||||
|
@ -607,7 +607,7 @@ export const calcExpectedUserDataAfterBorrow = (
|
||||||
currentTimestamp
|
currentTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
if (expectedUserData.principalVariableBorrowBalance.eq(0)) {
|
if (expectedUserData.principalVariableDebt.eq(0)) {
|
||||||
expectedUserData.variableBorrowIndex = new BigNumber(0);
|
expectedUserData.variableBorrowIndex = new BigNumber(0);
|
||||||
} else {
|
} else {
|
||||||
expectedUserData.variableBorrowIndex =
|
expectedUserData.variableBorrowIndex =
|
||||||
|
@ -676,33 +676,33 @@ export const calcExpectedUserDataAfterRepay = (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rateMode == RateMode.Stable) {
|
if (rateMode == RateMode.Stable) {
|
||||||
expectedUserData.principalVariableBorrowBalance =
|
expectedUserData.principalVariableDebt =
|
||||||
userDataBeforeAction.principalVariableBorrowBalance;
|
userDataBeforeAction.principalVariableDebt;
|
||||||
expectedUserData.currentVariableBorrowBalance = variableBorrowBalance;
|
expectedUserData.currentVariableDebt = variableBorrowBalance;
|
||||||
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
|
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
|
||||||
|
|
||||||
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = stableBorrowBalance.minus(
|
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = stableBorrowBalance.minus(
|
||||||
totalRepaid
|
totalRepaid
|
||||||
);
|
);
|
||||||
|
|
||||||
if (expectedUserData.currentStableBorrowBalance.eq('0')) {
|
if (expectedUserData.currentStableDebt.eq('0')) {
|
||||||
//user repaid everything
|
//user repaid everything
|
||||||
expectedUserData.stableBorrowRate = expectedUserData.stableRateLastUpdated = new BigNumber(
|
expectedUserData.stableBorrowRate = expectedUserData.stableRateLastUpdated = new BigNumber(
|
||||||
'0'
|
'0'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
expectedUserData.currentStableBorrowBalance = stableBorrowBalance;
|
expectedUserData.currentStableDebt = stableBorrowBalance;
|
||||||
expectedUserData.principalStableBorrowBalance =
|
expectedUserData.principalStableDebt =
|
||||||
userDataBeforeAction.principalStableBorrowBalance;
|
userDataBeforeAction.principalStableDebt;
|
||||||
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
||||||
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
|
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
|
||||||
|
|
||||||
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalVariableBorrowBalance = variableBorrowBalance.minus(
|
expectedUserData.currentVariableDebt = expectedUserData.principalVariableDebt = variableBorrowBalance.minus(
|
||||||
totalRepaid
|
totalRepaid
|
||||||
);
|
);
|
||||||
|
|
||||||
if (expectedUserData.currentVariableBorrowBalance.eq('0')) {
|
if (expectedUserData.currentVariableDebt.eq('0')) {
|
||||||
//user repaid everything
|
//user repaid everything
|
||||||
expectedUserData.variableBorrowIndex = new BigNumber('0');
|
expectedUserData.variableBorrowIndex = new BigNumber('0');
|
||||||
} else {
|
} else {
|
||||||
|
@ -785,7 +785,7 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
|
||||||
if (rateMode === RateMode.Stable) {
|
if (rateMode === RateMode.Stable) {
|
||||||
//swap user stable debt to variable
|
//swap user stable debt to variable
|
||||||
const debtAccrued = stableBorrowBalance.minus(
|
const debtAccrued = stableBorrowBalance.minus(
|
||||||
userDataBeforeAction.principalStableBorrowBalance
|
userDataBeforeAction.principalStableDebt
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
||||||
|
@ -802,16 +802,16 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedReserveData.totalBorrowsStable = reserveDataBeforeAction.totalBorrowsStable.minus(
|
expectedReserveData.totalBorrowsStable = reserveDataBeforeAction.totalBorrowsStable.minus(
|
||||||
userDataBeforeAction.principalStableBorrowBalance
|
userDataBeforeAction.principalStableDebt
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
const debtAccrued = variableBorrowBalance.minus(
|
const debtAccrued = variableBorrowBalance.minus(
|
||||||
userDataBeforeAction.principalVariableBorrowBalance
|
userDataBeforeAction.principalVariableDebt
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
||||||
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable.minus(
|
expectedReserveData.totalBorrowsVariable = reserveDataBeforeAction.totalBorrowsVariable.minus(
|
||||||
userDataBeforeAction.principalVariableBorrowBalance
|
userDataBeforeAction.principalVariableDebt
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedReserveData.totalBorrowsStable = reserveDataBeforeAction.totalBorrowsStable.plus(
|
expectedReserveData.totalBorrowsStable = reserveDataBeforeAction.totalBorrowsStable.plus(
|
||||||
|
@ -895,25 +895,25 @@ export const calcExpectedUserDataAfterSwapRateMode = (
|
||||||
|
|
||||||
if (rateMode === RateMode.Stable) {
|
if (rateMode === RateMode.Stable) {
|
||||||
// swap to variable
|
// swap to variable
|
||||||
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = new BigNumber(
|
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = new BigNumber(
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.stableBorrowRate = new BigNumber(0);
|
expectedUserData.stableBorrowRate = new BigNumber(0);
|
||||||
|
|
||||||
expectedUserData.principalVariableBorrowBalance = expectedUserData.currentVariableBorrowBalance = userDataBeforeAction.currentVariableBorrowBalance.plus(
|
expectedUserData.principalVariableDebt = expectedUserData.currentVariableDebt = userDataBeforeAction.currentVariableDebt.plus(
|
||||||
stableBorrowBalance
|
stableBorrowBalance
|
||||||
);
|
);
|
||||||
expectedUserData.variableBorrowIndex = expectedDataAfterAction.variableBorrowIndex;
|
expectedUserData.variableBorrowIndex = expectedDataAfterAction.variableBorrowIndex;
|
||||||
expectedUserData.stableRateLastUpdated = new BigNumber(0);
|
expectedUserData.stableRateLastUpdated = new BigNumber(0);
|
||||||
} else {
|
} else {
|
||||||
expectedUserData.principalStableBorrowBalance = expectedUserData.currentStableBorrowBalance = userDataBeforeAction.currentStableBorrowBalance.plus(
|
expectedUserData.principalStableDebt = expectedUserData.currentStableDebt = userDataBeforeAction.currentStableDebt.plus(
|
||||||
variableBorrowBalance
|
variableBorrowBalance
|
||||||
);
|
);
|
||||||
|
|
||||||
//weighted average of the previous and the current
|
//weighted average of the previous and the current
|
||||||
expectedUserData.stableBorrowRate = calcExpectedUserStableRate(
|
expectedUserData.stableBorrowRate = calcExpectedUserStableRate(
|
||||||
userDataBeforeAction.principalStableBorrowBalance,
|
userDataBeforeAction.principalStableDebt,
|
||||||
userDataBeforeAction.stableBorrowRate,
|
userDataBeforeAction.stableBorrowRate,
|
||||||
variableBorrowBalance,
|
variableBorrowBalance,
|
||||||
reserveDataBeforeAction.stableBorrowRate
|
reserveDataBeforeAction.stableBorrowRate
|
||||||
|
@ -921,7 +921,7 @@ export const calcExpectedUserDataAfterSwapRateMode = (
|
||||||
|
|
||||||
expectedUserData.stableRateLastUpdated = txTimestamp;
|
expectedUserData.stableRateLastUpdated = txTimestamp;
|
||||||
|
|
||||||
expectedUserData.currentVariableBorrowBalance = expectedUserData.principalVariableBorrowBalance = new BigNumber(
|
expectedUserData.currentVariableDebt = expectedUserData.principalVariableDebt = new BigNumber(
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -947,7 +947,7 @@ export const calcExpectedReserveDataAfterStableRateRebalance = (
|
||||||
|
|
||||||
const stableBorrowBalance = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp);
|
const stableBorrowBalance = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp);
|
||||||
|
|
||||||
const debtAccrued = stableBorrowBalance.minus(userDataBeforeAction.principalStableBorrowBalance);
|
const debtAccrued = stableBorrowBalance.minus(userDataBeforeAction.principalStableDebt);
|
||||||
|
|
||||||
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
|
||||||
|
|
||||||
|
@ -965,7 +965,7 @@ export const calcExpectedReserveDataAfterStableRateRebalance = (
|
||||||
|
|
||||||
expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate(
|
expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate(
|
||||||
avgRateBefore,
|
avgRateBefore,
|
||||||
reserveDataBeforeAction.totalBorrowsStable.minus(userDataBeforeAction.principalStableBorrowBalance),
|
reserveDataBeforeAction.totalBorrowsStable.minus(userDataBeforeAction.principalStableDebt),
|
||||||
stableBorrowBalance,
|
stableBorrowBalance,
|
||||||
reserveDataBeforeAction.stableBorrowRate
|
reserveDataBeforeAction.stableBorrowRate
|
||||||
);
|
);
|
||||||
|
@ -1014,23 +1014,23 @@ export const calcExpectedUserDataAfterStableRateRebalance = (
|
||||||
): UserReserveData => {
|
): UserReserveData => {
|
||||||
const expectedUserData = {...userDataBeforeAction};
|
const expectedUserData = {...userDataBeforeAction};
|
||||||
|
|
||||||
expectedUserData.principalVariableBorrowBalance = calcExpectedVariableDebtTokenBalance(
|
expectedUserData.principalVariableDebt = calcExpectedVariableDebtTokenBalance(
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
expectedUserData.currentStableBorrowBalance = expectedUserData.principalStableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.stableRateLastUpdated = txTimestamp;
|
expectedUserData.stableRateLastUpdated = txTimestamp;
|
||||||
|
|
||||||
expectedUserData.principalVariableBorrowBalance =
|
expectedUserData.principalVariableDebt =
|
||||||
userDataBeforeAction.principalVariableBorrowBalance;
|
userDataBeforeAction.principalVariableDebt;
|
||||||
|
|
||||||
const debtAccrued = expectedUserData.currentStableBorrowBalance.minus(
|
const debtAccrued = expectedUserData.currentStableDebt.minus(
|
||||||
userDataBeforeAction.principalStableBorrowBalance
|
userDataBeforeAction.principalStableDebt
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.stableBorrowRate = reserveDataBeforeAction.stableBorrowRate;
|
expectedUserData.stableBorrowRate = reserveDataBeforeAction.stableBorrowRate;
|
||||||
|
@ -1077,12 +1077,12 @@ export const calcExpectedUsersDataAfterRedirectInterest = (
|
||||||
const expectedFromData = {...fromDataBeforeAction};
|
const expectedFromData = {...fromDataBeforeAction};
|
||||||
const expectedToData = {...toDataBeforeAction};
|
const expectedToData = {...toDataBeforeAction};
|
||||||
|
|
||||||
expectedFromData.currentStableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
expectedFromData.currentStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
fromDataBeforeAction,
|
fromDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedToData.currentVariableBorrowBalance = calcExpectedVariableDebtTokenBalance(
|
expectedToData.currentVariableDebt = calcExpectedVariableDebtTokenBalance(
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
toDataBeforeAction,
|
toDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
|
@ -1253,13 +1253,13 @@ const calcExpectedVariableDebtTokenBalance = (
|
||||||
) => {
|
) => {
|
||||||
const debt = calcExpectedReserveNormalizedDebt(reserveDataBeforeAction, currentTimestamp);
|
const debt = calcExpectedReserveNormalizedDebt(reserveDataBeforeAction, currentTimestamp);
|
||||||
|
|
||||||
const {principalVariableBorrowBalance, variableBorrowIndex} = userDataBeforeAction;
|
const {principalVariableDebt, variableBorrowIndex} = userDataBeforeAction;
|
||||||
|
|
||||||
if (variableBorrowIndex.eq(0)) {
|
if (variableBorrowIndex.eq(0)) {
|
||||||
return principalVariableBorrowBalance;
|
return principalVariableDebt;
|
||||||
}
|
}
|
||||||
|
|
||||||
return principalVariableBorrowBalance
|
return principalVariableDebt
|
||||||
.wadToRay()
|
.wadToRay()
|
||||||
.rayMul(debt)
|
.rayMul(debt)
|
||||||
.rayDiv(variableBorrowIndex)
|
.rayDiv(variableBorrowIndex)
|
||||||
|
@ -1271,7 +1271,7 @@ const calcExpectedStableDebtTokenBalance = (
|
||||||
currentTimestamp: BigNumber
|
currentTimestamp: BigNumber
|
||||||
) => {
|
) => {
|
||||||
const {
|
const {
|
||||||
principalStableBorrowBalance,
|
principalStableDebt,
|
||||||
stableBorrowRate,
|
stableBorrowRate,
|
||||||
stableRateLastUpdated,
|
stableRateLastUpdated,
|
||||||
} = userDataBeforeAction;
|
} = userDataBeforeAction;
|
||||||
|
@ -1281,7 +1281,7 @@ const calcExpectedStableDebtTokenBalance = (
|
||||||
currentTimestamp.eq(stableRateLastUpdated) ||
|
currentTimestamp.eq(stableRateLastUpdated) ||
|
||||||
stableRateLastUpdated.eq(0)
|
stableRateLastUpdated.eq(0)
|
||||||
) {
|
) {
|
||||||
return principalStableBorrowBalance;
|
return principalStableDebt;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cumulatedInterest = calcCompoundedInterest(
|
const cumulatedInterest = calcCompoundedInterest(
|
||||||
|
@ -1290,7 +1290,7 @@ const calcExpectedStableDebtTokenBalance = (
|
||||||
stableRateLastUpdated
|
stableRateLastUpdated
|
||||||
);
|
);
|
||||||
|
|
||||||
return principalStableBorrowBalance.wadToRay().rayMul(cumulatedInterest).rayToWad();
|
return principalStableDebt.wadToRay().rayMul(cumulatedInterest).rayToWad();
|
||||||
};
|
};
|
||||||
|
|
||||||
const calcLinearInterest = (
|
const calcLinearInterest = (
|
||||||
|
|
|
@ -93,10 +93,10 @@ export const getUserData = async (
|
||||||
redirectedBalance: new BigNumber(redirectedBalance),
|
redirectedBalance: new BigNumber(redirectedBalance),
|
||||||
currentATokenUserIndex: new BigNumber(userIndex),
|
currentATokenUserIndex: new BigNumber(userIndex),
|
||||||
currentATokenBalance: new BigNumber(userData.currentATokenBalance.toString()),
|
currentATokenBalance: new BigNumber(userData.currentATokenBalance.toString()),
|
||||||
currentStableBorrowBalance: new BigNumber(userData.currentStableBorrowBalance.toString()),
|
currentStableDebt: new BigNumber(userData.currentStableDebt.toString()),
|
||||||
currentVariableBorrowBalance: new BigNumber(userData.currentVariableBorrowBalance.toString()),
|
currentVariableDebt: new BigNumber(userData.currentVariableDebt.toString()),
|
||||||
principalStableBorrowBalance: new BigNumber(userData.principalStableBorrowBalance.toString()),
|
principalStableDebt: new BigNumber(userData.principalStableDebt.toString()),
|
||||||
principalVariableBorrowBalance: new BigNumber(userData.principalVariableBorrowBalance.toString()),
|
principalVariableDebt: new BigNumber(userData.principalVariableDebt.toString()),
|
||||||
variableBorrowIndex: new BigNumber(userData.variableBorrowIndex.toString()),
|
variableBorrowIndex: new BigNumber(userData.variableBorrowIndex.toString()),
|
||||||
stableBorrowRate: new BigNumber(userData.stableBorrowRate.toString()),
|
stableBorrowRate: new BigNumber(userData.stableBorrowRate.toString()),
|
||||||
liquidityRate: new BigNumber(userData.liquidityRate.toString()),
|
liquidityRate: new BigNumber(userData.liquidityRate.toString()),
|
||||||
|
|
|
@ -7,10 +7,10 @@ export interface UserReserveData {
|
||||||
interestRedirectionAddress: string
|
interestRedirectionAddress: string
|
||||||
redirectionAddressRedirectedBalance: BigNumber
|
redirectionAddressRedirectedBalance: BigNumber
|
||||||
redirectedBalance: BigNumber
|
redirectedBalance: BigNumber
|
||||||
currentStableBorrowBalance: BigNumber
|
currentStableDebt: BigNumber
|
||||||
currentVariableBorrowBalance: BigNumber
|
currentVariableDebt: BigNumber
|
||||||
principalStableBorrowBalance: BigNumber
|
principalStableDebt: BigNumber
|
||||||
principalVariableBorrowBalance: BigNumber
|
principalVariableDebt: BigNumber
|
||||||
variableBorrowIndex: BigNumber
|
variableBorrowIndex: BigNumber
|
||||||
liquidityRate: BigNumber
|
liquidityRate: BigNumber
|
||||||
stableBorrowRate: BigNumber
|
stableBorrowRate: BigNumber
|
||||||
|
|
|
@ -17,7 +17,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
|
||||||
} = ProtocolErrors;
|
} = ProtocolErrors;
|
||||||
|
|
||||||
it('LIQUIDATION - Deposits ETH, borrows DAI/Check liquidation fails because health factor is above 1', async () => {
|
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 depositor = users[0];
|
||||||
const borrower = users[1];
|
const borrower = users[1];
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
|
||||||
);
|
);
|
||||||
|
|
||||||
it('LIQUIDATION - Liquidates the borrow', async () => {
|
it('LIQUIDATION - Liquidates the borrow', async () => {
|
||||||
const {pool, dai, users, addressesProvider, oracle} = testEnv;
|
const {pool, dai, users, addressesProvider, oracle} = testEnv;
|
||||||
const borrower = users[1];
|
const borrower = users[1];
|
||||||
|
|
||||||
//mints dai to the caller
|
//mints dai to the caller
|
||||||
|
@ -135,7 +135,8 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
|
||||||
const daiReserveDataBefore = await pool.getReserveData(dai.address);
|
const daiReserveDataBefore = await pool.getReserveData(dai.address);
|
||||||
const ethReserveDataBefore = await pool.getReserveData(MOCK_ETH_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)
|
.div(2)
|
||||||
.toFixed(0);
|
.toFixed(0);
|
||||||
|
|
||||||
|
@ -170,27 +171,15 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
|
||||||
.div(new BigNumber(collateralPrice).times(new BigNumber(10).pow(principalDecimals)))
|
.div(new BigNumber(collateralPrice).times(new BigNumber(10).pow(principalDecimals)))
|
||||||
.decimalPlaces(0, BigNumber.ROUND_DOWN);
|
.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(
|
expect(userGlobalDataAfter.healthFactor).to.be.bignumber.gt(
|
||||||
oneEther.toFixed(0),
|
oneEther.toFixed(0),
|
||||||
'Invalid health factor'
|
'Invalid health factor'
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(userReserveDataAfter.originationFee).to.be.bignumber.eq(
|
|
||||||
'0',
|
|
||||||
'Origination fee should be repaid'
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(feeAddressBalance).to.be.bignumber.gt('0');
|
expect(feeAddressBalance).to.be.bignumber.gt('0');
|
||||||
|
|
||||||
expect(userReserveDataAfter.principalBorrowBalance).to.be.bignumber.almostEqual(
|
expect(userReserveDataAfter.currentVariableDebt).to.be.bignumber.almostEqual(
|
||||||
new BigNumber(userReserveDataBefore.currentBorrowBalance.toString())
|
new BigNumber(userReserveDataBefore.currentVariableDebt.toString())
|
||||||
.minus(amountToLiquidate)
|
.minus(amountToLiquidate)
|
||||||
.toFixed(0),
|
.toFixed(0),
|
||||||
'Invalid user borrow balance after liquidation'
|
'Invalid user borrow balance after liquidation'
|
||||||
|
@ -204,9 +193,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(ethReserveDataAfter.availableLiquidity).to.be.bignumber.almostEqual(
|
expect(ethReserveDataAfter.availableLiquidity).to.be.bignumber.almostEqual(
|
||||||
new BigNumber(ethReserveDataBefore.availableLiquidity.toString())
|
new BigNumber(ethReserveDataBefore.availableLiquidity.toString()).toFixed(0),
|
||||||
.minus(expectedFeeLiquidated)
|
|
||||||
.toFixed(0),
|
|
||||||
'Invalid collateral available liquidity'
|
'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 3 deposits 1000 USDC, user 4 1 ETH,' +
|
||||||
' user 4 borrows - drops HF, liquidates the borrow',
|
' user 4 borrows - drops HF, liquidates the borrow',
|
||||||
async () => {
|
async () => {
|
||||||
const {users, pool, usdc, oracle, addressesProvider} = testEnv;
|
const {users, pool, usdc, oracle, addressesProvider} = testEnv;
|
||||||
const depositor = users[3];
|
const depositor = users[3];
|
||||||
const borrower = users[4];
|
const borrower = users[4];
|
||||||
//mints USDC to depositor
|
//mints USDC to depositor
|
||||||
|
@ -274,7 +261,8 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
|
||||||
const usdcReserveDataBefore = await pool.getReserveData(usdc.address);
|
const usdcReserveDataBefore = await pool.getReserveData(usdc.address);
|
||||||
const ethReserveDataBefore = await pool.getReserveData(MOCK_ETH_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)
|
.div(2)
|
||||||
.toFixed(0);
|
.toFixed(0);
|
||||||
|
|
||||||
|
@ -309,23 +297,11 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
|
||||||
.div(new BigNumber(collateralPrice).times(new BigNumber(10).pow(principalDecimals)))
|
.div(new BigNumber(collateralPrice).times(new BigNumber(10).pow(principalDecimals)))
|
||||||
.decimalPlaces(0, BigNumber.ROUND_DOWN);
|
.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(
|
expect(userGlobalDataAfter.healthFactor).to.be.bignumber.gt(
|
||||||
oneEther.toFixed(0),
|
oneEther.toFixed(0),
|
||||||
'Invalid health factor'
|
'Invalid health factor'
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(userReserveDataAfter.originationFee).to.be.bignumber.eq(
|
|
||||||
'0',
|
|
||||||
'Origination fee should be repaid'
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(feeAddressBalance).to.be.bignumber.gt('0');
|
expect(feeAddressBalance).to.be.bignumber.gt('0');
|
||||||
|
|
||||||
expect(userReserveDataAfter.principalBorrowBalance).to.be.bignumber.almostEqual(
|
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(
|
expect(ethReserveDataAfter.availableLiquidity).to.be.bignumber.almostEqual(
|
||||||
new BigNumber(ethReserveDataBefore.availableLiquidity.toString())
|
new BigNumber(ethReserveDataBefore.availableLiquidity.toString()).toFixed(0),
|
||||||
.minus(expectedFeeLiquidated)
|
|
||||||
.toFixed(0),
|
|
||||||
'Invalid collateral available liquidity'
|
'Invalid collateral available liquidity'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
16
types/LendingPool.d.ts
vendored
16
types/LendingPool.d.ts
vendored
|
@ -462,10 +462,10 @@ export class LendingPool extends Contract {
|
||||||
_user: string
|
_user: string
|
||||||
): Promise<{
|
): Promise<{
|
||||||
currentATokenBalance: BigNumber;
|
currentATokenBalance: BigNumber;
|
||||||
currentStableBorrowBalance: BigNumber;
|
currentStableDebt: BigNumber;
|
||||||
currentVariableBorrowBalance: BigNumber;
|
currentVariableDebt: BigNumber;
|
||||||
principalStableBorrowBalance: BigNumber;
|
principalStableDebt: BigNumber;
|
||||||
principalVariableBorrowBalance: BigNumber;
|
principalVariableDebt: BigNumber;
|
||||||
stableBorrowRate: BigNumber;
|
stableBorrowRate: BigNumber;
|
||||||
liquidityRate: BigNumber;
|
liquidityRate: BigNumber;
|
||||||
variableBorrowIndex: BigNumber;
|
variableBorrowIndex: BigNumber;
|
||||||
|
@ -727,10 +727,10 @@ export class LendingPool extends Contract {
|
||||||
_user: string
|
_user: string
|
||||||
): Promise<{
|
): Promise<{
|
||||||
currentATokenBalance: BigNumber;
|
currentATokenBalance: BigNumber;
|
||||||
currentStableBorrowBalance: BigNumber;
|
currentStableDebt: BigNumber;
|
||||||
currentVariableBorrowBalance: BigNumber;
|
currentVariableDebt: BigNumber;
|
||||||
principalStableBorrowBalance: BigNumber;
|
principalStableDebt: BigNumber;
|
||||||
principalVariableBorrowBalance: BigNumber;
|
principalVariableDebt: BigNumber;
|
||||||
stableBorrowRate: BigNumber;
|
stableBorrowRate: BigNumber;
|
||||||
liquidityRate: BigNumber;
|
liquidityRate: BigNumber;
|
||||||
variableBorrowIndex: BigNumber;
|
variableBorrowIndex: BigNumber;
|
||||||
|
|
|
@ -900,22 +900,22 @@ const _abi = [
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
internalType: "uint256",
|
internalType: "uint256",
|
||||||
name: "currentStableBorrowBalance",
|
name: "currentStableDebt",
|
||||||
type: "uint256"
|
type: "uint256"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
internalType: "uint256",
|
internalType: "uint256",
|
||||||
name: "currentVariableBorrowBalance",
|
name: "currentVariableDebt",
|
||||||
type: "uint256"
|
type: "uint256"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
internalType: "uint256",
|
internalType: "uint256",
|
||||||
name: "principalStableBorrowBalance",
|
name: "principalStableDebt",
|
||||||
type: "uint256"
|
type: "uint256"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
internalType: "uint256",
|
internalType: "uint256",
|
||||||
name: "principalVariableBorrowBalance",
|
name: "principalVariableDebt",
|
||||||
type: "uint256"
|
type: "uint256"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user