Merge branch 'feat/debt-token-fixes' into 'master'

Debt token fixes

See merge request aave-tech/protocol-v2!21
This commit is contained in:
The-3D 2020-08-20 14:35:31 +00:00
commit dc64b7f992
3 changed files with 51 additions and 98 deletions

View File

@ -42,7 +42,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
* @param _balanceIncrease the debt increase since the last update * @param _balanceIncrease the debt increase since the last update
* @param _newRate the rate of the debt after the minting * @param _newRate the rate of the debt after the minting
**/ **/
event mintDebt( event MintDebt(
address _user, address _user,
uint256 _amount, uint256 _amount,
uint256 _previousBalance, uint256 _previousBalance,
@ -59,7 +59,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
* @param _currentBalance the current balance of the user * @param _currentBalance the current balance of the user
* @param _balanceIncrease the debt increase since the last update * @param _balanceIncrease the debt increase since the last update
**/ **/
event burnDebt( event BurnDebt(
address _user, address _user,
uint256 _amount, uint256 _amount,
uint256 _previousBalance, uint256 _previousBalance,
@ -178,7 +178,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
_mint(_user, _amount.add(balanceIncrease)); _mint(_user, _amount.add(balanceIncrease));
emit mintDebt( emit MintDebt(
_user, _user,
_amount, _amount,
previousBalance, previousBalance,
@ -203,16 +203,12 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
uint256 supplyBeforeBurn = totalSupply.add(balanceIncrease); uint256 supplyBeforeBurn = totalSupply.add(balanceIncrease);
uint256 supplyAfterBurn = supplyBeforeBurn.sub(_amount); uint256 supplyAfterBurn = supplyBeforeBurn.sub(_amount);
uint256 newSupply = totalSupply.add(balanceIncrease).sub(_amount); if (supplyAfterBurn == 0) {
uint256 amountInRay = _amount.wadToRay();
if (newSupply == 0) {
avgStableRate = 0; avgStableRate = 0;
} else { } else {
avgStableRate = avgStableRate avgStableRate = avgStableRate
.rayMul(supplyBeforeBurn.wadToRay()) .rayMul(supplyBeforeBurn.wadToRay())
.sub(usersData[_user].currentRate.rayMul(amountInRay)) .sub(usersData[_user].currentRate.rayMul(_amount.wadToRay()))
.rayDiv(supplyAfterBurn.wadToRay()); .rayDiv(supplyAfterBurn.wadToRay());
} }
@ -227,36 +223,6 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
_burn(_user, _amount.sub(balanceIncrease)); _burn(_user, _amount.sub(balanceIncrease));
} }
emit burnDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease); emit BurnDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease);
}
/**
* @dev calculates the increase in balance since the last user action
* @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 _calculateBalanceIncrease(address _user)
internal
view
returns (
uint256,
uint256,
uint256
)
{
uint256 previousPrincipalBalance = balances[_user];
if (previousPrincipalBalance == 0) {
return (0, 0, 0);
}
//calculate the accrued interest since the last accumulation
uint256 balanceIncrease = balanceOf(_user).sub(previousPrincipalBalance);
return (
previousPrincipalBalance,
previousPrincipalBalance.add(balanceIncrease),
balanceIncrease
);
} }
} }

View File

@ -31,7 +31,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
* @param _balanceIncrease the debt accumulated since the last action * @param _balanceIncrease the debt accumulated since the last action
* @param _index the index of the user * @param _index the index of the user
**/ **/
event mintDebt( event MintDebt(
address _user, address _user,
uint256 _amount, uint256 _amount,
uint256 _previousBalance, uint256 _previousBalance,
@ -49,7 +49,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
* @param _balanceIncrease the debt accumulated since the last action * @param _balanceIncrease the debt accumulated since the last action
* @param _index the index of the user * @param _index the index of the user
**/ **/
event burnDebt( event BurnDebt(
address _user, address _user,
uint256 _amount, uint256 _amount,
uint256 _previousBalance, uint256 _previousBalance,
@ -95,7 +95,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
* @return the user index * @return the user index
**/ **/
function getUserIndex(address _user) public virtual override view returns (uint256) { function getUserIndex(address _user) external virtual override view returns (uint256) {
return userIndexes[_user]; return userIndexes[_user];
} }
@ -104,7 +104,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
* @param _user the user receiving the debt * @param _user the user receiving the debt
* @param _amount the amount of debt being minted * @param _amount the amount of debt being minted
**/ **/
function mint(address _user, uint256 _amount) public override onlyLendingPool { function mint(address _user, uint256 _amount) external override onlyLendingPool {
( (
uint256 previousBalance, uint256 previousBalance,
uint256 currentBalance, uint256 currentBalance,
@ -113,16 +113,10 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
_mint(_user, _amount.add(balanceIncrease)); _mint(_user, _amount.add(balanceIncrease));
userIndexes[_user] = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress); uint256 newUserIndex = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress);
userIndexes[_user] = newUserIndex;
emit mintDebt( emit MintDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease, newUserIndex);
_user,
_amount,
previousBalance,
currentBalance,
balanceIncrease,
userIndexes[_user]
);
} }
/** /**
@ -130,7 +124,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
* @param _user the user which debt is burnt * @param _user the user which debt is burnt
* @param _amount the amount of debt being burned * @param _amount the amount of debt being burned
**/ **/
function burn(address _user, uint256 _amount) public override onlyLendingPool { function burn(address _user, uint256 _amount) external override onlyLendingPool {
( (
uint256 previousBalance, uint256 previousBalance,
uint256 currentBalance, uint256 currentBalance,
@ -143,51 +137,13 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
_burn(_user, _amount.sub(balanceIncrease)); _burn(_user, _amount.sub(balanceIncrease));
} }
//if user repaid everything uint256 newUserIndex = 0;
if (currentBalance == _amount) { //if user not repaid everything
userIndexes[_user] = 0; if (currentBalance != _amount) {
} else { newUserIndex = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress);
userIndexes[_user] = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress);
} }
userIndexes[_user] = newUserIndex;
emit burnDebt( emit BurnDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease, newUserIndex);
_user,
_amount,
previousBalance,
currentBalance,
balanceIncrease,
userIndexes[_user]
);
}
/**
* @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
**/
function _calculateBalanceIncrease(address _user)
internal
view
returns (
uint256,
uint256,
uint256
)
{
uint256 previousPrincipalBalance = balances[_user];
if (previousPrincipalBalance == 0) {
return (0, 0, 0);
}
//calculate the accrued interest since the last accumulation
uint256 balanceIncrease = balanceOf(_user).sub(previousPrincipalBalance);
return (
previousPrincipalBalance,
previousPrincipalBalance.add(balanceIncrease),
balanceIncrease
);
} }
} }

View File

@ -140,4 +140,35 @@ abstract contract DebtTokenBase is IERC20, VersionedInitializable {
{ {
revert('ALLOWANCE_NOT_SUPPORTED'); revert('ALLOWANCE_NOT_SUPPORTED');
} }
/**
* @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
**/
function _calculateBalanceIncrease(address _user)
internal
view
returns (
uint256,
uint256,
uint256
)
{
uint256 previousPrincipalBalance = balances[_user];
if (previousPrincipalBalance == 0) {
return (0, 0, 0);
}
//calculate the accrued interest since the last accumulation
uint256 balanceIncrease = balanceOf(_user).sub(previousPrincipalBalance);
return (
previousPrincipalBalance,
previousPrincipalBalance.add(balanceIncrease),
balanceIncrease
);
}
} }