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

227 lines
6.2 KiB
Solidity
Raw Normal View History

2020-06-30 12:09:28 +00:00
pragma solidity ^0.6.0;
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';
import {Address} from '@openzeppelin/contracts/utils/Address.sol';
2020-06-30 12:09:28 +00:00
import {DebtTokenBase} from './base/DebtTokenBase.sol';
import {MathUtils} from '../libraries/MathUtils.sol';
import {WadRayMath} from '../libraries/WadRayMath.sol';
import {IStableDebtToken} from './interfaces/IStableDebtToken.sol';
/**
* @title contract StableDebtToken
*
* @notice defines the interface for the stable debt token
*
* @dev it does not inherit from IERC20 to save in code size
*
* @author Aave
*
**/
2020-06-30 12:09:28 +00:00
contract StableDebtToken is IStableDebtToken, DebtTokenBase {
using SafeMath for uint256;
using WadRayMath for uint256;
using Address for address;
struct UserData {
uint256 currentRate;
uint40 lastUpdateTimestamp;
}
uint256 private avgStableRate;
mapping(address => UserData) usersData;
/**
* @dev emitted when new stable debt is minted
* @param _user the address of the user
* @param _amount the amount minted
* @param _previousBalance the previous balance of the user
* @param _currentBalance the current balance of the user
* @param _balanceIncrease the debt increase since the last update
* @param _newRate the rate of the debt after the minting
**/
2020-06-30 12:09:28 +00:00
event mintDebt(
address _user,
uint256 _amount,
uint256 _previousBalance,
uint256 _currentBalance,
uint256 _balanceIncrease,
uint256 _newRate
);
/**
* @dev emitted when new stable debt is burned
* @param _user the address of the user
* @param _amount the amount minted
* @param _previousBalance the previous balance of the user
* @param _currentBalance the current balance of the user
* @param _balanceIncrease the debt increase since the last update
**/
2020-06-30 12:09:28 +00:00
event burnDebt(
address _user,
uint256 _amount,
uint256 _previousBalance,
uint256 _currentBalance,
uint256 _balanceIncrease
);
function getAverageStableRate() external virtual override view returns (uint256) {
return avgStableRate;
}
2020-07-03 21:20:02 +00:00
function getUserLastUpdated(address _user) external virtual override view returns (uint40) {
return usersData[_user].lastUpdateTimestamp;
}
2020-06-30 12:09:28 +00:00
function getUserStableRate(address _user) external virtual override view returns (uint256) {
return usersData[_user].currentRate;
}
2020-06-30 12:09:28 +00:00
function balanceOf(address account) public virtual override view returns (uint256) {
if(balances[account] == 0) {
return 0;
}
UserData storage userData = usersData[account];
uint256 cumulatedInterest = MathUtils.calculateCompoundedInterest(
userData.currentRate,
userData.lastUpdateTimestamp
);
return balances[account].wadToRay().rayMul(cumulatedInterest).rayToWad();
}
struct MintLocalVars {
uint256 newSupply;
uint256 amountInRay;
uint256 newStableRate;
}
/**
* @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-06-30 12:09:28 +00:00
function mint(
address _user,
uint256 _amount,
uint256 _rate
2020-06-30 12:09:28 +00:00
) public override onlyLendingPool {
MintLocalVars memory vars;
2020-07-07 10:07:31 +00:00
//cumulates the user debt
2020-06-30 12:09:28 +00:00
(
uint256 previousBalance,
uint256 currentBalance,
uint256 balanceIncrease
) = _cumulateBalance(_user);
2020-06-30 12:09:28 +00:00
vars.newSupply = totalSupply.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
.currentRate
.rayMul(currentBalance.wadToRay())
.add(vars.amountInRay.rayMul(_rate))
.rayDiv(currentBalance.add(_amount).wadToRay());
2020-07-03 21:20:02 +00:00
usersData[_user].currentRate = vars.newStableRate;
//solium-disable-next-line
usersData[_user].lastUpdateTimestamp = uint40(block.timestamp);
2020-06-30 12:09:28 +00:00
//calculates the updated average stable rate
2020-06-30 12:09:28 +00:00
avgStableRate = avgStableRate
.rayMul(totalSupply.wadToRay())
.add(_rate.rayMul(vars.amountInRay))
.rayDiv(vars.newSupply.wadToRay());
2020-06-30 12:09:28 +00:00
_mint(_user, _amount);
2020-06-30 12:09:28 +00:00
emit mintDebt(
_user,
_amount,
2020-06-30 12:09:28 +00:00
previousBalance,
currentBalance,
balanceIncrease,
vars.newStableRate
2020-06-30 12:09:28 +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
**/
function burn(address _user, uint256 _amount) public override onlyLendingPool {
2020-06-30 12:09:28 +00:00
(
uint256 previousBalance,
uint256 currentBalance,
uint256 balanceIncrease
) = _cumulateBalance(_user);
2020-06-30 12:09:28 +00:00
uint256 newSupply = totalSupply.sub(_amount);
uint256 amountInRay = _amount.wadToRay();
if (newSupply == 0) {
avgStableRate = 0;
} else {
avgStableRate = avgStableRate
.rayMul(totalSupply.wadToRay())
.sub(usersData[_user].currentRate.rayMul(amountInRay))
2020-06-30 12:09:28 +00:00
.rayDiv(newSupply.wadToRay());
}
2020-07-03 21:20:02 +00:00
if(_amount == currentBalance){
usersData[_user].currentRate = 0;
usersData[_user].lastUpdateTimestamp = 0;
2020-07-03 21:20:02 +00:00
}
_burn(_user, _amount);
2020-06-30 12:09:28 +00:00
emit burnDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease);
2020-06-30 12:09:28 +00:00
}
/**
* @dev accumulates the accrued interest of the user to the principal balance
* @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
**/
function _cumulateBalance(address _user)
2020-06-30 12:09:28 +00:00
internal
returns (
uint256,
uint256,
uint256
)
{
uint256 previousPrincipalBalance = balances[_user];
2020-07-03 21:20:02 +00:00
if(previousPrincipalBalance == 0){
return (0,0,0);
}
2020-06-30 12:09:28 +00:00
//calculate the accrued interest since the last accumulation
uint256 balanceIncrease = balanceOf(_user).sub(previousPrincipalBalance);
//mints an amount of tokens equivalent to the amount accumulated
_mint(_user, balanceIncrease);
2020-06-30 12:09:28 +00:00
return (
previousPrincipalBalance,
previousPrincipalBalance.add(balanceIncrease),
balanceIncrease
);
}
}