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-07-09 09:23:30 +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 {MathUtils} from '../libraries/math/MathUtils.sol';
|
|
|
|
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
|
2020-06-30 12:09:28 +00:00
|
|
|
import {IStableDebtToken} from './interfaces/IStableDebtToken.sol';
|
|
|
|
|
2020-07-09 09:23:30 +00:00
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @title contract StableDebtToken
|
2020-08-22 17:33:55 +00:00
|
|
|
* @notice Implements a stable 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 StableDebtToken is IStableDebtToken, DebtTokenBase {
|
|
|
|
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
|
|
|
|
2020-08-22 17:33:55 +00:00
|
|
|
uint256 private _avgStableRate;
|
|
|
|
mapping(address => uint40) _timestamps;
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-08-18 09:39:34 +00:00
|
|
|
constructor(
|
2020-08-21 10:38:08 +00:00
|
|
|
address pool,
|
|
|
|
address underlyingAsset,
|
|
|
|
string memory name,
|
|
|
|
string memory symbol
|
|
|
|
) public DebtTokenBase(pool, underlyingAsset, name, symbol) {}
|
2020-08-11 07:36:46 +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-13 08:54:08 +00:00
|
|
|
/**
|
|
|
|
* @dev returns the average stable rate across all the stable rate debt
|
|
|
|
* @return the average stable rate
|
|
|
|
**/
|
2020-06-30 12:09:28 +00:00
|
|
|
function getAverageStableRate() external virtual override view returns (uint256) {
|
2020-08-22 17:33:55 +00:00
|
|
|
return _avgStableRate;
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
/**
|
|
|
|
* @dev returns the timestamp of the last user action
|
|
|
|
* @return the last update timestamp
|
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
function getUserLastUpdated(address user) external virtual override view returns (uint40) {
|
2020-08-22 17:33:55 +00:00
|
|
|
return _timestamps[user];
|
2020-07-03 21:20:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
/**
|
|
|
|
* @dev returns the stable rate of the user
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user the address of the user
|
|
|
|
* @return the stable rate of user
|
2020-07-13 08:54:08 +00:00
|
|
|
**/
|
2020-08-21 10:38:08 +00:00
|
|
|
function getUserStableRate(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-09 14:50:56 +00:00
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @dev calculates the current user debt balance
|
|
|
|
* @return the accumulated debt of the user
|
|
|
|
**/
|
2020-06-30 12:09:28 +00:00
|
|
|
function balanceOf(address account) public virtual override view returns (uint256) {
|
2020-09-04 14:27:35 +00:00
|
|
|
uint256 accountBalance = principalBalanceOf(account);
|
2020-09-04 15:10:32 +00:00
|
|
|
uint256 stableRate = _usersData[account];
|
2020-08-21 10:40:56 +00:00
|
|
|
if (accountBalance == 0) {
|
2020-07-13 08:54:08 +00:00
|
|
|
return 0;
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
|
|
|
uint256 cumulatedInterest = MathUtils.calculateCompoundedInterest(
|
2020-08-22 17:33:55 +00:00
|
|
|
stableRate,
|
|
|
|
_timestamps[account]
|
2020-06-30 12:09:28 +00:00
|
|
|
);
|
2020-08-21 10:40:56 +00:00
|
|
|
return accountBalance.wadToRay().rayMul(cumulatedInterest).rayToWad();
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 21:44:19 +00:00
|
|
|
struct MintLocalVars {
|
2020-08-11 10:26:15 +00:00
|
|
|
uint256 supplyAfterMint;
|
|
|
|
uint256 supplyBeforeMint;
|
2020-07-05 21:44:19 +00:00
|
|
|
uint256 amountInRay;
|
|
|
|
uint256 newStableRate;
|
|
|
|
}
|
|
|
|
|
2020-07-09 09:23:30 +00:00
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @dev mints debt token to the target user. The resulting rate is the weighted average
|
|
|
|
* between the rate of the new debt and the rate of the previous debt
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user the address of the user
|
|
|
|
* @param amount the amount of debt tokens to mint
|
|
|
|
* @param rate the rate of the debt being minted.
|
2020-07-13 08:54:08 +00:00
|
|
|
**/
|
2020-06-30 12:09:28 +00:00
|
|
|
function mint(
|
2020-08-21 10:38:08 +00:00
|
|
|
address user,
|
|
|
|
uint256 amount,
|
|
|
|
uint256 rate
|
2020-08-21 07:24:59 +00:00
|
|
|
) external override onlyLendingPool {
|
2020-07-05 21:44:19 +00:00
|
|
|
MintLocalVars memory vars;
|
2020-07-07 10:07:31 +00:00
|
|
|
|
2020-07-09 09:23:30 +00:00
|
|
|
//cumulates the user debt
|
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-09-04 14:27:35 +00:00
|
|
|
vars.supplyBeforeMint = totalSupply().add(balanceIncrease);
|
2020-08-21 10:38:08 +00:00
|
|
|
vars.supplyAfterMint = vars.supplyBeforeMint.add(amount);
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
vars.amountInRay = amount.wadToRay();
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-07-09 09:23:30 +00:00
|
|
|
//calculates the new stable rate for the user
|
2020-08-21 10:38:08 +00:00
|
|
|
vars.newStableRate = _usersData[user]
|
2020-06-30 12:09:28 +00:00
|
|
|
.rayMul(currentBalance.wadToRay())
|
2020-08-21 10:38:08 +00:00
|
|
|
.add(vars.amountInRay.rayMul(rate))
|
|
|
|
.rayDiv(currentBalance.add(amount).wadToRay());
|
2020-07-03 21:20:02 +00:00
|
|
|
|
2020-09-09 19:26:52 +00:00
|
|
|
require(vars.newStableRate < (1 << 128), 'Debt token: stable rate overflow');
|
2020-09-04 15:10:32 +00:00
|
|
|
_usersData[user] = vars.newStableRate;
|
2020-07-05 21:44:19 +00:00
|
|
|
|
2020-07-09 09:23:30 +00:00
|
|
|
//solium-disable-next-line
|
2020-08-22 17:33:55 +00:00
|
|
|
_timestamps[user] = uint40(block.timestamp);
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-07-09 09:23:30 +00:00
|
|
|
//calculates the updated average stable rate
|
2020-08-22 17:33:55 +00:00
|
|
|
_avgStableRate = _avgStableRate
|
2020-08-11 10:26:15 +00:00
|
|
|
.rayMul(vars.supplyBeforeMint.wadToRay())
|
2020-08-21 10:38:08 +00:00
|
|
|
.add(rate.rayMul(vars.amountInRay))
|
2020-08-11 10:26:15 +00:00
|
|
|
.rayDiv(vars.supplyAfterMint.wadToRay());
|
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-08-20 14:26:06 +00:00
|
|
|
emit MintDebt(
|
2020-08-21 10:38:08 +00:00
|
|
|
user,
|
|
|
|
amount,
|
2020-06-30 12:09:28 +00:00
|
|
|
previousBalance,
|
|
|
|
currentBalance,
|
|
|
|
balanceIncrease,
|
2020-07-05 21:44:19 +00:00
|
|
|
vars.newStableRate
|
2020-06-30 12:09:28 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-13 08:54:08 +00:00
|
|
|
* @dev burns debt of the target user.
|
2020-08-21 10:38:08 +00:00
|
|
|
* @param user the address of the user
|
|
|
|
* @param amount the amount of debt tokens to mint
|
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-06-30 12:09:28 +00:00
|
|
|
|
2020-09-04 14:27:35 +00:00
|
|
|
uint256 supplyBeforeBurn = totalSupply().add(balanceIncrease);
|
2020-08-21 10:38:08 +00:00
|
|
|
uint256 supplyAfterBurn = supplyBeforeBurn.sub(amount);
|
2020-08-11 10:26:15 +00:00
|
|
|
|
2020-08-20 14:26:06 +00:00
|
|
|
if (supplyAfterBurn == 0) {
|
2020-08-22 17:33:55 +00:00
|
|
|
_avgStableRate = 0;
|
2020-06-30 12:09:28 +00:00
|
|
|
} else {
|
2020-08-22 17:33:55 +00:00
|
|
|
_avgStableRate = _avgStableRate
|
2020-08-11 10:26:15 +00:00
|
|
|
.rayMul(supplyBeforeBurn.wadToRay())
|
2020-09-04 15:10:32 +00:00
|
|
|
.sub(_usersData[user].rayMul(amount.wadToRay()))
|
2020-08-11 10:26:15 +00:00
|
|
|
.rayDiv(supplyAfterBurn.wadToRay());
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 10:38:08 +00:00
|
|
|
if (amount == currentBalance) {
|
2020-09-04 15:10:32 +00:00
|
|
|
_usersData[user] = 0;
|
2020-08-22 17:33:55 +00:00
|
|
|
_timestamps[user] = 0;
|
2020-08-20 15:09:23 +00:00
|
|
|
} else {
|
|
|
|
//solium-disable-next-line
|
2020-08-22 17:33:55 +00:00
|
|
|
_timestamps[user] = uint40(block.timestamp);
|
2020-07-03 21:20:02 +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-21 10:38:08 +00:00
|
|
|
emit BurnDebt(user, amount, previousBalance, currentBalance, balanceIncrease);
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
|
|
|
}
|