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

352 lines
11 KiB
Solidity
Raw Normal View History

// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
2020-09-15 15:08:28 +00:00
import {IncentivizedERC20} from './IncentivizedERC20.sol';
import {ILendingPool} from '../interfaces/ILendingPool.sol';
2020-08-20 07:51:21 +00:00
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
2020-10-15 13:16:05 +00:00
import {VersionedInitializable} from '../libraries/aave-upgradeability/VersionedInitializable.sol';
import {IAToken} from './interfaces/IAToken.sol';
2020-10-15 13:25:27 +00:00
import {IERC20} from '../dependencies/openzeppelin/contracts/IERC20.sol';
import {SafeERC20} from '../dependencies/openzeppelin/contracts/SafeERC20.sol';
2020-08-07 16:23:52 +00:00
/**
* @title Aave ERC20 AToken
*
2020-11-02 08:17:48 +00:00
* @dev Implementation of the interest bearing token for the Aave protocol.
* @author Aave
*/
2020-09-15 15:08:28 +00:00
contract AToken is VersionedInitializable, IncentivizedERC20, IAToken {
2020-07-13 08:54:08 +00:00
using WadRayMath for uint256;
2020-09-21 13:58:19 +00:00
using SafeERC20 for IERC20;
2020-07-13 08:54:08 +00:00
2020-09-14 17:59:00 +00:00
bytes public constant EIP712_REVISION = bytes('1');
bytes32 internal constant EIP712_DOMAIN = keccak256(
'EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'
);
bytes32 public constant PERMIT_TYPEHASH = keccak256(
'Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)'
);
2020-07-13 08:54:08 +00:00
2020-09-21 13:58:19 +00:00
uint256 public constant UINT_MAX_VALUE = uint256(-1);
uint256 public constant ATOKEN_REVISION = 0x1;
address public immutable UNDERLYING_ASSET_ADDRESS;
address public immutable RESERVE_TREASURY_ADDRESS;
ILendingPool public immutable POOL;
2020-09-21 13:58:19 +00:00
/// @dev owner => next valid nonce to submit with permit()
mapping(address => uint256) public _nonces;
2020-09-21 15:41:38 +00:00
2020-09-21 13:58:19 +00:00
bytes32 public DOMAIN_SEPARATOR;
2020-09-21 15:41:38 +00:00
2020-07-13 08:54:08 +00:00
modifier onlyLendingPool {
2020-10-30 12:40:06 +00:00
require(_msgSender() == address(POOL), Errors.AT_CALLER_MUST_BE_LENDING_POOL);
2020-07-13 08:54:08 +00:00
_;
}
constructor(
ILendingPool pool,
address underlyingAssetAddress,
2020-09-10 10:51:52 +00:00
address reserveTreasuryAddress,
string memory tokenName,
2020-09-15 13:53:20 +00:00
string memory tokenSymbol,
address incentivesController
2020-09-15 15:08:28 +00:00
) public IncentivizedERC20(tokenName, tokenSymbol, 18, incentivesController) {
2020-09-12 11:18:17 +00:00
POOL = pool;
UNDERLYING_ASSET_ADDRESS = underlyingAssetAddress;
2020-09-10 10:51:52 +00:00
RESERVE_TREASURY_ADDRESS = reserveTreasuryAddress;
}
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 {
2020-09-14 13:57:11 +00:00
uint256 chainId;
//solium-disable-next-line
assembly {
2020-09-14 17:59:00 +00:00
chainId := chainid()
2020-09-14 13:57:11 +00:00
}
2020-09-14 17:59:00 +00:00
DOMAIN_SEPARATOR = keccak256(
abi.encode(
2020-09-14 13:57:11 +00:00
EIP712_DOMAIN,
keccak256(bytes(tokenName)),
keccak256(EIP712_REVISION),
chainId,
address(this)
2020-09-14 17:59:00 +00:00
)
);
2020-09-14 13:57:11 +00:00
_setName(tokenName);
_setSymbol(tokenSymbol);
_setDecimals(underlyingAssetDecimals);
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,
2020-09-07 15:55:47 +00:00
address receiverOfUnderlying,
2020-09-12 11:18:17 +00:00
uint256 amount,
uint256 index
) external override onlyLendingPool {
2020-09-30 15:40:47 +00:00
uint256 amountScaled = amount.rayDiv(index);
require(amountScaled != 0, Errors.AT_INVALID_BURN_AMOUNT);
2020-09-30 15:40:47 +00:00
_burn(user, amountScaled);
//transfers the underlying to the target
2020-09-21 13:58:19 +00:00
IERC20(UNDERLYING_ASSET_ADDRESS).safeTransfer(receiverOfUnderlying, amount);
2020-09-07 15:55:47 +00:00
2020-09-15 12:36:02 +00:00
//transfer event to track balances
emit Transfer(user, address(0), amount);
2020-11-01 08:11:53 +00:00
emit Burn(user, receiverOfUnderlying, amount, index);
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
* @param index the the last index of the reserve
2020-10-15 12:13:46 +00:00
* @return true if the the previous balance of the user is 0
2020-07-13 08:54:08 +00:00
*/
function mint(
address user,
uint256 amount,
uint256 index
2020-10-15 12:13:46 +00:00
) external override onlyLendingPool returns (bool) {
uint256 previousBalance = super.balanceOf(user);
2020-09-30 15:40:47 +00:00
uint256 amountScaled = amount.rayDiv(index);
require(amountScaled != 0, Errors.AT_INVALID_MINT_AMOUNT);
2020-09-30 15:40:47 +00:00
_mint(user, amountScaled);
2020-07-13 08:54:08 +00:00
2020-09-15 12:36:02 +00:00
//transfer event to track balances
emit Transfer(address(0), user, amount);
2020-09-07 15:55:47 +00:00
emit Mint(user, amount, index);
2020-10-15 12:13:46 +00:00
return previousBalance == 0;
2020-07-13 08:54:08 +00:00
}
/**
* @dev mints aTokens to reserve treasury
* only lending pools can call this function
* @param amount the amount of tokens to mint to the treasury
* @param index the the last index of the reserve
*/
2020-09-14 13:09:16 +00:00
function mintToTreasury(uint256 amount, uint256 index) external override onlyLendingPool {
2020-10-12 13:19:27 +00:00
if (amount == 0) {
2020-10-05 11:11:53 +00:00
return;
}
//compared to the normal mint, we don't check for rounding errors.
//the amount to mint can easily be very small since is a fraction of the interest
//accrued. in that case, the treasury will experience a (very small) loss, but it
//wont cause potentially valid transactions to fail.
_mint(RESERVE_TREASURY_ADDRESS, amount.rayDiv(index));
2020-09-21 15:58:02 +00:00
2020-09-21 15:41:38 +00:00
//transfer event to track balances
emit Transfer(address(0), RESERVE_TREASURY_ADDRESS, amount);
emit Mint(RESERVE_TREASURY_ADDRESS, amount, index);
2020-09-10 10:51:52 +00:00
}
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
2020-09-07 15:55:47 +00:00
_transfer(from, to, value, false);
2020-07-13 08:54:08 +00:00
}
/**
* @dev calculates the balance of the user, which is the
2020-09-15 15:08:28 +00:00
* principal balance + interest generated by the principal 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
**/
2020-09-15 15:08:28 +00:00
function balanceOf(address user)
public
override(IncentivizedERC20, IERC20)
view
returns (uint256)
{
2020-09-12 11:18:17 +00:00
return super.balanceOf(user).rayMul(POOL.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS));
2020-07-13 08:54:08 +00:00
}
/**
2020-09-07 15:55:47 +00:00
* @dev returns the scaled balance of the user. The scaled balance is the sum of all the
* updated stored balance divided the reserve index at the moment of the update
* @param user the address of the user
2020-09-07 15:55:47 +00:00
* @return the scaled balance of the user
2020-07-13 08:54:08 +00:00
**/
2020-09-09 17:43:41 +00:00
function scaledBalanceOf(address user) external override view returns (uint256) {
return super.balanceOf(user);
2020-07-13 08:54:08 +00:00
}
2020-09-15 13:53:20 +00:00
/**
* @dev returns the principal balance of the user and principal total supply.
* @param user the address of the user
* @return the principal balance of the user
* @return the principal total supply
**/
function getScaledUserBalanceAndSupply(address user)
external
override
view
returns (uint256, uint256)
{
return (super.balanceOf(user), super.totalSupply());
}
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
**/
2020-09-15 15:08:28 +00:00
function totalSupply() public override(IncentivizedERC20, IERC20) view returns (uint256) {
2020-09-07 15:55:47 +00:00
uint256 currentSupplyScaled = super.totalSupply();
2020-07-13 08:54:08 +00:00
2020-09-07 15:55:47 +00:00
if (currentSupplyScaled == 0) {
2020-07-13 08:54:08 +00:00
return 0;
}
2020-09-15 15:08:28 +00:00
return currentSupplyScaled.rayMul(POOL.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS));
2020-07-13 08:54:08 +00:00
}
2020-09-21 15:41:38 +00:00
/**
* @dev Returns the scaled total supply of the variable debt token. Represents sum(borrows/index)
* @return the scaled total supply
**/
function scaledTotalSupply() public virtual override view returns (uint256) {
return super.totalSupply();
}
2020-07-13 08:54:08 +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
* @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
2020-07-13 08:54:08 +00:00
returns (uint256)
{
2020-09-21 13:58:19 +00:00
IERC20(UNDERLYING_ASSET_ADDRESS).safeTransfer(target, amount);
return amount;
2020-07-13 08:54:08 +00:00
}
2020-09-14 13:57:11 +00:00
/**
2020-09-14 17:59:00 +00:00
* @dev implements the permit function as for https://github.com/ethereum/EIPs/blob/8a34d644aacf0f9f8f00815307fd7dd5da07655f/EIPS/eip-2612.md
* @param owner the owner of the funds
* @param spender the spender
* @param value the amount
* @param deadline the deadline timestamp, type(uint256).max for max deadline
* @param v signature param
* @param s signature param
* @param r signature param
*/
2020-09-14 13:57:11 +00:00
function permit(
2020-09-14 17:59:00 +00:00
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
2020-09-14 13:57:11 +00:00
) external {
2020-09-14 17:59:00 +00:00
require(owner != address(0), 'INVALID_OWNER');
//solium-disable-next-line
require(block.timestamp <= deadline, 'INVALID_EXPIRATION');
uint256 currentValidNonce = _nonces[owner];
bytes32 digest = keccak256(
abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR,
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, currentValidNonce, deadline))
)
);
require(owner == ecrecover(digest, v, r, s), 'INVALID_SIGNATURE');
_nonces[owner] = currentValidNonce.add(1);
_approve(owner, spender, value);
2020-09-14 13:57:11 +00:00
}
2020-09-21 15:58:02 +00:00
/**
* @dev transfers the aTokens between two users. Validates the transfer
* (ie checks for valid HF after the transfer) if required
* @param from the source address
* @param to the destination address
* @param amount the amount to transfer
* @param validate true if the transfer needs to be validated
**/
2020-09-08 11:45:24 +00:00
function _transfer(
address from,
address to,
2020-09-08 11:45:24 +00:00
uint256 amount,
bool validate
) internal {
2020-09-12 11:18:17 +00:00
uint256 index = POOL.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS);
2020-10-29 10:57:43 +00:00
uint256 fromBalanceBefore = super.balanceOf(from).rayMul(index);
uint256 toBalanceBefore = super.balanceOf(to).rayMul(index);
2020-09-21 15:58:02 +00:00
super._transfer(from, to, amount.rayDiv(index));
2020-07-13 08:54:08 +00:00
2020-10-29 10:57:43 +00:00
if (validate) {
POOL.finalizeTransfer(
UNDERLYING_ASSET_ADDRESS,
from,
to,
amount,
fromBalanceBefore,
toBalanceBefore
);
}
2020-09-08 11:45:24 +00:00
emit BalanceTransfer(from, to, amount, index);
2020-07-13 08:54:08 +00:00
}
2020-09-21 15:58:02 +00:00
/**
* @dev overrides the parent _transfer to force validated transfer() and transferFrom()
* @param from the source address
* @param to the destination address
* @param amount the amount to transfer
**/
2020-09-09 08:03:19 +00:00
function _transfer(
address from,
address to,
uint256 amount
) internal override {
_transfer(from, to, amount, true);
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
}
}