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
|
|
|
|
|
|
|
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-09-30 15:40:47 +00:00
|
|
|
import {Errors} from '../libraries/helpers/Errors.sol';
|
2020-06-30 12:09:28 +00:00
|
|
|
|
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-14 13:09:16 +00:00
|
|
|
uint256 scaledBalance = super.balanceOf(user);
|
2020-09-21 14:11:14 +00:00
|
|
|
|
2020-09-10 09:25:45 +00:00
|
|
|
if (scaledBalance == 0) {
|
2020-06-30 12:09:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:24:49 +00:00
|
|
|
return scaledBalance.rayMul(POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET_ADDRESS));
|
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-09-14 07:33:53 +00:00
|
|
|
* @param index the variable debt index of the reserve
|
2020-10-15 12:13:46 +00:00
|
|
|
* @return true if the the previous balance of the user is 0
|
2020-07-13 08:54:08 +00:00
|
|
|
**/
|
2020-09-21 14:11:14 +00:00
|
|
|
function mint(
|
|
|
|
address user,
|
|
|
|
uint256 amount,
|
|
|
|
uint256 index
|
2020-10-15 12:13:46 +00:00
|
|
|
) external override onlyLendingPool returns (bool) {
|
|
|
|
uint256 previousBalance = super.balanceOf(user);
|
2020-09-30 15:40:47 +00:00
|
|
|
uint256 amountScaled = amount.rayDiv(index);
|
|
|
|
require(amountScaled != 0, Errors.INVALID_MINT_AMOUNT);
|
|
|
|
|
|
|
|
_mint(user, amountScaled);
|
2020-07-03 21:20:02 +00:00
|
|
|
|
2020-09-15 13:20:32 +00:00
|
|
|
emit Transfer(address(0), user, amount);
|
2020-09-21 15:41:38 +00:00
|
|
|
emit Mint(user, amount, index);
|
2020-10-15 12:13:46 +00:00
|
|
|
|
|
|
|
return previousBalance == 0;
|
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
|
2020-09-14 07:33:53 +00:00
|
|
|
* @param index the variable debt index of the reserve
|
2020-07-13 08:54:08 +00:00
|
|
|
**/
|
2020-09-21 14:11:14 +00:00
|
|
|
function burn(
|
|
|
|
address user,
|
|
|
|
uint256 amount,
|
|
|
|
uint256 index
|
|
|
|
) external override onlyLendingPool {
|
2020-09-30 15:40:47 +00:00
|
|
|
uint256 amountScaled = amount.rayDiv(index);
|
|
|
|
require(amountScaled != 0, Errors.INVALID_BURN_AMOUNT);
|
|
|
|
|
|
|
|
_burn(user, amountScaled);
|
2020-07-03 21:20:02 +00:00
|
|
|
|
2020-09-15 13:20:32 +00:00
|
|
|
emit Transfer(user, address(0), amount);
|
2020-09-21 15:41:38 +00:00
|
|
|
emit Burn(user, amount, index);
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
2020-09-10 10:51:52 +00:00
|
|
|
|
2020-09-10 11:05:02 +00:00
|
|
|
/**
|
2020-09-21 14:11:14 +00:00
|
|
|
* @dev Returns the principal debt balance of the user from
|
|
|
|
* @return The debt balance of the user since the last burn/mint action
|
|
|
|
**/
|
2020-09-10 11:05:02 +00:00
|
|
|
function scaledBalanceOf(address user) public virtual override view returns (uint256) {
|
|
|
|
return super.balanceOf(user);
|
|
|
|
}
|
|
|
|
|
2020-09-14 08:43:30 +00:00
|
|
|
/**
|
2020-09-21 14:11:14 +00:00
|
|
|
* @dev Returns the total supply of the variable debt token. Represents the total debt accrued by the users
|
|
|
|
* @return the total supply
|
|
|
|
**/
|
|
|
|
function totalSupply() public virtual override view returns (uint256) {
|
2020-10-19 14:24:49 +00:00
|
|
|
return
|
|
|
|
super.totalSupply().rayMul(POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET_ADDRESS));
|
2020-09-10 11:05:02 +00:00
|
|
|
}
|
2020-09-14 08:43:30 +00:00
|
|
|
|
|
|
|
/**
|
2020-09-21 14:11:14 +00:00
|
|
|
* @dev Returns the scaled total supply of the variable debt token. Represents sum(borrows/index)
|
|
|
|
* @return the scaled total supply
|
|
|
|
**/
|
|
|
|
function scaledTotalSupply() public virtual override view returns (uint256) {
|
2020-09-14 08:43:30 +00:00
|
|
|
return super.totalSupply();
|
|
|
|
}
|
2020-09-21 15:41:38 +00:00
|
|
|
|
2020-10-08 13:41:48 +00:00
|
|
|
/**
|
2020-09-21 15:41:38 +00:00
|
|
|
* @dev returns the principal balance of the user and principal total supply.
|
|
|
|
* @param user the address of the user
|
|
|
|
* @return the principal balance of the user
|
|
|
|
* @return the principal total supply
|
|
|
|
**/
|
2020-10-08 13:41:48 +00:00
|
|
|
function getScaledUserBalanceAndSupply(address user)
|
|
|
|
external
|
|
|
|
override
|
|
|
|
view
|
|
|
|
returns (uint256, uint256)
|
|
|
|
{
|
2020-09-21 15:41:38 +00:00
|
|
|
return (super.balanceOf(user), super.totalSupply());
|
2020-06-30 12:09:28 +00:00
|
|
|
}
|
|
|
|
}
|