2020-05-29 16:45:37 +00:00
|
|
|
// 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';
|
2020-09-02 14:34:15 +00:00
|
|
|
import {Errors} from '../libraries/helpers/Errors.sol';
|
2020-08-07 17:29:13 +00:00
|
|
|
import {
|
|
|
|
VersionedInitializable
|
|
|
|
} from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol';
|
2020-09-04 14:27:35 +00:00
|
|
|
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
|
|
|
|
2020-05-29 16:45:37 +00:00
|
|
|
/**
|
|
|
|
* @title Aave ERC20 AToken
|
|
|
|
*
|
|
|
|
* @dev Implementation of the interest bearing token for the DLP protocol.
|
|
|
|
* @author Aave
|
|
|
|
*/
|
2020-08-20 12:32:20 +00:00
|
|
|
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);
|
2020-08-21 10:38:08 +00:00
|
|
|
address public immutable UNDERLYING_ASSET_ADDRESS;
|
2020-08-07 16:23:52 +00:00
|
|
|
uint256 public constant ATOKEN_REVISION = 0x1;
|
2020-09-14 09:13:53 +00:00
|
|
|
LendingPool public immutable POOL;
|
2020-08-07 16:23:52 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
modifier onlyLendingPool {
|
2020-09-12 11:18:17 +00:00
|
|
|
require(msg.sender == address(POOL), Errors.CALLER_MUST_BE_LENDING_POOL);
|
2020-07-13 08:54:08 +00:00
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2020-08-08 17:21:23 +00:00
|
|
|
constructor(
|
2020-08-21 10:38:08 +00:00
|
|
|
LendingPool pool,
|
|
|
|
address underlyingAssetAddress,
|
|
|
|
string memory tokenName,
|
|
|
|
string memory tokenSymbol
|
2020-09-04 14:27:35 +00:00
|
|
|
) public ERC20(tokenName, tokenSymbol, 18) {
|
2020-09-12 11:18:17 +00:00
|
|
|
POOL = pool;
|
2020-08-21 10:38:08 +00:00
|
|
|
UNDERLYING_ASSET_ADDRESS = underlyingAssetAddress;
|
2020-08-08 17:21:23 +00:00
|
|
|
}
|
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(
|
2020-08-21 10:38:08 +00:00
|
|
|
uint8 underlyingAssetDecimals,
|
|
|
|
string calldata tokenName,
|
|
|
|
string calldata tokenSymbol
|
2020-08-10 18:20:08 +00:00
|
|
|
) external virtual initializer {
|
2020-09-04 14:27:35 +00:00
|
|
|
_setName(tokenName);
|
|
|
|
_setSymbol(tokenSymbol);
|
|
|
|
_setDecimals(underlyingAssetDecimals);
|
2020-07-13 08:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-19 10:56:39 +00:00
|
|
|
* @dev burns the aTokens and sends the equivalent amount of underlying to the target.
|
|
|
|
* only lending pools can call this function
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param amount the amount being burned
|
2020-07-13 08:54:08 +00:00
|
|
|
**/
|
2020-08-19 10:56:39 +00:00
|
|
|
function burn(
|
2020-08-21 10:38:08 +00:00
|
|
|
address user,
|
2020-09-07 15:55:47 +00:00
|
|
|
address receiverOfUnderlying,
|
2020-09-12 11:18:17 +00:00
|
|
|
uint256 amount,
|
|
|
|
uint256 index
|
2020-08-20 12:32:20 +00:00
|
|
|
) external override onlyLendingPool {
|
2020-09-07 15:55:47 +00:00
|
|
|
uint256 currentBalance = balanceOf(user);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-08 14:14:32 +00:00
|
|
|
require(amount <= currentBalance, Errors.INVALID_ATOKEN_BALANCE);
|
2020-09-07 15:55:47 +00:00
|
|
|
|
|
|
|
uint256 scaledAmount = amount.rayDiv(index);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-09-07 15:55:47 +00:00
|
|
|
_burn(user, scaledAmount);
|
|
|
|
|
2020-08-19 10:56:39 +00:00
|
|
|
//transfers the underlying to the target
|
2020-09-07 15:55:47 +00:00
|
|
|
ERC20(UNDERLYING_ASSET_ADDRESS).safeTransfer(receiverOfUnderlying, amount);
|
|
|
|
|
|
|
|
emit Burn(msg.sender, receiverOfUnderlying, amount, index);
|
2020-07-13 08:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-21 10:38:08 +00:00
|
|
|
* @dev mints aTokens to user
|
2020-07-13 08:54:08 +00:00
|
|
|
* only lending pools can call this function
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user the address receiving the minted tokens
|
|
|
|
* @param amount the amount of tokens to mint
|
2020-07-13 08:54:08 +00:00
|
|
|
*/
|
2020-09-14 11:03:39 +00:00
|
|
|
function mint(
|
|
|
|
address user,
|
|
|
|
uint256 amount,
|
|
|
|
uint256 index
|
|
|
|
) external override onlyLendingPool {
|
2020-09-07 15:55:47 +00:00
|
|
|
uint256 scaledAmount = amount.rayDiv(index);
|
2020-09-14 11:03:39 +00:00
|
|
|
|
2020-09-07 15:55:47 +00:00
|
|
|
//mint an equivalent amount of tokens to cover the new deposit
|
2020-09-14 11:03:39 +00:00
|
|
|
_mint(user, scaledAmount);
|
2020-08-18 19:19:11 +00:00
|
|
|
|
2020-09-07 15:55:47 +00:00
|
|
|
emit Mint(user, amount, 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
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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(
|
2020-08-21 10:38:08 +00:00
|
|
|
address from,
|
|
|
|
address to,
|
|
|
|
uint256 value
|
2020-08-20 12:32:20 +00:00
|
|
|
) 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-14 11:03:39 +00:00
|
|
|
* principal balance + interest generated by the principal balance
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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-08-21 10:38:08 +00:00
|
|
|
function balanceOf(address user) public override(ERC20, 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
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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) {
|
2020-08-21 10:38:08 +00:00
|
|
|
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
|
|
|
|
**/
|
2020-08-20 12:32:20 +00:00
|
|
|
function totalSupply() public override(ERC20, 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-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 11:03:39 +00:00
|
|
|
return currentSupplyScaled.rayMul(POOL.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS));
|
2020-07-13 08:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Used to validate transfers before actually executing them.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
function isTransferAllowed(address user, uint256 amount) public override view returns (bool) {
|
2020-09-14 11:03:39 +00:00
|
|
|
return !POOL.isPaused() && POOL.balanceDecreaseAllowed(UNDERLYING_ASSET_ADDRESS, user, amount);
|
2020-07-13 08:54:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 13:19:47 +00:00
|
|
|
/**
|
2020-09-14 11:03:39 +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
|
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
function transferUnderlyingTo(address target, uint256 amount)
|
2020-07-10 17:16:04 +00:00
|
|
|
external
|
2020-08-20 12:32:20 +00:00
|
|
|
override
|
2020-07-10 17:16:04 +00:00
|
|
|
onlyLendingPool
|
|
|
|
returns (uint256)
|
|
|
|
{
|
2020-08-21 10:38:08 +00:00
|
|
|
ERC20(UNDERLYING_ASSET_ADDRESS).safeTransfer(target, amount);
|
|
|
|
return amount;
|
2020-07-10 17:16:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 11:45:24 +00:00
|
|
|
function _transfer(
|
|
|
|
address from,
|
|
|
|
address to,
|
|
|
|
uint256 amount,
|
|
|
|
bool validate
|
2020-09-14 11:03:39 +00:00
|
|
|
) internal {
|
|
|
|
if (validate) {
|
2020-09-08 11:45:24 +00:00
|
|
|
require(isTransferAllowed(from, amount), Errors.TRANSFER_NOT_ALLOWED);
|
|
|
|
}
|
|
|
|
|
2020-09-12 11:18:17 +00:00
|
|
|
uint256 index = POOL.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS);
|
2020-09-08 11:45:24 +00:00
|
|
|
|
|
|
|
uint256 scaledAmount = amount.rayDiv(index);
|
|
|
|
|
|
|
|
super._transfer(from, to, scaledAmount);
|
|
|
|
|
|
|
|
emit BalanceTransfer(from, to, amount, index);
|
|
|
|
}
|
2020-09-14 11:03:39 +00:00
|
|
|
|
2020-09-09 08:03:19 +00:00
|
|
|
function _transfer(
|
|
|
|
address from,
|
|
|
|
address to,
|
|
|
|
uint256 amount
|
2020-09-14 11:03:39 +00:00
|
|
|
) internal override {
|
|
|
|
_transfer(from, to, amount, true);
|
2020-09-09 08:03:19 +00:00
|
|
|
}
|
2020-09-14 11:03:39 +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
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|