aave-protocol-v2/contracts/tokenization/StableDebtToken.sol

334 lines
10 KiB
Solidity
Raw Normal View History

// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
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-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;
uint40 _totalSupplyTimestamp;
2020-06-30 12:09:28 +00:00
2020-08-18 09:39:34 +00:00
constructor(
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 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
**/
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
* @param user the address of the user
* @return the stable rate of user
2020-07-13 08:54:08 +00:00
**/
function getUserStableRate(address user) external virtual override view returns (uint256) {
return _usersData[user];
2020-06-30 12:09:28 +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-14 13:09:16 +00:00
uint256 accountBalance = super.balanceOf(account);
uint256 stableRate = _usersData[account];
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
);
return accountBalance.rayMul(cumulatedInterest);
2020-06-30 12:09:28 +00:00
}
struct MintLocalVars {
uint256 previousSupply;
2020-09-15 16:49:53 +00:00
uint256 nextSupply;
uint256 amountInRay;
uint256 newStableRate;
2020-09-15 16:49:53 +00:00
uint256 currentAvgStableRate;
}
/**
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
* @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(
address user,
uint256 amount,
uint256 rate
) external override onlyLendingPool {
MintLocalVars memory vars;
2020-07-07 10:07:31 +00:00
//cumulates the user debt
(
uint256 previousBalance,
uint256 currentBalance,
uint256 balanceIncrease
) = _calculateBalanceIncrease(user);
2020-06-30 12:09:28 +00:00
2020-09-16 18:00:13 +00:00
//accrueing the interest accumulation to the stored total supply and caching it
vars.previousSupply = totalSupply();
2020-09-15 16:49:53 +00:00
vars.currentAvgStableRate = _avgStableRate;
vars.nextSupply = _totalSupply = vars.previousSupply.add(amount);
vars.amountInRay = amount.wadToRay();
2020-06-30 12:09:28 +00:00
//calculates the new stable rate for the user
vars.newStableRate = _usersData[user]
2020-06-30 12:09:28 +00:00
.rayMul(currentBalance.wadToRay())
.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');
_usersData[user] = vars.newStableRate;
2020-09-16 18:00:13 +00:00
//updating the user and supply timestamp
//solium-disable-next-line
_totalSupplyTimestamp = _timestamps[user] = uint40(block.timestamp);
//calculates the updated average stable rate
2020-10-09 08:03:50 +00:00
_avgStableRate = vars
.currentAvgStableRate
.rayMul(vars.previousSupply.wadToRay())
.add(rate.rayMul(vars.amountInRay))
2020-09-15 16:49:53 +00:00
.rayDiv(vars.nextSupply.wadToRay());
2020-06-30 12:09:28 +00:00
_mint(user, amount.add(balanceIncrease), vars.previousSupply);
2020-06-30 12:09:28 +00:00
2020-09-15 13:20:32 +00:00
// transfer event to track balances
emit Transfer(address(0), user, amount);
2020-10-09 08:03:50 +00:00
emit Mint(user, amount, previousBalance, currentBalance, balanceIncrease, 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.
* @param user the address of the user
* @param amount the amount of debt tokens to mint
2020-07-13 08:54:08 +00:00
**/
function burn(address user, uint256 amount) external override onlyLendingPool {
(
uint256 previousBalance,
uint256 currentBalance,
uint256 balanceIncrease
) = _calculateBalanceIncrease(user);
2020-06-30 12:09:28 +00:00
uint256 previousSupply = totalSupply();
2020-09-18 15:56:33 +00:00
//since the total supply and each single user debt accrue separately,
//there might be accumulation errors so that the last borrower repaying
//might actually try to repay more than the available debt supply.
//in this case we simply set the total supply and the avg stable rate to 0
if (previousSupply <= amount) {
2020-08-22 17:33:55 +00:00
_avgStableRate = 0;
2020-09-17 08:53:55 +00:00
_totalSupply = 0;
2020-06-30 12:09:28 +00:00
} else {
2020-10-09 08:03:50 +00:00
uint256 nextSupply = _totalSupply = previousSupply.sub(amount);
2020-08-22 17:33:55 +00:00
_avgStableRate = _avgStableRate
.rayMul(previousSupply.wadToRay())
.sub(_usersData[user].rayMul(amount.wadToRay()))
2020-09-17 08:53:55 +00:00
.rayDiv(nextSupply.wadToRay());
2020-06-30 12:09:28 +00:00
}
if (amount == currentBalance) {
_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-09-17 08:53:55 +00:00
_timestamps[user] = uint40(block.timestamp);
2020-07-03 21:20:02 +00:00
}
2020-09-17 08:53:55 +00:00
//solium-disable-next-line
_totalSupplyTimestamp = uint40(block.timestamp);
2020-07-03 21:20:02 +00:00
if (balanceIncrease > amount) {
_mint(user, balanceIncrease.sub(amount), previousSupply);
} else {
_burn(user, amount.sub(balanceIncrease), previousSupply);
}
2020-06-30 12:09:28 +00:00
2020-09-15 13:20:32 +00:00
// transfer event to track balances
emit Transfer(user, address(0), amount);
2020-10-09 08:03:50 +00:00
emit Burn(user, amount, previousBalance, currentBalance, balanceIncrease);
2020-06-30 12:09:28 +00:00
}
2020-09-10 09:25:45 +00:00
/**
* @dev Calculates the increase in balance since the last user interaction
* @param user The address of the user for which the interest is being accumulated
* @return The previous principal balance, the new principal balance, the balance increase
* and the new user index
**/
2020-09-14 13:09:16 +00:00
function _calculateBalanceIncrease(address user)
internal
view
returns (
uint256,
uint256,
uint256
)
{
uint256 previousPrincipalBalance = super.balanceOf(user);
2020-09-10 09:25:45 +00:00
if (previousPrincipalBalance == 0) {
return (0, 0, 0);
}
// Calculation of the accrued interest since the last accumulation
uint256 balanceIncrease = balanceOf(user).sub(previousPrincipalBalance);
return (
previousPrincipalBalance,
previousPrincipalBalance.add(balanceIncrease),
balanceIncrease
);
}
2020-09-18 15:56:33 +00:00
/**
2020-09-21 16:51:51 +00:00
* @dev returns the principal and total supply, the average borrow rate and the last supply update timestamp
2020-09-18 15:56:33 +00:00
**/
2020-10-09 08:03:50 +00:00
function getSupplyData()
public
override
view
returns (
uint256,
uint256,
uint256,
uint40
)
{
2020-09-16 18:00:13 +00:00
uint256 avgRate = _avgStableRate;
2020-09-21 16:51:51 +00:00
return (super.totalSupply(), _calcTotalSupply(avgRate), avgRate, _totalSupplyTimestamp);
}
2020-09-14 13:09:16 +00:00
2020-09-18 15:56:33 +00:00
/**
* @dev returns the the total supply and the average stable rate
**/
2020-09-14 17:25:45 +00:00
function getTotalSupplyAndAvgRate() public override view returns (uint256, uint256) {
uint256 avgRate = _avgStableRate;
return (_calcTotalSupply(avgRate), avgRate);
}
2020-09-18 15:56:33 +00:00
/**
* @dev returns the total supply
**/
2020-09-14 13:09:16 +00:00
function totalSupply() public override view returns (uint256) {
return _calcTotalSupply(_avgStableRate);
2020-09-14 17:25:45 +00:00
}
2020-09-18 15:56:33 +00:00
/**
* @dev returns the timestamp at which the total supply was updated
**/
2020-10-09 08:03:50 +00:00
function getTotalSupplyLastUpdated() public override view returns (uint40) {
2020-09-14 17:25:45 +00:00
return _totalSupplyTimestamp;
}
2020-09-15 16:49:53 +00:00
2020-09-14 17:25:45 +00:00
/**
* @dev Returns the principal debt balance of the user from
2020-09-18 15:56:33 +00:00
* @param user the user
2020-09-14 17:25:45 +00:00
* @return The debt balance of the user since the last burn/mint action
**/
function principalBalanceOf(address user) external virtual override view returns (uint256) {
return super.balanceOf(user);
}
2020-09-18 15:56:33 +00:00
/**
2020-10-09 08:03:50 +00:00
* @dev calculates the total supply
2020-09-18 15:56:33 +00:00
* @param avgRate the average rate at which calculate the total supply
* @return The debt balance of the user since the last burn/mint action
**/
2020-10-09 08:03:50 +00:00
function _calcTotalSupply(uint256 avgRate) internal view returns (uint256) {
uint256 principalSupply = super.totalSupply();
if (principalSupply == 0) {
return 0;
}
uint256 cumulatedInterest = MathUtils.calculateCompoundedInterest(
2020-09-14 17:25:45 +00:00
avgRate,
_totalSupplyTimestamp
);
return principalSupply.rayMul(cumulatedInterest);
}
2020-10-09 08:03:50 +00:00
/**
2020-09-18 15:56:33 +00:00
* @dev mints stable debt tokens to an user
* @param account the account receiving the debt tokens
* @param amount the amount being minted
* @param oldTotalSupply the total supply before the minting event
**/
2020-10-09 08:03:50 +00:00
function _mint(
address account,
uint256 amount,
uint256 oldTotalSupply
) internal {
uint256 oldAccountBalance = _balances[account];
_balances[account] = oldAccountBalance.add(amount);
if (address(_incentivesController) != address(0)) {
_incentivesController.handleAction(account, oldTotalSupply, oldAccountBalance);
}
}
2020-10-09 08:03:50 +00:00
/**
2020-09-18 15:56:33 +00:00
* @dev burns stable debt tokens of an user
* @param account the user getting his debt burned
* @param amount the amount being burned
* @param oldTotalSupply the total supply before the burning event
**/
2020-10-09 08:03:50 +00:00
function _burn(
address account,
uint256 amount,
uint256 oldTotalSupply
) internal {
uint256 oldAccountBalance = _balances[account];
_balances[account] = oldAccountBalance.sub(amount, 'ERC20: burn amount exceeds balance');
if (address(_incentivesController) != address(0)) {
_incentivesController.handleAction(account, oldTotalSupply, oldAccountBalance);
}
}
2020-06-30 12:09:28 +00:00
}