2020-08-20 12:32:20 +00:00
|
|
|
// SPDX-License-Identifier: agpl-3.0
|
|
|
|
pragma solidity ^0.6.8;
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-08-20 07:51:21 +00:00
|
|
|
import {Context} from '@openzeppelin/contracts/GSN/Context.sol';
|
|
|
|
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
|
|
|
|
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
|
2020-06-30 12:09:28 +00:00
|
|
|
import {DebtTokenBase} from './base/DebtTokenBase.sol';
|
2020-08-20 07:51:21 +00:00
|
|
|
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
|
2020-06-30 12:09:28 +00:00
|
|
|
import {IVariableDebtToken} from './interfaces/IVariableDebtToken.sol';
|
|
|
|
|
2020-07-09 09:59:49 +00:00
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @title interface IVariableDebtToken
|
|
|
|
* @author Aave
|
|
|
|
* @notice defines the basic interface for a variable debt token.
|
|
|
|
* @dev does not inherit from IERC20 to save in contract size
|
|
|
|
**/
|
2020-06-30 12:09:28 +00:00
|
|
|
contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
|
|
|
|
using SafeMath for uint256;
|
|
|
|
using WadRayMath for uint256;
|
|
|
|
|
2020-08-17 19:28:50 +00:00
|
|
|
uint256 public constant DEBT_TOKEN_REVISION = 0x1;
|
|
|
|
|
2020-06-30 12:09:28 +00:00
|
|
|
mapping(address => uint256) private userIndexes;
|
|
|
|
|
2020-07-09 09:59:49 +00:00
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @dev emitted when new variable debt is minted
|
|
|
|
* @param _user the user receiving the debt
|
|
|
|
* @param _amount the amount of debt being minted
|
|
|
|
* @param _previousBalance the previous balance of the user
|
|
|
|
* @param _currentBalance the current balance of the user
|
|
|
|
* @param _balanceIncrease the debt accumulated since the last action
|
|
|
|
* @param _index the index of the user
|
|
|
|
**/
|
2020-08-20 14:26:06 +00:00
|
|
|
event MintDebt(
|
2020-06-30 12:09:28 +00:00
|
|
|
address _user,
|
|
|
|
uint256 _amount,
|
|
|
|
uint256 _previousBalance,
|
|
|
|
uint256 _currentBalance,
|
|
|
|
uint256 _balanceIncrease,
|
|
|
|
uint256 _index
|
|
|
|
);
|
2020-07-09 09:59:49 +00:00
|
|
|
|
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @dev emitted when variable debt is burnt
|
|
|
|
* @param _user the user which debt has been burned
|
|
|
|
* @param _amount the amount of debt being burned
|
|
|
|
* @param _previousBalance the previous balance of the user
|
|
|
|
* @param _currentBalance the current balance of the user
|
|
|
|
* @param _balanceIncrease the debt accumulated since the last action
|
|
|
|
* @param _index the index of the user
|
|
|
|
**/
|
2020-08-20 14:26:06 +00:00
|
|
|
event BurnDebt(
|
2020-06-30 12:09:28 +00:00
|
|
|
address _user,
|
|
|
|
uint256 _amount,
|
|
|
|
uint256 _previousBalance,
|
|
|
|
uint256 _currentBalance,
|
|
|
|
uint256 _balanceIncrease,
|
|
|
|
uint256 _index
|
|
|
|
);
|
|
|
|
|
2020-08-18 09:39:34 +00:00
|
|
|
constructor(
|
|
|
|
address _pool,
|
|
|
|
address _underlyingAsset,
|
|
|
|
string memory _name,
|
|
|
|
string memory _symbol
|
|
|
|
) public DebtTokenBase(_pool, _underlyingAsset, _name, _symbol) {}
|
2020-08-11 10:26:15 +00:00
|
|
|
|
2020-08-17 19:28:50 +00:00
|
|
|
/**
|
|
|
|
* @dev gets the revision of the stable debt token implementation
|
|
|
|
* @return the debt token implementation revision
|
|
|
|
**/
|
2020-08-18 09:39:34 +00:00
|
|
|
function getRevision() internal virtual override pure returns (uint256) {
|
2020-08-17 19:28:50 +00:00
|
|
|
return DEBT_TOKEN_REVISION;
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:52:57 +00:00
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @dev calculates the accumulated debt balance of the user
|
|
|
|
* @return the debt balance of the user
|
|
|
|
**/
|
2020-07-09 09:59:49 +00:00
|
|
|
function balanceOf(address _user) public virtual override view returns (uint256) {
|
|
|
|
if (balances[_user] == 0) {
|
2020-06-30 12:09:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2020-07-09 09:59:49 +00:00
|
|
|
balances[_user]
|
2020-06-30 12:09:28 +00:00
|
|
|
.wadToRay()
|
|
|
|
.rayMul(pool.getReserveNormalizedVariableDebt(underlyingAssetAddress))
|
2020-07-09 09:59:49 +00:00
|
|
|
.rayDiv(userIndexes[_user])
|
2020-06-30 12:09:28 +00:00
|
|
|
.rayToWad();
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:52:57 +00:00
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @dev returns the index of the last user action
|
|
|
|
* @return the user index
|
|
|
|
**/
|
2020-07-09 14:52:57 +00:00
|
|
|
|
2020-08-20 14:26:06 +00:00
|
|
|
function getUserIndex(address _user) external virtual override view returns (uint256) {
|
2020-06-30 12:09:28 +00:00
|
|
|
return userIndexes[_user];
|
|
|
|
}
|
2020-07-03 21:20:02 +00:00
|
|
|
|
2020-07-09 09:59:49 +00:00
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @dev mints new variable debt
|
|
|
|
* @param _user the user receiving the debt
|
|
|
|
* @param _amount the amount of debt being minted
|
|
|
|
**/
|
2020-08-20 14:26:06 +00:00
|
|
|
function mint(address _user, uint256 _amount) external override onlyLendingPool {
|
2020-08-11 10:26:15 +00:00
|
|
|
(
|
|
|
|
uint256 previousBalance,
|
|
|
|
uint256 currentBalance,
|
|
|
|
uint256 balanceIncrease
|
2020-08-12 17:42:40 +00:00
|
|
|
) = _calculateBalanceIncrease(_user);
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-08-11 10:26:15 +00:00
|
|
|
_mint(_user, _amount.add(balanceIncrease));
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-08-20 14:26:06 +00:00
|
|
|
uint256 newUserIndex = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress);
|
|
|
|
userIndexes[_user] = newUserIndex;
|
2020-07-03 21:20:02 +00:00
|
|
|
|
2020-08-20 14:26:06 +00:00
|
|
|
emit MintDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease, newUserIndex);
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @dev burns user variable debt
|
|
|
|
* @param _user the user which debt is burnt
|
|
|
|
* @param _amount the amount of debt being burned
|
|
|
|
**/
|
2020-08-20 14:26:06 +00:00
|
|
|
function burn(address _user, uint256 _amount) external override onlyLendingPool {
|
2020-08-11 10:26:15 +00:00
|
|
|
(
|
|
|
|
uint256 previousBalance,
|
|
|
|
uint256 currentBalance,
|
|
|
|
uint256 balanceIncrease
|
|
|
|
) = _calculateBalanceIncrease(_user);
|
|
|
|
|
|
|
|
if (balanceIncrease > _amount) {
|
|
|
|
_mint(_user, balanceIncrease.sub(_amount));
|
|
|
|
} else {
|
|
|
|
_burn(_user, _amount.sub(balanceIncrease));
|
|
|
|
}
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-08-20 14:26:06 +00:00
|
|
|
uint256 newUserIndex = 0;
|
|
|
|
//if user not repaid everything
|
|
|
|
if (currentBalance != _amount) {
|
|
|
|
newUserIndex = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress);
|
2020-07-03 21:20:02 +00:00
|
|
|
}
|
2020-08-20 14:26:06 +00:00
|
|
|
userIndexes[_user] = newUserIndex;
|
2020-07-03 21:20:02 +00:00
|
|
|
|
2020-08-20 14:26:06 +00:00
|
|
|
emit BurnDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease, newUserIndex);
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
|
|
|
}
|