2020-05-29 16:45:37 +00:00
|
|
|
// SPDX-License-Identifier: agpl-3.0
|
|
|
|
pragma solidity ^0.6.8;
|
2020-07-23 15:18:06 +00:00
|
|
|
pragma experimental ABIEncoderV2;
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-10-15 13:41:56 +00:00
|
|
|
import {SafeMath} from '../dependencies/openzeppelin/contracts/SafeMath.sol';
|
|
|
|
import {IERC20} from '../dependencies/openzeppelin/contracts/IERC20.sol';
|
2020-10-15 13:16:05 +00:00
|
|
|
import {VersionedInitializable} from '../libraries/aave-upgradeability/VersionedInitializable.sol';
|
2020-08-21 12:03:17 +00:00
|
|
|
import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol';
|
2020-08-21 10:38:08 +00:00
|
|
|
import {IAToken} from '../tokenization/interfaces/IAToken.sol';
|
2020-08-20 07:51:21 +00:00
|
|
|
import {Helpers} from '../libraries/helpers/Helpers.sol';
|
2020-09-04 08:27:32 +00:00
|
|
|
import {Errors} from '../libraries/helpers/Errors.sol';
|
2020-08-20 07:51:21 +00:00
|
|
|
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
|
2020-09-21 17:52:22 +00:00
|
|
|
import {PercentageMath} from '../libraries/math/PercentageMath.sol';
|
2020-08-20 07:51:21 +00:00
|
|
|
import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol';
|
|
|
|
import {GenericLogic} from '../libraries/logic/GenericLogic.sol';
|
|
|
|
import {ValidationLogic} from '../libraries/logic/ValidationLogic.sol';
|
|
|
|
import {ReserveConfiguration} from '../libraries/configuration/ReserveConfiguration.sol';
|
|
|
|
import {UserConfiguration} from '../libraries/configuration/UserConfiguration.sol';
|
|
|
|
import {IStableDebtToken} from '../tokenization/interfaces/IStableDebtToken.sol';
|
|
|
|
import {IVariableDebtToken} from '../tokenization/interfaces/IVariableDebtToken.sol';
|
2020-09-10 10:51:52 +00:00
|
|
|
import {DebtTokenBase} from '../tokenization/base/DebtTokenBase.sol';
|
2020-08-20 07:51:21 +00:00
|
|
|
import {IFlashLoanReceiver} from '../flashloan/interfaces/IFlashLoanReceiver.sol';
|
2020-09-16 10:41:12 +00:00
|
|
|
import {LendingPoolCollateralManager} from './LendingPoolCollateralManager.sol';
|
2020-08-20 07:51:21 +00:00
|
|
|
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';
|
2020-10-15 13:41:56 +00:00
|
|
|
import {SafeERC20} from '../dependencies/openzeppelin/contracts/SafeERC20.sol';
|
2020-08-20 12:32:20 +00:00
|
|
|
import {ILendingPool} from '../interfaces/ILendingPool.sol';
|
2020-09-16 10:16:51 +00:00
|
|
|
import {LendingPoolStorage} from './LendingPoolStorage.sol';
|
2020-11-10 16:50:13 +00:00
|
|
|
import {Address} from '../dependencies/openzeppelin/contracts/Address.sol';
|
2020-05-29 16:45:37 +00:00
|
|
|
|
|
|
|
/**
|
2020-07-05 21:44:19 +00:00
|
|
|
* @title LendingPool contract
|
|
|
|
* @notice Implements the actions of the LendingPool, and exposes accessory methods to fetch the users and reserve data
|
|
|
|
* @author Aave
|
2020-05-29 16:45:37 +00:00
|
|
|
**/
|
2020-09-16 10:16:51 +00:00
|
|
|
contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage {
|
2020-07-05 21:44:19 +00:00
|
|
|
using SafeMath for uint256;
|
|
|
|
using WadRayMath for uint256;
|
2020-09-21 17:52:22 +00:00
|
|
|
using PercentageMath for uint256;
|
2020-08-21 12:03:17 +00:00
|
|
|
using SafeERC20 for IERC20;
|
2020-07-05 21:44:19 +00:00
|
|
|
|
|
|
|
//main configuration parameters
|
2020-09-21 19:15:12 +00:00
|
|
|
uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 2500;
|
2020-09-04 08:27:32 +00:00
|
|
|
uint256 public constant FLASHLOAN_PREMIUM_TOTAL = 9;
|
2020-09-16 15:12:07 +00:00
|
|
|
uint256 public constant MAX_NUMBER_RESERVES = 128;
|
2020-09-14 09:33:34 +00:00
|
|
|
uint256 public constant LENDINGPOOL_REVISION = 0x2;
|
2020-09-09 11:06:46 +00:00
|
|
|
|
2020-11-10 16:38:06 +00:00
|
|
|
/**
|
2020-11-10 16:52:23 +00:00
|
|
|
* @dev functions marked by this modifier can only be called when the protocol is not paused
|
|
|
|
**/
|
2020-11-10 16:38:06 +00:00
|
|
|
modifier whenNotPaused() {
|
|
|
|
_whenNotPaused();
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-10 16:52:23 +00:00
|
|
|
* @dev functions marked by this modifier can only be called by the LendingPoolConfigurator
|
|
|
|
**/
|
2020-11-10 16:38:06 +00:00
|
|
|
modifier onlyLendingPoolConfigurator() {
|
|
|
|
_onlyLendingPoolConfigurator();
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
/**
|
|
|
|
* @dev only lending pools configurator can use functions affected by this modifier
|
|
|
|
**/
|
2020-09-21 19:15:12 +00:00
|
|
|
function _onlyLendingPoolConfigurator() internal view {
|
2020-09-04 08:27:32 +00:00
|
|
|
require(
|
|
|
|
_addressesProvider.getLendingPoolConfigurator() == msg.sender,
|
2020-10-14 09:03:32 +00:00
|
|
|
Errors.LP_CALLER_NOT_LENDING_POOL_CONFIGURATOR
|
2020-09-04 08:27:32 +00:00
|
|
|
);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 09:56:08 +00:00
|
|
|
/**
|
2020-09-15 11:13:28 +00:00
|
|
|
* @dev Function to make a function callable only when the contract is not paused.
|
2020-09-15 09:56:08 +00:00
|
|
|
*
|
|
|
|
* Requirements:
|
|
|
|
*
|
|
|
|
* - The contract must not be paused.
|
|
|
|
*/
|
2020-09-21 19:15:12 +00:00
|
|
|
function _whenNotPaused() internal view {
|
2020-10-31 12:55:19 +00:00
|
|
|
require(!_paused, Errors.LP_IS_PAUSED);
|
2020-09-15 09:56:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
function getRevision() internal override pure returns (uint256) {
|
|
|
|
return LENDINGPOOL_REVISION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev this function is invoked by the proxy contract when the LendingPool contract is added to the
|
|
|
|
* AddressesProvider.
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param provider the address of the LendingPoolAddressesProvider registry
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
2020-08-21 12:03:17 +00:00
|
|
|
function initialize(ILendingPoolAddressesProvider provider) public initializer {
|
2020-09-04 08:27:32 +00:00
|
|
|
_addressesProvider = provider;
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev deposits The underlying asset into the reserve. A corresponding amount of the overlying asset (aTokens)
|
|
|
|
* is minted.
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param asset the address of the reserve
|
|
|
|
* @param amount the amount to be deposited
|
|
|
|
* @param referralCode integrators are assigned a referral code and can potentially receive rewards.
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
|
|
|
function deposit(
|
2020-08-21 11:15:29 +00:00
|
|
|
address asset,
|
|
|
|
uint256 amount,
|
2020-09-09 10:47:27 +00:00
|
|
|
address onBehalfOf,
|
2020-08-21 11:15:29 +00:00
|
|
|
uint16 referralCode
|
2020-11-10 16:38:06 +00:00
|
|
|
) external override whenNotPaused {
|
2020-08-21 11:15:29 +00:00
|
|
|
ReserveLogic.ReserveData storage reserve = _reserves[asset];
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-08-21 11:15:29 +00:00
|
|
|
ValidationLogic.validateDeposit(reserve, amount);
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
address aToken = reserve.aTokenAddress;
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-09-14 07:53:21 +00:00
|
|
|
reserve.updateState();
|
2020-09-04 08:27:32 +00:00
|
|
|
reserve.updateInterestRates(asset, aToken, amount, 0);
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-10-15 12:13:46 +00:00
|
|
|
bool isFirstDeposit = IAToken(aToken).mint(onBehalfOf, amount, reserve.liquidityIndex);
|
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
if (isFirstDeposit) {
|
2020-09-13 09:58:45 +00:00
|
|
|
_usersConfig[onBehalfOf].setUsingAsCollateral(reserve.id, true);
|
2020-11-02 15:40:31 +00:00
|
|
|
emit ReserveUsedAsCollateralEnabled(asset, onBehalfOf);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-10 17:16:04 +00:00
|
|
|
//transfer to the aToken contract
|
2020-09-04 08:27:32 +00:00
|
|
|
IERC20(asset).safeTransferFrom(msg.sender, aToken, amount);
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-09-09 10:47:27 +00:00
|
|
|
emit Deposit(asset, msg.sender, onBehalfOf, amount, referralCode);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-04 08:27:32 +00:00
|
|
|
* @dev withdraws the _reserves of user.
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param asset the address of the reserve
|
|
|
|
* @param amount the underlying amount to be redeemed
|
2020-10-30 11:38:41 +00:00
|
|
|
* @param to address that will receive the underlying
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
2020-10-30 11:38:41 +00:00
|
|
|
function withdraw(
|
|
|
|
address asset,
|
|
|
|
uint256 amount,
|
|
|
|
address to
|
2020-11-10 16:38:06 +00:00
|
|
|
) external override whenNotPaused {
|
2020-08-21 11:15:29 +00:00
|
|
|
ReserveLogic.ReserveData storage reserve = _reserves[asset];
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
address aToken = reserve.aTokenAddress;
|
2020-07-10 17:16:04 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
uint256 userBalance = IAToken(aToken).balanceOf(msg.sender);
|
2020-08-18 19:19:11 +00:00
|
|
|
|
2020-08-21 11:15:29 +00:00
|
|
|
uint256 amountToWithdraw = amount;
|
2020-08-18 19:19:11 +00:00
|
|
|
|
|
|
|
//if amount is equal to uint(-1), the user wants to redeem everything
|
2020-09-16 10:16:51 +00:00
|
|
|
if (amount == type(uint256).max) {
|
2020-08-18 19:19:11 +00:00
|
|
|
amountToWithdraw = userBalance;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidationLogic.validateWithdraw(
|
2020-08-21 11:15:29 +00:00
|
|
|
asset,
|
2020-08-18 19:19:11 +00:00
|
|
|
amountToWithdraw,
|
|
|
|
userBalance,
|
2020-08-21 11:15:29 +00:00
|
|
|
_reserves,
|
|
|
|
_usersConfig[msg.sender],
|
2020-09-04 08:27:32 +00:00
|
|
|
_reservesList,
|
2020-10-06 13:51:48 +00:00
|
|
|
_reservesCount,
|
2020-09-04 08:27:32 +00:00
|
|
|
_addressesProvider.getPriceOracle()
|
2020-08-18 19:19:11 +00:00
|
|
|
);
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-09-14 07:53:21 +00:00
|
|
|
reserve.updateState();
|
2020-07-10 17:16:04 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
reserve.updateInterestRates(asset, aToken, 0, amountToWithdraw);
|
2020-07-10 17:16:04 +00:00
|
|
|
|
2020-08-18 19:19:11 +00:00
|
|
|
if (amountToWithdraw == userBalance) {
|
2020-09-13 09:58:45 +00:00
|
|
|
_usersConfig[msg.sender].setUsingAsCollateral(reserve.id, false);
|
2020-11-02 15:40:31 +00:00
|
|
|
emit ReserveUsedAsCollateralDisabled(asset, msg.sender);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-10-30 11:38:41 +00:00
|
|
|
IAToken(aToken).burn(msg.sender, to, amountToWithdraw, reserve.liquidityIndex);
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-10-30 11:38:41 +00:00
|
|
|
emit Withdraw(asset, msg.sender, to, amountToWithdraw);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-19 09:46:10 +00:00
|
|
|
* @dev Allows users to borrow a specific amount of the reserve underlying asset, provided that the borrower
|
|
|
|
* already deposited enough collateral, or he was given enough allowance by a credit delegator on the
|
|
|
|
* corresponding debt token (StableDebtToken or VariableDebtToken)
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param asset the address of the reserve
|
|
|
|
* @param amount the amount to be borrowed
|
2020-11-19 09:46:10 +00:00
|
|
|
* @param interestRateMode the interest rate mode at which the user wants to borrow: 1 for Stable, 2 for Variable
|
2020-09-04 08:27:32 +00:00
|
|
|
* @param referralCode a referral code for integrators
|
2020-11-19 09:46:10 +00:00
|
|
|
* @param onBehalfOf address of the user who will receive the debt. Should be the address of the borrower itself
|
|
|
|
* calling the function if he wants to borrow against his own collateral, or the address of the credit delegator
|
|
|
|
* if he has been given credit delegation allowance
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
|
|
|
function borrow(
|
2020-08-21 11:15:29 +00:00
|
|
|
address asset,
|
|
|
|
uint256 amount,
|
|
|
|
uint256 interestRateMode,
|
2020-09-09 12:33:05 +00:00
|
|
|
uint16 referralCode,
|
|
|
|
address onBehalfOf
|
2020-11-10 16:38:06 +00:00
|
|
|
) external override whenNotPaused {
|
2020-09-14 10:52:05 +00:00
|
|
|
ReserveLogic.ReserveData storage reserve = _reserves[asset];
|
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
_executeBorrow(
|
|
|
|
ExecuteBorrowParams(
|
|
|
|
asset,
|
|
|
|
msg.sender,
|
2020-09-09 12:33:05 +00:00
|
|
|
onBehalfOf,
|
2020-09-04 08:27:32 +00:00
|
|
|
amount,
|
|
|
|
interestRateMode,
|
2020-09-14 10:52:05 +00:00
|
|
|
reserve.aTokenAddress,
|
2020-09-04 08:27:32 +00:00
|
|
|
referralCode,
|
|
|
|
true
|
|
|
|
)
|
2020-07-05 21:44:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-20 07:51:21 +00:00
|
|
|
/**
|
|
|
|
* @notice repays a borrow on the specific reserve, for the specified amount (or for the whole amount, if uint256(-1) is specified).
|
2020-09-04 08:27:32 +00:00
|
|
|
* @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.
|
2020-08-21 11:15:29 +00:00
|
|
|
* @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
|
2020-09-04 08:27:32 +00:00
|
|
|
* @param onBehalfOf the address for which msg.sender is repaying.
|
2020-08-20 07:51:21 +00:00
|
|
|
**/
|
2020-07-05 21:44:19 +00:00
|
|
|
function repay(
|
2020-08-21 11:15:29 +00:00
|
|
|
address asset,
|
|
|
|
uint256 amount,
|
2020-09-04 08:27:32 +00:00
|
|
|
uint256 rateMode,
|
|
|
|
address onBehalfOf
|
2020-11-10 16:38:06 +00:00
|
|
|
) external override whenNotPaused {
|
2020-08-21 11:15:29 +00:00
|
|
|
ReserveLogic.ReserveData storage reserve = _reserves[asset];
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(onBehalfOf, reserve);
|
|
|
|
|
|
|
|
ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode);
|
2020-08-21 17:10:48 +00:00
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
ValidationLogic.validateRepay(
|
|
|
|
reserve,
|
2020-08-21 11:15:29 +00:00
|
|
|
amount,
|
2020-09-04 08:27:32 +00:00
|
|
|
interestRateMode,
|
|
|
|
onBehalfOf,
|
2020-08-20 13:12:19 +00:00
|
|
|
stableDebt,
|
2020-08-21 16:18:12 +00:00
|
|
|
variableDebt
|
2020-07-05 21:44:19 +00:00
|
|
|
);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-10-26 09:57:45 +00:00
|
|
|
//default to max amount
|
|
|
|
uint256 paybackAmount = interestRateMode == ReserveLogic.InterestRateMode.STABLE
|
|
|
|
? stableDebt
|
|
|
|
: variableDebt;
|
|
|
|
|
|
|
|
if (amount < paybackAmount) {
|
|
|
|
paybackAmount = amount;
|
|
|
|
}
|
|
|
|
|
2020-09-14 07:53:21 +00:00
|
|
|
reserve.updateState();
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
//burns an equivalent amount of debt tokens
|
2020-09-04 08:27:32 +00:00
|
|
|
if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) {
|
|
|
|
IStableDebtToken(reserve.stableDebtTokenAddress).burn(onBehalfOf, paybackAmount);
|
2020-07-05 21:44:19 +00:00
|
|
|
} else {
|
2020-09-21 13:35:22 +00:00
|
|
|
IVariableDebtToken(reserve.variableDebtTokenAddress).burn(
|
|
|
|
onBehalfOf,
|
|
|
|
paybackAmount,
|
|
|
|
reserve.variableBorrowIndex
|
|
|
|
);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
address aToken = reserve.aTokenAddress;
|
|
|
|
reserve.updateInterestRates(asset, aToken, paybackAmount, 0);
|
2020-08-18 19:19:11 +00:00
|
|
|
|
2020-08-20 13:12:19 +00:00
|
|
|
if (stableDebt.add(variableDebt).sub(paybackAmount) == 0) {
|
2020-09-13 09:58:45 +00:00
|
|
|
_usersConfig[onBehalfOf].setBorrowing(reserve.id, false);
|
2020-08-05 10:40:24 +00:00
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-14 09:34:40 +00:00
|
|
|
IERC20(asset).safeTransferFrom(msg.sender, aToken, paybackAmount);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-14 09:34:40 +00:00
|
|
|
emit Repay(asset, onBehalfOf, msg.sender, paybackAmount);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev borrowers can user this function to swap between stable and variable borrow rate modes.
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param asset the address of the reserve on which the user borrowed
|
2020-09-04 08:27:32 +00:00
|
|
|
* @param rateMode the rate mode that the user wants to swap
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
2020-11-10 16:38:06 +00:00
|
|
|
function swapBorrowRateMode(address asset, uint256 rateMode) external override whenNotPaused {
|
2020-08-21 11:15:29 +00:00
|
|
|
ReserveLogic.ReserveData storage reserve = _reserves[asset];
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-08-06 07:52:15 +00:00
|
|
|
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-08-18 19:19:11 +00:00
|
|
|
ValidationLogic.validateSwapRateMode(
|
|
|
|
reserve,
|
2020-08-21 11:15:29 +00:00
|
|
|
_usersConfig[msg.sender],
|
2020-08-18 19:19:11 +00:00
|
|
|
stableDebt,
|
|
|
|
variableDebt,
|
2020-09-04 08:27:32 +00:00
|
|
|
interestRateMode
|
2020-08-18 19:19:11 +00:00
|
|
|
);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-14 07:53:21 +00:00
|
|
|
reserve.updateState();
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) {
|
2020-07-05 21:44:19 +00:00
|
|
|
//burn stable rate tokens, mint variable rate tokens
|
2020-07-13 08:54:08 +00:00
|
|
|
IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender, stableDebt);
|
2020-09-21 13:35:22 +00:00
|
|
|
IVariableDebtToken(reserve.variableDebtTokenAddress).mint(
|
2020-11-03 18:47:57 +00:00
|
|
|
msg.sender,
|
2020-09-21 13:35:22 +00:00
|
|
|
msg.sender,
|
|
|
|
stableDebt,
|
|
|
|
reserve.variableBorrowIndex
|
|
|
|
);
|
2020-07-05 21:44:19 +00:00
|
|
|
} else {
|
|
|
|
//do the opposite
|
2020-09-21 13:35:22 +00:00
|
|
|
IVariableDebtToken(reserve.variableDebtTokenAddress).burn(
|
|
|
|
msg.sender,
|
|
|
|
variableDebt,
|
|
|
|
reserve.variableBorrowIndex
|
|
|
|
);
|
2020-07-05 21:44:19 +00:00
|
|
|
IStableDebtToken(reserve.stableDebtTokenAddress).mint(
|
2020-11-03 18:47:57 +00:00
|
|
|
msg.sender,
|
2020-07-05 21:44:19 +00:00
|
|
|
msg.sender,
|
2020-07-07 15:14:44 +00:00
|
|
|
variableDebt,
|
2020-07-05 21:44:19 +00:00
|
|
|
reserve.currentStableBorrowRate
|
|
|
|
);
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-10-29 17:03:19 +00:00
|
|
|
emit Swap(asset, msg.sender, rateMode);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
/**
|
2020-10-26 10:18:25 +00:00
|
|
|
* @dev rebalances the stable interest rate of a user. Users can be rebalanced if the following conditions are satisfied:
|
|
|
|
* 1. Usage ratio is above 95%
|
|
|
|
* 2. the current deposit APY is below REBALANCE_UP_THRESHOLD * maxVariableBorrowRate, which means that too much has been
|
|
|
|
* borrowed at a stable rate and depositors are not earning enough.
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param asset the address of the reserve
|
2020-09-04 08:27:32 +00:00
|
|
|
* @param user the address of the user to be rebalanced
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
2020-11-10 16:38:06 +00:00
|
|
|
function rebalanceStableBorrowRate(address asset, address user) external override whenNotPaused {
|
2020-08-21 11:15:29 +00:00
|
|
|
ReserveLogic.ReserveData storage reserve = _reserves[asset];
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-21 17:52:22 +00:00
|
|
|
IERC20 stableDebtToken = IERC20(reserve.stableDebtTokenAddress);
|
|
|
|
IERC20 variableDebtToken = IERC20(reserve.variableDebtTokenAddress);
|
2020-09-21 20:08:44 +00:00
|
|
|
address aTokenAddress = reserve.aTokenAddress;
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-11-10 14:57:49 +00:00
|
|
|
uint256 stableDebt = IERC20(stableDebtToken).balanceOf(user);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-11-10 14:57:49 +00:00
|
|
|
ValidationLogic.validateRebalanceStableBorrowRate(
|
|
|
|
reserve,
|
2020-11-10 15:07:13 +00:00
|
|
|
asset,
|
2020-11-10 14:57:49 +00:00
|
|
|
stableDebtToken,
|
|
|
|
variableDebtToken,
|
|
|
|
aTokenAddress
|
2020-07-05 21:44:19 +00:00
|
|
|
);
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-09-14 07:53:21 +00:00
|
|
|
reserve.updateState();
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-11-10 14:57:49 +00:00
|
|
|
IStableDebtToken(address(stableDebtToken)).burn(user, stableDebt);
|
2020-10-08 13:41:48 +00:00
|
|
|
IStableDebtToken(address(stableDebtToken)).mint(
|
2020-11-03 18:47:57 +00:00
|
|
|
user,
|
2020-10-08 13:41:48 +00:00
|
|
|
user,
|
2020-11-10 14:57:49 +00:00
|
|
|
stableDebt,
|
2020-10-08 13:41:48 +00:00
|
|
|
reserve.currentStableBorrowRate
|
|
|
|
);
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-09-21 20:08:44 +00:00
|
|
|
reserve.updateInterestRates(asset, aTokenAddress, 0, 0);
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
emit RebalanceStableBorrowRate(asset, user);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev allows depositors to enable or disable a specific deposit as collateral.
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param asset the address of the reserve
|
2020-10-26 10:58:57 +00:00
|
|
|
* @param useAsCollateral true if the user wants to use the deposit as collateral, false otherwise.
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
2020-11-10 16:52:23 +00:00
|
|
|
function setUserUseReserveAsCollateral(address asset, bool useAsCollateral)
|
|
|
|
external
|
|
|
|
override
|
|
|
|
whenNotPaused
|
|
|
|
{
|
2020-08-21 11:15:29 +00:00
|
|
|
ReserveLogic.ReserveData storage reserve = _reserves[asset];
|
2020-07-05 21:44:19 +00:00
|
|
|
|
|
|
|
ValidationLogic.validateSetUseReserveAsCollateral(
|
|
|
|
reserve,
|
2020-08-21 11:15:29 +00:00
|
|
|
asset,
|
2020-11-10 12:45:08 +00:00
|
|
|
useAsCollateral,
|
2020-08-21 11:15:29 +00:00
|
|
|
_reserves,
|
|
|
|
_usersConfig[msg.sender],
|
2020-09-04 08:27:32 +00:00
|
|
|
_reservesList,
|
2020-10-06 13:51:48 +00:00
|
|
|
_reservesCount,
|
2020-09-04 08:27:32 +00:00
|
|
|
_addressesProvider.getPriceOracle()
|
2020-07-05 21:44:19 +00:00
|
|
|
);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-13 09:58:45 +00:00
|
|
|
_usersConfig[msg.sender].setUsingAsCollateral(reserve.id, useAsCollateral);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
if (useAsCollateral) {
|
2020-08-21 11:15:29 +00:00
|
|
|
emit ReserveUsedAsCollateralEnabled(asset, msg.sender);
|
2020-07-05 21:44:19 +00:00
|
|
|
} else {
|
2020-08-21 11:15:29 +00:00
|
|
|
emit ReserveUsedAsCollateralDisabled(asset, msg.sender);
|
2020-06-20 23:40:03 +00:00
|
|
|
}
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev users can invoke this function to liquidate an undercollateralized position.
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param asset the address of the collateral to liquidated
|
|
|
|
* @param asset the address of the principal reserve
|
2020-09-04 08:27:32 +00:00
|
|
|
* @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
|
2020-07-05 21:44:19 +00:00
|
|
|
* he wants to receive the underlying asset directly
|
|
|
|
**/
|
|
|
|
function liquidationCall(
|
2020-09-04 08:27:32 +00:00
|
|
|
address collateral,
|
2020-08-21 11:15:29 +00:00
|
|
|
address asset,
|
2020-09-04 08:27:32 +00:00
|
|
|
address user,
|
|
|
|
uint256 purchaseAmount,
|
|
|
|
bool receiveAToken
|
2020-11-10 16:38:06 +00:00
|
|
|
) external override whenNotPaused {
|
2020-09-16 10:41:12 +00:00
|
|
|
address collateralManager = _addressesProvider.getLendingPoolCollateralManager();
|
2020-07-05 21:44:19 +00:00
|
|
|
|
|
|
|
//solium-disable-next-line
|
2020-09-16 10:41:12 +00:00
|
|
|
(bool success, bytes memory result) = collateralManager.delegatecall(
|
2020-07-05 21:44:19 +00:00
|
|
|
abi.encodeWithSignature(
|
|
|
|
'liquidationCall(address,address,address,uint256,bool)',
|
2020-09-04 08:27:32 +00:00
|
|
|
collateral,
|
2020-08-21 11:15:29 +00:00
|
|
|
asset,
|
2020-09-04 08:27:32 +00:00
|
|
|
user,
|
|
|
|
purchaseAmount,
|
|
|
|
receiveAToken
|
2020-07-05 21:44:19 +00:00
|
|
|
)
|
|
|
|
);
|
2020-10-14 09:03:32 +00:00
|
|
|
require(success, Errors.LP_LIQUIDATION_CALL_FAILED);
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
(uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string));
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-11-11 14:52:42 +00:00
|
|
|
require(returnCode == 0, string(abi.encodePacked(returnMessage)));
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 17:52:22 +00:00
|
|
|
struct FlashLoanLocalVars {
|
2020-09-15 16:54:59 +00:00
|
|
|
IFlashLoanReceiver receiver;
|
|
|
|
address oracle;
|
2020-10-22 18:37:50 +00:00
|
|
|
uint256 i;
|
|
|
|
address currentAsset;
|
|
|
|
address currentATokenAddress;
|
|
|
|
uint256 currentAmount;
|
|
|
|
uint256 currentPremium;
|
|
|
|
uint256 currentAmountPlusPremium;
|
2020-10-30 10:40:31 +00:00
|
|
|
address debtToken;
|
2020-09-15 16:54:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
/**
|
|
|
|
* @dev allows smartcontracts 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
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param receiverAddress The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface.
|
2020-10-23 16:41:08 +00:00
|
|
|
* @param assets The addresss of the assets being flashborrowed
|
|
|
|
* @param amounts The amounts requested for this flashloan for each asset
|
2020-10-29 14:14:28 +00:00
|
|
|
* @param modes Types of the debt to open if the flash loan is not returned. 0 -> Don't open any debt, just revert, 1 -> stable, 2 -> variable
|
2020-10-27 12:10:10 +00:00
|
|
|
* @param onBehalfOf If mode is not 0, then the address to take the debt onBehalfOf. The onBehalfOf address must already have approved `msg.sender` to incur the debt on their behalf.
|
2020-09-04 08:27:32 +00:00
|
|
|
* @param params Variadic packed params to pass to the receiver as extra information
|
|
|
|
* @param referralCode Referral code of the flash loan
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
|
|
|
function flashLoan(
|
2020-08-21 11:15:29 +00:00
|
|
|
address receiverAddress,
|
2020-10-22 16:15:56 +00:00
|
|
|
address[] calldata assets,
|
|
|
|
uint256[] calldata amounts,
|
2020-10-29 14:14:28 +00:00
|
|
|
uint256[] calldata modes,
|
2020-10-26 12:27:31 +00:00
|
|
|
address onBehalfOf,
|
2020-09-04 08:27:32 +00:00
|
|
|
bytes calldata params,
|
|
|
|
uint16 referralCode
|
2020-11-10 16:38:06 +00:00
|
|
|
) external override whenNotPaused {
|
2020-09-04 08:27:32 +00:00
|
|
|
FlashLoanLocalVars memory vars;
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-10-30 11:06:02 +00:00
|
|
|
ValidationLogic.validateFlashloan(assets, amounts);
|
2020-07-10 17:16:04 +00:00
|
|
|
|
2020-10-22 16:15:56 +00:00
|
|
|
address[] memory aTokenAddresses = new address[](assets.length);
|
|
|
|
uint256[] memory premiums = new uint256[](assets.length);
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-10-22 16:15:56 +00:00
|
|
|
vars.receiver = IFlashLoanReceiver(receiverAddress);
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-10-22 18:37:50 +00:00
|
|
|
for (vars.i = 0; vars.i < assets.length; vars.i++) {
|
|
|
|
aTokenAddresses[vars.i] = _reserves[assets[vars.i]].aTokenAddress;
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-10-22 18:37:50 +00:00
|
|
|
premiums[vars.i] = amounts[vars.i].mul(FLASHLOAN_PREMIUM_TOTAL).div(10000);
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-10-22 16:15:56 +00:00
|
|
|
//transfer funds to the receiver
|
2020-10-22 18:37:50 +00:00
|
|
|
IAToken(aTokenAddresses[vars.i]).transferUnderlyingTo(receiverAddress, amounts[vars.i]);
|
2020-10-22 16:15:56 +00:00
|
|
|
}
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
//execute action of the receiver
|
2020-10-07 14:20:32 +00:00
|
|
|
require(
|
2020-10-30 12:32:42 +00:00
|
|
|
vars.receiver.executeOperation(assets, amounts, premiums, msg.sender, params),
|
2020-10-14 09:03:32 +00:00
|
|
|
Errors.LP_INVALID_FLASH_LOAN_EXECUTOR_RETURN
|
2020-10-07 14:20:32 +00:00
|
|
|
);
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-10-22 18:37:50 +00:00
|
|
|
for (vars.i = 0; vars.i < assets.length; vars.i++) {
|
|
|
|
vars.currentAsset = assets[vars.i];
|
|
|
|
vars.currentAmount = amounts[vars.i];
|
|
|
|
vars.currentPremium = premiums[vars.i];
|
|
|
|
vars.currentATokenAddress = aTokenAddresses[vars.i];
|
2020-10-23 16:41:08 +00:00
|
|
|
vars.currentAmountPlusPremium = vars.currentAmount.add(vars.currentPremium);
|
2020-10-22 16:15:56 +00:00
|
|
|
|
2020-10-30 10:55:28 +00:00
|
|
|
if (ReserveLogic.InterestRateMode(modes[vars.i]) == ReserveLogic.InterestRateMode.NONE) {
|
2020-10-22 18:37:50 +00:00
|
|
|
_reserves[vars.currentAsset].updateState();
|
|
|
|
_reserves[vars.currentAsset].cumulateToLiquidityIndex(
|
|
|
|
IERC20(vars.currentATokenAddress).totalSupply(),
|
|
|
|
vars.currentPremium
|
|
|
|
);
|
|
|
|
_reserves[vars.currentAsset].updateInterestRates(
|
2020-10-23 16:41:08 +00:00
|
|
|
vars.currentAsset,
|
2020-10-22 18:37:50 +00:00
|
|
|
vars.currentATokenAddress,
|
|
|
|
vars.currentPremium,
|
|
|
|
0
|
2020-10-22 16:15:56 +00:00
|
|
|
);
|
|
|
|
|
2020-10-22 18:37:50 +00:00
|
|
|
IERC20(vars.currentAsset).safeTransferFrom(
|
2020-10-22 16:15:56 +00:00
|
|
|
receiverAddress,
|
2020-10-22 18:37:50 +00:00
|
|
|
vars.currentATokenAddress,
|
|
|
|
vars.currentAmountPlusPremium
|
2020-10-22 16:15:56 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
//if the user didn't choose to return the funds, the system checks if there
|
|
|
|
//is enough collateral and eventually open a position
|
|
|
|
_executeBorrow(
|
|
|
|
ExecuteBorrowParams(
|
2020-10-22 18:37:50 +00:00
|
|
|
vars.currentAsset,
|
2020-10-22 16:15:56 +00:00
|
|
|
msg.sender,
|
2020-10-27 11:05:48 +00:00
|
|
|
onBehalfOf,
|
2020-10-22 18:37:50 +00:00
|
|
|
vars.currentAmount,
|
2020-10-29 14:14:28 +00:00
|
|
|
modes[vars.i],
|
2020-10-22 18:37:50 +00:00
|
|
|
vars.currentATokenAddress,
|
2020-10-22 16:15:56 +00:00
|
|
|
referralCode,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2020-10-30 10:40:31 +00:00
|
|
|
emit FlashLoan(
|
|
|
|
receiverAddress,
|
2020-10-30 10:49:23 +00:00
|
|
|
msg.sender,
|
2020-10-30 10:40:31 +00:00
|
|
|
vars.currentAsset,
|
|
|
|
vars.currentAmount,
|
|
|
|
vars.currentPremium,
|
|
|
|
referralCode
|
|
|
|
);
|
2020-09-14 19:06:16 +00:00
|
|
|
}
|
2020-09-07 09:46:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
/**
|
2020-10-12 08:50:06 +00:00
|
|
|
* @dev returns the state and configuration of the reserve
|
|
|
|
* @param asset the address of the reserve
|
|
|
|
* @return the state of the reserve
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
2020-08-21 11:15:29 +00:00
|
|
|
function getReserveData(address asset)
|
2020-07-05 21:44:19 +00:00
|
|
|
external
|
2020-08-20 12:32:20 +00:00
|
|
|
override
|
2020-07-05 21:44:19 +00:00
|
|
|
view
|
2020-10-09 14:35:02 +00:00
|
|
|
returns (ReserveLogic.ReserveData memory)
|
2020-07-05 21:44:19 +00:00
|
|
|
{
|
2020-10-09 14:35:02 +00:00
|
|
|
return _reserves[asset];
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 08:50:06 +00:00
|
|
|
/**
|
|
|
|
* @dev returns the user account data across all the reserves
|
|
|
|
* @param user the address of the user
|
|
|
|
* @return totalCollateralETH the total collateral in ETH of the user
|
|
|
|
* @return totalDebtETH the total debt in ETH of the user
|
|
|
|
* @return availableBorrowsETH the borrowing power left of the user
|
|
|
|
* @return currentLiquidationThreshold the liquidation threshold of the user
|
|
|
|
* @return ltv the loan to value of the user
|
|
|
|
* @return healthFactor the current health factor of the user
|
|
|
|
**/
|
2020-09-04 08:27:32 +00:00
|
|
|
function getUserAccountData(address user)
|
2020-07-05 21:44:19 +00:00
|
|
|
external
|
2020-08-20 12:32:20 +00:00
|
|
|
override
|
2020-07-05 21:44:19 +00:00
|
|
|
view
|
|
|
|
returns (
|
|
|
|
uint256 totalCollateralETH,
|
2020-10-12 08:50:06 +00:00
|
|
|
uint256 totalDebtETH,
|
2020-07-05 21:44:19 +00:00
|
|
|
uint256 availableBorrowsETH,
|
|
|
|
uint256 currentLiquidationThreshold,
|
|
|
|
uint256 ltv,
|
|
|
|
uint256 healthFactor
|
|
|
|
)
|
|
|
|
{
|
|
|
|
(
|
|
|
|
totalCollateralETH,
|
2020-10-12 08:50:06 +00:00
|
|
|
totalDebtETH,
|
2020-07-05 21:44:19 +00:00
|
|
|
ltv,
|
|
|
|
currentLiquidationThreshold,
|
|
|
|
healthFactor
|
|
|
|
) = GenericLogic.calculateUserAccountData(
|
2020-09-04 08:27:32 +00:00
|
|
|
user,
|
2020-08-21 11:15:29 +00:00
|
|
|
_reserves,
|
2020-09-04 08:27:32 +00:00
|
|
|
_usersConfig[user],
|
|
|
|
_reservesList,
|
2020-10-06 13:51:48 +00:00
|
|
|
_reservesCount,
|
2020-09-04 08:27:32 +00:00
|
|
|
_addressesProvider.getPriceOracle()
|
2020-07-05 21:44:19 +00:00
|
|
|
);
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
availableBorrowsETH = GenericLogic.calculateAvailableBorrowsETH(
|
|
|
|
totalCollateralETH,
|
2020-10-12 08:50:06 +00:00
|
|
|
totalDebtETH,
|
2020-07-23 15:18:06 +00:00
|
|
|
ltv
|
2020-07-05 21:44:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-12 08:50:06 +00:00
|
|
|
/**
|
|
|
|
* @dev returns the configuration of the reserve
|
|
|
|
* @param asset the address of the reserve
|
|
|
|
* @return the configuration of the reserve
|
|
|
|
**/
|
|
|
|
function getConfiguration(address asset)
|
2020-07-05 21:44:19 +00:00
|
|
|
external
|
2020-08-20 12:32:20 +00:00
|
|
|
override
|
2020-07-05 21:44:19 +00:00
|
|
|
view
|
2020-10-12 08:50:06 +00:00
|
|
|
returns (ReserveConfiguration.Map memory)
|
2020-07-05 21:44:19 +00:00
|
|
|
{
|
2020-10-12 08:50:06 +00:00
|
|
|
return _reserves[asset].configuration;
|
|
|
|
}
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-10-13 11:41:57 +00:00
|
|
|
/**
|
2020-10-14 12:34:17 +00:00
|
|
|
* @dev returns the configuration of the user across all the reserves
|
2020-10-12 12:37:53 +00:00
|
|
|
* @param user the user
|
|
|
|
* @return the configuration of the user
|
|
|
|
**/
|
|
|
|
function getUserConfiguration(address user)
|
|
|
|
external
|
|
|
|
override
|
|
|
|
view
|
|
|
|
returns (UserConfiguration.Map memory)
|
|
|
|
{
|
|
|
|
return _usersConfig[user];
|
|
|
|
}
|
|
|
|
|
2020-10-13 11:41:57 +00:00
|
|
|
/**
|
2020-10-12 08:50:06 +00:00
|
|
|
* @dev returns the normalized income per unit of asset
|
|
|
|
* @param asset the address of the reserve
|
|
|
|
* @return the reserve normalized income
|
|
|
|
*/
|
2020-10-30 15:04:31 +00:00
|
|
|
function getReserveNormalizedIncome(address asset)
|
|
|
|
external
|
|
|
|
virtual
|
|
|
|
override
|
|
|
|
view
|
|
|
|
returns (uint256)
|
|
|
|
{
|
2020-10-12 08:50:06 +00:00
|
|
|
return _reserves[asset].getNormalizedIncome();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev returns the normalized variable debt per unit of asset
|
|
|
|
* @param asset the address of the reserve
|
|
|
|
* @return the reserve normalized debt
|
|
|
|
*/
|
|
|
|
function getReserveNormalizedVariableDebt(address asset)
|
|
|
|
external
|
|
|
|
override
|
|
|
|
view
|
|
|
|
returns (uint256)
|
|
|
|
{
|
|
|
|
return _reserves[asset].getNormalizedDebt();
|
|
|
|
}
|
|
|
|
|
2020-10-13 11:41:57 +00:00
|
|
|
/**
|
2020-10-12 08:50:06 +00:00
|
|
|
* @dev Returns if the LendingPool is paused
|
|
|
|
*/
|
|
|
|
function paused() external override view returns (bool) {
|
|
|
|
return _paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev returns the list of the initialized reserves
|
|
|
|
**/
|
|
|
|
function getReservesList() external override view returns (address[] memory) {
|
|
|
|
address[] memory _activeReserves = new address[](_reservesCount);
|
|
|
|
|
|
|
|
for (uint256 i = 0; i < _reservesCount; i++) {
|
|
|
|
_activeReserves[i] = _reservesList[i];
|
|
|
|
}
|
|
|
|
return _activeReserves;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev returns the addresses provider
|
|
|
|
**/
|
2020-11-02 11:54:56 +00:00
|
|
|
function getAddressesProvider() external override view returns (ILendingPoolAddressesProvider) {
|
2020-10-12 08:50:06 +00:00
|
|
|
return _addressesProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-29 10:57:43 +00:00
|
|
|
* @dev validates and finalizes an aToken transfer
|
2020-10-12 08:50:06 +00:00
|
|
|
* @param asset the address of the reserve
|
2020-10-29 10:57:43 +00:00
|
|
|
* @param from the user from which the aTokens are transferred
|
|
|
|
* @param to the user receiving the aTokens
|
2020-10-12 08:50:06 +00:00
|
|
|
* @param amount the amount being transferred/redeemed
|
2020-10-29 10:57:43 +00:00
|
|
|
* @param balanceFromBefore the balance of the from user before the transfer
|
|
|
|
* @param balanceToBefore the balance of the to user before the transfer
|
2020-10-12 08:50:06 +00:00
|
|
|
*/
|
2020-10-29 10:57:43 +00:00
|
|
|
function finalizeTransfer(
|
2020-10-12 08:50:06 +00:00
|
|
|
address asset,
|
2020-10-29 10:57:43 +00:00
|
|
|
address from,
|
|
|
|
address to,
|
|
|
|
uint256 amount,
|
|
|
|
uint256 balanceFromBefore,
|
|
|
|
uint256 balanceToBefore
|
2020-11-10 16:38:06 +00:00
|
|
|
) external override whenNotPaused {
|
2020-10-30 12:40:06 +00:00
|
|
|
require(msg.sender == _reserves[asset].aTokenAddress, Errors.LP_CALLER_MUST_BE_AN_ATOKEN);
|
2020-10-29 12:43:24 +00:00
|
|
|
|
2020-10-29 10:57:43 +00:00
|
|
|
ValidationLogic.validateTransfer(
|
|
|
|
from,
|
|
|
|
_reserves,
|
|
|
|
_usersConfig[from],
|
|
|
|
_reservesList,
|
|
|
|
_reservesCount,
|
|
|
|
_addressesProvider.getPriceOracle()
|
2020-07-05 21:44:19 +00:00
|
|
|
);
|
2020-10-29 10:57:43 +00:00
|
|
|
|
|
|
|
uint256 reserveId = _reserves[asset].id;
|
|
|
|
|
|
|
|
if (from != to) {
|
|
|
|
if (balanceFromBefore.sub(amount) == 0) {
|
|
|
|
UserConfiguration.Map storage fromConfig = _usersConfig[from];
|
|
|
|
fromConfig.setUsingAsCollateral(reserveId, false);
|
2020-11-02 15:40:31 +00:00
|
|
|
emit ReserveUsedAsCollateralDisabled(asset, from);
|
2020-10-29 10:57:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 12:43:24 +00:00
|
|
|
if (balanceToBefore == 0 && amount != 0) {
|
2020-10-29 10:57:43 +00:00
|
|
|
UserConfiguration.Map storage toConfig = _usersConfig[to];
|
|
|
|
toConfig.setUsingAsCollateral(reserveId, true);
|
2020-11-02 15:40:31 +00:00
|
|
|
emit ReserveUsedAsCollateralEnabled(asset, to);
|
2020-10-29 10:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev initializes a reserve
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param asset the address of the reserve
|
2020-09-04 08:27:32 +00:00
|
|
|
* @param aTokenAddress the address of the overlying aToken contract
|
|
|
|
* @param interestRateStrategyAddress the address of the interest rate strategy contract
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
|
|
|
function initReserve(
|
2020-08-21 11:15:29 +00:00
|
|
|
address asset,
|
2020-09-04 08:27:32 +00:00
|
|
|
address aTokenAddress,
|
|
|
|
address stableDebtAddress,
|
|
|
|
address variableDebtAddress,
|
|
|
|
address interestRateStrategyAddress
|
2020-11-10 16:38:06 +00:00
|
|
|
) external override onlyLendingPoolConfigurator {
|
2020-11-10 16:50:13 +00:00
|
|
|
require(Address.isContract(asset), Errors.LP_NOT_CONTRACT);
|
2020-08-21 11:15:29 +00:00
|
|
|
_reserves[asset].init(
|
2020-09-04 08:27:32 +00:00
|
|
|
aTokenAddress,
|
|
|
|
stableDebtAddress,
|
|
|
|
variableDebtAddress,
|
|
|
|
interestRateStrategyAddress
|
2020-07-05 21:44:19 +00:00
|
|
|
);
|
2020-08-21 12:03:17 +00:00
|
|
|
_addReserveToList(asset);
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev updates the address of the interest rate strategy contract
|
2020-08-21 11:15:29 +00:00
|
|
|
* @param asset the address of the reserve
|
2020-08-21 12:03:17 +00:00
|
|
|
* @param rateStrategyAddress the address of the interest rate strategy contract
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
2020-08-21 12:03:17 +00:00
|
|
|
function setReserveInterestRateStrategyAddress(address asset, address rateStrategyAddress)
|
2020-07-05 21:44:19 +00:00
|
|
|
external
|
2020-08-20 12:32:20 +00:00
|
|
|
override
|
2020-11-10 16:38:06 +00:00
|
|
|
onlyLendingPoolConfigurator
|
2020-07-05 21:44:19 +00:00
|
|
|
{
|
2020-08-21 12:03:17 +00:00
|
|
|
_reserves[asset].interestRateStrategyAddress = rateStrategyAddress;
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 08:50:06 +00:00
|
|
|
/**
|
2020-10-13 11:41:57 +00:00
|
|
|
* @dev sets the configuration map of the reserve
|
|
|
|
* @param asset the address of the reserve
|
|
|
|
* @param configuration the configuration map
|
|
|
|
**/
|
2020-11-10 16:52:23 +00:00
|
|
|
function setConfiguration(address asset, uint256 configuration)
|
|
|
|
external
|
|
|
|
override
|
|
|
|
onlyLendingPoolConfigurator
|
|
|
|
{
|
2020-08-21 12:03:17 +00:00
|
|
|
_reserves[asset].configuration.data = configuration;
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 08:50:06 +00:00
|
|
|
/**
|
|
|
|
* @dev Set the _pause state
|
|
|
|
* @param val the boolean value to set the current pause state of LendingPool
|
|
|
|
*/
|
2020-11-10 16:38:06 +00:00
|
|
|
function setPause(bool val) external override onlyLendingPoolConfigurator {
|
2020-10-12 08:50:06 +00:00
|
|
|
_paused = val;
|
|
|
|
if (_paused) {
|
|
|
|
emit Paused();
|
|
|
|
} else {
|
|
|
|
emit Unpaused();
|
|
|
|
}
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
// internal functions
|
|
|
|
struct ExecuteBorrowParams {
|
|
|
|
address asset;
|
|
|
|
address user;
|
2020-09-09 12:33:05 +00:00
|
|
|
address onBehalfOf;
|
2020-09-04 08:27:32 +00:00
|
|
|
uint256 amount;
|
|
|
|
uint256 interestRateMode;
|
|
|
|
address aTokenAddress;
|
|
|
|
uint16 referralCode;
|
|
|
|
bool releaseUnderlying;
|
|
|
|
}
|
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
/**
|
2020-09-04 08:27:32 +00:00
|
|
|
* @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
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
2020-09-04 08:27:32 +00:00
|
|
|
function _executeBorrow(ExecuteBorrowParams memory vars) internal {
|
|
|
|
ReserveLogic.ReserveData storage reserve = _reserves[vars.asset];
|
2020-09-09 12:33:05 +00:00
|
|
|
UserConfiguration.Map storage userConfig = _usersConfig[vars.onBehalfOf];
|
2020-09-04 08:27:32 +00:00
|
|
|
|
|
|
|
address oracle = _addressesProvider.getPriceOracle();
|
|
|
|
|
|
|
|
uint256 amountInETH = IPriceOracleGetter(oracle).getAssetPrice(vars.asset).mul(vars.amount).div(
|
|
|
|
10**reserve.configuration.getDecimals()
|
|
|
|
);
|
|
|
|
|
|
|
|
ValidationLogic.validateBorrow(
|
2020-10-28 10:48:55 +00:00
|
|
|
vars.asset,
|
2020-09-04 08:27:32 +00:00
|
|
|
reserve,
|
2020-09-09 12:33:05 +00:00
|
|
|
vars.onBehalfOf,
|
2020-09-04 08:27:32 +00:00
|
|
|
vars.amount,
|
|
|
|
amountInETH,
|
|
|
|
vars.interestRateMode,
|
|
|
|
MAX_STABLE_RATE_BORROW_SIZE_PERCENT,
|
|
|
|
_reserves,
|
|
|
|
userConfig,
|
|
|
|
_reservesList,
|
2020-10-06 13:51:48 +00:00
|
|
|
_reservesCount,
|
2020-09-04 08:27:32 +00:00
|
|
|
oracle
|
|
|
|
);
|
|
|
|
|
2020-09-14 07:53:21 +00:00
|
|
|
reserve.updateState();
|
2020-09-04 08:27:32 +00:00
|
|
|
|
|
|
|
//caching the current stable borrow rate
|
|
|
|
uint256 currentStableRate = 0;
|
|
|
|
|
2020-10-29 13:55:11 +00:00
|
|
|
bool isFirstBorrowing = false;
|
2020-09-04 08:27:32 +00:00
|
|
|
if (
|
|
|
|
ReserveLogic.InterestRateMode(vars.interestRateMode) == ReserveLogic.InterestRateMode.STABLE
|
|
|
|
) {
|
|
|
|
currentStableRate = reserve.currentStableBorrowRate;
|
|
|
|
|
2020-10-29 13:55:11 +00:00
|
|
|
isFirstBorrowing = IStableDebtToken(reserve.stableDebtTokenAddress).mint(
|
2020-11-03 18:47:57 +00:00
|
|
|
vars.user,
|
2020-09-09 12:33:05 +00:00
|
|
|
vars.onBehalfOf,
|
2020-09-04 08:27:32 +00:00
|
|
|
vars.amount,
|
|
|
|
currentStableRate
|
|
|
|
);
|
|
|
|
} else {
|
2020-10-29 13:55:11 +00:00
|
|
|
isFirstBorrowing = IVariableDebtToken(reserve.variableDebtTokenAddress).mint(
|
2020-11-03 18:47:57 +00:00
|
|
|
vars.user,
|
2020-09-21 13:35:22 +00:00
|
|
|
vars.onBehalfOf,
|
|
|
|
vars.amount,
|
|
|
|
reserve.variableBorrowIndex
|
|
|
|
);
|
2020-09-04 08:27:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 13:55:11 +00:00
|
|
|
if (isFirstBorrowing) {
|
2020-10-30 10:55:50 +00:00
|
|
|
userConfig.setBorrowing(reserve.id, true);
|
2020-10-29 13:55:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 10:47:27 +00:00
|
|
|
reserve.updateInterestRates(
|
|
|
|
vars.asset,
|
|
|
|
vars.aTokenAddress,
|
|
|
|
0,
|
|
|
|
vars.releaseUnderlying ? vars.amount : 0
|
|
|
|
);
|
|
|
|
|
|
|
|
if (vars.releaseUnderlying) {
|
2020-09-09 12:33:05 +00:00
|
|
|
IAToken(vars.aTokenAddress).transferUnderlyingTo(vars.user, vars.amount);
|
2020-09-04 08:27:32 +00:00
|
|
|
}
|
2020-09-09 10:47:27 +00:00
|
|
|
|
2020-09-04 08:27:32 +00:00
|
|
|
emit Borrow(
|
|
|
|
vars.asset,
|
2020-09-09 12:33:05 +00:00
|
|
|
vars.user,
|
|
|
|
vars.onBehalfOf,
|
2020-09-04 08:27:32 +00:00
|
|
|
vars.amount,
|
|
|
|
vars.interestRateMode,
|
|
|
|
ReserveLogic.InterestRateMode(vars.interestRateMode) == ReserveLogic.InterestRateMode.STABLE
|
|
|
|
? currentStableRate
|
|
|
|
: reserve.currentVariableBorrowRate,
|
|
|
|
vars.referralCode
|
|
|
|
);
|
|
|
|
}
|
2020-07-05 21:44:19 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-21 11:15:29 +00:00
|
|
|
* @dev adds a reserve to the array of the _reserves address
|
2020-07-05 21:44:19 +00:00
|
|
|
**/
|
2020-08-21 12:03:17 +00:00
|
|
|
function _addReserveToList(address asset) internal {
|
2020-10-28 11:02:40 +00:00
|
|
|
uint256 reservesCount = _reservesCount;
|
|
|
|
|
2020-10-30 12:40:06 +00:00
|
|
|
require(reservesCount < MAX_NUMBER_RESERVES, Errors.LP_NO_MORE_RESERVES_ALLOWED);
|
2020-10-06 16:20:44 +00:00
|
|
|
|
2020-10-08 13:41:48 +00:00
|
|
|
bool reserveAlreadyAdded = _reserves[asset].id != 0 || _reservesList[0] == asset;
|
|
|
|
|
2020-08-05 10:40:24 +00:00
|
|
|
if (!reserveAlreadyAdded) {
|
2020-10-28 11:02:40 +00:00
|
|
|
_reserves[asset].id = uint8(reservesCount);
|
|
|
|
_reservesList[reservesCount] = asset;
|
2020-10-06 13:51:48 +00:00
|
|
|
|
2020-11-10 16:28:43 +00:00
|
|
|
_reservesCount = reservesCount + 1;
|
2020-08-05 10:40:24 +00:00
|
|
|
}
|
2020-07-05 21:44:19 +00:00
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|