Merged master

This commit is contained in:
The3D 2020-09-04 10:27:32 +02:00
parent 3e951e7bcb
commit 9aad57978d
29 changed files with 907 additions and 683 deletions

View File

@ -3,6 +3,7 @@
"trailingComma": "es5",
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"overrides": [
{
"files": "*.sol",

View File

@ -5,6 +5,7 @@ import {Ownable} from '@openzeppelin/contracts/access/Ownable.sol';
import {
ILendingPoolAddressesProviderRegistry
} from '../interfaces/ILendingPoolAddressesProviderRegistry.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
/**
* @title LendingPoolAddressesProviderRegistry contract
@ -63,7 +64,7 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP
* @param provider the pool address to be unregistered
**/
function unregisterAddressesProvider(address provider) external override onlyOwner {
require(addressesProviders[provider] > 0, 'Provider is not registered');
require(addressesProviders[provider] > 0, Errors.PROVIDER_NOT_REGISTERED);
addressesProviders[provider] = 0;
emit AddressesProviderUnregistered(provider);
}

View File

@ -12,27 +12,12 @@ abstract contract FlashLoanReceiverBase is IFlashLoanReceiver {
using SafeERC20 for IERC20;
using SafeMath for uint256;
ILendingPoolAddressesProvider public addressesProvider;
ILendingPoolAddressesProvider internal _addressesProvider;
constructor(ILendingPoolAddressesProvider provider) public {
addressesProvider = provider;
_addressesProvider = provider;
}
receive() external payable {}
function _transferFundsBack(
address reserve,
address destination,
uint256 amount
) internal {
transferInternal(destination, reserve, amount);
}
function transferInternal(
address destination,
address reserve,
uint256 amount
) internal {
IERC20(reserve).safeTransfer(destination, amount);
}
}

View File

@ -10,7 +10,6 @@ pragma solidity ^0.6.8;
interface IFlashLoanReceiver {
function executeOperation(
address reserve,
address destination,
uint256 amount,
uint256 fee,
bytes calldata params

View File

@ -63,7 +63,7 @@ interface ILendingPool {
* @param reserve the address of the reserve
* @param user the address of the user executing the swap
**/
event Swap(address indexed reserve, address indexed user, uint256 timestamp);
event Swap(address indexed reserve, address indexed user);
/**
* @dev emitted when a user enables a reserve as collateral
@ -90,13 +90,15 @@ interface ILendingPool {
* @param target the address of the flashLoanReceiver
* @param reserve the address of the reserve
* @param amount the amount requested
* @param totalFee the total fee on the amount
* @param totalPremium the total fee on the amount
* @param referralCode the referral code of the caller
**/
event FlashLoan(
address indexed target,
address indexed reserve,
uint256 amount,
uint256 totalFee
uint256 totalPremium,
uint16 referralCode
);
/**
* @dev these events are not emitted directly by the LendingPool
@ -105,21 +107,6 @@ interface ILendingPool {
* This allows to have the events in the generated ABI for LendingPool.
**/
/**
* @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
**/
event OriginationFeeLiquidated(
address indexed collateral,
address indexed reserve,
address indexed user,
uint256 feeLiquidated,
uint256 liquidatedCollateralForFee
);
/**
* @dev emitted when a borrower is liquidated
* @param collateral the address of the collateral being liquidated
@ -238,12 +225,16 @@ interface ILendingPool {
* @param receiver The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface.
* @param reserve the address of the principal reserve
* @param amount the amount requested for this flashloan
* @param params a bytes array to be sent to the flashloan executor
* @param referralCode the referral code of the caller
**/
function flashLoan(
address receiver,
address reserve,
uint256 amount,
bytes calldata params
uint256 debtType,
bytes calldata params,
uint16 referralCode
) external;
/**

View File

@ -11,7 +11,7 @@ interface IReserveInterestRateStrategy {
* @dev returns the base variable borrow rate, in rays
*/
function getBaseVariableBorrowRate() external view returns (uint256);
function baseVariableBorrowRate() external view returns (uint256);
/**
* @dev calculates the liquidity, stable, and variable rates depending on the current utilization rate

View File

@ -69,23 +69,23 @@ contract DefaultReserveInterestRateStrategy is IReserveInterestRateStrategy {
* @dev accessors
*/
function getVariableRateSlope1() external view returns (uint256) {
function variableRateSlope1() external view returns (uint256) {
return _variableRateSlope1;
}
function getVariableRateSlope2() external view returns (uint256) {
function variableRateSlope2() external view returns (uint256) {
return _variableRateSlope2;
}
function getStableRateSlope1() external view returns (uint256) {
function stableRateSlope1() external view returns (uint256) {
return _stableRateSlope1;
}
function getStableRateSlope2() external view returns (uint256) {
function stableRateSlope2() external view returns (uint256) {
return _stableRateSlope2;
}
function getBaseVariableBorrowRate() external override view returns (uint256) {
function baseVariableBorrowRate() external override view returns (uint256) {
return _baseVariableBorrowRate;
}
@ -121,7 +121,7 @@ contract DefaultReserveInterestRateStrategy is IReserveInterestRateStrategy {
uint256 currentStableBorrowRate = 0;
uint256 currentLiquidityRate = 0;
uint256 utilizationRate = (totalBorrows == 0 && availableLiquidity == 0)
uint256 utilizationRate = totalBorrows == 0
? 0
: totalBorrows.rayDiv(availableLiquidity.add(totalBorrows));
@ -157,9 +157,7 @@ contract DefaultReserveInterestRateStrategy is IReserveInterestRateStrategy {
)
.rayMul(utilizationRate);
return (currentLiquidityRate,
currentStableBorrowRate,
currentVariableBorrowRate);
return (currentLiquidityRate, currentStableBorrowRate, currentVariableBorrowRate);
}
/**

View File

@ -3,7 +3,6 @@ pragma solidity ^0.6.8;
pragma experimental ABIEncoderV2;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {
VersionedInitializable
@ -11,6 +10,7 @@ import {
import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol';
import {IAToken} from '../tokenization/interfaces/IAToken.sol';
import {Helpers} from '../libraries/helpers/Helpers.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol';
import {GenericLogic} from '../libraries/logic/GenericLogic.sol';
@ -31,7 +31,7 @@ import {ILendingPool} from '../interfaces/ILendingPool.sol';
* @author Aave
**/
contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
contract LendingPool is VersionedInitializable, ILendingPool {
using SafeMath for uint256;
using WadRayMath for uint256;
using ReserveLogic for ReserveLogic.ReserveData;
@ -42,20 +42,23 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
//main configuration parameters
uint256 public constant REBALANCE_DOWN_RATE_DELTA = (1e27) / 5;
uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25;
uint256 public constant FLASHLOAN_FEE_TOTAL = 9;
uint256 public constant FLASHLOAN_PREMIUM_TOTAL = 9;
ILendingPoolAddressesProvider internal addressesProvider;
ILendingPoolAddressesProvider internal _addressesProvider;
mapping(address => ReserveLogic.ReserveData) internal _reserves;
mapping(address => UserConfiguration.Map) internal _usersConfig;
address[] internal reservesList;
address[] internal _reservesList;
/**
* @dev only lending pools configurator can use functions affected by this modifier
**/
modifier onlyLendingPoolConfigurator {
require(addressesProvider.getLendingPoolConfigurator() == msg.sender, '30');
require(
_addressesProvider.getLendingPoolConfigurator() == msg.sender,
Errors.CALLER_NOT_LENDING_POOL_CONFIGURATOR
);
_;
}
@ -73,7 +76,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @param provider the address of the LendingPoolAddressesProvider registry
**/
function initialize(ILendingPoolAddressesProvider provider) public initializer {
addressesProvider = provider;
_addressesProvider = provider;
}
/**
@ -87,43 +90,41 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
address asset,
uint256 amount,
uint16 referralCode
) external override nonReentrant {
) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset];
ValidationLogic.validateDeposit(reserve, amount);
IAToken aToken = IAToken(reserve.aTokenAddress);
bool isFirstDeposit = aToken.balanceOf(msg.sender) == 0;
address aToken = reserve.aTokenAddress;
reserve.updateCumulativeIndexesAndTimestamp();
reserve.updateInterestRates(asset, amount, 0);
reserve.updateInterestRates(asset, aToken, amount, 0);
bool isFirstDeposit = IAToken(aToken).balanceOf(msg.sender) == 0;
if (isFirstDeposit) {
_usersConfig[msg.sender].setUsingAsCollateral(reserve.index, true);
}
//minting AToken to user 1:1 with the specific exchange rate
aToken.mint(msg.sender, amount);
IAToken(aToken).mint(msg.sender, amount);
//transfer to the aToken contract
IERC20(asset).safeTransferFrom(msg.sender, address(aToken), amount);
IERC20(asset).safeTransferFrom(msg.sender, aToken, amount);
//solium-disable-next-line
emit Deposit(asset, msg.sender, amount, referralCode);
}
/**
* @dev withdraws the _reserves of _user.
* @dev withdraws the _reserves of user.
* @param asset the address of the reserve
* @param amount the underlying amount to be redeemed
**/
function withdraw(address asset, uint256 amount) external override nonReentrant {
function withdraw(address asset, uint256 amount) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset];
IAToken aToken = IAToken(reserve.aTokenAddress);
address aToken = reserve.aTokenAddress;
uint256 userBalance = aToken.balanceOf(msg.sender);
uint256 userBalance = IAToken(aToken).balanceOf(msg.sender);
uint256 amountToWithdraw = amount;
@ -134,26 +135,25 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
ValidationLogic.validateWithdraw(
asset,
address(aToken),
aToken,
amountToWithdraw,
userBalance,
_reserves,
_usersConfig[msg.sender],
reservesList,
addressesProvider.getPriceOracle()
_reservesList,
_addressesProvider.getPriceOracle()
);
reserve.updateCumulativeIndexesAndTimestamp();
reserve.updateInterestRates(asset, 0, amountToWithdraw);
reserve.updateInterestRates(asset, aToken, 0, amountToWithdraw);
if (amountToWithdraw == userBalance) {
_usersConfig[msg.sender].setUsingAsCollateral(reserve.index, false);
}
aToken.burn(msg.sender, msg.sender, amountToWithdraw);
IAToken(aToken).burn(msg.sender, msg.sender, amountToWithdraw);
//solium-disable-next-line
emit Withdraw(asset, msg.sender, amount);
}
@ -163,88 +163,49 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @param asset the address of the reserve
* @param amount the amount to be borrowed
* @param interestRateMode the interest rate mode at which the user wants to borrow. Can be 0 (STABLE) or 1 (VARIABLE)
* @param referralCode a referral code for integrators
**/
function borrow(
address asset,
uint256 amount,
uint256 interestRateMode,
uint16 referralCode
) external override nonReentrant {
ReserveLogic.ReserveData storage reserve = _reserves[asset];
UserConfiguration.Map storage userConfig = _usersConfig[msg.sender];
uint256 amountInETH = IPriceOracleGetter(addressesProvider.getPriceOracle())
.getAssetPrice(asset)
.mul(amount)
.div(10**reserve.configuration.getDecimals()); //price is in ether
ValidationLogic.validateBorrow(
reserve,
asset,
amount,
amountInETH,
interestRateMode,
MAX_STABLE_RATE_BORROW_SIZE_PERCENT,
_reserves,
_usersConfig[msg.sender],
reservesList,
addressesProvider.getPriceOracle()
);
//caching the current stable borrow rate
uint256 userStableRate = reserve.currentStableBorrowRate;
reserve.updateCumulativeIndexesAndTimestamp();
if (ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE) {
IStableDebtToken(reserve.stableDebtTokenAddress).mint(msg.sender, amount, userStableRate);
} else {
IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, amount);
}
reserve.updateInterestRates(asset, 0, amount);
if (!userConfig.isBorrowing(reserve.index)) {
userConfig.setBorrowing(reserve.index, true);
}
//if we reached this point, we can transfer
IAToken(reserve.aTokenAddress).transferUnderlyingTo(msg.sender, amount);
emit Borrow(
asset,
msg.sender,
amount,
interestRateMode,
ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE
? userStableRate
: reserve.currentVariableBorrowRate,
referralCode
) external override {
_executeBorrow(
ExecuteBorrowParams(
asset,
msg.sender,
amount,
interestRateMode,
_reserves[asset].aTokenAddress,
referralCode,
true
)
);
}
/**
* @notice repays a borrow on the specific reserve, for the specified amount (or for the whole amount, if uint256(-1) is specified).
* @dev the target user is defined by _onBehalfOf. If there is no repayment on behalf of another account,
* _onBehalfOf must be equal to msg.sender.
* @dev the target user is defined by onBehalfOf. If there is no repayment on behalf of another account,
* onBehalfOf must be equal to msg.sender.
* @param asset the address of the reserve on which the user borrowed
* @param amount the amount to repay, or uint256(-1) if the user wants to repay everything
* @param _onBehalfOf the address for which msg.sender is repaying.
* @param onBehalfOf the address for which msg.sender is repaying.
**/
function repay(
address asset,
uint256 amount,
uint256 _rateMode,
address _onBehalfOf
) external override nonReentrant {
uint256 rateMode,
address onBehalfOf
) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset];
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(_onBehalfOf, reserve);
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(onBehalfOf, reserve);
ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode);
ReserveLogic.InterestRateMode rateMode = ReserveLogic.InterestRateMode(_rateMode);
//default to max amount
uint256 paybackAmount = rateMode == ReserveLogic.InterestRateMode.STABLE
uint256 paybackAmount = interestRateMode == ReserveLogic.InterestRateMode.STABLE
? stableDebt
: variableDebt;
@ -255,8 +216,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
ValidationLogic.validateRepay(
reserve,
amount,
rateMode,
_onBehalfOf,
interestRateMode,
onBehalfOf,
stableDebt,
variableDebt
);
@ -264,46 +225,47 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
reserve.updateCumulativeIndexesAndTimestamp();
//burns an equivalent amount of debt tokens
if (rateMode == ReserveLogic.InterestRateMode.STABLE) {
IStableDebtToken(reserve.stableDebtTokenAddress).burn(_onBehalfOf, paybackAmount);
if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) {
IStableDebtToken(reserve.stableDebtTokenAddress).burn(onBehalfOf, paybackAmount);
} else {
IVariableDebtToken(reserve.variableDebtTokenAddress).burn(_onBehalfOf, paybackAmount);
IVariableDebtToken(reserve.variableDebtTokenAddress).burn(onBehalfOf, paybackAmount);
}
reserve.updateInterestRates(asset, paybackAmount, 0);
address aToken = reserve.aTokenAddress;
reserve.updateInterestRates(asset, aToken, paybackAmount, 0);
if (stableDebt.add(variableDebt).sub(paybackAmount) == 0) {
_usersConfig[_onBehalfOf].setBorrowing(reserve.index, false);
_usersConfig[onBehalfOf].setBorrowing(reserve.index, false);
}
IERC20(asset).safeTransferFrom(msg.sender, reserve.aTokenAddress, paybackAmount);
IERC20(asset).safeTransferFrom(msg.sender, aToken, paybackAmount);
emit Repay(asset, _onBehalfOf, msg.sender, paybackAmount);
emit Repay(asset, onBehalfOf, msg.sender, paybackAmount);
}
/**
* @dev borrowers can user this function to swap between stable and variable borrow rate modes.
* @param asset the address of the reserve on which the user borrowed
* @param _rateMode the rate mode that the user wants to swap
* @param rateMode the rate mode that the user wants to swap
**/
function swapBorrowRateMode(address asset, uint256 _rateMode) external override nonReentrant {
function swapBorrowRateMode(address asset, uint256 rateMode) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset];
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve);
ReserveLogic.InterestRateMode rateMode = ReserveLogic.InterestRateMode(_rateMode);
ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode);
ValidationLogic.validateSwapRateMode(
reserve,
_usersConfig[msg.sender],
stableDebt,
variableDebt,
rateMode
interestRateMode
);
reserve.updateCumulativeIndexesAndTimestamp();
if (rateMode == ReserveLogic.InterestRateMode.STABLE) {
if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) {
//burn stable rate tokens, mint variable rate tokens
IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender, stableDebt);
IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, stableDebt);
@ -317,14 +279,9 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
);
}
reserve.updateInterestRates(asset, 0, 0);
reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0);
emit Swap(
asset,
msg.sender,
//solium-disable-next-line
block.timestamp
);
emit Swap(asset, msg.sender);
}
/**
@ -332,17 +289,17 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* this is regulated by Aave to ensure that the protocol is not abused, and the user is paying a fair
* rate. Anyone can call this function.
* @param asset the address of the reserve
* @param _user the address of the user to be rebalanced
* @param user the address of the user to be rebalanced
**/
function rebalanceStableBorrowRate(address asset, address _user) external override nonReentrant {
function rebalanceStableBorrowRate(address asset, address user) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset];
IStableDebtToken stableDebtToken = IStableDebtToken(reserve.stableDebtTokenAddress);
uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(_user);
uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user);
// user must be borrowing on asset at a stable rate
require(stableBorrowBalance > 0, 'User does not have any stable rate loan for this reserve');
require(stableBorrowBalance > 0, Errors.NOT_ENOUGH_STABLE_BORROW_BALANCE);
uint256 rebalanceDownRateThreshold = WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA).rayMul(
reserve.currentStableBorrowRate
@ -353,23 +310,23 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
//2. user stable rate is above the market avg borrow rate of a certain delta, and utilization rate is low.
//In this case, the user is paying an interest that is too high, and needs to be rescaled down.
uint256 userStableRate = stableDebtToken.getUserStableRate(_user);
uint256 userStableRate = stableDebtToken.getUserStableRate(user);
require(
userStableRate < reserve.currentLiquidityRate || userStableRate > rebalanceDownRateThreshold,
'Interest rate rebalance conditions were not met'
Errors.INTEREST_RATE_REBALANCE_CONDITIONS_NOT_MET
);
//burn old debt tokens, mint new ones
reserve.updateCumulativeIndexesAndTimestamp();
stableDebtToken.burn(_user, stableBorrowBalance);
stableDebtToken.mint(_user, stableBorrowBalance, reserve.currentStableBorrowRate);
stableDebtToken.burn(user, stableBorrowBalance);
stableDebtToken.mint(user, stableBorrowBalance, reserve.currentStableBorrowRate);
reserve.updateInterestRates(asset, 0, 0);
reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0);
emit RebalanceStableBorrowRate(asset, _user);
emit RebalanceStableBorrowRate(asset, user);
return;
}
@ -377,13 +334,9 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
/**
* @dev allows depositors to enable or disable a specific deposit as collateral.
* @param asset the address of the reserve
* @param _useAsCollateral true if the user wants to user the deposit as collateral, false otherwise.
* @param useAsCollateral true if the user wants to user the deposit as collateral, false otherwise.
**/
function setUserUseReserveAsCollateral(address asset, bool _useAsCollateral)
external
override
nonReentrant
{
function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset];
ValidationLogic.validateSetUseReserveAsCollateral(
@ -391,13 +344,13 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
asset,
_reserves,
_usersConfig[msg.sender],
reservesList,
addressesProvider.getPriceOracle()
_reservesList,
_addressesProvider.getPriceOracle()
);
_usersConfig[msg.sender].setUsingAsCollateral(reserve.index, _useAsCollateral);
_usersConfig[msg.sender].setUsingAsCollateral(reserve.index, useAsCollateral);
if (_useAsCollateral) {
if (useAsCollateral) {
emit ReserveUsedAsCollateralEnabled(asset, msg.sender);
} else {
emit ReserveUsedAsCollateralDisabled(asset, msg.sender);
@ -408,32 +361,32 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @dev users can invoke this function to liquidate an undercollateralized position.
* @param asset the address of the collateral to liquidated
* @param asset the address of the principal reserve
* @param _user the address of the borrower
* @param _purchaseAmount the amount of principal that the liquidator wants to repay
* @param _receiveAToken true if the liquidators wants to receive the aTokens, false if
* @param user the address of the borrower
* @param purchaseAmount the amount of principal that the liquidator wants to repay
* @param receiveAToken true if the liquidators wants to receive the aTokens, false if
* he wants to receive the underlying asset directly
**/
function liquidationCall(
address _collateral,
address collateral,
address asset,
address _user,
uint256 _purchaseAmount,
bool _receiveAToken
) external override nonReentrant {
address liquidationManager = addressesProvider.getLendingPoolLiquidationManager();
address user,
uint256 purchaseAmount,
bool receiveAToken
) external override {
address liquidationManager = _addressesProvider.getLendingPoolLiquidationManager();
//solium-disable-next-line
(bool success, bytes memory result) = liquidationManager.delegatecall(
abi.encodeWithSignature(
'liquidationCall(address,address,address,uint256,bool)',
_collateral,
collateral,
asset,
_user,
_purchaseAmount,
_receiveAToken
user,
purchaseAmount,
receiveAToken
)
);
require(success, 'Liquidation call failed');
require(success, Errors.LIQUIDATION_CALL_FAILED);
(uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string));
@ -443,68 +396,83 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
}
}
struct FlashLoanLocalVars {
uint256 premium;
uint256 amountPlusPremium;
uint256 amountPlusPremiumInETH;
uint256 receiverBalance;
uint256 receiverAllowance;
uint256 availableBalance;
uint256 assetPrice;
IFlashLoanReceiver receiver;
address aTokenAddress;
address oracle;
}
/**
* @dev allows smartcontracts to access the liquidity of the pool within one transaction,
* @dev allows smart contracts to access the liquidity of the pool within one transaction,
* as long as the amount taken plus a fee is returned. NOTE There are security concerns for developers of flashloan receiver contracts
* that must be kept into consideration. For further details please visit https://developers.aave.com
* @param receiverAddress The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface.
* @param asset the address of the principal reserve
* @param amount the amount requested for this flashloan
* @param asset The address of the principal reserve
* @param amount The amount requested for this flashloan
* @param mode Type of the debt to open if the flash loan is not returned. 0 -> Don't open any debt, just revert, 1 -> stable, 2 -> variable
* @param params Variadic packed params to pass to the receiver as extra information
* @param referralCode Referral code of the flash loan
**/
function flashLoan(
address receiverAddress,
address asset,
uint256 amount,
bytes calldata params
) external override nonReentrant {
uint256 mode,
bytes calldata params,
uint16 referralCode
) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset];
FlashLoanLocalVars memory vars;
address aTokenAddress = reserve.aTokenAddress;
vars.aTokenAddress = reserve.aTokenAddress;
//check that the reserve has enough available liquidity
uint256 availableLiquidityBefore = IERC20(asset).balanceOf(aTokenAddress);
vars.premium = amount.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000);
//calculate amount fee
uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000);
ValidationLogic.validateFlashloan(mode, vars.premium);
require(
availableLiquidityBefore >= amount,
'There is not enough liquidity available to borrow'
);
require(amountFee > 0, 'The requested amount is too small for a FlashLoan.');
ReserveLogic.InterestRateMode debtMode = ReserveLogic.InterestRateMode(mode);
//get the FlashLoanReceiver instance
IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress);
vars.receiver = IFlashLoanReceiver(receiverAddress);
//transfer funds to the receiver
IAToken(aTokenAddress).transferUnderlyingTo(receiverAddress, amount);
IAToken(vars.aTokenAddress).transferUnderlyingTo(receiverAddress, amount);
//execute action of the receiver
receiver.executeOperation(asset, aTokenAddress, amount, amountFee, params);
vars.receiver.executeOperation(asset, amount, vars.premium, params);
//check that the actual balance of the core contract includes the returned amount
uint256 availableLiquidityAfter = IERC20(asset).balanceOf(aTokenAddress);
vars.amountPlusPremium = amount.add(vars.premium);
require(
availableLiquidityAfter == availableLiquidityBefore.add(amountFee),
'The actual balance of the protocol is inconsistent'
);
if (debtMode == ReserveLogic.InterestRateMode.NONE) {
IERC20(asset).transferFrom(receiverAddress, vars.aTokenAddress, vars.amountPlusPremium);
reserve.updateCumulativeIndexesAndTimestamp();
reserve.cumulateToLiquidityIndex(IERC20(vars.aTokenAddress).totalSupply(), vars.premium);
reserve.updateInterestRates(asset, vars.aTokenAddress, vars.premium, 0);
emit FlashLoan(receiverAddress, asset, amount, vars.premium, referralCode);
//compounding the cumulated interest
reserve.updateCumulativeIndexesAndTimestamp();
uint256 totalLiquidityBefore = availableLiquidityBefore
.add(IERC20(reserve.variableDebtTokenAddress).totalSupply())
.add(IERC20(reserve.stableDebtTokenAddress).totalSupply());
//compounding the received fee into the reserve
reserve.cumulateToLiquidityIndex(totalLiquidityBefore, amountFee);
//refresh interest rates
reserve.updateInterestRates(asset, amountFee, 0);
//solium-disable-next-line
emit FlashLoan(receiverAddress, asset, amount, amountFee);
} else {
// If the transfer didn't succeed, the receiver either didn't return the funds, or didn't approve the transfer.
_executeBorrow(
ExecuteBorrowParams(
asset,
msg.sender,
vars.amountPlusPremium.sub(vars.availableBalance),
mode,
vars.aTokenAddress,
referralCode,
false
)
);
}
}
/**
@ -595,7 +563,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
);
}
function getUserAccountData(address _user)
function getUserAccountData(address user)
external
override
view
@ -615,11 +583,11 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
currentLiquidationThreshold,
healthFactor
) = GenericLogic.calculateUserAccountData(
_user,
user,
_reserves,
_usersConfig[_user],
reservesList,
addressesProvider.getPriceOracle()
_usersConfig[user],
_reservesList,
_addressesProvider.getPriceOracle()
);
availableBorrowsETH = GenericLogic.calculateAvailableBorrowsETH(
@ -629,7 +597,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
);
}
function getUserReserveData(address asset, address _user)
function getUserReserveData(address asset, address user)
external
override
view
@ -648,20 +616,20 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
{
ReserveLogic.ReserveData storage reserve = _reserves[asset];
currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(_user);
(currentStableDebt, currentVariableDebt) = Helpers.getUserCurrentDebt(_user, reserve);
(principalStableDebt, principalVariableDebt) = Helpers.getUserPrincipalDebt(_user, reserve);
currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(user);
(currentStableDebt, currentVariableDebt) = Helpers.getUserCurrentDebt(user, reserve);
(principalStableDebt, principalVariableDebt) = Helpers.getUserPrincipalDebt(user, reserve);
liquidityRate = reserve.currentLiquidityRate;
stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(_user);
stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(user);
stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated(
_user
user
);
usageAsCollateralEnabled = _usersConfig[_user].isUsingAsCollateral(reserve.index);
variableBorrowIndex = IVariableDebtToken(reserve.variableDebtTokenAddress).getUserIndex(_user);
usageAsCollateralEnabled = _usersConfig[user].isUsingAsCollateral(reserve.index);
variableBorrowIndex = IVariableDebtToken(reserve.variableDebtTokenAddress).getUserIndex(user);
}
function getReserves() external override view returns (address[] memory) {
return reservesList;
return _reservesList;
}
receive() external payable {
@ -671,21 +639,21 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
/**
* @dev initializes a reserve
* @param asset the address of the reserve
* @param _aTokenAddress the address of the overlying aToken contract
* @param _interestRateStrategyAddress the address of the interest rate strategy contract
* @param aTokenAddress the address of the overlying aToken contract
* @param interestRateStrategyAddress the address of the interest rate strategy contract
**/
function initReserve(
address asset,
address _aTokenAddress,
address _stableDebtAddress,
address _variableDebtAddress,
address _interestRateStrategyAddress
address aTokenAddress,
address stableDebtAddress,
address variableDebtAddress,
address interestRateStrategyAddress
) external override onlyLendingPoolConfigurator {
_reserves[asset].init(
_aTokenAddress,
_stableDebtAddress,
_variableDebtAddress,
_interestRateStrategyAddress
aTokenAddress,
stableDebtAddress,
variableDebtAddress,
interestRateStrategyAddress
);
_addReserveToList(asset);
}
@ -721,22 +689,102 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
return _reserves[asset].configuration;
}
// internal functions
struct ExecuteBorrowParams {
address asset;
address user;
uint256 amount;
uint256 interestRateMode;
address aTokenAddress;
uint16 referralCode;
bool releaseUnderlying;
}
/**
* @notice internal functions
* @dev Internal function to execute a borrowing action, allowing to transfer or not the underlying
* @param vars Input struct for the borrowing action, in order to avoid STD errors
**/
function _executeBorrow(ExecuteBorrowParams memory vars) internal {
ReserveLogic.ReserveData storage reserve = _reserves[vars.asset];
UserConfiguration.Map storage userConfig = _usersConfig[msg.sender];
address oracle = _addressesProvider.getPriceOracle();
uint256 amountInETH = IPriceOracleGetter(oracle).getAssetPrice(vars.asset).mul(vars.amount).div(
10**reserve.configuration.getDecimals()
);
ValidationLogic.validateBorrow(
reserve,
vars.asset,
vars.amount,
amountInETH,
vars.interestRateMode,
MAX_STABLE_RATE_BORROW_SIZE_PERCENT,
_reserves,
userConfig,
_reservesList,
oracle
);
uint256 reserveIndex = reserve.index;
if (!userConfig.isBorrowing(reserveIndex)) {
userConfig.setBorrowing(reserveIndex, true);
}
reserve.updateCumulativeIndexesAndTimestamp();
//caching the current stable borrow rate
uint256 currentStableRate = 0;
if (
ReserveLogic.InterestRateMode(vars.interestRateMode) == ReserveLogic.InterestRateMode.STABLE
) {
currentStableRate = reserve.currentStableBorrowRate;
IStableDebtToken(reserve.stableDebtTokenAddress).mint(
vars.user,
vars.amount,
currentStableRate
);
} else {
IVariableDebtToken(reserve.variableDebtTokenAddress).mint(vars.user, vars.amount);
}
reserve.updateInterestRates(vars.asset, vars.aTokenAddress, 0, vars.releaseUnderlying ? vars.amount : 0);
if(vars.releaseUnderlying){
IAToken(vars.aTokenAddress).transferUnderlyingTo(msg.sender, vars.amount);
}
emit Borrow(
vars.asset,
msg.sender,
vars.amount,
vars.interestRateMode,
ReserveLogic.InterestRateMode(vars.interestRateMode) == ReserveLogic.InterestRateMode.STABLE
? currentStableRate
: reserve.currentVariableBorrowRate,
vars.referralCode
);
}
/**
* @dev adds a reserve to the array of the _reserves address
**/
function _addReserveToList(address asset) internal {
bool reserveAlreadyAdded = false;
for (uint256 i = 0; i < reservesList.length; i++)
if (reservesList[i] == asset) {
for (uint256 i = 0; i < _reservesList.length; i++)
if (_reservesList[i] == asset) {
reserveAlreadyAdded = true;
}
if (!reserveAlreadyAdded) {
_reserves[asset].index = uint8(reservesList.length);
reservesList.push(asset);
_reserves[asset].index = uint8(_reservesList.length);
_reservesList.push(asset);
}
}
@ -782,8 +830,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
amount,
_reserves,
_usersConfig[user],
reservesList,
addressesProvider.getPriceOracle()
_reservesList,
_addressesProvider.getPriceOracle()
);
}
@ -791,13 +839,13 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @dev returns the list of the initialized reserves
**/
function getReservesList() external view returns (address[] memory) {
return reservesList;
return _reservesList;
}
/**
* @dev returns the addresses provider
**/
function getAddressesProvider() external view returns (ILendingPoolAddressesProvider) {
return addressesProvider;
return _addressesProvider;
}
}

View File

@ -13,6 +13,7 @@ import {ReserveConfiguration} from '../libraries/configuration/ReserveConfigurat
import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol';
import {ILendingPool} from '../interfaces/ILendingPool.sol';
import {IERC20Detailed} from '../interfaces/IERC20Detailed.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
/**
* @title LendingPoolConfigurator contract
@ -164,10 +165,10 @@ contract LendingPoolConfigurator is VersionedInitializable {
/**
* @dev emitted when the implementation of a variable debt token is upgraded
* @param asset the address of the reserve
* @param _proxy the variable debt token proxy address
* @param _implementation the new aToken implementation
* @param proxy the variable debt token proxy address
* @param implementation the new aToken implementation
**/
event VariableDebtTokenUpgraded(address asset, address _proxy, address _implementation);
event VariableDebtTokenUpgraded(address asset, address proxy, address implementation);
ILendingPoolAddressesProvider internal addressesProvider;
ILendingPool internal pool;
@ -178,7 +179,7 @@ contract LendingPoolConfigurator is VersionedInitializable {
modifier onlyLendingPoolManager {
require(
addressesProvider.getLendingPoolManager() == msg.sender,
'The caller must be a lending pool manager'
Errors.CALLER_NOT_LENDING_POOL_MANAGER
);
_;
}
@ -211,10 +212,7 @@ contract LendingPoolConfigurator is VersionedInitializable {
uint8 underlyingAssetDecimals,
address interestRateStrategyAddress
) public onlyLendingPoolManager {
address aTokenProxyAddress = _initTokenWithProxy(
aTokenImpl,
underlyingAssetDecimals
);
address aTokenProxyAddress = _initTokenWithProxy(aTokenImpl, underlyingAssetDecimals);
address stableDebtTokenProxyAddress = _initTokenWithProxy(
stableDebtTokenImpl,
@ -280,6 +278,7 @@ contract LendingPoolConfigurator is VersionedInitializable {
emit StableDebtTokenUpgraded(asset, stableDebtToken, implementation);
}
/**
* @dev updates the variable debt token implementation for the asset
* @param asset the address of the reserve to be updated
@ -349,12 +348,7 @@ contract LendingPoolConfigurator is VersionedInitializable {
pool.setConfiguration(asset, currentConfig.data);
emit ReserveEnabledAsCollateral(
asset,
ltv,
liquidationThreshold,
liquidationBonus
);
emit ReserveEnabledAsCollateral(asset, ltv, liquidationThreshold, liquidationBonus);
}
/**
@ -432,7 +426,7 @@ contract LendingPoolConfigurator is VersionedInitializable {
) = pool.getReserveData(asset);
require(
availableLiquidity == 0 && totalBorrowsStable == 0 && totalBorrowsVariable == 0,
'The liquidity of the reserve needs to be 0'
Errors.RESERVE_LIQUIDITY_NOT_0
);
ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset);
@ -553,10 +547,7 @@ contract LendingPoolConfigurator is VersionedInitializable {
* @param implementation the address of the implementation
* @param decimals the decimals of the token
**/
function _initTokenWithProxy(
address implementation,
uint8 decimals
) internal returns (address) {
function _initTokenWithProxy(address implementation, uint8 decimals) internal returns (address) {
InitializableAdminUpgradeabilityProxy proxy = new InitializableAdminUpgradeabilityProxy();
bytes memory params = abi.encodeWithSignature(

View File

@ -3,8 +3,6 @@ pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol';
import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol';
import {
VersionedInitializable
} from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol';
@ -21,13 +19,14 @@ import {Helpers} from '../libraries/helpers/Helpers.sol';
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {PercentageMath} from '../libraries/math/PercentageMath.sol';
import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
/**
* @title LendingPoolLiquidationManager contract
* @author Aave
* @notice Implements the liquidation function.
**/
contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializable {
contract LendingPoolLiquidationManager is VersionedInitializable {
using SafeERC20 for IERC20;
using SafeMath for uint256;
using WadRayMath for uint256;
@ -132,11 +131,13 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (vars.healthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) {
return (
uint256(LiquidationErrors.HEALTH_FACTOR_ABOVE_THRESHOLD),
'Health factor is not below the threshold'
Errors.HEALTH_FACTOR_NOT_BELOW_THRESHOLD
);
}
vars.userCollateralBalance = IERC20(collateralReserve.aTokenAddress).balanceOf(user);
vars.collateralAtoken = IAToken(collateralReserve.aTokenAddress);
vars.userCollateralBalance = vars.collateralAtoken.balanceOf(user);
vars.isCollateralEnabled =
collateralReserve.configuration.getLiquidationThreshold() > 0 &&
@ -146,7 +147,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (!vars.isCollateralEnabled) {
return (
uint256(LiquidationErrors.COLLATERAL_CANNOT_BE_LIQUIDATED),
'The collateral chosen cannot be liquidated'
Errors.COLLATERAL_CANNOT_BE_LIQUIDATED
);
}
@ -159,7 +160,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (vars.userStableDebt == 0 && vars.userVariableDebt == 0) {
return (
uint256(LiquidationErrors.CURRRENCY_NOT_BORROWED),
'User did not borrow the specified currency'
Errors.SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER
);
}
@ -192,8 +193,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
vars.actualAmountToLiquidate = vars.principalAmountNeeded;
}
vars.collateralAtoken = IAToken(collateralReserve.aTokenAddress);
//if liquidator reclaims the underlying asset, we make sure there is enough available collateral in the reserve
if (!receiveAToken) {
uint256 currentAvailableCollateral = IERC20(collateral).balanceOf(
@ -202,14 +201,19 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (currentAvailableCollateral < vars.maxCollateralToLiquidate) {
return (
uint256(LiquidationErrors.NOT_ENOUGH_LIQUIDITY),
"There isn't enough liquidity available to liquidate"
Errors.NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE
);
}
}
//update the principal reserve
principalReserve.updateCumulativeIndexesAndTimestamp();
principalReserve.updateInterestRates(principal, vars.actualAmountToLiquidate, 0);
principalReserve.updateInterestRates(
principal,
principalReserve.aTokenAddress,
vars.actualAmountToLiquidate,
0
);
if (vars.userVariableDebt >= vars.actualAmountToLiquidate) {
IVariableDebtToken(principalReserve.variableDebtTokenAddress).burn(
@ -235,7 +239,12 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
//updating collateral reserve
collateralReserve.updateCumulativeIndexesAndTimestamp();
collateralReserve.updateInterestRates(collateral, 0, vars.maxCollateralToLiquidate);
collateralReserve.updateInterestRates(
collateral,
address(vars.collateralAtoken),
0,
vars.maxCollateralToLiquidate
);
//burn the equivalent amount of atoken
vars.collateralAtoken.burn(user, msg.sender, vars.maxCollateralToLiquidate);
@ -258,7 +267,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
receiveAToken
);
return (uint256(LiquidationErrors.NO_ERROR), 'No errors');
return (uint256(LiquidationErrors.NO_ERROR), Errors.NO_ERRORS);
}
struct AvailableCollateralToLiquidateLocalVars {
@ -283,8 +292,8 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
* @return principalAmountNeeded the purchase amount
**/
function calculateAvailableCollateralToLiquidate(
ReserveLogic.ReserveData storage _collateralReserve,
ReserveLogic.ReserveData storage _principalReserve,
ReserveLogic.ReserveData storage collateralReserve,
ReserveLogic.ReserveData storage principalReserve,
address collateralAddress,
address principalAddress,
uint256 purchaseAmount,
@ -300,10 +309,10 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
vars.collateralPrice = oracle.getAssetPrice(collateralAddress);
vars.principalCurrencyPrice = oracle.getAssetPrice(principalAddress);
(, , vars.liquidationBonus, vars.collateralDecimals) = _collateralReserve
(, , vars.liquidationBonus, vars.collateralDecimals) = collateralReserve
.configuration
.getParams();
vars.principalDecimals = _principalReserve.configuration.getDecimals();
vars.principalDecimals = principalReserve.configuration.getDecimals();
//this is the maximum possible amount of the selected collateral that can be liquidated, given the
//max amount of principal currency that is available for liquidation.

View File

@ -0,0 +1,66 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
/**
* @title Errors library
* @author Aave
* @notice Implements error messages.
*/
library Errors {
// require error messages - ValidationLogic
string public constant AMOUNT_NOT_GREATER_THAN_0 = '1'; // 'Amount must be greater than 0'
string public constant NO_ACTIVE_RESERVE = '2'; // 'Action requires an active reserve'
string public constant NO_UNFREEZED_RESERVE = '3'; // 'Action requires an unfreezed reserve'
string public constant CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH = '4'; // 'The current liquidity is not enough'
string public constant NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5'; // 'User cannot withdraw more than the available balance'
string public constant TRANSFER_NOT_ALLOWED = '6'; // 'Transfer cannot be allowed.'
string public constant BORROWING_NOT_ENABLED = '7'; // 'Borrowing is not enabled'
string public constant INVALID_INTEREST_RATE_MODE_SELECTED = '8'; // 'Invalid interest rate mode selected'
string public constant COLLATERAL_BALANCE_IS_0 = '9'; // 'The collateral balance is 0'
string public constant HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10'; // 'Health factor is lesser than the liquidation threshold'
string public constant COLLATERAL_CANNOT_COVER_NEW_BORROW = '11'; // 'There is not enough collateral to cover a new borrow'
string public constant STABLE_BORROWING_NOT_ENABLED = '12'; // stable borrowing not enabled
string public constant CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13'; // collateral is (mostly) the same currency that is being borrowed
string public constant AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14'; // 'The requested amount is greater than the max loan size in stable rate mode
string public constant NO_DEBT_OF_SELECTED_TYPE = '15'; // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt'
string public constant NO_EXPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16'; // 'To repay on behalf of an user an explicit amount to repay is needed'
string public constant NO_STABLE_RATE_LOAN_IN_RESERVE = '17'; // 'User does not have a stable rate loan in progress on this reserve'
string public constant NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18'; // 'User does not have a variable rate loan in progress on this reserve'
string public constant UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19'; // 'The underlying balance needs to be greater than 0'
string public constant DEPOSIT_ALREADY_IN_USE = '20'; // 'User deposit is already being used as collateral'
// require error messages - LendingPool
string public constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '21'; // 'User does not have any stable rate loan for this reserve'
string public constant INTEREST_RATE_REBALANCE_CONDITIONS_NOT_MET = '22'; // 'Interest rate rebalance conditions were not met'
string public constant LIQUIDATION_CALL_FAILED = '23'; // 'Liquidation call failed'
string public constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '24'; // 'There is not enough liquidity available to borrow'
string public constant REQUESTED_AMOUNT_TOO_SMALL = '25'; // 'The requested amount is too small for a FlashLoan.'
string public constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '26'; // 'The actual balance of the protocol is inconsistent'
string public constant CALLER_NOT_LENDING_POOL_CONFIGURATOR = '27'; // 'The actual balance of the protocol is inconsistent'
// require error messages - aToken
string public constant CALLER_MUST_BE_LENDING_POOL = '28'; // 'The caller of this function must be a lending pool'
string public constant INTEREST_REDIRECTION_NOT_ALLOWED = '29'; // 'Caller is not allowed to redirect the interest of the user'
string public constant CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30'; // 'User cannot give allowance to himself'
string public constant TRANSFER_AMOUNT_NOT_GT_0 = '31'; // 'Transferred amount needs to be greater than zero'
string public constant INTEREST_ALREADY_REDIRECTED = '32'; // 'Interest is already redirected to the user'
string public constant NO_VALID_BALANCE_FOR_REDIRECTION = '33'; // 'Interest stream can only be redirected if there is a valid balance'
// require error messages - ReserveLogic
string public constant RESERVE_ALREADY_INITIALIZED = '34'; // 'Reserve has already been initialized'
//require error messages - LendingPoolConfiguration
string public constant CALLER_NOT_LENDING_POOL_MANAGER = '35'; // 'The caller must be a lending pool manager'
string public constant RESERVE_LIQUIDITY_NOT_0 = '36'; // 'The liquidity of the reserve needs to be 0'
//require error messages - LendingPoolAddressesProviderRegistry
string public constant PROVIDER_NOT_REGISTERED = '37'; // 'Provider is not registered'
//return error messages - LendingPoolLiquidationManager
string public constant HEALTH_FACTOR_NOT_BELOW_THRESHOLD = '38'; // 'Health factor is not below the threshold'
string public constant COLLATERAL_CANNOT_BE_LIQUIDATED = '39'; // 'The collateral chosen cannot be liquidated'
string public constant SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '40'; // 'User did not borrow the specified currency'
string public constant NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '41'; // "There isn't enough liquidity available to liquidate"
string public constant NO_ERRORS = '42'; // 'No errors'
string public constant INVALID_FLASHLOAN_MODE = '43'; //Invalid flashloan mode selected
}

View File

@ -10,6 +10,7 @@ import {IStableDebtToken} from '../../tokenization/interfaces/IStableDebtToken.s
import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
import {IReserveInterestRateStrategy} from '../../interfaces/IReserveInterestRateStrategy.sol';
import {WadRayMath} from '../math/WadRayMath.sol';
import {Errors} from '../helpers/Errors.sol';
/**
* @title ReserveLogic library
@ -192,7 +193,7 @@ library ReserveLogic {
address variableDebtTokenAddress,
address interestRateStrategyAddress
) external {
require(reserve.aTokenAddress == address(0), 'Reserve has already been initialized');
require(reserve.aTokenAddress == address(0), Errors.RESERVE_ALREADY_INITIALIZED);
if (reserve.lastLiquidityIndex == 0) {
//if the reserve has not been initialized yet
reserve.lastLiquidityIndex = uint128(WadRayMath.ray());
@ -226,6 +227,7 @@ library ReserveLogic {
function updateInterestRates(
ReserveData storage reserve,
address reserveAddress,
address aTokenAddress,
uint256 liquidityAdded,
uint256 liquidityTaken
) internal {
@ -233,7 +235,7 @@ library ReserveLogic {
vars.stableDebtTokenAddress = reserve.stableDebtTokenAddress;
vars.currentAvgStableRate = IStableDebtToken(vars.stableDebtTokenAddress).getAverageStableRate();
vars.availableLiquidity = IERC20(reserveAddress).balanceOf(reserve.aTokenAddress);
vars.availableLiquidity = IERC20(reserveAddress).balanceOf(aTokenAddress);
(
vars.newLiquidityRate,

View File

@ -12,6 +12,7 @@ import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol';
import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
import {UserConfiguration} from '../configuration/UserConfiguration.sol';
import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol';
import {Errors} from '../helpers/Errors.sol';
/**
* @title ReserveLogic library
@ -32,15 +33,12 @@ library ValidationLogic {
* @param reserve the reserve state on which the user is depositing
* @param amount the amount to be deposited
*/
function validateDeposit(ReserveLogic.ReserveData storage reserve, uint256 amount)
internal
view
{
function validateDeposit(ReserveLogic.ReserveData storage reserve, uint256 amount) internal view {
(bool isActive, bool isFreezed, , ) = reserve.configuration.getFlags();
require(amount > 0, 'Amount must be greater than 0');
require(isActive, 'Action requires an active reserve');
require(!isFreezed, 'Action requires an unfreezed reserve');
require(amount > 0, Errors.AMOUNT_NOT_GREATER_THAN_0);
require(isActive, Errors.NO_ACTIVE_RESERVE);
require(!isFreezed, Errors.NO_UNFREEZED_RESERVE);
}
/**
@ -60,13 +58,9 @@ library ValidationLogic {
address[] calldata reserves,
address oracle
) external view {
require(amount > 0, 'Amount must be greater than 0');
require(amount > 0, Errors.AMOUNT_NOT_GREATER_THAN_0);
uint256 currentAvailableLiquidity = IERC20(reserveAddress).balanceOf(address(aTokenAddress));
require(currentAvailableLiquidity >= amount, '4');
require(amount <= userBalance, 'User cannot withdraw more than the available balance');
require(amount <= userBalance, Errors.NOT_ENOUGH_AVAILABLE_USER_BALANCE);
require(
GenericLogic.balanceDecreaseAllowed(
@ -78,7 +72,7 @@ library ValidationLogic {
reserves,
oracle
),
'Transfer cannot be allowed.'
Errors.TRANSFER_NOT_ALLOWED
);
}
@ -138,23 +132,18 @@ library ValidationLogic {
vars.stableRateBorrowingEnabled
) = reserve.configuration.getFlags();
require(vars.isActive, 'Action requires an active reserve');
require(!vars.isFreezed, 'Action requires an unfreezed reserve');
require(vars.isActive, Errors.NO_ACTIVE_RESERVE);
require(!vars.isFreezed, Errors.NO_UNFREEZED_RESERVE);
require(vars.borrowingEnabled, '5');
require(vars.borrowingEnabled, Errors.BORROWING_NOT_ENABLED);
//validate interest rate mode
require(
uint256(ReserveLogic.InterestRateMode.VARIABLE) == interestRateMode ||
uint256(ReserveLogic.InterestRateMode.STABLE) == interestRateMode,
'Invalid interest rate mode selected'
Errors.INVALID_INTEREST_RATE_MODE_SELECTED
);
//check that the amount is available in the reserve
vars.availableLiquidity = IERC20(reserveAddress).balanceOf(address(reserve.aTokenAddress));
require(vars.availableLiquidity >= amount, '7');
(
vars.userCollateralBalanceETH,
vars.userBorrowBalanceETH,
@ -169,9 +158,12 @@ library ValidationLogic {
oracle
);
require(vars.userCollateralBalanceETH > 0, 'The collateral balance is 0');
require(vars.userCollateralBalanceETH > 0, Errors.COLLATERAL_BALANCE_IS_0);
require(vars.healthFactor > GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD, '8');
require(
vars.healthFactor > GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD,
Errors.HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD
);
//add the current already borrowed amount to the amount requested to calculate the total collateral needed.
vars.amountOfCollateralNeededETH = vars.userBorrowBalanceETH.add(amountInETH).percentDiv(
@ -180,7 +172,7 @@ library ValidationLogic {
require(
vars.amountOfCollateralNeededETH <= vars.userCollateralBalanceETH,
'There is not enough collateral to cover a new borrow'
Errors.COLLATERAL_CANNOT_COVER_NEW_BORROW
);
/**
@ -195,20 +187,20 @@ library ValidationLogic {
if (vars.rateMode == ReserveLogic.InterestRateMode.STABLE) {
//check if the borrow mode is stable and if stable rate borrowing is enabled on this reserve
require(vars.stableRateBorrowingEnabled, '11');
require(vars.stableRateBorrowingEnabled, Errors.STABLE_BORROWING_NOT_ENABLED);
require(
!userConfig.isUsingAsCollateral(reserve.index) ||
reserve.configuration.getLtv() == 0 ||
amount > IERC20(reserve.aTokenAddress).balanceOf(msg.sender),
'12'
Errors.CALLATERAL_SAME_AS_BORROWING_CURRENCY
);
//calculate the max available loan size in stable rate mode as a percentage of the
//available liquidity
uint256 maxLoanSizeStable = vars.availableLiquidity.percentMul(maxStableLoanPercent);
require(amount <= maxLoanSizeStable, '13');
require(amount <= maxLoanSizeStable, Errors.AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE);
}
}
@ -230,21 +222,21 @@ library ValidationLogic {
) external view {
bool isActive = reserve.configuration.getActive();
require(isActive, 'Action requires an active reserve');
require(isActive, Errors.NO_ACTIVE_RESERVE);
require(amountSent > 0, 'Amount must be greater than 0');
require(amountSent > 0, Errors.AMOUNT_NOT_GREATER_THAN_0);
require(
(stableDebt > 0 &&
ReserveLogic.InterestRateMode(rateMode) == ReserveLogic.InterestRateMode.STABLE) ||
(variableDebt > 0 &&
ReserveLogic.InterestRateMode(rateMode) == ReserveLogic.InterestRateMode.VARIABLE),
'16'
Errors.NO_DEBT_OF_SELECTED_TYPE
);
require(
amountSent != uint256(-1) || msg.sender == onBehalfOf,
'To repay on behalf of an user an explicit amount to repay is needed'
Errors.NO_EXPLICIT_AMOUNT_TO_REPAY_ON_BEHALF
);
}
@ -265,19 +257,13 @@ library ValidationLogic {
) external view {
(bool isActive, bool isFreezed, , bool stableRateEnabled) = reserve.configuration.getFlags();
require(isActive, 'Action requires an active reserve');
require(!isFreezed, 'Action requires an unfreezed reserve');
require(isActive, Errors.NO_ACTIVE_RESERVE);
require(!isFreezed, Errors.NO_UNFREEZED_RESERVE);
if (currentRateMode == ReserveLogic.InterestRateMode.STABLE) {
require(
stableBorrowBalance > 0,
'User does not have a stable rate loan in progress on this reserve'
);
require(stableBorrowBalance > 0, Errors.NO_STABLE_RATE_LOAN_IN_RESERVE);
} else if (currentRateMode == ReserveLogic.InterestRateMode.VARIABLE) {
require(
variableBorrowBalance > 0,
'User does not have a variable rate loan in progress on this reserve'
);
require(variableBorrowBalance > 0, Errors.NO_VARIABLE_RATE_LOAN_IN_RESERVE);
/**
* user wants to swap to stable, before swapping we need to ensure that
* 1. stable borrow rate is enabled on the reserve
@ -285,17 +271,17 @@ library ValidationLogic {
* more collateral than he is borrowing, artificially lowering
* the interest rate, borrowing at variable, and switching to stable
**/
require(stableRateEnabled, '11');
require(stableRateEnabled, Errors.STABLE_BORROWING_NOT_ENABLED);
require(
!userConfig.isUsingAsCollateral(reserve.index) ||
reserve.configuration.getLtv() == 0 ||
stableBorrowBalance.add(variableBorrowBalance) >
IERC20(reserve.aTokenAddress).balanceOf(msg.sender),
'12'
Errors.CALLATERAL_SAME_AS_BORROWING_CURRENCY
);
} else {
revert('Invalid interest rate mode selected');
revert(Errors.INVALID_INTEREST_RATE_MODE_SELECTED);
}
}
@ -318,7 +304,7 @@ library ValidationLogic {
) external view {
uint256 underlyingBalance = IERC20(reserve.aTokenAddress).balanceOf(msg.sender);
require(underlyingBalance > 0, '22');
require(underlyingBalance > 0, Errors.UNDERLYING_BALANCE_NOT_GREATER_THAN_0);
require(
GenericLogic.balanceDecreaseAllowed(
@ -330,7 +316,17 @@ library ValidationLogic {
reserves,
oracle
),
'User deposit is already being used as collateral'
Errors.DEPOSIT_ALREADY_IN_USE
);
}
/**
* @dev validates a flashloan action
* @param mode the flashloan mode (0 = classic flashloan, 1 = open a stable rate loan, 2 = open a variable rate loan)
* @param premium the premium paid on the flashloan
**/
function validateFlashloan(uint256 mode, uint256 premium) internal pure {
require(premium > 0, Errors.REQUESTED_AMOUNT_TOO_SMALL);
require(mode <= uint256(ReserveLogic.InterestRateMode.VARIABLE), Errors.INVALID_FLASHLOAN_MODE);
}
}

View File

@ -13,46 +13,54 @@ contract MockFlashLoanReceiver is FlashLoanReceiverBase {
using SafeMath for uint256;
using SafeERC20 for IERC20;
ILendingPoolAddressesProvider internal _provider;
event ExecutedWithFail(address _reserve, uint256 _amount, uint256 _fee);
event ExecutedWithSuccess(address _reserve, uint256 _amount, uint256 _fee);
bool failExecution = false;
bool _failExecution;
uint256 _amountToApprove;
constructor(ILendingPoolAddressesProvider _provider) public FlashLoanReceiverBase(_provider) {}
constructor(ILendingPoolAddressesProvider provider) public FlashLoanReceiverBase(provider) {}
function setFailExecutionTransfer(bool _fail) public {
failExecution = _fail;
function setFailExecutionTransfer(bool fail) public {
_failExecution = fail;
}
function setAmountToApprove(uint256 amountToApprove) public {
_amountToApprove = amountToApprove;
}
function amountToApprove() public view returns (uint256) {
return _amountToApprove;
}
function executeOperation(
address _reserve,
address _destination,
uint256 _amount,
uint256 _fee,
bytes memory _params
address reserve,
uint256 amount,
uint256 fee,
bytes memory params
) public override {
//mint to this contract the specific amount
MintableERC20 token = MintableERC20(_reserve);
MintableERC20 token = MintableERC20(reserve);
//check the contract has the specified balance
require(
_amount <= IERC20(_reserve).balanceOf(address(this)),
'Invalid balance for the contract'
);
require(amount <= IERC20(reserve).balanceOf(address(this)), 'Invalid balance for the contract');
if (failExecution) {
emit ExecutedWithFail(_reserve, _amount, _fee);
uint256 amountToReturn = (_amountToApprove != 0) ? _amountToApprove : amount.add(fee);
if (_failExecution) {
emit ExecutedWithFail(reserve, amount, fee);
return;
}
//execution does not fail - mint tokens and return them to the _destination
//note: if the reserve is eth, the mock contract must receive at least _fee ETH before calling executeOperation
token.mint(_fee);
token.mint(fee);
//returning amount + fee to the destination
_transferFundsBack(_reserve, _destination, _amount.add(_fee));
IERC20(reserve).approve(_addressesProvider.getLendingPool(), amountToReturn);
emit ExecutedWithSuccess(_reserve, _amount, _fee);
emit ExecutedWithSuccess(reserve, amount, fee);
}
}

View File

@ -4,6 +4,7 @@ pragma solidity ^0.6.8;
import {ERC20} from './ERC20.sol';
import {LendingPool} from '../lendingpool/LendingPool.sol';
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol';
import {
VersionedInitializable
@ -34,12 +35,12 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
uint256 public constant ATOKEN_REVISION = 0x1;
modifier onlyLendingPool {
require(msg.sender == address(_pool), 'The caller of this function must be a lending pool');
require(msg.sender == address(_pool), Errors.CALLER_MUST_BE_LENDING_POOL);
_;
}
modifier whenTransferAllowed(address from, uint256 amount) {
require(isTransferAllowed(from, amount), 'Transfer cannot be allowed.');
require(isTransferAllowed(from, amount), Errors.TRANSFER_NOT_ALLOWED);
_;
}
@ -100,7 +101,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
function redirectInterestStreamOf(address from, address to) external override {
require(
msg.sender == _interestRedirectionAllowances[from],
'Caller is not allowed to redirect the interest of the user'
Errors.INTEREST_REDIRECTION_NOT_ALLOWED
);
_redirectInterestStream(from, to);
}
@ -112,7 +113,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
* the allowance.
**/
function allowInterestRedirectionTo(address to) external override {
require(to != msg.sender, 'User cannot give allowance to himself');
require(to != msg.sender, Errors.CANNOT_GIVE_ALLOWANCE_TO_HIMSELF);
_interestRedirectionAllowances[msg.sender] = to;
emit InterestRedirectionAllowanceChanged(msg.sender, to);
}
@ -434,15 +435,12 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
address to,
uint256 value
) internal {
require(value > 0, 'Transferred amount needs to be greater than zero');
require(value > 0, Errors.TRANSFER_AMOUNT_NOT_GT_0);
//cumulate the balance of the sender
(
,
uint256 fromBalance,
uint256 fromBalanceIncrease,
uint256 fromIndex
) = _cumulateBalance(from);
(, uint256 fromBalance, uint256 fromBalanceIncrease, uint256 fromIndex) = _cumulateBalance(
from
);
//cumulate the balance of the receiver
(, , uint256 toBalanceIncrease, uint256 toIndex) = _cumulateBalance(to);
@ -486,7 +484,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
function _redirectInterestStream(address from, address to) internal {
address currentRedirectionAddress = _interestRedirectionAddresses[from];
require(to != currentRedirectionAddress, 'Interest is already redirected to the user');
require(to != currentRedirectionAddress, Errors.INTEREST_ALREADY_REDIRECTED);
//accumulates the accrued interest to the principal
(
@ -496,7 +494,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
uint256 fromIndex
) = _cumulateBalance(from);
require(fromBalance > 0, 'Interest stream can only be redirected if there is a valid balance');
require(fromBalance > 0, Errors.NO_VALID_BALANCE_FOR_REDIRECTION);
//if the user is already redirecting the interest to someone, before changing
//the redirection address we substract the redirected balance of the previous

View File

@ -9,6 +9,7 @@ import {
VersionedInitializable
} from '../../libraries/openzeppelin-upgradeability/VersionedInitializable.sol';
import {IERC20Detailed} from '../../interfaces/IERC20Detailed.sol';
import {Errors} from '../../libraries/helpers/Errors.sol';
/**
* @title contract DebtTokenBase
@ -40,7 +41,7 @@ abstract contract DebtTokenBase is IERC20Detailed, VersionedInitializable {
* @dev only lending pool can call functions marked by this modifier
**/
modifier onlyLendingPool {
require(msg.sender == address(_pool), 'The caller of this function must be a lending pool');
require(msg.sender == address(_pool), Errors.CALLER_MUST_BE_LENDING_POOL);
_;
}

View File

@ -5,7 +5,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x463Ff14E7AA1312b897638AA40deA4FE95065D9d",
"address": "0x58F132FBB86E21545A4Bace3C19f1C05d86d7A22",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -15,7 +15,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x4B9b22A3Ae2465Fa849cf33fDAA26E73e12d0524",
"address": "0xa4bcDF64Cdd5451b6ac3743B414124A6299B65FF",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -25,7 +25,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x429444559a38F377d62a74EDB41FA33499cBa254",
"address": "0x5A0773Ff307Bf7C71a832dBB5312237fD3437f9F",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -53,7 +53,7 @@
"address": "0x6642B57e4265BAD868C17Fc1d1F4F88DBBA04Aa8"
},
"localhost": {
"address": "0xeB83C1577c44B99eB783e8eCC740cCcB39e6038a"
"address": "0x6642B57e4265BAD868C17Fc1d1F4F88DBBA04Aa8"
}
},
"LendingPoolDataProvider": {
@ -66,7 +66,7 @@
"address": "0xD9273d497eDBC967F39d419461CfcF382a0A822e"
},
"localhost": {
"address": "0xdFeCf1CAA3cDd9852cE7fD029720386bE2d5211e"
"address": "0xD9273d497eDBC967F39d419461CfcF382a0A822e"
}
},
"PriceOracle": {
@ -75,7 +75,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x85E78da53D4bdEb2ffF1CD95bfFb5989419a42F0",
"address": "0x1750499D05Ed1674d822430FB960d5F6731fDf64",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -85,7 +85,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xA4FF07Cd85f153004AaD52b39eD96C5Ad9732CBD",
"address": "0xEC1C93A9f6a9e18E97784c76aC52053587FcDB89",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -95,7 +95,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x5F81EB3b93AC6D83aCe2Ae179A936F6A8ECb2651",
"address": "0x7B6C3e5486D9e6959441ab554A889099eed76290",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -105,7 +105,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x07C1cd8182AAda58009D3b547295A64046679666",
"address": "0xD83D2773a7873ae2b5f8Fb92097e20a8C64F691E",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -115,7 +115,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xF25e04520a404a7C93194fE45aCB370E2168E73A",
"address": "0x626FdE749F9d499d3777320CAf29484B624ab84a",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -169,7 +169,7 @@
"address": "0x2B681757d757fbB80cc51c6094cEF5eE75bF55aA"
},
"localhost": {
"address": "0x03049DF4d8730C375CAe2c13a542cCE873369e20"
"address": "0x2B681757d757fbB80cc51c6094cEF5eE75bF55aA"
}
},
"WalletBalanceProvider": {
@ -178,7 +178,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x99b0df288A2Ddf84850157b04ef653833e668abE",
"address": "0xBEF0d4b9c089a5883741fC14cbA352055f35DDA2",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -188,7 +188,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x377eb7cBF694dd5a81c0381d4275d47941cc30F0",
"address": "0x7c2C195CD6D34B8F845992d380aADB2730bB9C6F",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -198,7 +198,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x052C908CD1fE0944428598f923289b9069D949b7",
"address": "0x8858eeB3DfffA017D4BCE9801D340D36Cf895CCf",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -208,7 +208,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x3e2b7c9bbfF5bb238f7Ee1Da97b4Ff7f4B367d1F",
"address": "0x0078371BDeDE8aAc7DeBfFf451B74c5EDB385Af7",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -218,7 +218,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x803B0Efe9d0D03d814f22a1ce6934ce00C6ad34E",
"address": "0xf4e77E5Da47AC3125140c470c71cBca77B5c638c",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -228,7 +228,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x74cAC7EE27ad5e6fa3157df4968BB51D6Fc71105",
"address": "0x3619DbE27d7c1e7E91aA738697Ae7Bc5FC3eACA5",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -238,7 +238,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x34634C72A8a87F8901B4B5669E7B1Ddc85e4D94d",
"address": "0x038B86d9d8FAFdd0a02ebd1A476432877b0107C8",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -248,7 +248,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xA191888d7d41e93db29D05507F0a81D6B01e9C87",
"address": "0x1A1FEe7EeD918BD762173e4dc5EfDB8a78C924A8",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -258,7 +258,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x7077FAaD4f62226913c99971C56885c9Ccc4A272",
"address": "0x500D1d6A4c7D8Ae28240b47c8FCde034D827fD5e",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -268,7 +268,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x8Ec0d65FA416f7F38Ea82768c2F242426Cf25F26",
"address": "0xc4905364b78a742ccce7B890A89514061E47068D",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -278,7 +278,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x0E287EACa131b5d80FA3d73e3d55a657bee2f5ee",
"address": "0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -288,7 +288,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x88A3c52bdD1f300D7471683d058b9C086A0477f2",
"address": "0x8B5B7a6055E54a36fF574bbE40cf2eA68d5554b3",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -298,7 +298,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xd014958E8666bAc96134EE289C04A0F246aaE606",
"address": "0xEcc0a6dbC0bb4D51E4F84A315a9e5B0438cAD4f0",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -308,7 +308,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x495719D5350d7E7bc192DfCAb32E77C7e35534C3",
"address": "0x20Ce94F404343aD2752A2D01b43fa407db9E0D00",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -318,7 +318,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xcc714eA59Ce23ed56A5909dadEbe6EB8323C8111",
"address": "0x1d80315fac6aBd3EfeEbE97dEc44461ba7556160",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -328,7 +328,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xbe0b1af73419afC9754e8cA9B0Cf2C11b1A4f3AE",
"address": "0x2D8553F9ddA85A9B3259F6Bf26911364B85556F5",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -338,7 +338,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x754159f9ae9277ca60E55ab0b06862Fb513d87B7",
"address": "0x52d3b94181f8654db2530b0fEe1B19173f519C52",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -348,7 +348,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xdee2a1ccb44676064284b424313bBdf1673ab0a2",
"address": "0xd15468525c35BDBC1eD8F2e09A00F8a173437f2f",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -358,7 +358,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xdb86D18F071330B129F6E456DFF37231044BFB05",
"address": "0x7e35Eaf7e8FBd7887ad538D4A38Df5BbD073814a",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -368,7 +368,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x05Fc03089bdf5E55E8864CA5df0d9D728d880842",
"address": "0x5bcb88A0d20426e451332eE6C4324b0e663c50E0",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -378,7 +378,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x20cB8a49019650A2bCF31E8E43925FDeaCd4e9b0",
"address": "0x3521eF8AaB0323004A6dD8b03CE890F4Ea3A13f5",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -388,7 +388,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xFab2Da6dc0B0a846848A8cF2799ba85D4309D073",
"address": "0x53369fd4680FfE3DfF39Fc6DDa9CfbfD43daeA2E",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -398,7 +398,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xFD34D9D62dF5f8867ac399637C32EB6F91efA697",
"address": "0xB00cC45B4a7d3e1FEE684cFc4417998A1c183e6d",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -408,7 +408,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x463Ff14E7AA1312b897638AA40deA4FE95065D9d",
"address": "0x58F132FBB86E21545A4Bace3C19f1C05d86d7A22",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -417,7 +417,7 @@
"address": "0xDf73fC454FA018051D4a1509e63D11530A59DE10"
},
"localhost": {
"address": "0xe3962a83c698CC25DFF81F98B08a9c2e749B367C"
"address": "0xDf73fC454FA018051D4a1509e63D11530A59DE10"
}
},
"StableDebtToken": {
@ -426,7 +426,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xC277bEF631f1B3F6F4D05a125Cbcb27bDA349240",
"address": "0xB660Fdd109a95718cB9d20E3A89EE6cE342aDcB6",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -436,13 +436,13 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xFa0FaAC558C92d751aD68E6C90FEf09fdA204749",
"address": "0x830bceA96E56DBC1F8578f75fBaC0AF16B32A07d",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
"AToken": {
"localhost": {
"address": "0xcc130B9925E9c083df4fc6dC3f60BE3d6B68Bf13",
"address": "0xA0AB1cB92A4AF81f84dCd258155B5c25D247b54E",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"buidlerevm": {
@ -456,7 +456,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xDf5b1bff699b50c68F36aC9817d2b2988554013e",
"address": "0x1203D1b97BF6E546c00C45Cda035D3010ACe1180",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -466,7 +466,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xF11D0A572031bE913eb3A36B6337098fA9532721",
"address": "0xf784709d2317D872237C4bC22f867d1BAe2913AB",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -476,7 +476,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xF68e61A94d6e5fa2b5061156a51a6714A30b13FA",
"address": "0x8733AfE8174BA7c04c6CD694bD673294079b7E10",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -486,7 +486,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x94FD7F068bB3F75e9FD4f77A13ddaF2B0ef99f0c",
"address": "0xA8083d78B6ABC328b4d3B714F76F384eCC7147e1",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
}

View File

@ -44,25 +44,73 @@ export enum eContractid {
}
export enum ProtocolErrors {
INVALID_CONFIGURATOR_CALLER_MSG = 'The caller must be a lending pool configurator contract',
INVALID_POOL_CALLER_MSG = 'The caller must be a lending pool contract',
INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool',
INVALID_POOL_MANAGER_CALLER_MSG = 'The caller must be a lending pool manager',
// require error messages - ValidationLogic
AMOUNT_NOT_GREATER_THAN_0 = '1', // 'Amount must be greater than 0'
NO_ACTIVE_RESERVE = '2', // 'Action requires an active reserve'
NO_UNFREEZED_RESERVE = '3', // 'Action requires an unfreezed reserve'
CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH = '4', // 'The current liquidity is not enough'
NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5', // 'User cannot withdraw more than the available balance'
TRANSFER_NOT_ALLOWED = '6', // 'Transfer cannot be allowed.'
BORROWING_NOT_ENABLED = '7', // 'Borrowing is not enabled'
INVALID_INTEREST_RATE_MODE_SELECTED = '8', // 'Invalid interest rate mode selected'
COLLATERAL_BALANCE_IS_0 = '9', // 'The collateral balance is 0'
HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10', // 'Health factor is lesser than the liquidation threshold'
COLLATERAL_CANNOT_COVER_NEW_BORROW = '11', // 'There is not enough collateral to cover a new borrow'
STABLE_BORROWING_NOT_ENABLED = '12', // stable borrowing not enabled
CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13', // collateral is (mostly) the same currency that is being borrowed
AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14', // 'The requested amount is greater than the max loan size in stable rate mode
NO_DEBT_OF_SELECTED_TYPE = '15', // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt'
NO_EXPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16', // 'To repay on behalf of an user an explicit amount to repay is needed'
NO_STABLE_RATE_LOAN_IN_RESERVE = '17', // 'User does not have a stable rate loan in progress on this reserve'
NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18', // 'User does not have a variable rate loan in progress on this reserve'
UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19', // 'The underlying balance needs to be greater than 0'
DEPOSIT_ALREADY_IN_USE = '20', // 'User deposit is already being used as collateral'
// require error messages - LendingPool
NOT_ENOUGH_STABLE_BORROW_BALANCE = '21', // 'User does not have any stable rate loan for this reserve'
INTEREST_RATE_REBALANCE_CONDITIONS_NOT_MET = '22', // 'Interest rate rebalance conditions were not met'
LIQUIDATION_CALL_FAILED = '23', // 'Liquidation call failed'
NOT_ENOUGH_LIQUIDITY_TO_BORROW = '24', // 'There is not enough liquidity available to borrow'
REQUESTED_AMOUNT_TOO_SMALL = '25', // 'The requested amount is too small for a FlashLoan.'
INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '26', // 'The actual balance of the protocol is inconsistent'
CALLER_NOT_LENDING_POOL_CONFIGURATOR = '27', // 'The actual balance of the protocol is inconsistent'
// require error messages - aToken
CALLER_MUST_BE_LENDING_POOL = '28', // 'The caller of this function must be a lending pool'
INTEREST_REDIRECTION_NOT_ALLOWED = '29', // 'Caller is not allowed to redirect the interest of the user'
CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30', // 'User cannot give allowance to himself'
TRANSFER_AMOUNT_NOT_GT_0 = '31', // 'Transferred amount needs to be greater than zero'
INTEREST_ALREADY_REDIRECTED = '32', // 'Interest is already redirected to the user'
NO_VALID_BALANCE_FOR_REDIRECTION = '33', // 'Interest stream can only be redirected if there is a valid balance'
// require error messages - ReserveLogic
RESERVE_ALREADY_INITIALIZED = '34', // 'Reserve has already been initialized'
//require error messages - LendingPoolConfiguration
CALLER_NOT_LENDING_POOL_MANAGER = '35', // 'The caller must be a lending pool manager'
RESERVE_LIQUIDITY_NOT_0 = '36', // 'The liquidity of the reserve needs to be 0'
//require error messages - LendingPoolAddressesProviderRegistry
PROVIDER_NOT_REGISTERED = '37', // 'Provider is not registered'
//return error messages - LendingPoolLiquidationManager
HEALTH_FACTOR_NOT_BELOW_THRESHOLD = '38', // 'Health factor is not below the threshold'
COLLATERAL_CANNOT_BE_LIQUIDATED = '39', // 'The collateral chosen cannot be liquidated'
SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '40', // 'User did not borrow the specified currency'
NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '41', // "There isn't enough liquidity available to liquidate"
NO_ERRORS = '42', // 'No errors'
INVALID_FLASHLOAN_MODE = '43', //Invalid flashloan mode
// old
INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer',
INVALID_TO_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer',
INVALID_OWNER_REVERT_MSG = 'Ownable: caller is not the owner',
INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER = 'Invalid redirected balance before transfer',
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER = 'Invalid redirected balance after transfer',
INVALID_REDIRECTION_ADDRESS = 'Invalid redirection address',
TRANSFERRED_AMOUNT_GT_ZERO = 'Transferred amount needs to be greater than zero',
ZERO_COLLATERAL = 'The collateral balance is 0',
INCONSISTENT_PROTOCOL_BALANCE = 'The actual balance of the protocol is inconsistent',
TOO_SMALL_FLASH_LOAN = 'The requested amount is too small for a FlashLoan.',
NOT_ENOUGH_LIQUIDITY_TO_BORROW = 'There is not enough liquidity available to borrow',
HF_IS_NOT_BELLOW_THRESHOLD = 'Health factor is not below the threshold',
INVALID_HF = 'Invalid health factor',
USER_DID_NOT_BORROW_SPECIFIED = 'User did not borrow the specified currency',
THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED = 'The collateral chosen cannot be liquidated',
TRANSFER_AMOUNT_EXCEEDS_BALANCE = 'ERC20: transfer amount exceeds balance'
}
export type tEthereumAddress = string;

View File

@ -13,6 +13,7 @@
"types-gen": "typechain --target ethers-v5 --outDir ./types './artifacts/*.json'",
"test": "buidler test",
"test-scenarios": "buidler test test/__setup.spec.ts test/scenario.spec.ts",
"test-flash": "buidler test test/__setup.spec.ts test/flashloan.spec.ts",
"dev:coverage": "buidler coverage",
"dev:deployment": "buidler dev-deployment",
"dev:deployExample": "buidler deploy-Example",

View File

@ -3,17 +3,17 @@ import {makeSuite, TestEnv} from './helpers/make-suite';
import {ProtocolErrors} from '../helpers/types';
makeSuite('AToken: Modifiers', (testEnv: TestEnv) => {
const {INVALID_POOL_CALLER_MSG_1} = ProtocolErrors;
const {CALLER_MUST_BE_LENDING_POOL} = ProtocolErrors;
it('Tries to invoke mint not being the LendingPool', async () => {
const {deployer, aDai} = testEnv;
await expect(aDai.mint(deployer.address, '1')).to.be.revertedWith(INVALID_POOL_CALLER_MSG_1);
await expect(aDai.mint(deployer.address, '1')).to.be.revertedWith(CALLER_MUST_BE_LENDING_POOL);
});
it('Tries to invoke burn not being the LendingPool', async () => {
const {deployer, aDai} = testEnv;
await expect(aDai.burn(deployer.address, deployer.address, '1')).to.be.revertedWith(
INVALID_POOL_CALLER_MSG_1
CALLER_MUST_BE_LENDING_POOL
);
});
@ -21,13 +21,13 @@ makeSuite('AToken: Modifiers', (testEnv: TestEnv) => {
const {deployer, users, aDai} = testEnv;
await expect(
aDai.transferOnLiquidation(deployer.address, users[0].address, '1')
).to.be.revertedWith(INVALID_POOL_CALLER_MSG_1);
).to.be.revertedWith(CALLER_MUST_BE_LENDING_POOL);
});
it('Tries to invoke transferUnderlyingTo not being the LendingPool', async () => {
const {deployer, users, aDai} = testEnv;
await expect(aDai.transferUnderlyingTo(deployer.address, '1')).to.be.revertedWith(
INVALID_POOL_CALLER_MSG_1
CALLER_MUST_BE_LENDING_POOL
);
});
});

View File

@ -17,8 +17,10 @@ makeSuite('AToken: Transfer', (testEnv: TestEnv) => {
INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER,
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER,
INVALID_REDIRECTION_ADDRESS,
ZERO_COLLATERAL,
TRANSFERRED_AMOUNT_GT_ZERO,
// ZERO_COLLATERAL,
TRANSFER_AMOUNT_NOT_GT_0,
COLLATERAL_BALANCE_IS_0,
TRANSFER_NOT_ALLOWED,
} = ProtocolErrors;
it('User 0 deposits 1000 DAI, transfers to user 1', async () => {
@ -96,16 +98,14 @@ makeSuite('AToken: Transfer', (testEnv: TestEnv) => {
await weth.connect(users[0].signer).mint(await convertToCurrencyDecimals(weth.address, '1'));
await weth.connect(users[0].signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await pool
.connect(users[0].signer)
.deposit(weth.address, ethers.utils.parseEther('1.0'), '0');
await pool.connect(users[0].signer).deposit(weth.address, ethers.utils.parseEther('1.0'), '0');
await expect(
pool
.connect(users[1].signer)
.borrow(weth.address, ethers.utils.parseEther('0.1'), RateMode.Stable, AAVE_REFERRAL),
ZERO_COLLATERAL
).to.be.revertedWith(ZERO_COLLATERAL);
COLLATERAL_BALANCE_IS_0
).to.be.revertedWith(COLLATERAL_BALANCE_IS_0);
});
it('User 1 sets the DAI as collateral and borrows, tries to transfer everything back to user 0 (revert expected)', async () => {
@ -120,25 +120,25 @@ makeSuite('AToken: Transfer', (testEnv: TestEnv) => {
await expect(
aDai.connect(users[1].signer).transfer(users[0].address, aDAItoTransfer),
'Transfer cannot be allowed.'
).to.be.revertedWith('Transfer cannot be allowed.');
TRANSFER_NOT_ALLOWED
).to.be.revertedWith(TRANSFER_NOT_ALLOWED);
});
it('User 0 tries to transfer 0 balance (revert expected)', async () => {
const {users, pool, aDai, dai, weth} = testEnv;
await expect(
aDai.connect(users[0].signer).transfer(users[1].address, '0'),
TRANSFERRED_AMOUNT_GT_ZERO
).to.be.revertedWith(TRANSFERRED_AMOUNT_GT_ZERO);
TRANSFER_AMOUNT_NOT_GT_0
).to.be.revertedWith(TRANSFER_AMOUNT_NOT_GT_0);
});
it('User 1 repays the borrow, transfers aDAI back to user 0', async () => {
const {users, pool, aDai, dai, weth} = testEnv;
await weth.connect(users[1].signer).mint(await convertToCurrencyDecimals(weth.address, '2'));
await weth.connect(users[1].signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await pool
.connect(users[1].signer)
.repay(weth.address, MAX_UINT_AMOUNT, RateMode.Stable, users[1].address);

View File

@ -6,7 +6,7 @@ import {ProtocolErrors} from '../helpers/types';
const {expect} = require('chai');
makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {INVALID_POOL_MANAGER_CALLER_MSG} = ProtocolErrors;
const {CALLER_NOT_LENDING_POOL_MANAGER, RESERVE_LIQUIDITY_NOT_0} = ProtocolErrors;
it('Deactivates the ETH reserve', async () => {
const {configurator, pool, weth} = testEnv;
@ -27,16 +27,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).deactivateReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Check the onlyLendingPoolManager on activateReserve ', async () => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).activateReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Freezes the ETH reserve', async () => {
@ -58,16 +58,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).freezeReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Check the onlyLendingPoolManager on unfreezeReserve ', async () => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).unfreezeReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Deactivates the ETH reserve for borrowing', async () => {
@ -90,16 +90,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).disableBorrowingOnReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Check the onlyLendingPoolManager on enableBorrowingOnReserve ', async () => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).enableBorrowingOnReserve(weth.address, true),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Deactivates the ETH reserve as collateral', async () => {
@ -121,8 +121,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).disableReserveAsCollateral(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Check the onlyLendingPoolManager on enableReserveAsCollateral ', async () => {
@ -131,8 +131,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
configurator
.connect(users[2].signer)
.enableReserveAsCollateral(weth.address, '75', '80', '105'),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Disable stable borrow rate on the ETH reserve', async () => {
@ -153,16 +153,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).disableReserveStableRate(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Check the onlyLendingPoolManager on enableReserveStableRate', async () => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).enableReserveStableRate(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Changes LTV of the reserve', async () => {
@ -176,8 +176,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).setLtv(weth.address, '75'),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Changes liquidation threshold of the reserve', async () => {
@ -194,8 +194,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).setLiquidationThreshold(weth.address, '80'),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Changes liquidation bonus of the reserve', async () => {
@ -212,24 +212,24 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).setLiquidationBonus(weth.address, '80'),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Check the onlyLendingPoolManager on setReserveDecimals', async () => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).setReserveDecimals(weth.address, '80'),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Check the onlyLendingPoolManager on setLiquidationBonus', async () => {
const {configurator, users, weth} = testEnv;
await expect(
configurator.connect(users[2].signer).setLiquidationBonus(weth.address, '80'),
INVALID_POOL_MANAGER_CALLER_MSG
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Reverts when trying to disable the DAI reserve with liquidity on it', async () => {
@ -246,7 +246,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
await expect(
configurator.deactivateReserve(dai.address),
'The liquidity of the reserve needs to be 0'
).to.be.revertedWith('The liquidity of the reserve needs to be 0');
RESERVE_LIQUIDITY_NOT_0
).to.be.revertedWith(RESERVE_LIQUIDITY_NOT_0);
});
});

View File

@ -1,19 +1,26 @@
import {TestEnv, makeSuite} from './helpers/make-suite';
import {APPROVAL_AMOUNT_LENDING_POOL, oneRay} from '../helpers/constants';
import {convertToCurrencyDecimals, getMockFlashLoanReceiver} from '../helpers/contracts-helpers';
import {
convertToCurrencyDecimals,
getMockFlashLoanReceiver,
getContract,
} from '../helpers/contracts-helpers';
import {ethers} from 'ethers';
import {MockFlashLoanReceiver} from '../types/MockFlashLoanReceiver';
import {ProtocolErrors} from '../helpers/types';
import {ProtocolErrors, eContractid} from '../helpers/types';
import BigNumber from 'bignumber.js';
import {VariableDebtToken} from '../types/VariableDebtToken';
import {StableDebtToken} from '../types/StableDebtToken';
const {expect} = require('chai');
makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
let _mockFlashLoanReceiver = {} as MockFlashLoanReceiver;
const {
INCONSISTENT_PROTOCOL_BALANCE,
TOO_SMALL_FLASH_LOAN,
NOT_ENOUGH_LIQUIDITY_TO_BORROW,
COLLATERAL_BALANCE_IS_0,
REQUESTED_AMOUNT_TOO_SMALL,
TRANSFER_AMOUNT_EXCEEDS_BALANCE,
INVALID_FLASHLOAN_MODE
} = ProtocolErrors;
before(async () => {
@ -31,14 +38,16 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
await pool.deposit(weth.address, amountToDeposit, '0');
});
it('Takes ETH flashloan, returns the funds correctly', async () => {
it('Takes WETH flashloan with mode = 0, returns the funds correctly', async () => {
const {pool, deployer, weth} = testEnv;
await pool.flashLoan(
_mockFlashLoanReceiver.address,
weth.address,
ethers.utils.parseEther('0.8'),
'0x10'
0,
'0x10',
'0'
);
ethers.utils.parseUnits('10000');
@ -57,18 +66,17 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
expect(currentLiquidityIndex.toString()).to.be.equal('1000720000000000000000000000');
});
it('Takes an ETH flashloan as big as the available liquidity', async () => {
it('Takes an ETH flashloan with mode = 0 as big as the available liquidity', async () => {
const {pool, weth} = testEnv;
const reserveDataBefore = await pool.getReserveData(weth.address);
console.log("Total liquidity is ", reserveDataBefore.availableLiquidity.toString());
const txResult = await pool.flashLoan(
_mockFlashLoanReceiver.address,
weth.address,
'1000720000000000000',
'0x10'
0,
'0x10',
'0'
);
const reserveData = await pool.getReserveData(weth.address);
@ -85,21 +93,79 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
expect(currentLiquidityIndex.toString()).to.be.equal('1001620648000000000000000000');
});
it('Takes WETH flashloan, does not return the funds (revert expected)', async () => {
const {pool, deployer, weth} = testEnv;
// move funds to the MockFlashLoanReceiver contract to pay the fee
it('Takes WETH flashloan, does not return the funds with mode = 0. (revert expected)', async () => {
const {pool, weth, users} = testEnv;
const caller = users[1];
await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
await expect(
pool.flashLoan(
pool
.connect(caller.signer)
.flashLoan(
_mockFlashLoanReceiver.address,
weth.address,
ethers.utils.parseEther('0.8'),
0,
'0x10',
'0'
)
).to.be.revertedWith(TRANSFER_AMOUNT_EXCEEDS_BALANCE);
});
it('Takes a WETH flashloan with an invalid mode. (revert expected)', async () => {
const {pool, weth, users} = testEnv;
const caller = users[1];
await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
await expect(
pool
.connect(caller.signer)
.flashLoan(
_mockFlashLoanReceiver.address,
weth.address,
ethers.utils.parseEther('0.8'),
4,
'0x10',
'0'
)
).to.be.revertedWith(INVALID_FLASHLOAN_MODE);
});
it('Caller deposits 1000 DAI as collateral, Takes WETH flashloan with mode = 2, does not return the funds. A variable loan for caller is created', async () => {
const {dai, pool, weth, users} = testEnv;
const caller = users[1];
await dai.connect(caller.signer).mint(await convertToCurrencyDecimals(dai.address, '1000'));
await dai.connect(caller.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
const amountToDeposit = await convertToCurrencyDecimals(dai.address, '1000');
await pool.connect(caller.signer).deposit(dai.address, amountToDeposit, '0');
await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
await pool
.connect(caller.signer)
.flashLoan(
_mockFlashLoanReceiver.address,
weth.address,
ethers.utils.parseEther('0.8'),
'0x10'
)
).to.be.revertedWith(INCONSISTENT_PROTOCOL_BALANCE);
2,
'0x10',
'0'
);
const {variableDebtTokenAddress} = await pool.getReserveTokensAddresses(weth.address);
const wethDebtToken = await getContract<VariableDebtToken>(
eContractid.VariableDebtToken,
variableDebtTokenAddress
);
const callerDebt = await wethDebtToken.balanceOf(caller.address);
expect(callerDebt.toString()).to.be.equal('800720000000000000', 'Invalid user debt');
});
it('tries to take a very small flashloan, which would result in 0 fees (revert expected)', async () => {
@ -110,9 +176,11 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address,
weth.address,
'1', //1 wei loan
'0x10'
2,
'0x10',
'0'
)
).to.be.revertedWith(TOO_SMALL_FLASH_LOAN);
).to.be.revertedWith(REQUESTED_AMOUNT_TOO_SMALL);
});
it('tries to take a flashloan that is bigger than the available liquidity (revert expected)', async () => {
@ -123,45 +191,52 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address,
weth.address,
'1004415000000000000', //slightly higher than the available liquidity
'0x10'
2,
'0x10',
'0'
),
NOT_ENOUGH_LIQUIDITY_TO_BORROW
).to.be.revertedWith(NOT_ENOUGH_LIQUIDITY_TO_BORROW);
TRANSFER_AMOUNT_EXCEEDS_BALANCE
).to.be.revertedWith(TRANSFER_AMOUNT_EXCEEDS_BALANCE);
});
it('tries to take a flashloan using a non contract address as receiver (revert expected)', async () => {
const {pool, deployer, weth} = testEnv;
await expect(pool.flashLoan(deployer.address, weth.address, '1000000000000000000', '0x10')).to
.be.reverted;
await expect(
pool.flashLoan(deployer.address, weth.address, '1000000000000000000', 2, '0x10', '0')
).to.be.reverted;
});
it('Deposits DAI into the reserve', async () => {
const {dai, pool} = testEnv;
it('Deposits USDC into the reserve', async () => {
const {usdc, pool} = testEnv;
await dai.mint(await convertToCurrencyDecimals(dai.address, '1000'));
await usdc.mint(await convertToCurrencyDecimals(usdc.address, '1000'));
await dai.approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await usdc.approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
const amountToDeposit = await convertToCurrencyDecimals(dai.address, '1000');
const amountToDeposit = await convertToCurrencyDecimals(usdc.address, '1000');
await pool.deposit(dai.address, amountToDeposit, '0');
await pool.deposit(usdc.address, amountToDeposit, '0');
});
it('Takes out a 500 DAI flashloan, returns the funds correctly', async () => {
const {dai, pool, deployer: depositor} = testEnv;
it('Takes out a 500 USDC flashloan, returns the funds correctly', async () => {
const {usdc, pool, deployer: depositor} = testEnv;
await _mockFlashLoanReceiver.setFailExecutionTransfer(false);
const flashloanAmount = await convertToCurrencyDecimals(usdc.address, '500');
await pool.flashLoan(
_mockFlashLoanReceiver.address,
dai.address,
ethers.utils.parseEther('500'),
'0x10'
usdc.address,
flashloanAmount,
0,
'0x10',
'0'
);
const reserveData = await pool.getReserveData(dai.address);
const userData = await pool.getUserReserveData(dai.address, depositor.address);
const reserveData = await pool.getReserveData(usdc.address);
const userData = await pool.getUserReserveData(usdc.address, depositor.address);
const totalLiquidity = reserveData.availableLiquidity
.add(reserveData.totalBorrowsStable)
@ -171,7 +246,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
const currentLiquidityIndex = reserveData.liquidityIndex.toString();
const currentUserBalance = userData.currentATokenBalance.toString();
const expectedLiquidity = ethers.utils.parseEther('1000.450');
const expectedLiquidity = await convertToCurrencyDecimals(usdc.address, '1000.450');
expect(totalLiquidity).to.be.equal(expectedLiquidity, 'Invalid total liquidity');
expect(currentLiqudityRate).to.be.equal('0', 'Invalid liquidity rate');
@ -182,19 +257,101 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
expect(currentUserBalance.toString()).to.be.equal(expectedLiquidity, 'Invalid user balance');
});
it('Takes out a 500 DAI flashloan, does not return the funds (revert expected)', async () => {
const {dai, pool} = testEnv;
it('Takes out a 500 USDC flashloan with mode = 0, does not return the funds. (revert expected)', async () => {
const {usdc, pool, users} = testEnv;
const caller = users[2];
const flashloanAmount = await convertToCurrencyDecimals(usdc.address, '500');
await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
await expect(
pool.flashLoan(
_mockFlashLoanReceiver.address,
dai.address,
ethers.utils.parseEther('500'),
'0x10'
),
INCONSISTENT_PROTOCOL_BALANCE
).to.be.revertedWith(INCONSISTENT_PROTOCOL_BALANCE);
pool
.connect(caller.signer)
.flashLoan(_mockFlashLoanReceiver.address, usdc.address, flashloanAmount, 2, '0x10', '0')
).to.be.revertedWith(COLLATERAL_BALANCE_IS_0);
});
it('Caller deposits 5 WETH as collateral, Takes a USDC flashloan with mode = 2, does not return the funds. A loan for caller is created', async () => {
const {usdc, pool, weth, users} = testEnv;
const caller = users[2];
await weth.connect(caller.signer).mint(await convertToCurrencyDecimals(weth.address, '5'));
await weth.connect(caller.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
const amountToDeposit = await convertToCurrencyDecimals(weth.address, '5');
await pool.connect(caller.signer).deposit(weth.address, amountToDeposit, '0');
await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
const flashloanAmount = await convertToCurrencyDecimals(usdc.address, '500');
await pool
.connect(caller.signer)
.flashLoan(_mockFlashLoanReceiver.address, usdc.address, flashloanAmount, 2, '0x10', '0');
const {variableDebtTokenAddress} = await pool.getReserveTokensAddresses(usdc.address);
const usdcDebtToken = await getContract<VariableDebtToken>(
eContractid.VariableDebtToken,
variableDebtTokenAddress
);
const callerDebt = await usdcDebtToken.balanceOf(caller.address);
expect(callerDebt.toString()).to.be.equal('500450000', 'Invalid user debt');
});
it('Caller deposits 1000 DAI as collateral, Takes a WETH flashloan with mode = 0, does not approve the transfer of the funds', async () => {
const {dai, pool, weth, users} = testEnv;
const caller = users[3];
await dai.connect(caller.signer).mint(await convertToCurrencyDecimals(dai.address, '1000'));
await dai.connect(caller.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
const amountToDeposit = await convertToCurrencyDecimals(dai.address, '1000');
await pool.connect(caller.signer).deposit(dai.address, amountToDeposit, '0');
const flashAmount = ethers.utils.parseEther('0.8');
await _mockFlashLoanReceiver.setFailExecutionTransfer(false);
await _mockFlashLoanReceiver.setAmountToApprove(flashAmount.div(2));
await expect(
pool
.connect(caller.signer)
.flashLoan(_mockFlashLoanReceiver.address, weth.address, flashAmount, 0, '0x10', '0')
).to.be.revertedWith('ERC20: transfer amount exceeds allowance');
});
it('Caller takes a WETH flashloan with mode = 1', async () => {
const {dai, pool, weth, users} = testEnv;
const caller = users[3];
const flashAmount = ethers.utils.parseEther('0.8');
await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
await pool
.connect(caller.signer)
.flashLoan(_mockFlashLoanReceiver.address, weth.address, flashAmount, 1, '0x10', '0');
const {stableDebtTokenAddress} = await pool.getReserveTokensAddresses(weth.address);
const wethDebtToken = await getContract<StableDebtToken>(
eContractid.VariableDebtToken,
stableDebtTokenAddress
);
const callerDebt = await wethDebtToken.balanceOf(caller.address);
expect(callerDebt.toString()).to.be.equal('800720000000000000', 'Invalid user debt');
});
});

View File

@ -7,15 +7,14 @@ import {
EXCESS_UTILIZATION_RATE,
ZERO_ADDRESS,
} from '../../../helpers/constants';
import {IReserveParams, iAavePoolAssets, RateMode} from '../../../helpers/types';
import { IReserveParams, iAavePoolAssets, RateMode } from '../../../helpers/types';
import './math';
import {ReserveData, UserReserveData} from './interfaces';
import { ReserveData, UserReserveData } from './interfaces';
export const strToBN = (amount: string): BigNumber => new BigNumber(amount);
interface Configuration {
reservesParams: iAavePoolAssets<IReserveParams>;
ethereumAddress: string;
}
export const configuration: Configuration = <Configuration>{};
@ -66,17 +65,7 @@ export const calcExpectedUserDataAfterDeposit = (
}
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
if (reserveDataBeforeAction.address === configuration.ethereumAddress) {
// console.log("** ETH CASE ****")
expectedUserData.walletBalance = userDataBeforeAction.walletBalance
.minus(txCost)
.minus(amountDeposited);
} else {
// console.log("** TOKEN CASE ****")
// console.log(userDataBeforeAction.walletBalance.toString())
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(amountDeposited);
}
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(amountDeposited);
expectedUserData.principalATokenBalance = expectedUserData.currentATokenBalance = calcExpectedATokenBalance(
reserveDataBeforeAction,
@ -171,14 +160,7 @@ export const calcExpectedUserDataAfterWithdraw = (
}
expectedUserData.variableBorrowIndex = userDataBeforeAction.variableBorrowIndex;
if (reserveDataBeforeAction.address === configuration.ethereumAddress) {
expectedUserData.walletBalance = userDataBeforeAction.walletBalance
.minus(txCost)
.plus(amountWithdrawn);
} else {
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.plus(amountWithdrawn);
}
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.plus(amountWithdrawn);
expectedUserData.redirectedBalance = userDataBeforeAction.redirectedBalance;
@ -600,13 +582,7 @@ export const calcExpectedUserDataAfterBorrow = (
userDataBeforeAction.redirectionAddressRedirectedBalance;
expectedUserData.currentATokenUserIndex = userDataBeforeAction.currentATokenUserIndex;
if (reserveDataBeforeAction.address === configuration.ethereumAddress) {
expectedUserData.walletBalance = userDataBeforeAction.walletBalance
.minus(txCost)
.plus(amountBorrowed);
} else {
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.plus(amountBorrowed);
}
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.plus(amountBorrowed);
return expectedUserData;
};
@ -657,8 +633,7 @@ export const calcExpectedUserDataAfterRepay = (
expectedUserData.stableBorrowRate = expectedUserData.stableRateLastUpdated = new BigNumber(
'0'
);
}
else{
} else {
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
expectedUserData.stableRateLastUpdated = txTimestamp;
}
@ -697,14 +672,7 @@ export const calcExpectedUserDataAfterRepay = (
expectedUserData.currentATokenUserIndex = userDataBeforeAction.currentATokenUserIndex;
if (user === onBehalfOf) {
//if user repaid for himself, update the wallet balances
if (reserveDataBeforeAction.address === configuration.ethereumAddress) {
expectedUserData.walletBalance = userDataBeforeAction.walletBalance
.minus(txCost)
.minus(totalRepaid);
} else {
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(totalRepaid);
}
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(totalRepaid);
} else {
//wallet balance didn't change
expectedUserData.walletBalance = userDataBeforeAction.walletBalance;
@ -719,14 +687,10 @@ export const calcExpectedUserDataAfterSetUseAsCollateral = (
userDataBeforeAction: UserReserveData,
txCost: BigNumber
): UserReserveData => {
const expectedUserData = {...userDataBeforeAction};
const expectedUserData = { ...userDataBeforeAction };
expectedUserData.usageAsCollateralEnabled = useAsCollateral;
if (reserveDataBeforeAction.address === configuration.ethereumAddress) {
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(txCost);
}
return expectedUserData;
};
@ -829,7 +793,7 @@ export const calcExpectedUserDataAfterSwapRateMode = (
txCost: BigNumber,
txTimestamp: BigNumber
): UserReserveData => {
const expectedUserData = {...userDataBeforeAction};
const expectedUserData = { ...userDataBeforeAction };
const variableBorrowBalance = calcExpectedVariableDebtTokenBalance(
reserveDataBeforeAction,
@ -892,9 +856,6 @@ export const calcExpectedUserDataAfterSwapRateMode = (
expectedUserData.liquidityRate = expectedDataAfterAction.liquidityRate;
if (reserveDataBeforeAction.address === configuration.ethereumAddress) {
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(txCost);
}
return expectedUserData;
};
@ -976,7 +937,7 @@ export const calcExpectedUserDataAfterStableRateRebalance = (
txCost: BigNumber,
txTimestamp: BigNumber
): UserReserveData => {
const expectedUserData = {...userDataBeforeAction};
const expectedUserData = { ...userDataBeforeAction };
expectedUserData.principalVariableDebt = calcExpectedVariableDebtTokenBalance(
reserveDataBeforeAction,
@ -1000,12 +961,6 @@ export const calcExpectedUserDataAfterStableRateRebalance = (
expectedUserData.liquidityRate = expectedDataAfterAction.liquidityRate;
if (reserveDataBeforeAction.address === configuration.ethereumAddress) {
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(txCost);
}
expectedUserData.liquidityRate = expectedDataAfterAction.liquidityRate;
expectedUserData.currentATokenBalance = calcExpectedATokenBalance(
reserveDataBeforeAction,
userDataBeforeAction,
@ -1037,8 +992,8 @@ export const calcExpectedUsersDataAfterRedirectInterest = (
txCost: BigNumber,
txTimestamp: BigNumber
): UserReserveData[] => {
const expectedFromData = {...fromDataBeforeAction};
const expectedToData = {...toDataBeforeAction};
const expectedFromData = { ...fromDataBeforeAction };
const expectedToData = { ...toDataBeforeAction };
expectedFromData.currentStableDebt = calcExpectedStableDebtTokenBalance(
fromDataBeforeAction,
@ -1069,12 +1024,6 @@ export const calcExpectedUsersDataAfterRedirectInterest = (
txTimestamp
);
if (isFromExecutingTx) {
if (reserveDataBeforeAction.address === configuration.ethereumAddress) {
expectedFromData.walletBalance = fromDataBeforeAction.walletBalance.minus(txCost);
}
}
expectedToData.redirectedBalance = toDataBeforeAction.redirectedBalance.plus(
expectedFromData.currentATokenBalance
);
@ -1215,7 +1164,7 @@ export const calcExpectedVariableDebtTokenBalance = (
) => {
const debt = calcExpectedReserveNormalizedDebt(reserveDataBeforeAction, currentTimestamp);
const {principalVariableDebt, variableBorrowIndex} = userDataBeforeAction;
const { principalVariableDebt, variableBorrowIndex } = userDataBeforeAction;
if (variableBorrowIndex.eq(0)) {
return principalVariableDebt;
@ -1228,7 +1177,7 @@ export const calcExpectedStableDebtTokenBalance = (
userDataBeforeAction: UserReserveData,
currentTimestamp: BigNumber
) => {
const {principalStableDebt, stableBorrowRate, stableRateLastUpdated} = userDataBeforeAction;
const { principalStableDebt, stableBorrowRate, stableRateLastUpdated } = userDataBeforeAction;
if (
stableBorrowRate.eq(0) ||
@ -1301,7 +1250,7 @@ const calcExpectedInterestRates = (
totalBorrowsVariable: BigNumber,
averageStableBorrowRate: BigNumber
): BigNumber[] => {
const {reservesParams} = configuration;
const { reservesParams } = configuration;
const reserveIndex = Object.keys(reservesParams).findIndex((value) => value === reserveSymbol);
const [, reserveConfiguration] = (Object.entries(reservesParams) as [string, IReserveParams][])[
@ -1391,7 +1340,7 @@ const calcExpectedReserveNormalizedIncome = (
reserveData: ReserveData,
currentTimestamp: BigNumber
) => {
const {liquidityRate, liquidityIndex, lastUpdateTimestamp} = reserveData;
const { liquidityRate, liquidityIndex, lastUpdateTimestamp } = reserveData;
//if utilization rate is 0, nothing to compound
if (liquidityRate.eq('0')) {
@ -1413,7 +1362,7 @@ const calcExpectedReserveNormalizedDebt = (
reserveData: ReserveData,
currentTimestamp: BigNumber
) => {
const {variableBorrowRate, variableBorrowIndex, lastUpdateTimestamp} = reserveData;
const { variableBorrowRate, variableBorrowIndex, lastUpdateTimestamp } = reserveData;
//if utilization rate is 0, nothing to compound
if (variableBorrowRate.eq('0')) {
@ -1472,31 +1421,3 @@ const calcExpectedVariableBorrowIndex = (reserveData: ReserveData, timestamp: Bi
return cumulatedInterest.rayMul(reserveData.variableBorrowIndex);
};
export const calculateHealthFactorFromBalances = (
collateralBalanceETH: BigNumber,
borrowBalanceETH: BigNumber,
currentLiquidationThreshold: BigNumber
): BigNumber => {
if (borrowBalanceETH.eq(0)) {
return strToBN('-1'); // invalid number
}
return collateralBalanceETH.multipliedBy(currentLiquidationThreshold).div(borrowBalanceETH);
};
const calculateAvailableBorrowsETH = (
collateralBalanceETH: BigNumber,
borrowBalanceETH: BigNumber,
currentLtv: BigNumber
): BigNumber => {
if (currentLtv.eq(0)) {
return strToBN('0');
}
let availableBorrowsETH = collateralBalanceETH.multipliedBy(currentLtv);
if (availableBorrowsETH.lt(borrowBalanceETH)) {
return strToBN('0');
}
availableBorrowsETH = availableBorrowsETH.minus(borrowBalanceETH);
const borrowFee = availableBorrowsETH.multipliedBy(0.0025);
return availableBorrowsETH.minus(borrowFee);
};

View File

@ -13,10 +13,10 @@ const {expect} = chai;
makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => {
const {
HF_IS_NOT_BELLOW_THRESHOLD,
HEALTH_FACTOR_NOT_BELOW_THRESHOLD,
INVALID_HF,
USER_DID_NOT_BORROW_SPECIFIED,
THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED,
SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER,
COLLATERAL_CANNOT_BE_LIQUIDATED,
} = ProtocolErrors;
it('LIQUIDATION - Deposits WETH, borrows DAI/Check liquidation fails because health factor is above 1', async () => {
@ -71,7 +71,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
//someone tries to liquidate user 2
await expect(
pool.liquidationCall(weth.address, dai.address, borrower.address, 1, true)
).to.be.revertedWith(HF_IS_NOT_BELLOW_THRESHOLD);
).to.be.revertedWith(HEALTH_FACTOR_NOT_BELOW_THRESHOLD);
});
it('LIQUIDATION - Drop the health factor below 1', async () => {
@ -96,7 +96,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
//user 2 tries to borrow
await expect(
pool.liquidationCall(weth.address, weth.address, borrower.address, oneEther.toString(), true)
).revertedWith(USER_DID_NOT_BORROW_SPECIFIED);
).revertedWith(SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER);
});
it('LIQUIDATION - Tries to liquidate a different collateral than the borrower collateral', async () => {
@ -105,7 +105,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
await expect(
pool.liquidationCall(dai.address, dai.address, borrower.address, oneEther.toString(), true)
).revertedWith(THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED);
).revertedWith(COLLATERAL_CANNOT_BE_LIQUIDATED);
});
it('LIQUIDATION - Liquidates the borrow', async () => {

View File

@ -13,12 +13,7 @@ const chai = require('chai');
const {expect} = chai;
makeSuite('LendingPool liquidation - liquidator receiving the underlying asset', (testEnv) => {
const {
HF_IS_NOT_BELLOW_THRESHOLD,
INVALID_HF,
USER_DID_NOT_BORROW_SPECIFIED,
THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED,
} = ProtocolErrors;
const {INVALID_HF} = ProtocolErrors;
it('LIQUIDATION - Deposits WETH, borrows DAI', async () => {
const {dai, weth, users, pool, oracle} = testEnv;
@ -67,7 +62,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset',
expect(userGlobalDataAfter.currentLiquidationThreshold.toString()).to.be.bignumber.equal(
'8000',
'Invalid liquidation threshold'
INVALID_HF
);
});
@ -86,7 +81,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset',
expect(userGlobalData.healthFactor.toString()).to.be.bignumber.lt(
oneEther.toFixed(0),
'Invalid health factor'
INVALID_HF
);
});

View File

@ -5,7 +5,7 @@ import {getContract} from '../helpers/contracts-helpers';
import {StableDebtToken} from '../types/StableDebtToken';
makeSuite('Stable debt token tests', (testEnv: TestEnv) => {
const {INVALID_POOL_CALLER_MSG_1} = ProtocolErrors;
const {CALLER_MUST_BE_LENDING_POOL} = ProtocolErrors;
it('Tries to invoke mint not being the LendingPool', async () => {
const {deployer, pool, dai} = testEnv;
@ -19,7 +19,7 @@ makeSuite('Stable debt token tests', (testEnv: TestEnv) => {
);
await expect(stableDebtContract.mint(deployer.address, '1', '1')).to.be.revertedWith(
INVALID_POOL_CALLER_MSG_1
CALLER_MUST_BE_LENDING_POOL
);
});
@ -35,7 +35,7 @@ makeSuite('Stable debt token tests', (testEnv: TestEnv) => {
);
await expect(stableDebtContract.burn(deployer.address, '1')).to.be.revertedWith(
INVALID_POOL_CALLER_MSG_1
CALLER_MUST_BE_LENDING_POOL
);
});
});

View File

@ -1,18 +1,22 @@
import {expect} from 'chai';
import {makeSuite, TestEnv} from './helpers/make-suite';
import {ProtocolErrors, eContractid} from '../helpers/types';
import {deployGenericAToken, getAToken, deployContract, getContract} from '../helpers/contracts-helpers';
import {
deployGenericAToken,
getAToken,
deployContract,
getContract,
} from '../helpers/contracts-helpers';
import {MockAToken} from '../types/MockAToken';
import { MockStableDebtToken } from '../types/MockStableDebtToken';
import { MockVariableDebtToken } from '../types/MockVariableDebtToken';
import {MockStableDebtToken} from '../types/MockStableDebtToken';
import {MockVariableDebtToken} from '../types/MockVariableDebtToken';
makeSuite('Upgradeability', (testEnv: TestEnv) => {
const {INVALID_POOL_MANAGER_CALLER_MSG} = ProtocolErrors;
const {CALLER_NOT_LENDING_POOL_MANAGER} = ProtocolErrors;
let newATokenAddress: string;
let newStableTokenAddress: string;
let newVariableTokenAddress: string;
before('deploying instances', async () => {
const {dai, pool} = testEnv;
const aTokenInstance = await deployContract<MockAToken>(eContractid.MockAToken, [
@ -22,24 +26,19 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => {
'aDAI',
]);
const stableDebtTokenInstance = await deployContract<MockStableDebtToken>(eContractid.MockStableDebtToken, [
pool.address,
dai.address,
'Aave stable debt bearing DAI updated',
'stableDebtDAI',
]);
const stableDebtTokenInstance = await deployContract<MockStableDebtToken>(
eContractid.MockStableDebtToken,
[pool.address, dai.address, 'Aave stable debt bearing DAI updated', 'stableDebtDAI']
);
const variableDebtTokenInstance = await deployContract<MockVariableDebtToken>(eContractid.MockVariableDebtToken, [
pool.address,
dai.address,
'Aave variable debt bearing DAI updated',
'variableDebtDAI',
]);
const variableDebtTokenInstance = await deployContract<MockVariableDebtToken>(
eContractid.MockVariableDebtToken,
[pool.address, dai.address, 'Aave variable debt bearing DAI updated', 'variableDebtDAI']
);
newATokenAddress = aTokenInstance.address;
newVariableTokenAddress = variableDebtTokenInstance.address;
newStableTokenAddress = stableDebtTokenInstance.address;
});
it('Tries to update the DAI Atoken implementation with a different address than the lendingPoolManager', async () => {
@ -47,7 +46,7 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => {
await expect(
configurator.connect(users[1].signer).updateAToken(dai.address, newATokenAddress)
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Upgrades the DAI Atoken implementation ', async () => {
@ -66,8 +65,10 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => {
const {dai, configurator, users} = testEnv;
await expect(
configurator.connect(users[1].signer).updateStableDebtToken(dai.address, newStableTokenAddress)
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
configurator
.connect(users[1].signer)
.updateStableDebtToken(dai.address, newStableTokenAddress)
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Upgrades the DAI stable debt token implementation ', async () => {
@ -79,7 +80,10 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => {
const {stableDebtTokenAddress} = await pool.getReserveTokensAddresses(dai.address);
const debtToken = await getContract<MockStableDebtToken>(eContractid.MockStableDebtToken, stableDebtTokenAddress);
const debtToken = await getContract<MockStableDebtToken>(
eContractid.MockStableDebtToken,
stableDebtTokenAddress
);
const tokenName = await debtToken.name();
@ -90,8 +94,10 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => {
const {dai, configurator, users} = testEnv;
await expect(
configurator.connect(users[1].signer).updateVariableDebtToken(dai.address, newVariableTokenAddress)
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG);
configurator
.connect(users[1].signer)
.updateVariableDebtToken(dai.address, newVariableTokenAddress)
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
});
it('Upgrades the DAI variable debt token implementation ', async () => {
@ -103,11 +109,13 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => {
const {variableDebtTokenAddress} = await pool.getReserveTokensAddresses(dai.address);
const debtToken = await getContract<MockStableDebtToken>(eContractid.MockStableDebtToken, variableDebtTokenAddress);
const debtToken = await getContract<MockStableDebtToken>(
eContractid.MockStableDebtToken,
variableDebtTokenAddress
);
const tokenName = await debtToken.name();
expect(tokenName).to.be.eq('Aave variable debt bearing DAI updated', 'Invalid token name');
});
});

View File

@ -5,7 +5,7 @@ import {getContract} from '../helpers/contracts-helpers';
import {VariableDebtToken} from '../types/VariableDebtToken';
makeSuite('Variable debt token tests', (testEnv: TestEnv) => {
const {INVALID_POOL_CALLER_MSG_1} = ProtocolErrors;
const {CALLER_MUST_BE_LENDING_POOL} = ProtocolErrors;
it('Tries to invoke mint not being the LendingPool', async () => {
const {deployer, pool, dai} = testEnv;
@ -19,7 +19,7 @@ makeSuite('Variable debt token tests', (testEnv: TestEnv) => {
);
await expect(variableDebtContract.mint(deployer.address, '1')).to.be.revertedWith(
INVALID_POOL_CALLER_MSG_1
CALLER_MUST_BE_LENDING_POOL
);
});
@ -35,7 +35,7 @@ makeSuite('Variable debt token tests', (testEnv: TestEnv) => {
);
await expect(variableDebtContract.burn(deployer.address, '1')).to.be.revertedWith(
INVALID_POOL_CALLER_MSG_1
CALLER_MUST_BE_LENDING_POOL
);
});
});