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-07-13 08:54:08 +00:00
|
|
|
|
2020-08-21 10:38: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
|
|
|
|
2020-08-21 10:38: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 {
|
2020-09-02 14:34:15 +00:00
|
|
|
require(msg.sender == address(_pool), Errors.CALLER_MUST_BE_LENDING_POOL);
|
2020-07-13 08:54:08 +00:00
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
modifier whenTransferAllowed(address from, uint256 amount) {
|
2020-09-02 15:54:34 +00:00
|
|
|
require(isTransferAllowed(from, amount), Errors.TRANSFER_NOT_ALLOWED);
|
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-08-21 10:38:08 +00:00
|
|
|
_pool = pool;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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(
|
2020-08-21 10:38:08 +00:00
|
|
|
address from,
|
|
|
|
address to,
|
|
|
|
uint256 amount
|
|
|
|
) internal override whenTransferAllowed(from, amount) {
|
2020-08-21 16:18:12 +00:00
|
|
|
_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.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param to the address to which the interest will be redirected
|
2020-07-13 08:54:08 +00:00
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
function redirectInterestStream(address to) external override {
|
2020-08-21 16:18:12 +00:00
|
|
|
_redirectInterestStream(msg.sender, to);
|
2020-07-13 08:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-21 10:38: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.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
function redirectInterestStreamOf(address from, address to) external override {
|
2020-07-13 08:54:08 +00:00
|
|
|
require(
|
2020-08-21 10:38:08 +00:00
|
|
|
msg.sender == _interestRedirectionAllowances[from],
|
2020-09-03 08:33:15 +00:00
|
|
|
Errors.INTEREST_REDIRECTION_NOT_ALLOWED
|
2020-05-29 16:45:37 +00:00
|
|
|
);
|
2020-08-21 16:18:12 +00:00
|
|
|
_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.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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.
|
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
function allowInterestRedirectionTo(address to) external override {
|
2020-09-02 14:34:15 +00:00
|
|
|
require(to != msg.sender, Errors.CANNOT_GIVE_ALLOWANCE_TO_HIMSELF);
|
2020-08-21 10:38:08 +00:00
|
|
|
_interestRedirectionAllowances[msg.sender] = to;
|
|
|
|
emit InterestRedirectionAllowanceChanged(msg.sender, to);
|
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,
|
|
|
|
address underlyingTarget,
|
|
|
|
uint256 amount
|
2020-08-20 12:32:20 +00:00
|
|
|
) external override onlyLendingPool {
|
2020-07-13 08:54:08 +00:00
|
|
|
//cumulates the balance of the user
|
2020-08-21 16:18:12 +00:00
|
|
|
(, uint256 currentBalance, uint256 balanceIncrease) = _calculateBalanceIncrease(user);
|
2020-05-29 16:45:37 +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 removing the amount to redeem
|
2020-08-21 16:18:12 +00:00
|
|
|
_updateRedirectedBalanceOfRedirectionAddress(user, balanceIncrease, amount);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
if (balanceIncrease > amount) {
|
|
|
|
_mint(user, balanceIncrease.sub(amount));
|
2020-08-18 19:19:11 +00:00
|
|
|
} else {
|
2020-08-21 10:38:08 +00:00
|
|
|
_burn(user, amount.sub(balanceIncrease));
|
2020-08-11 10:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint256 userIndex = 0;
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
//reset the user data if the remaining balance is 0
|
2020-08-21 10:38:08 +00:00
|
|
|
if (currentBalance.sub(amount) == 0) {
|
2020-08-21 16:18:12 +00:00
|
|
|
_resetDataOnZeroBalance(user);
|
2020-08-11 10:26:15 +00:00
|
|
|
} else {
|
2020-08-18 19:19:11 +00:00
|
|
|
//updates the user index
|
2020-08-21 10:38:08 +00:00
|
|
|
userIndex = _userIndexes[user] = _pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS);
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 10:56:39 +00:00
|
|
|
//transfers the underlying to the target
|
2020-08-21 10:38:08 +00:00
|
|
|
ERC20(UNDERLYING_ASSET_ADDRESS).safeTransfer(underlyingTarget, amount);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
emit Burn(msg.sender, underlyingTarget, amount, balanceIncrease, userIndex);
|
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-08-21 10:38: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
|
2020-08-21 16:18:12 +00:00
|
|
|
(, , uint256 balanceIncrease) = _calculateBalanceIncrease(user);
|
2020-07-13 08:54:08 +00:00
|
|
|
|
2020-08-11 10:26:15 +00:00
|
|
|
//updates the user index
|
2020-08-21 10:38:08 +00:00
|
|
|
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
|
2020-08-21 16:18:12 +00:00
|
|
|
_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
|
2020-08-21 10:38:08 +00:00
|
|
|
_mint(user, amount.add(balanceIncrease));
|
2020-07-13 08:54:08 +00:00
|
|
|
|
2020-08-21 10:38: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
|
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-08-21 16:18:12 +00:00
|
|
|
_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
|
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-07-13 08:54:08 +00:00
|
|
|
//current principal balance of the user
|
2020-08-21 10:38:08 +00:00
|
|
|
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;
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|
2020-08-21 10:38:08 +00:00
|
|
|
//if the user is not redirecting the interest to anybody, accrues
|
2020-07-13 08:54:08 +00:00
|
|
|
//the interest for himself
|
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
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
|
2020-08-21 16:18:12 +00:00
|
|
|
_calculateCumulatedBalance(user, currentPrincipalBalance.add(redirectedBalance)).sub(
|
2020-08-21 10:38:08 +00:00
|
|
|
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(
|
2020-08-21 16:18:12 +00:00
|
|
|
_calculateCumulatedBalance(user, redirectedBalance).sub(redirectedBalance)
|
2020-05-29 16:45:37 +00:00
|
|
|
);
|
|
|
|
}
|
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.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user the address of the user
|
2020-07-13 08:54:08 +00:00
|
|
|
* @return the principal balance of the user
|
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
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
|
|
|
|
**/
|
2020-08-20 12:32:20 +00:00
|
|
|
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-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
return
|
|
|
|
currentSupplyPrincipal
|
|
|
|
.wadToRay()
|
2020-08-21 10:38:08 +00:00
|
|
|
.rayMul(_pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS))
|
2020-07-13 08:54:08 +00:00
|
|
|
.rayToWad();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
|
|
|
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
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user address of the user
|
2020-07-13 08:54:08 +00:00
|
|
|
* @return the last user index
|
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
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
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user address of the user
|
2020-07-13 08:54:08 +00:00
|
|
|
* @return 0 if there is no redirection, an address otherwise
|
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
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.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user address of the user
|
2020-07-13 08:54:08 +00:00
|
|
|
* @return the total redirected balance
|
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
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
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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
|
|
|
|
**/
|
2020-08-21 16:18:12 +00:00
|
|
|
function _calculateBalanceIncrease(address user)
|
2020-07-13 08:54:08 +00:00
|
|
|
internal
|
2020-08-20 12:32:20 +00:00
|
|
|
view
|
2020-07-13 08:54:08 +00:00
|
|
|
returns (
|
|
|
|
uint256,
|
|
|
|
uint256,
|
|
|
|
uint256
|
|
|
|
)
|
|
|
|
{
|
2020-08-21 10:38:08 +00:00
|
|
|
uint256 currentBalance = balanceOf(user);
|
2020-08-08 17:21:23 +00:00
|
|
|
uint256 balanceIncrease = 0;
|
|
|
|
uint256 previousBalance = 0;
|
|
|
|
|
2020-08-11 10:26:15 +00:00
|
|
|
if (currentBalance != 0) {
|
2020-08-21 10:38:08 +00:00
|
|
|
previousBalance = super.balanceOf(user);
|
2020-08-08 17:21:23 +00:00
|
|
|
//calculate the accrued interest since the last accumulation
|
2020-08-11 10:26:15 +00:00
|
|
|
balanceIncrease = currentBalance.sub(previousBalance);
|
2020-08-08 17:21:23 +00:00
|
|
|
}
|
2020-08-18 19:19:11 +00:00
|
|
|
|
2020-08-11 10:26:15 +00:00
|
|
|
return (previousBalance, currentBalance, balanceIncrease);
|
2020-07-13 08:54:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:19:11 +00:00
|
|
|
/**
|
2020-08-11 10:26:15 +00:00
|
|
|
* @dev accumulates the accrued interest of the user to the principal balance
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user the address of the user for which the interest is being accumulated
|
2020-08-11 10:26:15 +00:00
|
|
|
* @return the previous principal balance, the new principal balance, the balance increase
|
|
|
|
* and the new user index
|
|
|
|
**/
|
2020-08-21 16:18:12 +00:00
|
|
|
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
|
2020-08-21 16:18:12 +00:00
|
|
|
) = _calculateBalanceIncrease(user);
|
2020-08-11 10:26:15 +00:00
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
_mint(user, balanceIncrease);
|
2020-08-11 10:26:15 +00:00
|
|
|
|
|
|
|
//updates the user index
|
2020-08-21 10:38:08 +00:00
|
|
|
uint256 index = _userIndexes[user] = _pool.getReserveNormalizedIncome(UNDERLYING_ASSET_ADDRESS);
|
2020-08-11 07:36:46 +00:00
|
|
|
|
2020-08-11 10:26:15 +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.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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
|
|
|
**/
|
2020-08-21 16:18:12 +00:00
|
|
|
function _updateRedirectedBalanceOfRedirectionAddress(
|
2020-08-21 10:38:08 +00:00
|
|
|
address user,
|
|
|
|
uint256 balanceToAdd,
|
|
|
|
uint256 balanceToRemove
|
2020-07-13 08:54:08 +00:00
|
|
|
) internal {
|
2020-08-21 10:38:08 +00:00
|
|
|
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-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
//compound balances of the redirected address
|
2020-08-21 16:18:12 +00:00
|
|
|
(, , uint256 balanceIncrease, uint256 index) = _cumulateBalance(redirectionAddress);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
//updating the redirected balance
|
2020-08-21 10:38:08 +00:00
|
|
|
_redirectedBalances[redirectionAddress] = _redirectedBalances[redirectionAddress]
|
|
|
|
.add(balanceToAdd)
|
|
|
|
.sub(balanceToRemove);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
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
|
2020-08-21 10:38:08 +00:00
|
|
|
address targetOfRedirectionAddress = _interestRedirectionAddresses[redirectionAddress];
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-14 12:53:07 +00:00
|
|
|
// 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)) {
|
2020-08-21 16:18:12 +00:00
|
|
|
_updateRedirectedBalanceOfRedirectionAddress(redirectionAddress, balanceIncrease, 0);
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
emit RedirectedBalanceUpdated(
|
|
|
|
redirectionAddress,
|
|
|
|
balanceIncrease,
|
|
|
|
index,
|
2020-08-21 10:38:08 +00:00
|
|
|
balanceToAdd,
|
|
|
|
balanceToRemove
|
2020-07-13 08:54:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-21 10:38: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
|
|
|
|
**/
|
2020-08-21 16:18:12 +00:00
|
|
|
function _calculateCumulatedBalance(address user, uint256 balance)
|
2020-07-13 08:54:08 +00:00
|
|
|
internal
|
|
|
|
view
|
|
|
|
returns (uint256)
|
|
|
|
{
|
|
|
|
return
|
2020-08-21 10:38:08 +00:00
|
|
|
balance
|
2020-07-13 08:54:08 +00:00
|
|
|
.wadToRay()
|
2020-08-21 10:38:08 +00:00
|
|
|
.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()
|
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
|
|
|
**/
|
2020-08-21 16:18:12 +00:00
|
|
|
function _executeTransfer(
|
2020-08-21 10:38:08 +00:00
|
|
|
address from,
|
|
|
|
address to,
|
|
|
|
uint256 value
|
2020-07-13 08:54:08 +00:00
|
|
|
) internal {
|
2020-09-02 14:34:15 +00:00
|
|
|
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
|
2020-08-21 16:18:12 +00:00
|
|
|
(, , 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
|
2020-08-21 16:18:12 +00:00
|
|
|
_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
|
2020-08-21 16:18:12 +00:00
|
|
|
_updateRedirectedBalanceOfRedirectionAddress(to, toBalanceIncrease.add(value), 0);
|
2020-07-13 08:54:08 +00:00
|
|
|
|
|
|
|
//performs the transfer
|
2020-08-21 10:38:08 +00:00
|
|
|
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
|
2020-08-21 10:38:08 +00:00
|
|
|
if (fromBalance.sub(value) == 0 && from != to) {
|
2020-08-21 16:18:12 +00:00
|
|
|
fromIndexReset = _resetDataOnZeroBalance(from);
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
emit BalanceTransfer(
|
2020-08-21 10:38:08 +00:00
|
|
|
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.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param from the address from which transfer the aTokens
|
|
|
|
* @param to the destination address
|
2020-07-13 08:54:08 +00:00
|
|
|
**/
|
2020-08-21 16:18:12 +00:00
|
|
|
function _redirectInterestStream(address from, address to) internal {
|
2020-08-21 10:38:08 +00:00
|
|
|
address currentRedirectionAddress = _interestRedirectionAddresses[from];
|
2020-07-13 08:54:08 +00:00
|
|
|
|
2020-09-02 14:34:15 +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
|
2020-08-21 16:18:12 +00:00
|
|
|
) = _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)) {
|
2020-08-21 16:18:12 +00:00
|
|
|
_updateRedirectedBalanceOfRedirectionAddress(from, 0, previousPrincipalBalance);
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
2020-08-21 10:38:08 +00:00
|
|
|
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-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
//first set the redirection address to the new recipient
|
2020-08-21 10:38:08 +00:00
|
|
|
_interestRedirectionAddresses[from] = to;
|
2020-07-13 08:54:08 +00:00
|
|
|
|
|
|
|
//adds the user balance to the redirected balance of the destination
|
2020-08-21 16:18:12 +00:00
|
|
|
_updateRedirectedBalanceOfRedirectionAddress(from, fromBalance, 0);
|
2020-07-13 08:54:08 +00:00
|
|
|
|
2020-08-21 10:38: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.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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
|
|
|
|
**/
|
2020-08-21 16:18:12 +00:00
|
|
|
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
|
2020-08-21 10:38:08 +00:00
|
|
|
_interestRedirectionAddresses[user] = address(0);
|
2020-07-13 08:54:08 +00:00
|
|
|
|
|
|
|
//emits a InterestStreamRedirected event to notify that the redirection has been reset
|
2020-08-21 10:38:08 +00:00
|
|
|
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
|
2020-08-21 10:38:08 +00:00
|
|
|
if (_redirectedBalances[user] == 0) {
|
|
|
|
_userIndexes[user] = 0;
|
2020-07-13 08:54:08 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|
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()
|
2020-08-21 10:38:08 +00:00
|
|
|
* @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
|
|
|
|
**/
|
|
|
|
|
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-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
|
|
|
}
|