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-08-22 17:33:55 +00:00
|
|
|
* @title contract VariableDebtToken
|
|
|
|
* @notice Implements a variable debt token to track the user positions
|
2020-07-13 08:54:08 +00:00
|
|
|
* @author Aave
|
|
|
|
**/
|
2020-06-30 12:09:28 +00:00
|
|
|
contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
|
|
|
|
using WadRayMath for uint256;
|
|
|
|
|
2020-08-17 19:28:50 +00:00
|
|
|
uint256 public constant DEBT_TOKEN_REVISION = 0x1;
|
|
|
|
|
2020-08-18 09:39:34 +00:00
|
|
|
constructor(
|
2020-08-21 10:38:08 +00:00
|
|
|
address pool,
|
|
|
|
address underlyingAsset,
|
|
|
|
string memory name,
|
2020-09-15 13:53:20 +00:00
|
|
|
string memory symbol,
|
|
|
|
address incentivesController
|
|
|
|
) public DebtTokenBase(pool, underlyingAsset, name, symbol, incentivesController) {}
|
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-08-21 10:38:08 +00:00
|
|
|
function balanceOf(address user) public virtual override view returns (uint256) {
|
2020-09-04 14:27:35 +00:00
|
|
|
uint256 userBalance = principalBalanceOf(user);
|
2020-09-04 15:10:32 +00:00
|
|
|
uint256 index = _usersData[user];
|
2020-08-21 10:38:08 +00:00
|
|
|
if (userBalance == 0) {
|
2020-06-30 12:09:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2020-08-21 10:38:08 +00:00
|
|
|
userBalance
|
2020-06-30 12:09:28 +00:00
|
|
|
.wadToRay()
|
2020-09-04 14:27:35 +00:00
|
|
|
.rayMul(POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET))
|
2020-08-22 17:33:55 +00:00
|
|
|
.rayDiv(index)
|
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-21 10:38:08 +00:00
|
|
|
function getUserIndex(address user) external virtual override view returns (uint256) {
|
2020-09-04 15:10:32 +00:00
|
|
|
return _usersData[user];
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
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
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user the user receiving the debt
|
|
|
|
* @param amount the amount of debt being minted
|
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-08-11 10:26:15 +00:00
|
|
|
(
|
|
|
|
uint256 previousBalance,
|
|
|
|
uint256 currentBalance,
|
|
|
|
uint256 balanceIncrease
|
2020-08-21 10:38:08 +00:00
|
|
|
) = _calculateBalanceIncrease(user);
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
_mint(user, amount.add(balanceIncrease));
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-09-04 14:27:35 +00:00
|
|
|
uint256 newUserIndex = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET);
|
2020-09-09 19:26:52 +00:00
|
|
|
require(newUserIndex < (1 << 128), 'Debt token: Index overflow');
|
2020-09-04 15:10:32 +00:00
|
|
|
_usersData[user] = newUserIndex;
|
2020-07-03 21:20:02 +00:00
|
|
|
|
2020-08-21 10:38:08 +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
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user the user which debt is burnt
|
|
|
|
* @param amount the amount of debt being burned
|
2020-07-13 08:54:08 +00:00
|
|
|
**/
|
2020-08-21 10:38:08 +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
|
2020-08-21 10:38:08 +00:00
|
|
|
) = _calculateBalanceIncrease(user);
|
2020-08-11 10:26:15 +00:00
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
if (balanceIncrease > amount) {
|
|
|
|
_mint(user, balanceIncrease.sub(amount));
|
2020-08-11 10:26:15 +00:00
|
|
|
} else {
|
2020-08-21 10:38:08 +00:00
|
|
|
_burn(user, amount.sub(balanceIncrease));
|
2020-08-11 10:26:15 +00:00
|
|
|
}
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-08-20 14:26:06 +00:00
|
|
|
uint256 newUserIndex = 0;
|
|
|
|
//if user not repaid everything
|
2020-08-21 10:38:08 +00:00
|
|
|
if (currentBalance != amount) {
|
2020-09-04 14:27:35 +00:00
|
|
|
newUserIndex = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET);
|
2020-09-09 19:26:52 +00:00
|
|
|
require(newUserIndex < (1 << 128), 'Debt token: Index overflow');
|
2020-07-03 21:20:02 +00:00
|
|
|
}
|
2020-09-04 15:10:32 +00:00
|
|
|
_usersData[user] = newUserIndex;
|
2020-07-03 21:20:02 +00:00
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
emit BurnDebt(user, amount, previousBalance, currentBalance, balanceIncrease, newUserIndex);
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
|
|
|
}
|