Merge branch 'master' into feat/26-repay-collateral

This commit is contained in:
eboado 2020-09-09 21:12:36 +02:00
commit 4b3abac526
47 changed files with 4246 additions and 971 deletions

View File

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

View File

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

View File

@ -12,27 +12,12 @@ abstract contract FlashLoanReceiverBase is IFlashLoanReceiver {
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
using SafeMath for uint256; using SafeMath for uint256;
ILendingPoolAddressesProvider public addressesProvider; ILendingPoolAddressesProvider internal _addressesProvider;
constructor(ILendingPoolAddressesProvider provider) public { constructor(ILendingPoolAddressesProvider provider) public {
addressesProvider = provider; _addressesProvider = provider;
} }
receive() external payable {} 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 { interface IFlashLoanReceiver {
function executeOperation( function executeOperation(
address reserve, address reserve,
address destination,
uint256 amount, uint256 amount,
uint256 fee, uint256 fee,
bytes calldata params bytes calldata params

View File

@ -0,0 +1,80 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.8;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: agpl-3.0 // SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8; pragma solidity ^0.6.8;
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {IERC20} from './IERC20.sol';
interface IERC20Detailed is IERC20 { interface IERC20Detailed is IERC20 {
function name() external view returns (string memory); function name() external view returns (string memory);

View File

@ -63,7 +63,7 @@ interface ILendingPool {
* @param reserve the address of the reserve * @param reserve the address of the reserve
* @param user the address of the user executing the swap * @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 * @dev emitted when a user enables a reserve as collateral
@ -90,13 +90,15 @@ interface ILendingPool {
* @param target the address of the flashLoanReceiver * @param target the address of the flashLoanReceiver
* @param reserve the address of the reserve * @param reserve the address of the reserve
* @param amount the amount requested * @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( event FlashLoan(
address indexed target, address indexed target,
address indexed reserve, address indexed reserve,
uint256 amount, uint256 amount,
uint256 totalFee uint256 totalPremium,
uint16 referralCode
); );
/** /**
* @dev these events are not emitted directly by the LendingPool * @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. * 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 * @dev emitted when a borrower is liquidated
* @param collateral the address of the collateral being liquidated * @param collateral the address of the collateral being liquidated
@ -259,12 +246,16 @@ interface ILendingPool {
* @param receiver The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface. * @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 reserve the address of the principal reserve
* @param amount the amount requested for this flashloan * @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( function flashLoan(
address receiver, address receiver,
address reserve, address reserve,
uint256 amount, uint256 amount,
bytes calldata params uint256 debtType,
bytes calldata params,
uint16 referralCode
) external; ) external;
/** /**

View File

@ -3,7 +3,6 @@ pragma solidity ^0.6.8;
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; 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 {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import { import {
VersionedInitializable VersionedInitializable
@ -11,6 +10,7 @@ import {
import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol';
import {IAToken} from '../tokenization/interfaces/IAToken.sol'; import {IAToken} from '../tokenization/interfaces/IAToken.sol';
import {Helpers} from '../libraries/helpers/Helpers.sol'; import {Helpers} from '../libraries/helpers/Helpers.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
import {WadRayMath} from '../libraries/math/WadRayMath.sol'; import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol'; import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol';
import {GenericLogic} from '../libraries/logic/GenericLogic.sol'; import {GenericLogic} from '../libraries/logic/GenericLogic.sol';
@ -31,7 +31,7 @@ import {ILendingPool} from '../interfaces/ILendingPool.sol';
* @author Aave * @author Aave
**/ **/
contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { contract LendingPool is VersionedInitializable, ILendingPool {
using SafeMath for uint256; using SafeMath for uint256;
using WadRayMath for uint256; using WadRayMath for uint256;
using ReserveLogic for ReserveLogic.ReserveData; using ReserveLogic for ReserveLogic.ReserveData;
@ -42,7 +42,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
//main configuration parameters //main configuration parameters
uint256 public constant REBALANCE_DOWN_RATE_DELTA = (1e27) / 5; uint256 public constant REBALANCE_DOWN_RATE_DELTA = (1e27) / 5;
uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25; 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;
@ -57,7 +57,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @dev only lending pools configurator can use functions affected by this modifier * @dev only lending pools configurator can use functions affected by this modifier
**/ **/
modifier onlyLendingPoolConfigurator { modifier onlyLendingPoolConfigurator {
require(_addressesProvider.getLendingPoolConfigurator() == msg.sender, '30'); require(
_addressesProvider.getLendingPoolConfigurator() == msg.sender,
Errors.CALLER_NOT_LENDING_POOL_CONFIGURATOR
);
_; _;
} }
@ -89,7 +92,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
address asset, address asset,
uint256 amount, uint256 amount,
uint16 referralCode uint16 referralCode
) external override nonReentrant { ) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
ValidationLogic.validateDeposit(reserve, amount); ValidationLogic.validateDeposit(reserve, amount);
@ -110,7 +113,6 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
//transfer to the aToken contract //transfer to the aToken contract
IERC20(asset).safeTransferFrom(msg.sender, aToken, amount); IERC20(asset).safeTransferFrom(msg.sender, aToken, amount);
//solium-disable-next-line
emit Deposit(asset, msg.sender, amount, referralCode); emit Deposit(asset, msg.sender, amount, referralCode);
} }
@ -119,7 +121,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @param asset the address of the reserve * @param asset the address of the reserve
* @param amount the underlying amount to be redeemed * @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]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
address aToken = reserve.aTokenAddress; address aToken = reserve.aTokenAddress;
@ -154,7 +156,6 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
IAToken(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); emit Withdraw(asset, msg.sender, amount);
} }
@ -164,65 +165,24 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @param asset the address of the reserve * @param asset the address of the reserve
* @param amount the amount to be borrowed * @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 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( function borrow(
address asset, address asset,
uint256 amount, uint256 amount,
uint256 interestRateMode, uint256 interestRateMode,
uint16 referralCode uint16 referralCode
) external override nonReentrant { ) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset]; _executeBorrow(
UserConfiguration.Map storage userConfig = _usersConfig[msg.sender]; ExecuteBorrowParams(
asset,
uint256 amountInETH = IPriceOracleGetter(_addressesProvider.getPriceOracle()) msg.sender,
.getAssetPrice(asset) amount,
.mul(amount) interestRateMode,
.div(10**reserve.configuration.getDecimals()); //price is in ether _reserves[asset].aTokenAddress,
referralCode,
ValidationLogic.validateBorrow( true
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);
}
address aToken = reserve.aTokenAddress;
reserve.updateInterestRates(asset, aToken, 0, amount);
uint256 reserveIndex = reserve.index;
if (!userConfig.isBorrowing(reserveIndex)) {
userConfig.setBorrowing(reserveIndex, true);
}
//if we reached this point, we can transfer
IAToken(aToken).transferUnderlyingTo(msg.sender, amount);
emit Borrow(
asset,
msg.sender,
amount,
interestRateMode,
ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE
? userStableRate
: reserve.currentVariableBorrowRate,
referralCode
); );
} }
@ -239,7 +199,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
uint256 amount, uint256 amount,
uint256 rateMode, uint256 rateMode,
address onBehalfOf address onBehalfOf
) external override nonReentrant { ) external override {
_executeRepay(asset, msg.sender, amount, rateMode, onBehalfOf); _executeRepay(asset, msg.sender, amount, rateMode, onBehalfOf);
} }
@ -300,7 +260,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @param asset the address of the reserve on which the user borrowed * @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]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve); (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve);
@ -333,12 +293,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0);
emit Swap( emit Swap(asset, msg.sender);
asset,
msg.sender,
//solium-disable-next-line
block.timestamp
);
} }
/** /**
@ -348,7 +303,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @param asset the address of the reserve * @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]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
IStableDebtToken stableDebtToken = IStableDebtToken(reserve.stableDebtTokenAddress); IStableDebtToken stableDebtToken = IStableDebtToken(reserve.stableDebtTokenAddress);
@ -356,10 +311,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user); uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user);
// user must be borrowing on asset at a stable rate // 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 = reserve.currentStableBorrowRate.rayMul( uint256 rebalanceDownRateThreshold = WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA).rayMul(
WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA) reserve.currentStableBorrowRate
); );
//1. user stable borrow rate is below the current liquidity rate. The loan needs to be rebalanced, //1. user stable borrow rate is below the current liquidity rate. The loan needs to be rebalanced,
@ -371,7 +326,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
require( require(
userStableRate < reserve.currentLiquidityRate || userStableRate > rebalanceDownRateThreshold, 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 //burn old debt tokens, mint new ones
@ -393,11 +348,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* @param asset the address of the reserve * @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) function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) external override {
external
override
nonReentrant
{
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
ValidationLogic.validateSetUseReserveAsCollateral( ValidationLogic.validateSetUseReserveAsCollateral(
@ -433,7 +384,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
address user, address user,
uint256 purchaseAmount, uint256 purchaseAmount,
bool receiveAToken bool receiveAToken
) external override nonReentrant { ) external override {
address liquidationManager = _addressesProvider.getLendingPoolLiquidationManager(); address liquidationManager = _addressesProvider.getLendingPoolLiquidationManager();
//solium-disable-next-line //solium-disable-next-line
@ -447,7 +398,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
receiveAToken receiveAToken
) )
); );
require(success, 'Liquidation call failed'); require(success, Errors.LIQUIDATION_CALL_FAILED);
(uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string)); (uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string));
@ -457,6 +408,19 @@ 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 flashes the underlying collateral on an user to swap for the owed asset and repay * @dev flashes the underlying collateral on an user to swap for the owed asset and repay
* - Both the owner of the position and other liquidators can execute it * - Both the owner of the position and other liquidators can execute it
@ -477,7 +441,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
address receiver, address receiver,
bytes calldata params bytes calldata params
) external override { ) external override {
require(!_flashLiquidationLocked, "REENTRANCY_NOT_ALLOWED"); require(!_flashLiquidationLocked, Errors.REENTRANCY_NOT_ALLOWED);
_flashLiquidationLocked = true; _flashLiquidationLocked = true;
address liquidationManager = _addressesProvider.getLendingPoolLiquidationManager(); address liquidationManager = _addressesProvider.getLendingPoolLiquidationManager();
@ -494,7 +458,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
params params
) )
); );
require(success, 'FAILED_REPAY_WITH_COLLATERAL'); require(success, Errors.FAILED_REPAY_WITH_COLLATERAL);
(uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string)); (uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string));
@ -510,63 +474,65 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
* as long as the amount taken plus a fee is returned. NOTE There are security concerns for developers of flashloan receiver contracts * 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 * 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 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 asset The address of the principal reserve
* @param amount the amount requested for this flashloan * @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( function flashLoan(
address receiverAddress, address receiverAddress,
address asset, address asset,
uint256 amount, uint256 amount,
bytes calldata params uint256 mode,
) external override nonReentrant { bytes calldata params,
uint16 referralCode
) external override {
ReserveLogic.ReserveData storage reserve = _reserves[asset]; 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 vars.premium = amount.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000);
uint256 availableLiquidityBefore = IERC20(asset).balanceOf(aTokenAddress);
//calculate amount fee ValidationLogic.validateFlashloan(mode, vars.premium);
uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000);
require( ReserveLogic.InterestRateMode debtMode = ReserveLogic.InterestRateMode(mode);
availableLiquidityBefore >= amount,
'There is not enough liquidity available to borrow'
);
require(amountFee > 0, 'The requested amount is too small for a FlashLoan.');
//get the FlashLoanReceiver instance vars.receiver = IFlashLoanReceiver(receiverAddress);
IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress);
//transfer funds to the receiver //transfer funds to the receiver
IAToken(aTokenAddress).transferUnderlyingTo(receiverAddress, amount); IAToken(vars.aTokenAddress).transferUnderlyingTo(receiverAddress, amount);
//execute action of the receiver //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 vars.amountPlusPremium = amount.add(vars.premium);
uint256 availableLiquidityAfter = IERC20(asset).balanceOf(aTokenAddress);
require( if (debtMode == ReserveLogic.InterestRateMode.NONE) {
availableLiquidityAfter == availableLiquidityBefore.add(amountFee),
'The actual balance of the protocol is inconsistent' 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 } else {
reserve.updateCumulativeIndexesAndTimestamp(); // If the transfer didn't succeed, the receiver either didn't return the funds, or didn't approve the transfer.
_executeBorrow(
uint256 totalLiquidityBefore = availableLiquidityBefore ExecuteBorrowParams(
.add(IERC20(reserve.variableDebtTokenAddress).totalSupply()) asset,
.add(IERC20(reserve.stableDebtTokenAddress).totalSupply()); msg.sender,
vars.amountPlusPremium.sub(vars.availableBalance),
//compounding the received fee into the reserve mode,
reserve.cumulateToLiquidityIndex(totalLiquidityBefore, amountFee); vars.aTokenAddress,
referralCode,
//refresh interest rates false
reserve.updateInterestRates(asset, aTokenAddress, amountFee, 0); )
);
//solium-disable-next-line }
emit FlashLoan(receiverAddress, asset, amount, amountFee);
} }
/** /**
@ -783,9 +749,89 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
return _reserves[asset].configuration; 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 * @dev adds a reserve to the array of the _reserves address

View File

@ -13,6 +13,7 @@ import {ReserveConfiguration} from '../libraries/configuration/ReserveConfigurat
import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol';
import {ILendingPool} from '../interfaces/ILendingPool.sol'; import {ILendingPool} from '../interfaces/ILendingPool.sol';
import {IERC20Detailed} from '../interfaces/IERC20Detailed.sol'; import {IERC20Detailed} from '../interfaces/IERC20Detailed.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
/** /**
* @title LendingPoolConfigurator contract * @title LendingPoolConfigurator contract
@ -178,7 +179,7 @@ contract LendingPoolConfigurator is VersionedInitializable {
modifier onlyLendingPoolManager { modifier onlyLendingPoolManager {
require( require(
addressesProvider.getLendingPoolManager() == msg.sender, addressesProvider.getLendingPoolManager() == msg.sender,
'The caller must be a lending pool manager' Errors.CALLER_NOT_LENDING_POOL_MANAGER
); );
_; _;
} }
@ -425,7 +426,7 @@ contract LendingPoolConfigurator is VersionedInitializable {
) = pool.getReserveData(asset); ) = pool.getReserveData(asset);
require( require(
availableLiquidity == 0 && totalBorrowsStable == 0 && totalBorrowsVariable == 0, 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); ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset);

View File

@ -3,8 +3,6 @@ pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.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 { import {
VersionedInitializable VersionedInitializable
} from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; } from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol';
@ -22,13 +20,14 @@ import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {PercentageMath} from '../libraries/math/PercentageMath.sol'; import {PercentageMath} from '../libraries/math/PercentageMath.sol';
import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol';
import {ISwapAdapter} from '../interfaces/ISwapAdapter.sol'; import {ISwapAdapter} from '../interfaces/ISwapAdapter.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
/** /**
* @title LendingPoolLiquidationManager contract * @title LendingPoolLiquidationManager contract
* @author Aave * @author Aave
* @notice Implements the liquidation function. * @notice Implements the liquidation function.
**/ **/
contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializable { contract LendingPoolLiquidationManager is VersionedInitializable {
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
using SafeMath for uint256; using SafeMath for uint256;
using WadRayMath for uint256; using WadRayMath for uint256;
@ -156,7 +155,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (vars.healthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) { if (vars.healthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) {
return ( return (
uint256(LiquidationErrors.HEALTH_FACTOR_ABOVE_THRESHOLD), uint256(LiquidationErrors.HEALTH_FACTOR_ABOVE_THRESHOLD),
'Health factor is not below the threshold' Errors.HEALTH_FACTOR_NOT_BELOW_THRESHOLD
); );
} }
@ -172,7 +171,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (!vars.isCollateralEnabled) { if (!vars.isCollateralEnabled) {
return ( return (
uint256(LiquidationErrors.COLLATERAL_CANNOT_BE_LIQUIDATED), uint256(LiquidationErrors.COLLATERAL_CANNOT_BE_LIQUIDATED),
'The collateral chosen cannot be liquidated' Errors.COLLATERAL_CANNOT_BE_LIQUIDATED
); );
} }
@ -185,7 +184,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (vars.userStableDebt == 0 && vars.userVariableDebt == 0) { if (vars.userStableDebt == 0 && vars.userVariableDebt == 0) {
return ( return (
uint256(LiquidationErrors.CURRRENCY_NOT_BORROWED), uint256(LiquidationErrors.CURRRENCY_NOT_BORROWED),
'User did not borrow the specified currency' Errors.SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER
); );
} }
@ -226,7 +225,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (currentAvailableCollateral < vars.maxCollateralToLiquidate) { if (currentAvailableCollateral < vars.maxCollateralToLiquidate) {
return ( return (
uint256(LiquidationErrors.NOT_ENOUGH_LIQUIDITY), uint256(LiquidationErrors.NOT_ENOUGH_LIQUIDITY),
"There isn't enough liquidity available to liquidate" Errors.NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE
); );
} }
} }
@ -292,7 +291,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
receiveAToken receiveAToken
); );
return (uint256(LiquidationErrors.NO_ERROR), 'No errors'); return (uint256(LiquidationErrors.NO_ERROR), Errors.NO_ERRORS);
} }
/** /**
@ -333,7 +332,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (msg.sender != user && vars.healthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) { if (msg.sender != user && vars.healthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) {
return ( return (
uint256(LiquidationErrors.HEALTH_FACTOR_ABOVE_THRESHOLD), uint256(LiquidationErrors.HEALTH_FACTOR_ABOVE_THRESHOLD),
'HEALTH_FACTOR_ABOVE_THRESHOLD' Errors.HEALTH_FACTOR_NOT_BELOW_THRESHOLD
); );
} }
@ -345,7 +344,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (!vars.isCollateralEnabled) { if (!vars.isCollateralEnabled) {
return ( return (
uint256(LiquidationErrors.COLLATERAL_CANNOT_BE_LIQUIDATED), uint256(LiquidationErrors.COLLATERAL_CANNOT_BE_LIQUIDATED),
'COLLATERAL_CANNOT_BE_LIQUIDATED' Errors.COLLATERAL_CANNOT_BE_LIQUIDATED
); );
} }
} }
@ -358,7 +357,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
if (vars.userStableDebt == 0 && vars.userVariableDebt == 0) { if (vars.userStableDebt == 0 && vars.userVariableDebt == 0) {
return ( return (
uint256(LiquidationErrors.CURRRENCY_NOT_BORROWED), uint256(LiquidationErrors.CURRRENCY_NOT_BORROWED),
'CURRRENCY_NOT_BORROWED' Errors.SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER
); );
} }
@ -458,7 +457,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
vars.maxCollateralToLiquidate vars.maxCollateralToLiquidate
); );
return (uint256(LiquidationErrors.NO_ERROR), 'SUCCESS'); return (uint256(LiquidationErrors.NO_ERROR), Errors.NO_ERRORS);
} }
struct AvailableCollateralToLiquidateLocalVars { struct AvailableCollateralToLiquidateLocalVars {

View File

@ -0,0 +1,78 @@
// 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'
string public constant INVALID_FLASHLOAN_MODE = '43'; //Invalid flashloan mode selected
string public constant REENTRANCY_NOT_ALLOWED = '52';
string public constant FAILED_REPAY_WITH_COLLATERAL = '53';
// 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'
string public constant LIQUIDITY_INDEX_OVERFLOW = '47'; // Liquidity index overflows uint128
string public constant VARIABLE_BORROW_INDEX_OVERFLOW = '48'; // Variable borrow index overflows uint128
string public constant LIQUIDITY_RATE_OVERFLOW = '49'; // Liquidity rate overflows uint128
string public constant VARIABLE_BORROW_RATE_OVERFLOW = '50'; // Variable borrow rate overflows uint128
string public constant STABLE_BORROW_RATE_OVERFLOW = '51'; // Stable borrow rate overflows uint128
//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'
//require error messages - Math libraries
string public constant MULTIPLICATION_OVERFLOW = '44';
string public constant ADDITION_OVERFLOW = '45';
string public constant DIVISION_BY_ZERO = '46';
}

View File

@ -10,6 +10,7 @@ import {IStableDebtToken} from '../../tokenization/interfaces/IStableDebtToken.s
import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol'; import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
import {IReserveInterestRateStrategy} from '../../interfaces/IReserveInterestRateStrategy.sol'; import {IReserveInterestRateStrategy} from '../../interfaces/IReserveInterestRateStrategy.sol';
import {WadRayMath} from '../math/WadRayMath.sol'; import {WadRayMath} from '../math/WadRayMath.sol';
import {Errors} from '../helpers/Errors.sol';
/** /**
* @title ReserveLogic library * @title ReserveLogic library
@ -48,25 +49,26 @@ library ReserveLogic {
// refer to the whitepaper, section 1.1 basic concepts for a formal description of these properties. // refer to the whitepaper, section 1.1 basic concepts for a formal description of these properties.
struct ReserveData { struct ReserveData {
//the liquidity index. Expressed in ray
uint256 lastLiquidityIndex;
//the current supply rate. Expressed in ray
uint256 currentLiquidityRate;
//the current variable borrow rate. Expressed in ray
uint256 currentVariableBorrowRate;
//the current stable borrow rate. Expressed in ray
uint256 currentStableBorrowRate;
//variable borrow index. Expressed in ray
uint256 lastVariableBorrowIndex;
//stores the reserve configuration //stores the reserve configuration
ReserveConfiguration.Map configuration; ReserveConfiguration.Map configuration;
address aTokenAddress; address aTokenAddress;
address stableDebtTokenAddress; address stableDebtTokenAddress;
address variableDebtTokenAddress; address variableDebtTokenAddress;
address interestRateStrategyAddress; address interestRateStrategyAddress;
//the liquidity index. Expressed in ray
uint128 lastLiquidityIndex;
//the current supply rate. Expressed in ray
uint128 currentLiquidityRate;
//the current variable borrow rate. Expressed in ray
uint128 currentVariableBorrowRate;
//the current stable borrow rate. Expressed in ray
uint128 currentStableBorrowRate;
//variable borrow index. Expressed in ray
uint128 lastVariableBorrowIndex;
uint40 lastUpdateTimestamp; uint40 lastUpdateTimestamp;
//the index of the reserve in the list of the active reserves //the index of the reserve in the list of the active reserves
uint8 index; uint8 index;
} }
/** /**
@ -120,28 +122,34 @@ library ReserveLogic {
* a formal specification. * a formal specification.
* @param reserve the reserve object * @param reserve the reserve object
**/ **/
function updateCumulativeIndexesAndTimestamp(ReserveData storage reserve) internal { function updateCumulativeIndexesAndTimestamp(ReserveData storage reserve) internal {
uint256 currentLiquidityRate = reserve.currentLiquidityRate;
//only cumulating if there is any income being produced //only cumulating if there is any income being produced
if ( if (currentLiquidityRate > 0) {
IERC20(reserve.variableDebtTokenAddress).totalSupply() > 0 ||
IERC20(reserve.stableDebtTokenAddress).totalSupply() > 0
) {
uint40 lastUpdateTimestamp = reserve.lastUpdateTimestamp; uint40 lastUpdateTimestamp = reserve.lastUpdateTimestamp;
uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest( uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest(
reserve.currentLiquidityRate, currentLiquidityRate,
lastUpdateTimestamp lastUpdateTimestamp
); );
uint256 index = cumulatedLiquidityInterest.rayMul(reserve.lastLiquidityIndex);
require(index < (1 << 128), Errors.LIQUIDITY_INDEX_OVERFLOW);
reserve.lastLiquidityIndex = cumulatedLiquidityInterest.rayMul(reserve.lastLiquidityIndex); reserve.lastLiquidityIndex = uint128(index);
uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest( //as the liquidity rate might come only from stable rate loans, we need to ensure
reserve.currentVariableBorrowRate, //that there is actual variable debt before accumulating
lastUpdateTimestamp if (IERC20(reserve.variableDebtTokenAddress).totalSupply() > 0) {
); uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest(
reserve.lastVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul( reserve.currentVariableBorrowRate,
reserve.lastVariableBorrowIndex lastUpdateTimestamp
); );
index = cumulatedVariableBorrowInterest.rayMul(
reserve.lastVariableBorrowIndex
);
require(index < (1 << 128), Errors.VARIABLE_BORROW_INDEX_OVERFLOW);
reserve.lastVariableBorrowIndex = uint128(index);
}
} }
//solium-disable-next-line //solium-disable-next-line
@ -162,9 +170,14 @@ library ReserveLogic {
) internal { ) internal {
uint256 amountToLiquidityRatio = amount.wadToRay().rayDiv(totalLiquidity.wadToRay()); uint256 amountToLiquidityRatio = amount.wadToRay().rayDiv(totalLiquidity.wadToRay());
uint256 cumulatedLiquidity = amountToLiquidityRatio.add(WadRayMath.ray()); uint256 result = amountToLiquidityRatio.add(WadRayMath.ray());
reserve.lastLiquidityIndex = cumulatedLiquidity.rayMul(reserve.lastLiquidityIndex); result = result.rayMul(
reserve.lastLiquidityIndex
);
require(result < (1 << 128), Errors.LIQUIDITY_INDEX_OVERFLOW);
reserve.lastLiquidityIndex = uint128(result);
} }
/** /**
@ -180,14 +193,14 @@ library ReserveLogic {
address variableDebtTokenAddress, address variableDebtTokenAddress,
address interestRateStrategyAddress address interestRateStrategyAddress
) external { ) 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 (reserve.lastLiquidityIndex == 0) {
//if the reserve has not been initialized yet //if the reserve has not been initialized yet
reserve.lastLiquidityIndex = WadRayMath.ray(); reserve.lastLiquidityIndex = uint128(WadRayMath.ray());
} }
if (reserve.lastVariableBorrowIndex == 0) { if (reserve.lastVariableBorrowIndex == 0) {
reserve.lastVariableBorrowIndex = WadRayMath.ray(); reserve.lastVariableBorrowIndex = uint128(WadRayMath.ray());
} }
reserve.aTokenAddress = aTokenAddress; reserve.aTokenAddress = aTokenAddress;
@ -196,6 +209,14 @@ library ReserveLogic {
reserve.interestRateStrategyAddress = interestRateStrategyAddress; reserve.interestRateStrategyAddress = interestRateStrategyAddress;
} }
struct UpdateInterestRatesLocalVars {
uint256 currentAvgStableRate;
uint256 availableLiquidity;
address stableDebtTokenAddress;
uint256 newLiquidityRate;
uint256 newStableRate;
uint256 newVariableRate;
}
/** /**
* @dev Updates the reserve current stable borrow rate Rf, the current variable borrow rate Rv and the current liquidity rate Rl. * @dev Updates the reserve current stable borrow rate Rf, the current variable borrow rate Rv and the current liquidity rate Rl.
* Also updates the lastUpdateTimestamp value. Please refer to the whitepaper for further information. * Also updates the lastUpdateTimestamp value. Please refer to the whitepaper for further information.
@ -210,31 +231,37 @@ library ReserveLogic {
uint256 liquidityAdded, uint256 liquidityAdded,
uint256 liquidityTaken uint256 liquidityTaken
) internal { ) internal {
uint256 currentAvgStableRate = IStableDebtToken(reserve.stableDebtTokenAddress) UpdateInterestRatesLocalVars memory vars;
.getAverageStableRate();
vars.stableDebtTokenAddress = reserve.stableDebtTokenAddress;
vars.currentAvgStableRate = IStableDebtToken(vars.stableDebtTokenAddress).getAverageStableRate();
vars.availableLiquidity = IERC20(reserveAddress).balanceOf(aTokenAddress);
( (
uint256 newLiquidityRate, vars.newLiquidityRate,
uint256 newStableRate, vars.newStableRate,
uint256 newVariableRate vars.newVariableRate
) = IReserveInterestRateStrategy(reserve.interestRateStrategyAddress).calculateInterestRates( ) = IReserveInterestRateStrategy(reserve.interestRateStrategyAddress).calculateInterestRates(
reserveAddress, reserveAddress,
IERC20(reserveAddress).balanceOf(aTokenAddress).add(liquidityAdded).sub(liquidityTaken), vars.availableLiquidity.add(liquidityAdded).sub(liquidityTaken),
IERC20(reserve.stableDebtTokenAddress).totalSupply(), IERC20(vars.stableDebtTokenAddress).totalSupply(),
IERC20(reserve.variableDebtTokenAddress).totalSupply(), IERC20(reserve.variableDebtTokenAddress).totalSupply(),
currentAvgStableRate vars.currentAvgStableRate
); );
require(vars.newLiquidityRate < (1 << 128), "ReserveLogic: Liquidity rate overflow");
require(vars.newStableRate < (1 << 128), "ReserveLogic: Stable borrow rate overflow");
require(vars.newVariableRate < (1 << 128), "ReserveLogic: Variable borrow rate overflow");
reserve.currentLiquidityRate = newLiquidityRate; reserve.currentLiquidityRate = uint128(vars.newLiquidityRate);
reserve.currentStableBorrowRate = newStableRate; reserve.currentStableBorrowRate = uint128(vars.newStableRate);
reserve.currentVariableBorrowRate = newVariableRate; reserve.currentVariableBorrowRate = uint128(vars.newVariableRate);
emit ReserveDataUpdated( emit ReserveDataUpdated(
reserveAddress, reserveAddress,
newLiquidityRate, vars.newLiquidityRate,
newStableRate, vars.newStableRate,
currentAvgStableRate, vars.currentAvgStableRate,
newVariableRate, vars.newVariableRate,
reserve.lastLiquidityIndex, reserve.lastLiquidityIndex,
reserve.lastVariableBorrowIndex reserve.lastVariableBorrowIndex
); );

View File

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

@ -55,17 +55,17 @@ library MathUtils {
return WadRayMath.ray(); return WadRayMath.ray();
} }
uint256 expMinusOne = exp.sub(1); uint256 expMinusOne = exp-1;
uint256 expMinusTwo = exp > 2 ? exp.sub(2) : 0; uint256 expMinusTwo = exp > 2 ? exp-2 : 0;
uint256 ratePerSecond = rate.div(31536000); uint256 ratePerSecond = rate/SECONDS_PER_YEAR;
uint256 basePowerTwo = ratePerSecond.rayMul(ratePerSecond); uint256 basePowerTwo = ratePerSecond.rayMul(ratePerSecond);
uint256 basePowerThree = basePowerTwo.rayMul(ratePerSecond); uint256 basePowerThree = basePowerTwo.rayMul(ratePerSecond);
uint256 secondTerm = exp.mul(expMinusOne).mul(basePowerTwo).div(2); uint256 secondTerm = exp.mul(expMinusOne).mul(basePowerTwo)/2;
uint256 thirdTerm = exp.mul(expMinusOne).mul(expMinusTwo).mul(basePowerThree).div(6); uint256 thirdTerm = exp.mul(expMinusOne).mul(expMinusTwo).mul(basePowerThree)/6;
return WadRayMath.ray().add(ratePerSecond.mul(exp)).add(secondTerm).add(thirdTerm); return WadRayMath.ray().add(ratePerSecond.mul(exp)).add(secondTerm).add(thirdTerm);
} }

View File

@ -1,7 +1,8 @@
// SPDX-License-Identifier: agpl-3.0 // SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8; pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {Errors} from '../helpers/Errors.sol';
/** /**
* @title PercentageMath library * @title PercentageMath library
@ -12,7 +13,6 @@ import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
**/ **/
library PercentageMath { library PercentageMath {
using SafeMath for uint256;
uint256 constant PERCENTAGE_FACTOR = 1e4; //percentage plus two decimals uint256 constant PERCENTAGE_FACTOR = 1e4; //percentage plus two decimals
uint256 constant HALF_PERCENT = PERCENTAGE_FACTOR / 2; uint256 constant HALF_PERCENT = PERCENTAGE_FACTOR / 2;
@ -24,7 +24,19 @@ library PercentageMath {
* @return the percentage of value * @return the percentage of value
**/ **/
function percentMul(uint256 value, uint256 percentage) internal pure returns (uint256) { function percentMul(uint256 value, uint256 percentage) internal pure returns (uint256) {
return HALF_PERCENT.add(value.mul(percentage)).div(PERCENTAGE_FACTOR); if(value == 0){
return 0;
}
uint256 result = value*percentage;
require(result/value == percentage, Errors.MULTIPLICATION_OVERFLOW);
result+=HALF_PERCENT;
require(result >= HALF_PERCENT, Errors.ADDITION_OVERFLOW);
return result/PERCENTAGE_FACTOR;
} }
/** /**
@ -34,8 +46,17 @@ library PercentageMath {
* @return the value divided the percentage * @return the value divided the percentage
**/ **/
function percentDiv(uint256 value, uint256 percentage) internal pure returns (uint256) { function percentDiv(uint256 value, uint256 percentage) internal pure returns (uint256) {
require(percentage != 0, Errors.DIVISION_BY_ZERO);
uint256 halfPercentage = percentage / 2; uint256 halfPercentage = percentage / 2;
uint256 result = value*PERCENTAGE_FACTOR;
return halfPercentage.add(value.mul(PERCENTAGE_FACTOR)).div(percentage); require(result/PERCENTAGE_FACTOR == value, Errors.MULTIPLICATION_OVERFLOW);
result += halfPercentage;
require(result >= halfPercentage, Errors.ADDITION_OVERFLOW);
return result/percentage;
} }
} }

View File

@ -0,0 +1,163 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.8;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, 'SafeMath: addition overflow');
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, 'SafeMath: subtraction overflow');
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, 'SafeMath: multiplication overflow');
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, 'SafeMath: division by zero');
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, 'SafeMath: modulo by zero');
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: agpl-3.0 // SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8; pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {Errors} from '../helpers/Errors.sol';
/** /**
* @title WadRayMath library * @title WadRayMath library
@ -10,7 +10,6 @@ import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
**/ **/
library WadRayMath { library WadRayMath {
using SafeMath for uint256;
uint256 internal constant WAD = 1e18; uint256 internal constant WAD = 1e18;
uint256 internal constant halfWAD = WAD / 2; uint256 internal constant halfWAD = WAD / 2;
@ -56,7 +55,20 @@ library WadRayMath {
* @return the result of a*b, in wad * @return the result of a*b, in wad
**/ **/
function wadMul(uint256 a, uint256 b) internal pure returns (uint256) { function wadMul(uint256 a, uint256 b) internal pure returns (uint256) {
return halfWAD.add(a.mul(b)).div(WAD);
if(a == 0){
return 0;
}
uint256 result = a*b;
require(result/a == b, Errors.MULTIPLICATION_OVERFLOW);
result+=halfWAD;
require(result >= halfWAD, Errors.ADDITION_OVERFLOW);
return result/WAD;
} }
/** /**
@ -66,9 +78,19 @@ library WadRayMath {
* @return the result of a/b, in wad * @return the result of a/b, in wad
**/ **/
function wadDiv(uint256 a, uint256 b) internal pure returns (uint256) { function wadDiv(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, Errors.DIVISION_BY_ZERO);
uint256 halfB = b / 2; uint256 halfB = b / 2;
return halfB.add(a.mul(WAD)).div(b); uint256 result = a*WAD;
require(result/WAD == a, Errors.MULTIPLICATION_OVERFLOW);
result += halfB;
require(result >= halfB, Errors.ADDITION_OVERFLOW);
return result/b;
} }
/** /**
@ -78,7 +100,19 @@ library WadRayMath {
* @return the result of a*b, in ray * @return the result of a*b, in ray
**/ **/
function rayMul(uint256 a, uint256 b) internal pure returns (uint256) { function rayMul(uint256 a, uint256 b) internal pure returns (uint256) {
return halfRAY.add(a.mul(b)).div(RAY); if(a == 0){
return 0;
}
uint256 result = a*b;
require(result/a == b, Errors.MULTIPLICATION_OVERFLOW);
result+=halfRAY;
require(result >= halfRAY, Errors.ADDITION_OVERFLOW);
return result/RAY;
} }
/** /**
@ -88,9 +122,20 @@ library WadRayMath {
* @return the result of a/b, in ray * @return the result of a/b, in ray
**/ **/
function rayDiv(uint256 a, uint256 b) internal pure returns (uint256) { function rayDiv(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, Errors.DIVISION_BY_ZERO);
uint256 halfB = b / 2; uint256 halfB = b / 2;
return halfB.add(a.mul(RAY)).div(b); uint256 result = a*RAY;
require(result/RAY == a, Errors.MULTIPLICATION_OVERFLOW);
result += halfB;
require(result >= halfB, Errors.ADDITION_OVERFLOW);
return result/b;
} }
/** /**
@ -100,8 +145,10 @@ library WadRayMath {
**/ **/
function rayToWad(uint256 a) internal pure returns (uint256) { function rayToWad(uint256 a) internal pure returns (uint256) {
uint256 halfRatio = WAD_RAY_RATIO / 2; uint256 halfRatio = WAD_RAY_RATIO / 2;
uint256 result = halfRatio+a;
require(result >= halfRatio, Errors.ADDITION_OVERFLOW);
return halfRatio.add(a).div(WAD_RAY_RATIO); return result/WAD_RAY_RATIO;
} }
/** /**
@ -110,6 +157,8 @@ library WadRayMath {
* @return a converted in ray * @return a converted in ray
**/ **/
function wadToRay(uint256 a) internal pure returns (uint256) { function wadToRay(uint256 a) internal pure returns (uint256) {
return a.mul(WAD_RAY_RATIO); uint256 result = a*WAD_RAY_RATIO;
require(result/WAD_RAY_RATIO == a, Errors.MULTIPLICATION_OVERFLOW);
return result;
} }
} }

View File

@ -0,0 +1,61 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.8;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/
function isContract(address account) internal view returns (bool) {
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
// for accounts without code, i.e. `keccak256('')`
bytes32 codehash;
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
// solhint-disable-next-line no-inline-assembly
assembly {
codehash := extcodehash(account)
}
return (codehash != accountHash && codehash != 0x0);
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, 'Address: insufficient balance');
// solhint-disable-next-line avoid-low-level-calls, avoid-call-value
(bool success, ) = recipient.call{value: amount}('');
require(success, 'Address: unable to send value, recipient may have reverted');
}
}

View File

@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.6.8;
/*
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with GSN meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal virtual view returns (address payable) {
return msg.sender;
}
function _msgData() internal virtual view returns (bytes memory) {
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
return msg.data;
}
}

View File

@ -0,0 +1,49 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.6.8;
import {IERC20} from "../interfaces/IERC20.sol";
import {SafeMath} from "../libraries/math/SafeMath.sol";
import {Address} from "./Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(IERC20 token, address spender, uint256 value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function callOptionalReturn(IERC20 token, bytes memory data) private {
require(address(token).isContract(), "SafeERC20: call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = address(token).call(data);
require(success, "SafeERC20: low-level call failed");
if (returndata.length > 0) { // Return data is optional
// solhint-disable-next-line max-line-length
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}

View File

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

View File

@ -3,7 +3,6 @@ pragma solidity ^0.6.8;
import {AToken} from '../../tokenization/AToken.sol'; import {AToken} from '../../tokenization/AToken.sol';
import {LendingPool} from '../../lendingpool/LendingPool.sol'; import {LendingPool} from '../../lendingpool/LendingPool.sol';
import '@nomiclabs/buidler/console.sol';
contract MockAToken is AToken { contract MockAToken is AToken {
constructor( constructor(
@ -22,8 +21,8 @@ contract MockAToken is AToken {
string calldata _tokenName, string calldata _tokenName,
string calldata _tokenSymbol string calldata _tokenSymbol
) external virtual override initializer { ) external virtual override initializer {
_name = _tokenName; _setName(_tokenName);
_symbol = _tokenSymbol; _setSymbol(_tokenSymbol);
_setupDecimals(_underlyingAssetDecimals); _setDecimals(_underlyingAssetDecimals);
} }
} }

View File

@ -3,7 +3,6 @@ pragma solidity ^0.6.8;
import {StableDebtToken} from '../../tokenization/StableDebtToken.sol'; import {StableDebtToken} from '../../tokenization/StableDebtToken.sol';
import {LendingPool} from '../../lendingpool/LendingPool.sol'; import {LendingPool} from '../../lendingpool/LendingPool.sol';
import '@nomiclabs/buidler/console.sol';
contract MockStableDebtToken is StableDebtToken { contract MockStableDebtToken is StableDebtToken {
constructor( constructor(

View File

@ -3,7 +3,6 @@ pragma solidity ^0.6.8;
import {VariableDebtToken} from '../../tokenization/VariableDebtToken.sol'; import {VariableDebtToken} from '../../tokenization/VariableDebtToken.sol';
import {LendingPool} from '../../lendingpool/LendingPool.sol'; import {LendingPool} from '../../lendingpool/LendingPool.sol';
import '@nomiclabs/buidler/console.sol';
contract MockVariableDebtToken is VariableDebtToken { contract MockVariableDebtToken is VariableDebtToken {
constructor( constructor(

View File

@ -4,11 +4,13 @@ pragma solidity ^0.6.8;
import {ERC20} from './ERC20.sol'; import {ERC20} from './ERC20.sol';
import {LendingPool} from '../lendingpool/LendingPool.sol'; import {LendingPool} from '../lendingpool/LendingPool.sol';
import {WadRayMath} from '../libraries/math/WadRayMath.sol'; import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import {Errors} from '../libraries/helpers/Errors.sol';
import { import {
VersionedInitializable VersionedInitializable
} from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; } from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol';
import {IAToken, IERC20} from './interfaces/IAToken.sol'; import {IAToken} from './interfaces/IAToken.sol';
import {IERC20} from '../interfaces/IERC20.sol';
import {SafeERC20} from "../misc/SafeERC20.sol";
/** /**
* @title Aave ERC20 AToken * @title Aave ERC20 AToken
@ -34,12 +36,12 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
uint256 public constant ATOKEN_REVISION = 0x1; uint256 public constant ATOKEN_REVISION = 0x1;
modifier onlyLendingPool { 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) { modifier whenTransferAllowed(address from, uint256 amount) {
require(isTransferAllowed(from, amount), 'Transfer cannot be allowed.'); require(isTransferAllowed(from, amount), Errors.TRANSFER_NOT_ALLOWED);
_; _;
} }
@ -48,7 +50,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
address underlyingAssetAddress, address underlyingAssetAddress,
string memory tokenName, string memory tokenName,
string memory tokenSymbol string memory tokenSymbol
) public ERC20(tokenName, tokenSymbol) { ) public ERC20(tokenName, tokenSymbol, 18) {
_pool = pool; _pool = pool;
UNDERLYING_ASSET_ADDRESS = underlyingAssetAddress; UNDERLYING_ASSET_ADDRESS = underlyingAssetAddress;
} }
@ -62,9 +64,9 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
string calldata tokenName, string calldata tokenName,
string calldata tokenSymbol string calldata tokenSymbol
) external virtual initializer { ) external virtual initializer {
_name = tokenName; _setName(tokenName);
_symbol = tokenSymbol; _setSymbol(tokenSymbol);
_setupDecimals(underlyingAssetDecimals); _setDecimals(underlyingAssetDecimals);
} }
/** /**
@ -100,7 +102,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
function redirectInterestStreamOf(address from, address to) external override { function redirectInterestStreamOf(address from, address to) external override {
require( require(
msg.sender == _interestRedirectionAllowances[from], msg.sender == _interestRedirectionAllowances[from],
'Caller is not allowed to redirect the interest of the user' Errors.INTEREST_REDIRECTION_NOT_ALLOWED
); );
_redirectInterestStream(from, to); _redirectInterestStream(from, to);
} }
@ -112,7 +114,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
* the allowance. * the allowance.
**/ **/
function allowInterestRedirectionTo(address to) external override { 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; _interestRedirectionAllowances[msg.sender] = to;
emit InterestRedirectionAllowanceChanged(msg.sender, to); emit InterestRedirectionAllowanceChanged(msg.sender, to);
} }
@ -434,15 +436,12 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
address to, address to,
uint256 value uint256 value
) internal { ) 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 //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 //cumulate the balance of the receiver
(, , uint256 toBalanceIncrease, uint256 toIndex) = _cumulateBalance(to); (, , uint256 toBalanceIncrease, uint256 toIndex) = _cumulateBalance(to);
@ -486,7 +485,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
function _redirectInterestStream(address from, address to) internal { function _redirectInterestStream(address from, address to) internal {
address currentRedirectionAddress = _interestRedirectionAddresses[from]; 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 //accumulates the accrued interest to the principal
( (
@ -496,7 +495,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken {
uint256 fromIndex uint256 fromIndex
) = _cumulateBalance(from); ) = _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 //if the user is already redirecting the interest to someone, before changing
//the redirection address we substract the redirected balance of the previous //the redirection address we substract the redirected balance of the previous

View File

@ -1,124 +1,88 @@
// SPDX-License-Identifier: agpl-3.0 // SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8; pragma solidity 0.6.8;
import {Context} from '@openzeppelin/contracts/GSN/Context.sol'; import {Context} from '../misc/Context.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {IERC20} from '../interfaces/IERC20.sol';
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {IERC20Detailed} from '../interfaces/IERC20Detailed.sol';
import {SafeMath} from '../libraries/math/SafeMath.sol';
/** /**
* @dev Implementation of the {IERC20} interface. * @title ERC20
* * @notice Basic ERC20 implementation
* This implementation is agnostic to the way tokens are created. This means * @author Aave
* that a supply mechanism has to be added in a derived contract using {_mint}. **/
* For a generic mechanism see {ERC20MinterPauser}. contract ERC20 is Context, IERC20, IERC20Detailed {
*
* TIP: For a detailed writeup see our guide
* https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin guidelines: functions revert instead
* of returning `false` on failure. This behavior is nonetheless conventional
* and does not conflict with the expectations of ERC20 applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is Context, IERC20 {
using SafeMath for uint256; using SafeMath for uint256;
mapping(address => uint256) private _balances; mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances; mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply; uint256 private _totalSupply;
string private _name;
string internal _name; string private _symbol;
string internal _symbol;
uint8 private _decimals; uint8 private _decimals;
/** constructor(
* @dev Sets the values for {name} and {symbol}, initializes {decimals} with string memory name,
* a default value of 18. string memory symbol,
* uint8 decimals
* To select a different value for {decimals}, use {_setupDecimals}. ) public {
*
* All three of these values are immutable: they can only be set once during
* construction.
*/
constructor(string memory name, string memory symbol) public {
_name = name; _name = name;
_symbol = symbol; _symbol = symbol;
_decimals = 18; _decimals = decimals;
} }
/** /**
* @dev Returns the name of the token. * @return the name of the token
*/ **/
function name() public view returns (string memory) { function name() public override view returns (string memory) {
return _name; return _name;
} }
/** /**
* @dev Returns the symbol of the token, usually a shorter version of the * @return the symbol of the token
* name. **/
*/ function symbol() public override view returns (string memory) {
function symbol() public view returns (string memory) {
return _symbol; return _symbol;
} }
/** /**
* @dev Returns the number of decimals used to get its user representation. * @return the decimals of the token
* For example, if `decimals` equals `2`, a balance of `505` tokens should **/
* be displayed to a user as `5,05` (`505 / 10 ** 2`). function decimals() public override view returns (uint8) {
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
* called.
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view returns (uint8) {
return _decimals; return _decimals;
} }
/** /**
* @dev See {IERC20-totalSupply}. * @return the total supply of the token
*/ **/
function totalSupply() public virtual override view returns (uint256) { function totalSupply() public virtual override view returns (uint256) {
return _totalSupply; return _totalSupply;
} }
/** /**
* @dev See {IERC20-balanceOf}. * @return the balance of the token
*/ **/
function balanceOf(address account) public virtual override view returns (uint256) { function balanceOf(address account) public virtual override view returns (uint256) {
return _balances[account]; return _balances[account];
} }
/** /**
* @dev See {IERC20-transfer}. * @dev executes a transfer of tokens from msg.sender to recipient
* * @param recipient the recipient of the tokens
* Requirements: * @param amount the amount of tokens being transferred
* * @return true if the transfer succeeds, false otherwise
* - `recipient` cannot be the zero address. **/
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) public virtual override returns (bool) { function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount); _transfer(_msgSender(), recipient, amount);
return true; return true;
} }
/** /**
* @dev See {IERC20-allowance}. * @dev returns the allowance of spender on the tokens owned by owner
*/ * @param owner the owner of the tokens
* @param spender the user allowed to spend the owner's tokens
* @return the amount of owner's tokens spender is allowed to spend
**/
function allowance(address owner, address spender) function allowance(address owner, address spender)
public public
virtual virtual
@ -130,29 +94,22 @@ contract ERC20 is Context, IERC20 {
} }
/** /**
* @dev See {IERC20-approve}. * @dev allows spender to spend the tokens owned by msg.sender
* * @param spender the user allowed to spend msg.sender tokens
* Requirements: * @return true
* **/
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) public virtual override returns (bool) { function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount); _approve(_msgSender(), spender, amount);
return true; return true;
} }
/** /**
* @dev See {IERC20-transferFrom}. * @dev executes a transfer of token from sender to recipient, if msg.sender is allowed to do so
* * @param sender the owner of the tokens
* Emits an {Approval} event indicating the updated allowance. This is not * @param recipient the recipient of the tokens
* required by the EIP. See the note at the beginning of {ERC20}; * @param amount the amount of tokens being transferred
* * @return true if the transfer succeeds, false otherwise
* Requirements: **/
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
* - the caller must have allowance for ``sender``'s tokens of at least
* `amount`.
*/
function transferFrom( function transferFrom(
address sender, address sender,
address recipient, address recipient,
@ -168,36 +125,22 @@ contract ERC20 is Context, IERC20 {
} }
/** /**
* @dev Atomically increases the allowance granted to `spender` by the caller. * @dev increases the allowance of spender to spend msg.sender tokens
* * @param spender the user allowed to spend on behalf of msg.sender
* This is an alternative to {approve} that can be used as a mitigation for * @param addedValue the amount being added to the allowance
* problems described in {IERC20-approve}. * @return true
* **/
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue)); _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true; return true;
} }
/** /**
* @dev Atomically decreases the allowance granted to `spender` by the caller. * @dev decreases the allowance of spender to spend msg.sender tokens
* * @param spender the user allowed to spend on behalf of msg.sender
* This is an alternative to {approve} that can be used as a mitigation for * @param subtractedValue the amount being subtracted to the allowance
* problems described in {IERC20-approve}. * @return true
* **/
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) function decreaseAllowance(address spender, uint256 subtractedValue)
public public
virtual virtual
@ -214,20 +157,6 @@ contract ERC20 is Context, IERC20 {
return true; return true;
} }
/**
* @dev Moves tokens `amount` from `sender` to `recipient`.
*
* This is internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `sender` cannot be the zero address.
* - `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
*/
function _transfer( function _transfer(
address sender, address sender,
address recipient, address recipient,
@ -243,15 +172,6 @@ contract ERC20 is Context, IERC20 {
emit Transfer(sender, recipient, amount); emit Transfer(sender, recipient, amount);
} }
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements
*
* - `to` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual { function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), 'ERC20: mint to the zero address'); require(account != address(0), 'ERC20: mint to the zero address');
@ -262,17 +182,6 @@ contract ERC20 is Context, IERC20 {
emit Transfer(address(0), account, amount); emit Transfer(address(0), account, amount);
} }
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual { function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), 'ERC20: burn from the zero address'); require(account != address(0), 'ERC20: burn from the zero address');
@ -283,19 +192,6 @@ contract ERC20 is Context, IERC20 {
emit Transfer(account, address(0), amount); emit Transfer(account, address(0), amount);
} }
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.
*
* This is internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve( function _approve(
address owner, address owner,
address spender, address spender,
@ -308,31 +204,18 @@ contract ERC20 is Context, IERC20 {
emit Approval(owner, spender, amount); emit Approval(owner, spender, amount);
} }
/** function _setName(string memory newName) internal {
* @dev Sets {decimals} to a value other than the default one of 18. _name = newName;
* }
* WARNING: This function should only be called from the constructor. Most
* applications that interact with token contracts will not expect function _setSymbol(string memory newSymbol) internal {
* {decimals} to ever change, and may work incorrectly if it does. _symbol = newSymbol;
*/ }
function _setupDecimals(uint8 decimals_) internal {
_decimals = decimals_; function _setDecimals(uint8 newDecimals) internal {
_decimals = newDecimals;
} }
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be to transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer( function _beforeTokenTransfer(
address from, address from,
address to, address to,

View File

@ -11,27 +11,16 @@ import {IStableDebtToken} from './interfaces/IStableDebtToken.sol';
/** /**
* @title contract StableDebtToken * @title contract StableDebtToken
* * @notice Implements a stable debt token to track the user positions
* @notice defines the interface for the stable debt token
*
* @dev it does not inherit from IERC20 to save in code size
*
* @author Aave * @author Aave
*
**/ **/
contract StableDebtToken is IStableDebtToken, DebtTokenBase { contract StableDebtToken is IStableDebtToken, DebtTokenBase {
using SafeMath for uint256;
using WadRayMath for uint256; using WadRayMath for uint256;
uint256 public constant DEBT_TOKEN_REVISION = 0x1; uint256 public constant DEBT_TOKEN_REVISION = 0x1;
struct UserData {
uint256 currentRate;
uint40 lastUpdateTimestamp;
}
uint256 private avgStableRate; uint256 private _avgStableRate;
mapping(address => uint40) _timestamps;
mapping(address => UserData) private _usersData;
constructor( constructor(
address pool, address pool,
@ -53,7 +42,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
* @return the average stable rate * @return the average stable rate
**/ **/
function getAverageStableRate() external virtual override view returns (uint256) { function getAverageStableRate() external virtual override view returns (uint256) {
return avgStableRate; return _avgStableRate;
} }
/** /**
@ -61,7 +50,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
* @return the last update timestamp * @return the last update timestamp
**/ **/
function getUserLastUpdated(address user) external virtual override view returns (uint40) { function getUserLastUpdated(address user) external virtual override view returns (uint40) {
return _usersData[user].lastUpdateTimestamp; return _timestamps[user];
} }
/** /**
@ -70,7 +59,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
* @return the stable rate of user * @return the stable rate of user
**/ **/
function getUserStableRate(address user) external virtual override view returns (uint256) { function getUserStableRate(address user) external virtual override view returns (uint256) {
return _usersData[user].currentRate; return _usersData[user];
} }
/** /**
@ -78,16 +67,14 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
* @return the accumulated debt of the user * @return the accumulated debt of the user
**/ **/
function balanceOf(address account) public virtual override view returns (uint256) { function balanceOf(address account) public virtual override view returns (uint256) {
uint256 accountBalance = _balances[account]; uint256 accountBalance = principalBalanceOf(account);
uint256 stableRate = _usersData[account];
if (accountBalance == 0) { if (accountBalance == 0) {
return 0; return 0;
} }
UserData storage userData = _usersData[account];
uint256 cumulatedInterest = MathUtils.calculateCompoundedInterest( uint256 cumulatedInterest = MathUtils.calculateCompoundedInterest(
userData.currentRate, stableRate,
userData.lastUpdateTimestamp _timestamps[account]
); );
return accountBalance.wadToRay().rayMul(cumulatedInterest).rayToWad(); return accountBalance.wadToRay().rayMul(cumulatedInterest).rayToWad();
} }
@ -120,25 +107,25 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
uint256 balanceIncrease uint256 balanceIncrease
) = _calculateBalanceIncrease(user); ) = _calculateBalanceIncrease(user);
vars.supplyBeforeMint = _totalSupply.add(balanceIncrease); vars.supplyBeforeMint = totalSupply().add(balanceIncrease);
vars.supplyAfterMint = vars.supplyBeforeMint.add(amount); vars.supplyAfterMint = vars.supplyBeforeMint.add(amount);
vars.amountInRay = amount.wadToRay(); vars.amountInRay = amount.wadToRay();
//calculates the new stable rate for the user //calculates the new stable rate for the user
vars.newStableRate = _usersData[user] vars.newStableRate = _usersData[user]
.currentRate
.rayMul(currentBalance.wadToRay()) .rayMul(currentBalance.wadToRay())
.add(vars.amountInRay.rayMul(rate)) .add(vars.amountInRay.rayMul(rate))
.rayDiv(currentBalance.add(amount).wadToRay()); .rayDiv(currentBalance.add(amount).wadToRay());
_usersData[user].currentRate = vars.newStableRate; require(vars.newStableRate < (1 << 128), "Debt token: stable rate overflow");
_usersData[user] = vars.newStableRate;
//solium-disable-next-line //solium-disable-next-line
_usersData[user].lastUpdateTimestamp = uint40(block.timestamp); _timestamps[user] = uint40(block.timestamp);
//calculates the updated average stable rate //calculates the updated average stable rate
avgStableRate = avgStableRate _avgStableRate = _avgStableRate
.rayMul(vars.supplyBeforeMint.wadToRay()) .rayMul(vars.supplyBeforeMint.wadToRay())
.add(rate.rayMul(vars.amountInRay)) .add(rate.rayMul(vars.amountInRay))
.rayDiv(vars.supplyAfterMint.wadToRay()); .rayDiv(vars.supplyAfterMint.wadToRay());
@ -167,24 +154,24 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
uint256 balanceIncrease uint256 balanceIncrease
) = _calculateBalanceIncrease(user); ) = _calculateBalanceIncrease(user);
uint256 supplyBeforeBurn = _totalSupply.add(balanceIncrease); uint256 supplyBeforeBurn = totalSupply().add(balanceIncrease);
uint256 supplyAfterBurn = supplyBeforeBurn.sub(amount); uint256 supplyAfterBurn = supplyBeforeBurn.sub(amount);
if (supplyAfterBurn == 0) { if (supplyAfterBurn == 0) {
avgStableRate = 0; _avgStableRate = 0;
} else { } else {
avgStableRate = avgStableRate _avgStableRate = _avgStableRate
.rayMul(supplyBeforeBurn.wadToRay()) .rayMul(supplyBeforeBurn.wadToRay())
.sub(_usersData[user].currentRate.rayMul(amount.wadToRay())) .sub(_usersData[user].rayMul(amount.wadToRay()))
.rayDiv(supplyAfterBurn.wadToRay()); .rayDiv(supplyAfterBurn.wadToRay());
} }
if (amount == currentBalance) { if (amount == currentBalance) {
_usersData[user].currentRate = 0; _usersData[user] = 0;
_usersData[user].lastUpdateTimestamp = 0; _timestamps[user] = 0;
} else { } else {
//solium-disable-next-line //solium-disable-next-line
_usersData[user].lastUpdateTimestamp = uint40(block.timestamp); _timestamps[user] = uint40(block.timestamp);
} }
if (balanceIncrease > amount) { if (balanceIncrease > amount) {

View File

@ -9,19 +9,15 @@ import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {IVariableDebtToken} from './interfaces/IVariableDebtToken.sol'; import {IVariableDebtToken} from './interfaces/IVariableDebtToken.sol';
/** /**
* @title interface IVariableDebtToken * @title contract VariableDebtToken
* @notice Implements a variable debt token to track the user positions
* @author Aave * @author Aave
* @notice defines the basic interface for a variable debt token.
* @dev does not inherit from IERC20 to save in contract size
**/ **/
contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
using SafeMath for uint256;
using WadRayMath for uint256; using WadRayMath for uint256;
uint256 public constant DEBT_TOKEN_REVISION = 0x1; uint256 public constant DEBT_TOKEN_REVISION = 0x1;
mapping(address => uint256) private _userIndexes;
constructor( constructor(
address pool, address pool,
address underlyingAsset, address underlyingAsset,
@ -42,7 +38,8 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
* @return the debt balance of the user * @return the debt balance of the user
**/ **/
function balanceOf(address user) public virtual override view returns (uint256) { function balanceOf(address user) public virtual override view returns (uint256) {
uint256 userBalance = _balances[user]; uint256 userBalance = principalBalanceOf(user);
uint256 index = _usersData[user];
if (userBalance == 0) { if (userBalance == 0) {
return 0; return 0;
} }
@ -50,8 +47,8 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
return return
userBalance userBalance
.wadToRay() .wadToRay()
.rayMul(_pool.getReserveNormalizedVariableDebt(_underlyingAssetAddress)) .rayMul(POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET))
.rayDiv(_userIndexes[user]) .rayDiv(index)
.rayToWad(); .rayToWad();
} }
@ -61,7 +58,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
**/ **/
function getUserIndex(address user) external virtual override view returns (uint256) { function getUserIndex(address user) external virtual override view returns (uint256) {
return _userIndexes[user]; return _usersData[user];
} }
/** /**
@ -78,8 +75,9 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
_mint(user, amount.add(balanceIncrease)); _mint(user, amount.add(balanceIncrease));
uint256 newUserIndex = _pool.getReserveNormalizedVariableDebt(_underlyingAssetAddress); uint256 newUserIndex = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET);
_userIndexes[user] = newUserIndex; require(newUserIndex < (1 << 128), "Debt token: Index overflow");
_usersData[user] = newUserIndex;
emit MintDebt(user, amount, previousBalance, currentBalance, balanceIncrease, newUserIndex); emit MintDebt(user, amount, previousBalance, currentBalance, balanceIncrease, newUserIndex);
} }
@ -105,9 +103,10 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
uint256 newUserIndex = 0; uint256 newUserIndex = 0;
//if user not repaid everything //if user not repaid everything
if (currentBalance != amount) { if (currentBalance != amount) {
newUserIndex = _pool.getReserveNormalizedVariableDebt(_underlyingAssetAddress); newUserIndex = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET);
require(newUserIndex < (1 << 128), "Debt token: Index overflow");
} }
_userIndexes[user] = newUserIndex; _usersData[user] = newUserIndex;
emit BurnDebt(user, amount, previousBalance, currentBalance, balanceIncrease, newUserIndex); emit BurnDebt(user, amount, previousBalance, currentBalance, balanceIncrease, newUserIndex);
} }

View File

@ -5,184 +5,120 @@ import {Context} from '@openzeppelin/contracts/GSN/Context.sol';
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {ILendingPoolAddressesProvider} from '../../interfaces/ILendingPoolAddressesProvider.sol'; import {ILendingPoolAddressesProvider} from '../../interfaces/ILendingPoolAddressesProvider.sol';
import {ILendingPool} from '../../interfaces/ILendingPool.sol'; import {ILendingPool} from '../../interfaces/ILendingPool.sol';
import { import {VersionedInitializable} from '../../libraries/openzeppelin-upgradeability/VersionedInitializable.sol';
VersionedInitializable import {ERC20} from '../ERC20.sol';
} from '../../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; import {Errors} from '../../libraries/helpers/Errors.sol';
import {IERC20Detailed} from '../../interfaces/IERC20Detailed.sol';
/** /**
* @title contract DebtTokenBase * @title DebtTokenBase
* @notice Base contract for different types of debt tokens, like StableDebtToken or VariableDebtToken
* @author Aave * @author Aave
* @notice base contract for StableDebtToken and VariableDebtToken
*/ */
abstract contract DebtTokenBase is IERC20Detailed, VersionedInitializable { abstract contract DebtTokenBase is ERC20, VersionedInitializable {
using SafeMath for uint256;
uint256 internal _totalSupply; address internal immutable UNDERLYING_ASSET;
ILendingPool internal immutable POOL;
string internal _name; mapping(address => uint256) internal _usersData;
string internal _symbol;
uint8 internal _decimals;
address internal immutable _underlyingAssetAddress;
ILendingPool internal immutable _pool;
mapping(address => uint256) internal _balances;
/** /**
* @dev only lending pool can call functions marked by this modifier * @dev Only lending pool can call functions marked by this modifier
**/ **/
modifier onlyLendingPool { 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);
_; _;
} }
/**
* @dev The metadata of the token will be set on the proxy, that the reason of
* passing "NULL" and 0 as metadata
*/
constructor( constructor(
address pool, address pool,
address underlyingAssetAddress, address underlyingAssetAddress,
string memory name, string memory name,
string memory symbol string memory symbol
) public { ) public ERC20(name, symbol, 18) {
_pool = ILendingPool(pool); POOL = ILendingPool(pool);
_underlyingAssetAddress = underlyingAssetAddress; UNDERLYING_ASSET = underlyingAssetAddress;
_name = name;
_symbol = symbol;
} }
/** /**
* @dev initializes the debt token. * @dev Initializes the debt token.
* @param name the name of the token * @param name The name of the token
* @param symbol the symbol of the token * @param symbol The symbol of the token
* @param decimals the decimals of the token * @param decimals The decimals of the token
*/ */
function initialize( function initialize(
uint8 decimals, uint8 decimals,
string memory name, string memory name,
string memory symbol string memory symbol
) public initializer { ) public initializer {
_name = name; _setName(name);
_symbol = symbol; _setSymbol(symbol);
_decimals = decimals; _setDecimals(decimals);
}
function name() public override view returns (string memory) {
return _name;
}
function symbol() public override view returns (string memory) {
return _symbol;
}
function decimals() public override view returns (uint8) {
return _decimals;
}
function totalSupply() public override view returns (uint256) {
return _totalSupply;
} }
function underlyingAssetAddress() public view returns (address) { function underlyingAssetAddress() public view returns (address) {
return _underlyingAssetAddress; return UNDERLYING_ASSET;
} }
/** /**
* @dev calculates the accumulated debt balance of the user * @dev Returns the principal debt balance of the user from
* @return the debt balance of the user * @return The debt balance of the user since the last burn/mint action
**/
function balanceOf(address user) public virtual override view returns (uint256);
/**
* @dev returns the principal debt balance of the user from
* @return the debt balance of the user since the last burn/mint action
**/ **/
function principalBalanceOf(address user) public view returns (uint256) { function principalBalanceOf(address user) public view returns (uint256) {
return _balances[user]; return super.balanceOf(user);
} }
/** /**
* @dev basic accounting for the mint action * @dev Being non transferrable, the debt token does not implement any of the
* @dev _user the target user of the minting action
* @dev _amount the amount to mint
**/
function _mint(address user, uint256 amount) internal {
_totalSupply = _totalSupply.add(amount);
_balances[user] = _balances[user].add(amount);
}
/**
* @dev basic accounting for the burn action
* @dev _user the target user of the burning action
* @dev _amount the amount to burn
**/
function _burn(address user, uint256 amount) internal {
_totalSupply = _totalSupply.sub(amount);
_balances[user] = _balances[user].sub(amount);
}
/**
* @dev being non transferrable, the debt token does not implement any of the
* standard ERC20 functions for transfer and allowance. * standard ERC20 functions for transfer and allowance.
**/ **/
function transfer(address recipient, uint256 amount) external virtual override returns (bool) { function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
recipient; amount;
revert('TRANSFER_NOT_SUPPORTED'); revert('TRANSFER_NOT_SUPPORTED');
} }
function allowance(address owner, address spender) function allowance(address owner, address spender) public virtual override view returns (uint256) {
external owner; spender;
virtual
override
view
returns (uint256)
{
revert('ALLOWANCE_NOT_SUPPORTED'); revert('ALLOWANCE_NOT_SUPPORTED');
} }
function approve(address spender, uint256 amount) external virtual override returns (bool) { function approve(address spender, uint256 amount) public virtual override returns (bool) {
spender; amount;
revert('APPROVAL_NOT_SUPPORTED'); revert('APPROVAL_NOT_SUPPORTED');
} }
function transferFrom( function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
address sender, sender; recipient; amount;
address recipient,
uint256 amount
) external virtual override returns (bool) {
revert('TRANSFER_NOT_SUPPORTED'); revert('TRANSFER_NOT_SUPPORTED');
} }
function increaseAllowance(address spender, uint256 addedValue) external virtual returns (bool) { function increaseAllowance(address spender, uint256 addedValue) public virtual override returns (bool) {
spender; addedValue;
revert('ALLOWANCE_NOT_SUPPORTED'); revert('ALLOWANCE_NOT_SUPPORTED');
} }
function decreaseAllowance(address spender, uint256 subtractedValue) function decreaseAllowance(address spender, uint256 subtractedValue) public virtual override returns (bool) {
external spender; subtractedValue;
virtual
returns (bool)
{
revert('ALLOWANCE_NOT_SUPPORTED'); revert('ALLOWANCE_NOT_SUPPORTED');
} }
/** /**
* @dev calculates the increase in balance since the last user interaction * @dev Calculates the increase in balance since the last user interaction
* @param user the address of the user for which the interest is being accumulated * @param user The address of the user for which the interest is being accumulated
* @return the previous principal balance, the new principal balance, the balance increase * @return The previous principal balance, the new principal balance, the balance increase
* and the new user index * and the new user index
**/ **/
function _calculateBalanceIncrease(address user) function _calculateBalanceIncrease(address user) internal view returns (uint256, uint256, uint256) {
internal uint256 previousPrincipalBalance = principalBalanceOf(user);
view
returns (
uint256,
uint256,
uint256
)
{
uint256 previousPrincipalBalance = _balances[user];
if (previousPrincipalBalance == 0) { if (previousPrincipalBalance == 0) {
return (0, 0, 0); return (0, 0, 0);
} }
//calculate the accrued interest since the last accumulation // Calculation of the accrued interest since the last accumulation
uint256 balanceIncrease = balanceOf(user).sub(previousPrincipalBalance); uint256 balanceIncrease = balanceOf(user).sub(previousPrincipalBalance);
return ( return (

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: agpl-3.0 // SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8; pragma solidity ^0.6.8;
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {IERC20} from '../../interfaces/IERC20.sol';
interface IAToken is IERC20 { interface IAToken is IERC20 {
/** /**

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x58F132FBB86E21545A4Bace3C19f1C05d86d7A22", "address": "0xf8c6eB390cDc5C08717bC2268aa0c1169A9B5deE",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -15,7 +15,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xa4bcDF64Cdd5451b6ac3743B414124A6299B65FF", "address": "0x4a716924Dad0c0d0E558844F304548814e7089F1",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -25,7 +25,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x5A0773Ff307Bf7C71a832dBB5312237fD3437f9F", "address": "0x798c5b4b62b1eA9D64955D6751B03075A003F123",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -53,7 +53,7 @@
"address": "0x6642B57e4265BAD868C17Fc1d1F4F88DBBA04Aa8" "address": "0x6642B57e4265BAD868C17Fc1d1F4F88DBBA04Aa8"
}, },
"localhost": { "localhost": {
"address": "0x9EC0480CF106d6dc1c7849BA141a56F874170F97" "address": "0x6642B57e4265BAD868C17Fc1d1F4F88DBBA04Aa8"
} }
}, },
"LendingPoolDataProvider": { "LendingPoolDataProvider": {
@ -66,7 +66,7 @@
"address": "0xD9273d497eDBC967F39d419461CfcF382a0A822e" "address": "0xD9273d497eDBC967F39d419461CfcF382a0A822e"
}, },
"localhost": { "localhost": {
"address": "0x6642B57e4265BAD868C17Fc1d1F4F88DBBA04Aa8" "address": "0xD9273d497eDBC967F39d419461CfcF382a0A822e"
} }
}, },
"PriceOracle": { "PriceOracle": {
@ -75,7 +75,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x099d9fF8F818290C8b5B7Db5bFca84CEebd2714c", "address": "0x1750499D05Ed1674d822430FB960d5F6731fDf64",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -85,7 +85,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xAF6BA11790D1942625C0c2dA07da19AB63845cfF", "address": "0xEC1C93A9f6a9e18E97784c76aC52053587FcDB89",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -95,7 +95,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xD83D2773a7873ae2b5f8Fb92097e20a8C64F691E", "address": "0x7B6C3e5486D9e6959441ab554A889099eed76290",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -105,7 +105,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xf91aC1098F3b154671Ce83290114aaE45ac0225f", "address": "0xD83D2773a7873ae2b5f8Fb92097e20a8C64F691E",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -115,7 +115,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x830bceA96E56DBC1F8578f75fBaC0AF16B32A07d", "address": "0x626FdE749F9d499d3777320CAf29484B624ab84a",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -169,7 +169,7 @@
"address": "0x2B681757d757fbB80cc51c6094cEF5eE75bF55aA" "address": "0x2B681757d757fbB80cc51c6094cEF5eE75bF55aA"
}, },
"localhost": { "localhost": {
"address": "0x3bDA11B584dDff7F66E0cFe1da1562c92B45db60" "address": "0x2B681757d757fbB80cc51c6094cEF5eE75bF55aA"
} }
}, },
"WalletBalanceProvider": { "WalletBalanceProvider": {
@ -178,7 +178,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x392E5355a0e88Bd394F717227c752670fb3a8020", "address": "0xBEF0d4b9c089a5883741fC14cbA352055f35DDA2",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -188,7 +188,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x7c2C195CD6D34B8F845992d380aADB2730bB9C6F", "address": "0x11df1AF606b85226Ab9a8B1FDa90395298e7494F",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -198,7 +198,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x8858eeB3DfffA017D4BCE9801D340D36Cf895CCf", "address": "0x8f9A92c125FFEb83d8eC808Cd9f8cb80084c1E37",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -208,7 +208,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x0078371BDeDE8aAc7DeBfFf451B74c5EDB385Af7", "address": "0xc4007844AE6bBe168cE8D692C86a7A4414FBcD26",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -218,7 +218,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xf4e77E5Da47AC3125140c470c71cBca77B5c638c", "address": "0xAb768C858C33DfcB6651d1174AFb750433a87Be0",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -228,7 +228,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x3619DbE27d7c1e7E91aA738697Ae7Bc5FC3eACA5", "address": "0xA089557D64DAE4b4FcB65aB7C8A520AABb213e37",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -238,7 +238,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x038B86d9d8FAFdd0a02ebd1A476432877b0107C8", "address": "0x20FAE2042b362E3FaB2806820b9A43CC116e2846",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -248,7 +248,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x1A1FEe7EeD918BD762173e4dc5EfDB8a78C924A8", "address": "0x8880F314112f15C2AfF674c3B27f9a44Ca86e4d0",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -258,7 +258,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x500D1d6A4c7D8Ae28240b47c8FCde034D827fD5e", "address": "0xDcb10C2e15110Db4B02C0a1df459768E680ce245",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -268,7 +268,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xc4905364b78a742ccce7B890A89514061E47068D", "address": "0xfD408ec64Da574b1859814F810564f73ea2Ff003",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -278,7 +278,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe", "address": "0x0006F7c3542BEE76Dd887f54eD22405Ac4ae905a",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -288,7 +288,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x8B5B7a6055E54a36fF574bbE40cf2eA68d5554b3", "address": "0x6ca94a51c644eca3F9CA315bcC41CbA6940A66Eb",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -298,7 +298,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xEcc0a6dbC0bb4D51E4F84A315a9e5B0438cAD4f0", "address": "0x6765291Cab755B980F377445eFd0F9F945CDA6C4",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -308,7 +308,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x20Ce94F404343aD2752A2D01b43fa407db9E0D00", "address": "0xa7dB4d25Fc525d19Fbda4E74AAF447B88420FbcB",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -318,7 +318,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x1d80315fac6aBd3EfeEbE97dEc44461ba7556160", "address": "0x273D60904A8DBa3Ae6B20505c59902644124fF0E",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -328,7 +328,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x2D8553F9ddA85A9B3259F6Bf26911364B85556F5", "address": "0xfc37dE87C1Ee39cc856782BF96fEdcB6FA5c5A7f",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -338,7 +338,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x52d3b94181f8654db2530b0fEe1B19173f519C52", "address": "0x049228dFFEdf91ff224e9F96247aEBA700e3590c",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -348,7 +348,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xd15468525c35BDBC1eD8F2e09A00F8a173437f2f", "address": "0xA410D1f3fEAF300842142Cd7AA1709D84944DCb7",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -358,7 +358,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x7e35Eaf7e8FBd7887ad538D4A38Df5BbD073814a", "address": "0x835973768750b3ED2D5c3EF5AdcD5eDb44d12aD4",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -368,7 +368,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x5bcb88A0d20426e451332eE6C4324b0e663c50E0", "address": "0x1181FC27dbF04B5105243E60BB1936c002e9d5C8",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -378,7 +378,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x3521eF8AaB0323004A6dD8b03CE890F4Ea3A13f5", "address": "0x6F96975e2a0e1380b6e2e406BB33Ae96e4b6DB65",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -388,7 +388,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x53369fd4680FfE3DfF39Fc6DDa9CfbfD43daeA2E", "address": "0xc032930653da193EDE295B4DcE3DD093a695c3b3",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -398,7 +398,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xB00cC45B4a7d3e1FEE684cFc4417998A1c183e6d", "address": "0xb3363f4349b1160DbA55ec4D82fDe874A4123A2a",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -408,7 +408,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x58F132FBB86E21545A4Bace3C19f1C05d86d7A22", "address": "0xf8c6eB390cDc5C08717bC2268aa0c1169A9B5deE",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -417,7 +417,7 @@
"address": "0x2cfcA5785261fbC88EFFDd46fCFc04c22525F9e4" "address": "0x2cfcA5785261fbC88EFFDd46fCFc04c22525F9e4"
}, },
"localhost": { "localhost": {
"address": "0x3b050AFb4ac4ACE646b31fF3639C1CD43aC31460" "address": "0xDf73fC454FA018051D4a1509e63D11530A59DE10"
} }
}, },
"StableDebtToken": { "StableDebtToken": {
@ -426,7 +426,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xA0AB1cB92A4AF81f84dCd258155B5c25D247b54E", "address": "0xB660Fdd109a95718cB9d20E3A89EE6cE342aDcB6",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -436,13 +436,13 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x5f7134cd38C826a7649f9Cc47dda24d834DD2967", "address": "0x830bceA96E56DBC1F8578f75fBaC0AF16B32A07d",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
"AToken": { "AToken": {
"localhost": { "localhost": {
"address": "0xE91bBe8ee03560E3dda2786f95335F5399813Ca0", "address": "0xA0AB1cB92A4AF81f84dCd258155B5c25D247b54E",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"buidlerevm": { "buidlerevm": {
@ -456,7 +456,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x7f23223A2FAf869962B38f5eC4aAB7f37454A45e", "address": "0x1203D1b97BF6E546c00C45Cda035D3010ACe1180",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -466,7 +466,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0xf784709d2317D872237C4bC22f867d1BAe2913AB", "address": "0x2cc20bE530F92865c2ed8CeD0b020a11bFe62Fe7",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -476,7 +476,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x1203D1b97BF6E546c00C45Cda035D3010ACe1180", "address": "0x8733AfE8174BA7c04c6CD694bD673294079b7E10",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },
@ -486,7 +486,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
"address": "0x8733AfE8174BA7c04c6CD694bD673294079b7E10", "address": "0xA8083d78B6ABC328b4d3B714F76F384eCC7147e1",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
}, },

View File

@ -45,25 +45,74 @@ export enum eContractid {
} }
export enum ProtocolErrors { export enum ProtocolErrors {
INVALID_CONFIGURATOR_CALLER_MSG = 'The caller must be a lending pool configurator contract', // require error messages - ValidationLogic
INVALID_POOL_CALLER_MSG = 'The caller must be a lending pool contract', AMOUNT_NOT_GREATER_THAN_0 = '1', // 'Amount must be greater than 0'
INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', NO_ACTIVE_RESERVE = '2', // 'Action requires an active reserve'
INVALID_POOL_MANAGER_CALLER_MSG = 'The caller must be a lending pool manager', 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_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer',
INVALID_TO_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_OWNER_REVERT_MSG = 'Ownable: caller is not the owner',
INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER = 'Invalid redirected balance before transfer', INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER = 'Invalid redirected balance before transfer',
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER = 'Invalid redirected balance after transfer', INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER = 'Invalid redirected balance after transfer',
INVALID_REDIRECTION_ADDRESS = 'Invalid redirection address', 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', INVALID_HF = 'Invalid health factor',
USER_DID_NOT_BORROW_SPECIFIED = 'User did not borrow the specified currency', TRANSFER_AMOUNT_EXCEEDS_BALANCE = 'ERC20: transfer amount exceeds balance',
THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED = 'The collateral chosen cannot be liquidated', SAFEERC20_LOWLEVEL_CALL = 'SafeERC20: low-level call failed'
} }
export type tEthereumAddress = string; export type tEthereumAddress = string;

View File

@ -15,6 +15,7 @@
"test-scenarios": "buidler test test/__setup.spec.ts test/scenario.spec.ts", "test-scenarios": "buidler test test/__setup.spec.ts test/scenario.spec.ts",
"test-repay-with-collateral": "buidler test test/__setup.spec.ts test/repay-with-collateral.spec.ts", "test-repay-with-collateral": "buidler test test/__setup.spec.ts test/repay-with-collateral.spec.ts",
"test-liquidate-with-collateral": "buidler test test/__setup.spec.ts test/flash-liquidation-with-collateral.spec.ts", "test-liquidate-with-collateral": "buidler test test/__setup.spec.ts test/flash-liquidation-with-collateral.spec.ts",
"test-flash": "buidler test test/__setup.spec.ts test/flashloan.spec.ts",
"dev:coverage": "buidler coverage", "dev:coverage": "buidler coverage",
"dev:deployment": "buidler dev-deployment", "dev:deployment": "buidler dev-deployment",
"dev:deployExample": "buidler deploy-Example", "dev:deployExample": "buidler deploy-Example",

2756
test.log Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,17 +3,17 @@ import {makeSuite, TestEnv} from './helpers/make-suite';
import {ProtocolErrors} from '../helpers/types'; import {ProtocolErrors} from '../helpers/types';
makeSuite('AToken: Modifiers', (testEnv: TestEnv) => { 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 () => { it('Tries to invoke mint not being the LendingPool', async () => {
const {deployer, aDai} = testEnv; 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 () => { it('Tries to invoke burn not being the LendingPool', async () => {
const {deployer, aDai} = testEnv; const {deployer, aDai} = testEnv;
await expect(aDai.burn(deployer.address, deployer.address, '1')).to.be.revertedWith( 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; const {deployer, users, aDai} = testEnv;
await expect( await expect(
aDai.transferOnLiquidation(deployer.address, users[0].address, '1') 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 () => { it('Tries to invoke transferUnderlyingTo not being the LendingPool', async () => {
const {deployer, users, aDai} = testEnv; const {deployer, users, aDai} = testEnv;
await expect(aDai.transferUnderlyingTo(deployer.address, '1')).to.be.revertedWith( 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_BEFORE_TRANSFER,
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER, INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER,
INVALID_REDIRECTION_ADDRESS, INVALID_REDIRECTION_ADDRESS,
ZERO_COLLATERAL, // ZERO_COLLATERAL,
TRANSFERRED_AMOUNT_GT_ZERO, TRANSFER_AMOUNT_NOT_GT_0,
COLLATERAL_BALANCE_IS_0,
TRANSFER_NOT_ALLOWED,
} = ProtocolErrors; } = ProtocolErrors;
it('User 0 deposits 1000 DAI, transfers to user 1', async () => { 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).mint(await convertToCurrencyDecimals(weth.address, '1'));
await weth.connect(users[0].signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL); await weth.connect(users[0].signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await pool await pool.connect(users[0].signer).deposit(weth.address, ethers.utils.parseEther('1.0'), '0');
.connect(users[0].signer)
.deposit(weth.address, ethers.utils.parseEther('1.0'), '0');
await expect( await expect(
pool pool
.connect(users[1].signer) .connect(users[1].signer)
.borrow(weth.address, ethers.utils.parseEther('0.1'), RateMode.Stable, AAVE_REFERRAL), .borrow(weth.address, ethers.utils.parseEther('0.1'), RateMode.Stable, AAVE_REFERRAL),
ZERO_COLLATERAL COLLATERAL_BALANCE_IS_0
).to.be.revertedWith(ZERO_COLLATERAL); ).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 () => { 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( await expect(
aDai.connect(users[1].signer).transfer(users[0].address, aDAItoTransfer), aDai.connect(users[1].signer).transfer(users[0].address, aDAItoTransfer),
'Transfer cannot be allowed.' TRANSFER_NOT_ALLOWED
).to.be.revertedWith('Transfer cannot be allowed.'); ).to.be.revertedWith(TRANSFER_NOT_ALLOWED);
}); });
it('User 0 tries to transfer 0 balance (revert expected)', async () => { it('User 0 tries to transfer 0 balance (revert expected)', async () => {
const {users, pool, aDai, dai, weth} = testEnv; const {users, pool, aDai, dai, weth} = testEnv;
await expect( await expect(
aDai.connect(users[0].signer).transfer(users[1].address, '0'), aDai.connect(users[0].signer).transfer(users[1].address, '0'),
TRANSFERRED_AMOUNT_GT_ZERO TRANSFER_AMOUNT_NOT_GT_0
).to.be.revertedWith(TRANSFERRED_AMOUNT_GT_ZERO); ).to.be.revertedWith(TRANSFER_AMOUNT_NOT_GT_0);
}); });
it('User 1 repays the borrow, transfers aDAI back to user 0', async () => { it('User 1 repays the borrow, transfers aDAI back to user 0', async () => {
const {users, pool, aDai, dai, weth} = testEnv; 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).mint(await convertToCurrencyDecimals(weth.address, '2'));
await weth.connect(users[1].signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL); await weth.connect(users[1].signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await pool await pool
.connect(users[1].signer) .connect(users[1].signer)
.repay(weth.address, MAX_UINT_AMOUNT, RateMode.Stable, users[1].address); .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'); const {expect} = require('chai');
makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { 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 () => { it('Deactivates the ETH reserve', async () => {
const {configurator, pool, weth} = testEnv; const {configurator, pool, weth} = testEnv;
@ -27,16 +27,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).deactivateReserve(weth.address), configurator.connect(users[2].signer).deactivateReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Check the onlyLendingPoolManager on activateReserve ', async () => { it('Check the onlyLendingPoolManager on activateReserve ', async () => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).activateReserve(weth.address), configurator.connect(users[2].signer).activateReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Freezes the ETH reserve', async () => { it('Freezes the ETH reserve', async () => {
@ -58,16 +58,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).freezeReserve(weth.address), configurator.connect(users[2].signer).freezeReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Check the onlyLendingPoolManager on unfreezeReserve ', async () => { it('Check the onlyLendingPoolManager on unfreezeReserve ', async () => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).unfreezeReserve(weth.address), configurator.connect(users[2].signer).unfreezeReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Deactivates the ETH reserve for borrowing', async () => { it('Deactivates the ETH reserve for borrowing', async () => {
@ -90,16 +90,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).disableBorrowingOnReserve(weth.address), configurator.connect(users[2].signer).disableBorrowingOnReserve(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Check the onlyLendingPoolManager on enableBorrowingOnReserve ', async () => { it('Check the onlyLendingPoolManager on enableBorrowingOnReserve ', async () => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).enableBorrowingOnReserve(weth.address, true), configurator.connect(users[2].signer).enableBorrowingOnReserve(weth.address, true),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Deactivates the ETH reserve as collateral', async () => { it('Deactivates the ETH reserve as collateral', async () => {
@ -121,8 +121,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).disableReserveAsCollateral(weth.address), configurator.connect(users[2].signer).disableReserveAsCollateral(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Check the onlyLendingPoolManager on enableReserveAsCollateral ', async () => { it('Check the onlyLendingPoolManager on enableReserveAsCollateral ', async () => {
@ -131,8 +131,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
configurator configurator
.connect(users[2].signer) .connect(users[2].signer)
.enableReserveAsCollateral(weth.address, '75', '80', '105'), .enableReserveAsCollateral(weth.address, '75', '80', '105'),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Disable stable borrow rate on the ETH reserve', async () => { it('Disable stable borrow rate on the ETH reserve', async () => {
@ -153,16 +153,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).disableReserveStableRate(weth.address), configurator.connect(users[2].signer).disableReserveStableRate(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Check the onlyLendingPoolManager on enableReserveStableRate', async () => { it('Check the onlyLendingPoolManager on enableReserveStableRate', async () => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).enableReserveStableRate(weth.address), configurator.connect(users[2].signer).enableReserveStableRate(weth.address),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Changes LTV of the reserve', async () => { it('Changes LTV of the reserve', async () => {
@ -176,8 +176,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).setLtv(weth.address, '75'), configurator.connect(users[2].signer).setLtv(weth.address, '75'),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Changes liquidation threshold of the reserve', async () => { it('Changes liquidation threshold of the reserve', async () => {
@ -194,8 +194,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).setLiquidationThreshold(weth.address, '80'), configurator.connect(users[2].signer).setLiquidationThreshold(weth.address, '80'),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Changes liquidation bonus of the reserve', async () => { it('Changes liquidation bonus of the reserve', async () => {
@ -212,24 +212,24 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).setLiquidationBonus(weth.address, '80'), configurator.connect(users[2].signer).setLiquidationBonus(weth.address, '80'),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Check the onlyLendingPoolManager on setReserveDecimals', async () => { it('Check the onlyLendingPoolManager on setReserveDecimals', async () => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).setReserveDecimals(weth.address, '80'), configurator.connect(users[2].signer).setReserveDecimals(weth.address, '80'),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Check the onlyLendingPoolManager on setLiquidationBonus', async () => { it('Check the onlyLendingPoolManager on setLiquidationBonus', async () => {
const {configurator, users, weth} = testEnv; const {configurator, users, weth} = testEnv;
await expect( await expect(
configurator.connect(users[2].signer).setLiquidationBonus(weth.address, '80'), configurator.connect(users[2].signer).setLiquidationBonus(weth.address, '80'),
INVALID_POOL_MANAGER_CALLER_MSG CALLER_NOT_LENDING_POOL_MANAGER
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
}); });
it('Reverts when trying to disable the DAI reserve with liquidity on it', async () => { it('Reverts when trying to disable the DAI reserve with liquidity on it', async () => {
@ -246,7 +246,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
await expect( await expect(
configurator.deactivateReserve(dai.address), configurator.deactivateReserve(dai.address),
'The liquidity of the reserve needs to be 0' RESERVE_LIQUIDITY_NOT_0
).to.be.revertedWith('The liquidity of the reserve needs to be 0'); ).to.be.revertedWith(RESERVE_LIQUIDITY_NOT_0);
}); });
}); });

View File

@ -12,33 +12,11 @@ import {timeLatest} from '../helpers/misc-utils';
import {tEthereumAddress, ProtocolErrors} from '../helpers/types'; import {tEthereumAddress, ProtocolErrors} from '../helpers/types';
import {convertToCurrencyDecimals} from '../helpers/contracts-helpers'; import {convertToCurrencyDecimals} from '../helpers/contracts-helpers';
import {formatUnits, formatEther} from 'ethers/lib/utils'; import {formatUnits, formatEther} from 'ethers/lib/utils';
import {expectRepayWithCollateralEvent} from './repay-with-collateral.spec'
const {expect} = require('chai'); const {expect} = require('chai');
const {parseUnits, parseEther} = ethers.utils; const {parseUnits, parseEther} = ethers.utils;
const expectRepayWithCollateralEvent = (
events: ethers.Event[],
pool: tEthereumAddress,
collateral: tEthereumAddress,
borrowing: tEthereumAddress,
user: tEthereumAddress
) => {
if (!events || events.length < 14) {
expect(false, 'INVALID_EVENTS_LENGTH_ON_REPAY_COLLATERAL');
}
const repayWithCollateralEvent = events[13];
expect(repayWithCollateralEvent.address).to.be.equal(pool);
expect(`0x${repayWithCollateralEvent.topics[1].slice(26)}`.toLowerCase()).to.be.equal(
collateral.toLowerCase()
);
expect(`0x${repayWithCollateralEvent.topics[2].slice(26)}`).to.be.equal(borrowing.toLowerCase());
expect(`0x${repayWithCollateralEvent.topics[3].slice(26)}`.toLowerCase()).to.be.equal(
user.toLowerCase()
);
};
makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEnv) => { makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEnv) => {
const {INVALID_HF} = ProtocolErrors; const {INVALID_HF} = ProtocolErrors;
@ -359,7 +337,7 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
mockSwapAdapter.address, mockSwapAdapter.address,
'0x' '0x'
) )
).to.be.revertedWith('revert CURRRENCY_NOT_BORROWED'); ).to.be.revertedWith('40');
await oracle.setAssetPrice(usdc.address, usdcPrice); await oracle.setAssetPrice(usdc.address, usdcPrice);
}); });
@ -525,7 +503,7 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
mockSwapAdapter.address, mockSwapAdapter.address,
'0x' '0x'
) )
).to.be.revertedWith('FAILED_REPAY_WITH_COLLATERAL'); ).to.be.revertedWith('53');
// Resets DAI Price // Resets DAI Price
await oracle.setAssetPrice(dai.address, daiPrice); await oracle.setAssetPrice(dai.address, daiPrice);
@ -559,7 +537,7 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
mockSwapAdapter.address, mockSwapAdapter.address,
'0x' '0x'
) )
).to.be.revertedWith('HEALTH_FACTOR_ABOVE_THRESHOLD'); ).to.be.revertedWith('38');
}); });
it('User 5 liquidates User 2 DAI Variable loan using his WETH collateral, half the amount', async () => { it('User 5 liquidates User 2 DAI Variable loan using his WETH collateral, half the amount', async () => {
const {pool, weth, dai, users, mockSwapAdapter, oracle} = testEnv; const {pool, weth, dai, users, mockSwapAdapter, oracle} = testEnv;

View File

@ -1,19 +1,27 @@
import {TestEnv, makeSuite} from './helpers/make-suite'; import {TestEnv, makeSuite} from './helpers/make-suite';
import {APPROVAL_AMOUNT_LENDING_POOL, oneRay} from '../helpers/constants'; 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 {ethers} from 'ethers';
import {MockFlashLoanReceiver} from '../types/MockFlashLoanReceiver'; import {MockFlashLoanReceiver} from '../types/MockFlashLoanReceiver';
import {ProtocolErrors} from '../helpers/types'; import {ProtocolErrors, eContractid} from '../helpers/types';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import {VariableDebtToken} from '../types/VariableDebtToken';
import {StableDebtToken} from '../types/StableDebtToken';
const {expect} = require('chai'); const {expect} = require('chai');
makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
let _mockFlashLoanReceiver = {} as MockFlashLoanReceiver; let _mockFlashLoanReceiver = {} as MockFlashLoanReceiver;
const { const {
INCONSISTENT_PROTOCOL_BALANCE, COLLATERAL_BALANCE_IS_0,
TOO_SMALL_FLASH_LOAN, REQUESTED_AMOUNT_TOO_SMALL,
NOT_ENOUGH_LIQUIDITY_TO_BORROW, TRANSFER_AMOUNT_EXCEEDS_BALANCE,
INVALID_FLASHLOAN_MODE,
SAFEERC20_LOWLEVEL_CALL
} = ProtocolErrors; } = ProtocolErrors;
before(async () => { before(async () => {
@ -31,14 +39,16 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
await pool.deposit(weth.address, amountToDeposit, '0'); 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; const {pool, deployer, weth} = testEnv;
await pool.flashLoan( await pool.flashLoan(
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
weth.address, weth.address,
ethers.utils.parseEther('0.8'), ethers.utils.parseEther('0.8'),
'0x10' 0,
'0x10',
'0'
); );
ethers.utils.parseUnits('10000'); ethers.utils.parseUnits('10000');
@ -57,18 +67,17 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
expect(currentLiquidityIndex.toString()).to.be.equal('1000720000000000000000000000'); 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 {pool, weth} = testEnv;
const reserveDataBefore = await pool.getReserveData(weth.address); const reserveDataBefore = await pool.getReserveData(weth.address);
console.log("Total liquidity is ", reserveDataBefore.availableLiquidity.toString());
const txResult = await pool.flashLoan( const txResult = await pool.flashLoan(
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
weth.address, weth.address,
'1000720000000000000', '1000720000000000000',
'0x10' 0,
'0x10',
'0'
); );
const reserveData = await pool.getReserveData(weth.address); const reserveData = await pool.getReserveData(weth.address);
@ -85,21 +94,79 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
expect(currentLiquidityIndex.toString()).to.be.equal('1001620648000000000000000000'); expect(currentLiquidityIndex.toString()).to.be.equal('1001620648000000000000000000');
}); });
it('Takes WETH flashloan, does not return the funds (revert expected)', async () => { it('Takes WETH flashloan, does not return the funds with mode = 0. (revert expected)', async () => {
const {pool, deployer, weth} = testEnv; const {pool, weth, users} = testEnv;
const caller = users[1];
// move funds to the MockFlashLoanReceiver contract to pay the fee
await _mockFlashLoanReceiver.setFailExecutionTransfer(true); await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
await expect( 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, _mockFlashLoanReceiver.address,
weth.address, weth.address,
ethers.utils.parseEther('0.8'), ethers.utils.parseEther('0.8'),
'0x10' 2,
) '0x10',
).to.be.revertedWith(INCONSISTENT_PROTOCOL_BALANCE); '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 () => { it('tries to take a very small flashloan, which would result in 0 fees (revert expected)', async () => {
@ -110,9 +177,11 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
weth.address, weth.address,
'1', //1 wei loan '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 () => { it('tries to take a flashloan that is bigger than the available liquidity (revert expected)', async () => {
@ -123,45 +192,52 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
weth.address, weth.address,
'1004415000000000000', //slightly higher than the available liquidity '1004415000000000000', //slightly higher than the available liquidity
'0x10' 2,
'0x10',
'0'
), ),
NOT_ENOUGH_LIQUIDITY_TO_BORROW TRANSFER_AMOUNT_EXCEEDS_BALANCE
).to.be.revertedWith(NOT_ENOUGH_LIQUIDITY_TO_BORROW); ).to.be.revertedWith(SAFEERC20_LOWLEVEL_CALL);
}); });
it('tries to take a flashloan using a non contract address as receiver (revert expected)', async () => { it('tries to take a flashloan using a non contract address as receiver (revert expected)', async () => {
const {pool, deployer, weth} = testEnv; const {pool, deployer, weth} = testEnv;
await expect(pool.flashLoan(deployer.address, weth.address, '1000000000000000000', '0x10')).to await expect(
.be.reverted; pool.flashLoan(deployer.address, weth.address, '1000000000000000000', 2, '0x10', '0')
).to.be.reverted;
}); });
it('Deposits DAI into the reserve', async () => { it('Deposits USDC into the reserve', async () => {
const {dai, pool} = testEnv; 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 () => { it('Takes out a 500 USDC flashloan, returns the funds correctly', async () => {
const {dai, pool, deployer: depositor} = testEnv; const {usdc, pool, deployer: depositor} = testEnv;
await _mockFlashLoanReceiver.setFailExecutionTransfer(false); await _mockFlashLoanReceiver.setFailExecutionTransfer(false);
const flashloanAmount = await convertToCurrencyDecimals(usdc.address, '500');
await pool.flashLoan( await pool.flashLoan(
_mockFlashLoanReceiver.address, _mockFlashLoanReceiver.address,
dai.address, usdc.address,
ethers.utils.parseEther('500'), flashloanAmount,
'0x10' 0,
'0x10',
'0'
); );
const reserveData = await pool.getReserveData(dai.address); const reserveData = await pool.getReserveData(usdc.address);
const userData = await pool.getUserReserveData(dai.address, depositor.address); const userData = await pool.getUserReserveData(usdc.address, depositor.address);
const totalLiquidity = reserveData.availableLiquidity const totalLiquidity = reserveData.availableLiquidity
.add(reserveData.totalBorrowsStable) .add(reserveData.totalBorrowsStable)
@ -171,7 +247,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
const currentLiquidityIndex = reserveData.liquidityIndex.toString(); const currentLiquidityIndex = reserveData.liquidityIndex.toString();
const currentUserBalance = userData.currentATokenBalance.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(totalLiquidity).to.be.equal(expectedLiquidity, 'Invalid total liquidity');
expect(currentLiqudityRate).to.be.equal('0', 'Invalid liquidity rate'); expect(currentLiqudityRate).to.be.equal('0', 'Invalid liquidity rate');
@ -182,19 +258,101 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
expect(currentUserBalance.toString()).to.be.equal(expectedLiquidity, 'Invalid user balance'); 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 () => { it('Takes out a 500 USDC flashloan with mode = 0, does not return the funds. (revert expected)', async () => {
const {dai, pool} = testEnv; const {usdc, pool, users} = testEnv;
const caller = users[2];
const flashloanAmount = await convertToCurrencyDecimals(usdc.address, '500');
await _mockFlashLoanReceiver.setFailExecutionTransfer(true); await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
await expect( await expect(
pool.flashLoan( pool
_mockFlashLoanReceiver.address, .connect(caller.signer)
dai.address, .flashLoan(_mockFlashLoanReceiver.address, usdc.address, flashloanAmount, 2, '0x10', '0')
ethers.utils.parseEther('500'), ).to.be.revertedWith(COLLATERAL_BALANCE_IS_0);
'0x10' });
),
INCONSISTENT_PROTOCOL_BALANCE 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 () => {
).to.be.revertedWith(INCONSISTENT_PROTOCOL_BALANCE); 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

@ -1408,8 +1408,8 @@ const calcExpectedLiquidityIndex = (reserveData: ReserveData, timestamp: BigNumb
}; };
const calcExpectedVariableBorrowIndex = (reserveData: ReserveData, timestamp: BigNumber) => { const calcExpectedVariableBorrowIndex = (reserveData: ReserveData, timestamp: BigNumber) => {
//if utilization rate is 0, nothing to compound //if totalBorrowsVariable is 0, nothing to compound
if (reserveData.utilizationRate.eq('0')) { if (reserveData.totalBorrowsVariable.eq('0')) {
return reserveData.variableBorrowIndex; return reserveData.variableBorrowIndex;
} }

View File

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

View File

@ -13,15 +13,12 @@ const chai = require('chai');
const {expect} = chai; const {expect} = chai;
makeSuite('LendingPool liquidation - liquidator receiving the underlying asset', (testEnv) => { makeSuite('LendingPool liquidation - liquidator receiving the underlying asset', (testEnv) => {
const { const {INVALID_HF} = ProtocolErrors;
HF_IS_NOT_BELLOW_THRESHOLD,
INVALID_HF,
USER_DID_NOT_BORROW_SPECIFIED,
THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED,
} = ProtocolErrors;
before('Before LendingPool liquidation: set config', () => { before('Before LendingPool liquidation: set config', () => {
BigNumber.config({DECIMAL_PLACES: 0, ROUNDING_MODE: BigNumber.ROUND_DOWN}); BigNumber.config({DECIMAL_PLACES: 0, ROUNDING_MODE: BigNumber.ROUND_DOWN});
}); });
after('After LendingPool liquidation: reset config', () => { after('After LendingPool liquidation: reset config', () => {
BigNumber.config({DECIMAL_PLACES: 20, ROUNDING_MODE: BigNumber.ROUND_HALF_UP}); BigNumber.config({DECIMAL_PLACES: 20, ROUNDING_MODE: BigNumber.ROUND_HALF_UP});
}); });
@ -73,7 +70,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset',
expect(userGlobalDataAfter.currentLiquidationThreshold.toString()).to.be.bignumber.equal( expect(userGlobalDataAfter.currentLiquidationThreshold.toString()).to.be.bignumber.equal(
'8000', '8000',
'Invalid liquidation threshold' INVALID_HF
); );
}); });
@ -92,7 +89,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset',
expect(userGlobalData.healthFactor.toString()).to.be.bignumber.lt( expect(userGlobalData.healthFactor.toString()).to.be.bignumber.lt(
oneEther.toFixed(0), oneEther.toFixed(0),
'Invalid health factor' INVALID_HF
); );
}); });

View File

@ -14,18 +14,18 @@ import {tEthereumAddress} from '../helpers/types';
const {expect} = require('chai'); const {expect} = require('chai');
const {parseUnits, parseEther} = ethers.utils; const {parseUnits, parseEther} = ethers.utils;
const expectRepayWithCollateralEvent = ( export const expectRepayWithCollateralEvent = (
events: ethers.Event[], events: ethers.Event[],
pool: tEthereumAddress, pool: tEthereumAddress,
collateral: tEthereumAddress, collateral: tEthereumAddress,
borrowing: tEthereumAddress, borrowing: tEthereumAddress,
user: tEthereumAddress user: tEthereumAddress
) => { ) => {
if (!events || events.length < 14) { if (!events || events.length < 16) {
expect(false, 'INVALID_EVENTS_LENGTH_ON_REPAY_COLLATERAL'); expect(false, 'INVALID_EVENTS_LENGTH_ON_REPAY_COLLATERAL');
} }
const repayWithCollateralEvent = events[13]; const repayWithCollateralEvent = events[15];
expect(repayWithCollateralEvent.address).to.be.equal(pool); expect(repayWithCollateralEvent.address).to.be.equal(pool);
expect(`0x${repayWithCollateralEvent.topics[1].slice(26)}`.toLowerCase()).to.be.equal( expect(`0x${repayWithCollateralEvent.topics[1].slice(26)}`.toLowerCase()).to.be.equal(
@ -87,7 +87,7 @@ makeSuite('LendingPool. repayWithCollateral()', (testEnv: TestEnv) => {
mockSwapAdapter.address, mockSwapAdapter.address,
'0x' '0x'
) )
).to.be.revertedWith("FAILED_REPAY_WITH_COLLATERAL") ).to.be.revertedWith("53")
}); });
@ -294,7 +294,7 @@ makeSuite('LendingPool. repayWithCollateral()', (testEnv: TestEnv) => {
mockSwapAdapter.address, mockSwapAdapter.address,
'0x' '0x'
) )
).to.be.revertedWith('revert CURRRENCY_NOT_BORROWED'); ).to.be.revertedWith('40');
}); });
it('User 3 tries to repay with his collateral all his variable debt and part of the stable', async () => { it('User 3 tries to repay with his collateral all his variable debt and part of the stable', async () => {

View File

@ -5,7 +5,7 @@ import {getContract} from '../helpers/contracts-helpers';
import {StableDebtToken} from '../types/StableDebtToken'; import {StableDebtToken} from '../types/StableDebtToken';
makeSuite('Stable debt token tests', (testEnv: TestEnv) => { 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 () => { it('Tries to invoke mint not being the LendingPool', async () => {
const {deployer, pool, dai} = testEnv; 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( 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( 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 {expect} from 'chai';
import {makeSuite, TestEnv} from './helpers/make-suite'; import {makeSuite, TestEnv} from './helpers/make-suite';
import {ProtocolErrors, eContractid} from '../helpers/types'; 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 {MockAToken} from '../types/MockAToken';
import { MockStableDebtToken } from '../types/MockStableDebtToken'; import {MockStableDebtToken} from '../types/MockStableDebtToken';
import { MockVariableDebtToken } from '../types/MockVariableDebtToken'; import {MockVariableDebtToken} from '../types/MockVariableDebtToken';
makeSuite('Upgradeability', (testEnv: TestEnv) => { makeSuite('Upgradeability', (testEnv: TestEnv) => {
const {INVALID_POOL_MANAGER_CALLER_MSG} = ProtocolErrors; const {CALLER_NOT_LENDING_POOL_MANAGER} = ProtocolErrors;
let newATokenAddress: string; let newATokenAddress: string;
let newStableTokenAddress: string; let newStableTokenAddress: string;
let newVariableTokenAddress: string; let newVariableTokenAddress: string;
before('deploying instances', async () => { before('deploying instances', async () => {
const {dai, pool} = testEnv; const {dai, pool} = testEnv;
const aTokenInstance = await deployContract<MockAToken>(eContractid.MockAToken, [ const aTokenInstance = await deployContract<MockAToken>(eContractid.MockAToken, [
@ -22,24 +26,19 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => {
'aDAI', 'aDAI',
]); ]);
const stableDebtTokenInstance = await deployContract<MockStableDebtToken>(eContractid.MockStableDebtToken, [ const stableDebtTokenInstance = await deployContract<MockStableDebtToken>(
pool.address, eContractid.MockStableDebtToken,
dai.address, [pool.address, dai.address, 'Aave stable debt bearing DAI updated', 'stableDebtDAI']
'Aave stable debt bearing DAI updated', );
'stableDebtDAI',
]);
const variableDebtTokenInstance = await deployContract<MockVariableDebtToken>(eContractid.MockVariableDebtToken, [ const variableDebtTokenInstance = await deployContract<MockVariableDebtToken>(
pool.address, eContractid.MockVariableDebtToken,
dai.address, [pool.address, dai.address, 'Aave variable debt bearing DAI updated', 'variableDebtDAI']
'Aave variable debt bearing DAI updated', );
'variableDebtDAI',
]);
newATokenAddress = aTokenInstance.address; newATokenAddress = aTokenInstance.address;
newVariableTokenAddress = variableDebtTokenInstance.address; newVariableTokenAddress = variableDebtTokenInstance.address;
newStableTokenAddress = stableDebtTokenInstance.address; newStableTokenAddress = stableDebtTokenInstance.address;
}); });
it('Tries to update the DAI Atoken implementation with a different address than the lendingPoolManager', async () => { 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( await expect(
configurator.connect(users[1].signer).updateAToken(dai.address, newATokenAddress) 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 () => { it('Upgrades the DAI Atoken implementation ', async () => {
@ -66,8 +65,10 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => {
const {dai, configurator, users} = testEnv; const {dai, configurator, users} = testEnv;
await expect( await expect(
configurator.connect(users[1].signer).updateStableDebtToken(dai.address, newStableTokenAddress) configurator
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); .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 () => { 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 {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(); const tokenName = await debtToken.name();
@ -90,8 +94,10 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => {
const {dai, configurator, users} = testEnv; const {dai, configurator, users} = testEnv;
await expect( await expect(
configurator.connect(users[1].signer).updateVariableDebtToken(dai.address, newVariableTokenAddress) configurator
).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); .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 () => { 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 {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(); const tokenName = await debtToken.name();
expect(tokenName).to.be.eq('Aave variable debt bearing DAI updated', 'Invalid token 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'; import {VariableDebtToken} from '../types/VariableDebtToken';
makeSuite('Variable debt token tests', (testEnv: TestEnv) => { 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 () => { it('Tries to invoke mint not being the LendingPool', async () => {
const {deployer, pool, dai} = testEnv; 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( 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( await expect(variableDebtContract.burn(deployer.address, '1')).to.be.revertedWith(
INVALID_POOL_CALLER_MSG_1 CALLER_MUST_BE_LENDING_POOL
); );
}); });
}); });