From 735a00b5f34a4b17a9314ffdb3b4b97457aadf35 Mon Sep 17 00:00:00 2001 From: andyk Date: Thu, 20 Aug 2020 17:26:06 +0300 Subject: [PATCH] fix minor debt token issues --- contracts/tokenization/StableDebtToken.sol | 46 ++---------- contracts/tokenization/VariableDebtToken.sol | 72 ++++--------------- contracts/tokenization/base/DebtTokenBase.sol | 31 ++++++++ 3 files changed, 51 insertions(+), 98 deletions(-) diff --git a/contracts/tokenization/StableDebtToken.sol b/contracts/tokenization/StableDebtToken.sol index 51f7cdd0..961ba5cd 100644 --- a/contracts/tokenization/StableDebtToken.sol +++ b/contracts/tokenization/StableDebtToken.sol @@ -42,7 +42,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { * @param _balanceIncrease the debt increase since the last update * @param _newRate the rate of the debt after the minting **/ - event mintDebt( + event MintDebt( address _user, uint256 _amount, uint256 _previousBalance, @@ -59,7 +59,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { * @param _currentBalance the current balance of the user * @param _balanceIncrease the debt increase since the last update **/ - event burnDebt( + event BurnDebt( address _user, uint256 _amount, uint256 _previousBalance, @@ -178,7 +178,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { _mint(_user, _amount.add(balanceIncrease)); - emit mintDebt( + emit MintDebt( _user, _amount, previousBalance, @@ -203,16 +203,12 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { uint256 supplyBeforeBurn = totalSupply.add(balanceIncrease); uint256 supplyAfterBurn = supplyBeforeBurn.sub(_amount); - uint256 newSupply = totalSupply.add(balanceIncrease).sub(_amount); - - uint256 amountInRay = _amount.wadToRay(); - - if (newSupply == 0) { + if (supplyAfterBurn == 0) { avgStableRate = 0; } else { avgStableRate = avgStableRate .rayMul(supplyBeforeBurn.wadToRay()) - .sub(usersData[_user].currentRate.rayMul(amountInRay)) + .sub(usersData[_user].currentRate.rayMul(_amount.wadToRay())) .rayDiv(supplyAfterBurn.wadToRay()); } @@ -227,36 +223,6 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { _burn(_user, _amount.sub(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 - ); + emit BurnDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease); } } diff --git a/contracts/tokenization/VariableDebtToken.sol b/contracts/tokenization/VariableDebtToken.sol index efca5db5..10005959 100644 --- a/contracts/tokenization/VariableDebtToken.sol +++ b/contracts/tokenization/VariableDebtToken.sol @@ -31,7 +31,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { * @param _balanceIncrease the debt accumulated since the last action * @param _index the index of the user **/ - event mintDebt( + event MintDebt( address _user, uint256 _amount, uint256 _previousBalance, @@ -49,7 +49,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { * @param _balanceIncrease the debt accumulated since the last action * @param _index the index of the user **/ - event burnDebt( + event BurnDebt( address _user, uint256 _amount, uint256 _previousBalance, @@ -95,7 +95,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { * @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]; } @@ -104,7 +104,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { * @param _user the user receiving the debt * @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 currentBalance, @@ -113,16 +113,10 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { _mint(_user, _amount.add(balanceIncrease)); - userIndexes[_user] = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress); + uint256 newUserIndex = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress); + userIndexes[_user] = newUserIndex; - emit mintDebt( - _user, - _amount, - previousBalance, - currentBalance, - balanceIncrease, - userIndexes[_user] - ); + emit MintDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease, newUserIndex); } /** @@ -130,7 +124,7 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { * @param _user the user which debt is burnt * @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 currentBalance, @@ -143,51 +137,13 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { _burn(_user, _amount.sub(balanceIncrease)); } - //if user repaid everything - if (currentBalance == _amount) { - userIndexes[_user] = 0; - } else { - userIndexes[_user] = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress); + uint256 newUserIndex = 0; + //if user not repaid everything + if (currentBalance != _amount) { + newUserIndex = pool.getReserveNormalizedVariableDebt(underlyingAssetAddress); } + userIndexes[_user] = newUserIndex; - emit burnDebt( - _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 - ); + emit BurnDebt(_user, _amount, previousBalance, currentBalance, balanceIncrease, newUserIndex); } } diff --git a/contracts/tokenization/base/DebtTokenBase.sol b/contracts/tokenization/base/DebtTokenBase.sol index e7995ea5..63412174 100644 --- a/contracts/tokenization/base/DebtTokenBase.sol +++ b/contracts/tokenization/base/DebtTokenBase.sol @@ -140,4 +140,35 @@ abstract contract DebtTokenBase is IERC20, VersionedInitializable { { 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 + ); + } }