aave-protocol-v2/contracts/tokenization/AToken.sol

571 lines
19 KiB
Solidity
Raw Normal View History

// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
2020-07-10 17:16:04 +00:00
import {ERC20} from './ERC20.sol';
import {LendingPool} from '../lendingpool/LendingPool.sol';
2020-08-20 07:51:21 +00:00
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
2020-08-07 17:29:13 +00:00
import {
VersionedInitializable
} from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol';
import {IAToken} from './interfaces/IAToken.sol';
import {IERC20} from '../interfaces/IERC20.sol';
2020-09-09 19:26:52 +00:00
import {SafeERC20} from '../misc/SafeERC20.sol';
2020-08-07 16:23:52 +00:00
/**
* @title Aave ERC20 AToken
*
* @dev Implementation of the interest bearing token for the DLP protocol.
* @author Aave
*/
contract AToken is VersionedInitializable, ERC20, IAToken {
2020-07-13 08:54:08 +00:00
using WadRayMath for uint256;
2020-08-12 17:36:58 +00:00
using SafeERC20 for ERC20;
2020-07-13 08:54:08 +00:00
uint256 public constant UINT_MAX_VALUE = uint256(-1);
address public immutable UNDERLYING_ASSET_ADDRESS;
2020-07-13 08:54:08 +00:00
mapping(address => uint256) private _userIndexes;
mapping(address => address) private _interestRedirectionAddresses;
mapping(address => uint256) private _redirectedBalances;
mapping(address => address) private _interestRedirectionAllowances;
2020-07-13 08:54:08 +00:00
LendingPool private immutable _pool;
2020-07-13 08:54:08 +00:00
2020-08-07 16:23:52 +00:00
uint256 public constant ATOKEN_REVISION = 0x1;
2020-07-13 08:54:08 +00:00
modifier onlyLendingPool {
require(msg.sender == address(_pool), Errors.CALLER_MUST_BE_LENDING_POOL);
2020-07-13 08:54:08 +00:00
_;
}
modifier whenTransferAllowed(address from, uint256 amount) {
require(isTransferAllowed(from, amount), Errors.TRANSFER_NOT_ALLOWED);
2020-07-13 08:54:08 +00:00
_;
}
constructor(
LendingPool pool,
address underlyingAssetAddress,
string memory tokenName,
string memory tokenSymbol
) public ERC20(tokenName, tokenSymbol, 18) {
_pool = pool;
UNDERLYING_ASSET_ADDRESS = underlyingAssetAddress;
}
2020-08-07 16:23:52 +00:00
2020-08-10 18:20:08 +00:00
function getRevision() internal virtual override pure returns (uint256) {
2020-08-07 16:23:52 +00:00
return ATOKEN_REVISION;
}
function initialize(
uint8 underlyingAssetDecimals,
string calldata tokenName,
string calldata tokenSymbol
2020-08-10 18:20:08 +00:00
) external virtual initializer {
_setName(tokenName);
_setSymbol(tokenSymbol);
_setDecimals(underlyingAssetDecimals);
2020-07-13 08:54:08 +00:00
}
/**
* @notice ERC20 implementation internal function backing transfer() and transferFrom()
* @dev validates the transfer before allowing it. NOTE: This is not standard ERC20 behavior
**/
function _transfer(
address from,
address to,
uint256 amount
) internal override whenTransferAllowed(from, amount) {
_executeTransfer(from, to, amount);
2020-07-13 08:54:08 +00:00
}
/**
* @dev redirects the interest generated to a target address.
* when the interest is redirected, the user balance is added to
* the recepient redirected balance.
* @param to the address to which the interest will be redirected
2020-07-13 08:54:08 +00:00
**/
function redirectInterestStream(address to) external override {
_redirectInterestStream(msg.sender, to);
2020-07-13 08:54:08 +00:00
}
/**
* @dev redirects the interest generated by from to a target address.
2020-07-13 08:54:08 +00:00
* when the interest is redirected, the user balance is added to
* the recepient redirected balance. The caller needs to have allowance on
* the interest redirection to be able to execute the function.
* @param from the address of the user whom interest is being redirected
* @param to the address to which the interest will be redirected
2020-07-13 08:54:08 +00:00
**/
function redirectInterestStreamOf(address from, address to) external override {
2020-07-13 08:54:08 +00:00
require(
msg.sender == _interestRedirectionAllowances[from],
2020-09-03 08:33:15 +00:00
Errors.INTEREST_REDIRECTION_NOT_ALLOWED
);
_redirectInterestStream(from, to);
2020-07-13 08:54:08 +00:00
}
/**
* @dev gives allowance to an address to execute the interest redirection
* on behalf of the caller.
* @param to the address to which the interest will be redirected. Pass address(0) to reset
2020-07-13 08:54:08 +00:00
* the allowance.
**/
function allowInterestRedirectionTo(address to) external override {
require(to != msg.sender, Errors.CANNOT_GIVE_ALLOWANCE_TO_HIMSELF);
_interestRedirectionAllowances[msg.sender] = to;
emit InterestRedirectionAllowanceChanged(msg.sender, to);
2020-07-13 08:54:08 +00:00
}
/**
* @dev burns the aTokens and sends the equivalent amount of underlying to the target.
* only lending pools can call this function
* @param amount the amount being burned
2020-07-13 08:54:08 +00:00
**/
function burn(
address user,
address underlyingTarget,
uint256 amount
) external override onlyLendingPool {
2020-07-13 08:54:08 +00:00
//cumulates the balance of the user
(, uint256 currentBalance, uint256 balanceIncrease) = _calculateBalanceIncrease(user);
2020-07-13 08:54:08 +00:00
//if the user is redirecting his interest towards someone else,
//we update the redirected balance of the redirection address by adding the accrued interest,
//and removing the amount to redeem
_updateRedirectedBalanceOfRedirectionAddress(user, balanceIncrease, amount);
if (balanceIncrease > amount) {
_mint(user, balanceIncrease.sub(amount));
2020-08-18 19:19:11 +00:00
} else {
_burn(user, amount.sub(balanceIncrease));
}
uint256 userIndex = 0;
2020-07-13 08:54:08 +00:00
//reset the user data if the remaining balance is 0
if (currentBalance.sub(amount) == 0) {
_resetDataOnZeroBalance(user);
} else {
2020-08-18 19:19:11 +00:00
//updates the user index
userIndex = _userIndexes[user] = _pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS);
}
//transfers the underlying to the target
ERC20(UNDERLYING_ASSET_ADDRESS).safeTransfer(underlyingTarget, amount);
emit Burn(msg.sender, underlyingTarget, amount, balanceIncrease, userIndex);
2020-07-13 08:54:08 +00:00
}
/**
* @dev mints aTokens to user
2020-07-13 08:54:08 +00:00
* only lending pools can call this function
* @param user the address receiving the minted tokens
* @param amount the amount of tokens to mint
2020-07-13 08:54:08 +00:00
*/
function mint(address user, uint256 amount) external override onlyLendingPool {
2020-07-13 08:54:08 +00:00
//cumulates the balance of the user
(, , uint256 balanceIncrease) = _calculateBalanceIncrease(user);
2020-07-13 08:54:08 +00:00
//updates the user index
uint256 index = _userIndexes[user] = _pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS);
2020-08-18 19:19:11 +00:00
2020-07-13 08:54:08 +00:00
//if the user is redirecting his interest towards someone else,
//we update the redirected balance of the redirection address by adding the accrued interest
//and the amount deposited
_updateRedirectedBalanceOfRedirectionAddress(user, balanceIncrease.add(amount), 0);
2020-07-13 08:54:08 +00:00
//mint an equivalent amount of tokens to cover the new deposit
_mint(user, amount.add(balanceIncrease));
2020-07-13 08:54:08 +00:00
emit Mint(user, amount, balanceIncrease, index);
2020-07-13 08:54:08 +00:00
}
/**
* @dev transfers tokens in the event of a borrow being liquidated, in case the liquidators reclaims the aToken
* only lending pools can call this function
* @param from the address from which transfer the aTokens
* @param to the destination address
* @param value the amount to transfer
2020-07-13 08:54:08 +00:00
**/
function transferOnLiquidation(
address from,
address to,
uint256 value
) external override onlyLendingPool {
2020-07-13 08:54:08 +00:00
//being a normal transfer, the Transfer() and BalanceTransfer() are emitted
//so no need to emit a specific event here
_executeTransfer(from, to, value);
2020-07-13 08:54:08 +00:00
}
/**
* @dev calculates the balance of the user, which is the
* principal balance + interest generated by the principal balance + interest generated by the redirected balance
* @param user the user for which the balance is being calculated
2020-07-13 08:54:08 +00:00
* @return the total balance of the user
**/
function balanceOf(address user) public override(ERC20, IERC20) view returns (uint256) {
2020-07-13 08:54:08 +00:00
//current principal balance of the user
uint256 currentPrincipalBalance = super.balanceOf(user);
//balance redirected by other users to user for interest rate accrual
uint256 redirectedBalance = _redirectedBalances[user];
2020-07-13 08:54:08 +00:00
if (currentPrincipalBalance == 0 && redirectedBalance == 0) {
return 0;
}
//if the user is not redirecting the interest to anybody, accrues
2020-07-13 08:54:08 +00:00
//the interest for himself
if (_interestRedirectionAddresses[user] == address(0)) {
2020-07-13 08:54:08 +00:00
//accruing for himself means that both the principal balance and
//the redirected balance partecipate in the interest
return
_calculateCumulatedBalance(user, currentPrincipalBalance.add(redirectedBalance)).sub(
redirectedBalance
);
2020-07-13 08:54:08 +00:00
} else {
//if the user redirected the interest, then only the redirected
//balance generates interest. In that case, the interest generated
//by the redirected balance is added to the current principal balance.
return
currentPrincipalBalance.add(
_calculateCumulatedBalance(user, redirectedBalance).sub(redirectedBalance)
);
}
2020-07-13 08:54:08 +00:00
}
/**
* @dev returns the principal balance of the user. The principal balance is the last
* updated stored balance, which does not consider the perpetually accruing interest.
* @param user the address of the user
2020-07-13 08:54:08 +00:00
* @return the principal balance of the user
**/
function principalBalanceOf(address user) external override view returns (uint256) {
return super.balanceOf(user);
2020-07-13 08:54:08 +00:00
}
/**
* @dev calculates the total supply of the specific aToken
* since the balance of every single user increases over time, the total supply
* does that too.
* @return the current total supply
**/
function totalSupply() public override(ERC20, IERC20) view returns (uint256) {
2020-07-13 08:54:08 +00:00
uint256 currentSupplyPrincipal = super.totalSupply();
if (currentSupplyPrincipal == 0) {
return 0;
}
2020-07-13 08:54:08 +00:00
return
currentSupplyPrincipal
.wadToRay()
.rayMul(_pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS))
2020-07-13 08:54:08 +00:00
.rayToWad();
}
/**
* @dev Used to validate transfers before actually executing them.
* @param user address of the user to check
* @param amount the amount to check
* @return true if the user can transfer amount, false otherwise
2020-07-13 08:54:08 +00:00
**/
function isTransferAllowed(address user, uint256 amount) public override view returns (bool) {
return _pool.balanceDecreaseAllowed(UNDERLYING_ASSET_ADDRESS, user, amount);
2020-07-13 08:54:08 +00:00
}
/**
* @dev returns the last index of the user, used to calculate the balance of the user
* @param user address of the user
2020-07-13 08:54:08 +00:00
* @return the last user index
**/
function getUserIndex(address user) external override view returns (uint256) {
return _userIndexes[user];
2020-07-13 08:54:08 +00:00
}
/**
* @dev returns the address to which the interest is redirected
* @param user address of the user
2020-07-13 08:54:08 +00:00
* @return 0 if there is no redirection, an address otherwise
**/
function getInterestRedirectionAddress(address user) external override view returns (address) {
return _interestRedirectionAddresses[user];
2020-07-13 08:54:08 +00:00
}
/**
* @dev returns the redirected balance of the user. The redirected balance is the balance
* redirected by other accounts to the user, that is accrueing interest for him.
* @param user address of the user
2020-07-13 08:54:08 +00:00
* @return the total redirected balance
**/
function getRedirectedBalance(address user) external override view returns (uint256) {
return _redirectedBalances[user];
2020-07-13 08:54:08 +00:00
}
/**
2020-08-18 19:19:11 +00:00
* @dev calculates the increase in balance since the last user action
* @param user the address of the user
2020-08-18 19:19:11 +00:00
* @return the last user principal balance, the current balance and the balance increase
**/
function _calculateBalanceIncrease(address user)
2020-07-13 08:54:08 +00:00
internal
view
2020-07-13 08:54:08 +00:00
returns (
uint256,
uint256,
uint256
)
{
uint256 currentBalance = balanceOf(user);
uint256 balanceIncrease = 0;
uint256 previousBalance = 0;
if (currentBalance != 0) {
previousBalance = super.balanceOf(user);
//calculate the accrued interest since the last accumulation
balanceIncrease = currentBalance.sub(previousBalance);
}
2020-08-18 19:19:11 +00:00
return (previousBalance, currentBalance, balanceIncrease);
2020-07-13 08:54:08 +00:00
}
2020-08-18 19:19:11 +00:00
/**
* @dev accumulates the accrued interest of the user to the principal balance
* @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
* and the new user index
**/
function _cumulateBalance(address user)
2020-08-11 07:36:46 +00:00
internal
returns (
uint256,
uint256,
uint256,
uint256
)
{
2020-08-18 19:19:11 +00:00
(
uint256 previousBalance,
uint256 currentBalance,
uint256 balanceIncrease
) = _calculateBalanceIncrease(user);
_mint(user, balanceIncrease);
//updates the user index
uint256 index = _userIndexes[user] = _pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS);
2020-08-11 07:36:46 +00:00
return (previousBalance, currentBalance, balanceIncrease, index);
2020-08-18 19:19:11 +00:00
}
2020-08-11 07:36:46 +00:00
2020-07-13 08:54:08 +00:00
/**
* @dev updates the redirected balance of the user. If the user is not redirecting his
* interest, nothing is executed.
* @param user the address of the user for which the interest is being accumulated
* @param balanceToAdd the amount to add to the redirected balance
* @param balanceToRemove the amount to remove from the redirected balance
2020-07-13 08:54:08 +00:00
**/
function _updateRedirectedBalanceOfRedirectionAddress(
address user,
uint256 balanceToAdd,
uint256 balanceToRemove
2020-07-13 08:54:08 +00:00
) internal {
address redirectionAddress = _interestRedirectionAddresses[user];
2020-07-13 08:54:08 +00:00
//if there isn't any redirection, nothing to be done
if (redirectionAddress == address(0)) {
return;
}
2020-07-13 08:54:08 +00:00
//compound balances of the redirected address
(, , uint256 balanceIncrease, uint256 index) = _cumulateBalance(redirectionAddress);
2020-07-13 08:54:08 +00:00
//updating the redirected balance
_redirectedBalances[redirectionAddress] = _redirectedBalances[redirectionAddress]
.add(balanceToAdd)
.sub(balanceToRemove);
2020-07-13 08:54:08 +00:00
//if the interest of redirectionAddress is also being redirected, we need to update
//the redirected balance of the redirection target by adding the balance increase
address targetOfRedirectionAddress = _interestRedirectionAddresses[redirectionAddress];
// if the redirection address is also redirecting the interest, we accumulate his balance
// and update his chain of redirection
2020-07-13 08:54:08 +00:00
if (targetOfRedirectionAddress != address(0)) {
_updateRedirectedBalanceOfRedirectionAddress(redirectionAddress, balanceIncrease, 0);
}
2020-07-13 08:54:08 +00:00
emit RedirectedBalanceUpdated(
redirectionAddress,
balanceIncrease,
index,
balanceToAdd,
balanceToRemove
2020-07-13 08:54:08 +00:00
);
}
/**
* @dev calculate the interest accrued by user on a specific balance
* @param user the address of the user for which the interest is being accumulated
* @param balance the balance on which the interest is calculated
2020-07-13 08:54:08 +00:00
* @return the interest rate accrued
**/
function _calculateCumulatedBalance(address user, uint256 balance)
2020-07-13 08:54:08 +00:00
internal
view
returns (uint256)
{
return
balance
2020-07-13 08:54:08 +00:00
.wadToRay()
.rayMul(_pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS))
.rayDiv(_userIndexes[user])
2020-07-13 08:54:08 +00:00
.rayToWad();
}
/**
* @dev executes the transfer of aTokens, invoked by both _transfer() and
* transferOnLiquidation()
* @param from the address from which transfer the aTokens
* @param to the destination address
* @param value the amount to transfer
2020-07-13 08:54:08 +00:00
**/
function _executeTransfer(
address from,
address to,
uint256 value
2020-07-13 08:54:08 +00:00
) internal {
require(value > 0, Errors.TRANSFER_AMOUNT_NOT_GT_0);
2020-07-13 08:54:08 +00:00
//cumulate the balance of the sender
2020-09-02 13:48:38 +00:00
(, uint256 fromBalance, uint256 fromBalanceIncrease, uint256 fromIndex) = _cumulateBalance(
from
);
2020-07-13 08:54:08 +00:00
//cumulate the balance of the receiver
(, , uint256 toBalanceIncrease, uint256 toIndex) = _cumulateBalance(to);
2020-07-13 08:54:08 +00:00
//if the sender is redirecting his interest towards someone else,
//adds to the redirected balance the accrued interest and removes the amount
//being transferred
_updateRedirectedBalanceOfRedirectionAddress(from, fromBalanceIncrease, value);
2020-07-13 08:54:08 +00:00
//if the receiver is redirecting his interest towards someone else,
//adds to the redirected balance the accrued interest and the amount
//being transferred
_updateRedirectedBalanceOfRedirectionAddress(to, toBalanceIncrease.add(value), 0);
2020-07-13 08:54:08 +00:00
//performs the transfer
super._transfer(from, to, value);
2020-07-13 08:54:08 +00:00
bool fromIndexReset = false;
//reset the user data if the remaining balance is 0
if (fromBalance.sub(value) == 0 && from != to) {
fromIndexReset = _resetDataOnZeroBalance(from);
}
2020-07-13 08:54:08 +00:00
emit BalanceTransfer(
from,
to,
value,
2020-07-13 08:54:08 +00:00
fromBalanceIncrease,
toBalanceIncrease,
fromIndexReset ? 0 : fromIndex,
toIndex
);
}
/**
* @dev executes the redirection of the interest from one address to another.
* immediately after redirection, the destination address will start to accrue interest.
* @param from the address from which transfer the aTokens
* @param to the destination address
2020-07-13 08:54:08 +00:00
**/
function _redirectInterestStream(address from, address to) internal {
address currentRedirectionAddress = _interestRedirectionAddresses[from];
2020-07-13 08:54:08 +00:00
require(to != currentRedirectionAddress, Errors.INTEREST_ALREADY_REDIRECTED);
2020-07-13 08:54:08 +00:00
//accumulates the accrued interest to the principal
(
uint256 previousPrincipalBalance,
uint256 fromBalance,
uint256 balanceIncrease,
uint256 fromIndex
) = _cumulateBalance(from);
2020-07-13 08:54:08 +00:00
2020-09-03 08:33:15 +00:00
require(fromBalance > 0, Errors.NO_VALID_BALANCE_FOR_REDIRECTION);
2020-07-13 08:54:08 +00:00
//if the user is already redirecting the interest to someone, before changing
//the redirection address we substract the redirected balance of the previous
//recipient
if (currentRedirectionAddress != address(0)) {
_updateRedirectedBalanceOfRedirectionAddress(from, 0, previousPrincipalBalance);
}
2020-07-13 08:54:08 +00:00
//if the user is redirecting the interest back to himself,
//we simply set to 0 the interest redirection address
if (to == from) {
_interestRedirectionAddresses[from] = address(0);
emit InterestStreamRedirected(from, address(0), fromBalance, balanceIncrease, fromIndex);
2020-07-13 08:54:08 +00:00
return;
}
2020-07-13 08:54:08 +00:00
//first set the redirection address to the new recipient
_interestRedirectionAddresses[from] = to;
2020-07-13 08:54:08 +00:00
//adds the user balance to the redirected balance of the destination
_updateRedirectedBalanceOfRedirectionAddress(from, fromBalance, 0);
2020-07-13 08:54:08 +00:00
emit InterestStreamRedirected(from, to, fromBalance, balanceIncrease, fromIndex);
2020-07-13 08:54:08 +00:00
}
/**
* @dev function to reset the interest stream redirection and the user index, if the
* user has no balance left.
* @param user the address of the user
2020-07-13 08:54:08 +00:00
* @return true if the user index has also been reset, false otherwise. useful to emit the proper user index value
**/
function _resetDataOnZeroBalance(address user) internal returns (bool) {
2020-07-13 08:54:08 +00:00
//if the user has 0 principal balance, the interest stream redirection gets reset
_interestRedirectionAddresses[user] = address(0);
2020-07-13 08:54:08 +00:00
//emits a InterestStreamRedirected event to notify that the redirection has been reset
emit InterestStreamRedirected(user, address(0), 0, 0, 0);
2020-07-13 08:54:08 +00:00
//if the redirected balance is also 0, we clear up the user index
if (_redirectedBalances[user] == 0) {
_userIndexes[user] = 0;
2020-07-13 08:54:08 +00:00
return true;
} else {
return false;
}
2020-07-13 08:54:08 +00:00
}
2020-07-10 17:16:04 +00:00
2020-07-13 13:19:47 +00:00
/**
* @dev transfers the underlying asset to the target. Used by the lendingpool to transfer
* assets in borrow(), redeem() and flashLoan()
* @param target the target of the transfer
* @param amount the amount to transfer
2020-07-13 13:19:47 +00:00
* @return the amount transferred
**/
function transferUnderlyingTo(address target, uint256 amount)
2020-07-10 17:16:04 +00:00
external
override
2020-07-10 17:16:04 +00:00
onlyLendingPool
returns (uint256)
{
ERC20(UNDERLYING_ASSET_ADDRESS).safeTransfer(target, amount);
return amount;
2020-07-10 17:16:04 +00:00
}
2020-07-13 13:19:47 +00:00
/**
2020-08-12 17:36:58 +00:00
* @dev aTokens should not receive ETH
2020-07-13 13:19:47 +00:00
**/
receive() external payable {
2020-08-12 17:36:58 +00:00
revert();
2020-07-10 17:16:04 +00:00
}
}