From 3df87a8e5d8f289c80612cbe756f66900c2874e9 Mon Sep 17 00:00:00 2001 From: The3D Date: Thu, 10 Sep 2020 11:25:45 +0200 Subject: [PATCH] Initial commit --- contracts/tokenization/StableDebtToken.sol | 25 ++++++++ contracts/tokenization/VariableDebtToken.sol | 62 ++++--------------- contracts/tokenization/base/DebtTokenBase.sol | 23 ------- .../interfaces/IVariableDebtToken.sol | 12 ---- 4 files changed, 38 insertions(+), 84 deletions(-) diff --git a/contracts/tokenization/StableDebtToken.sol b/contracts/tokenization/StableDebtToken.sol index 4fdcad09..a30219e3 100644 --- a/contracts/tokenization/StableDebtToken.sol +++ b/contracts/tokenization/StableDebtToken.sol @@ -182,4 +182,29 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { emit BurnDebt(user, amount, previousBalance, currentBalance, balanceIncrease); } + + + /** + * @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 = principalBalanceOf(user); + + 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 + ); + } + } diff --git a/contracts/tokenization/VariableDebtToken.sol b/contracts/tokenization/VariableDebtToken.sol index 580151eb..1fcda07a 100644 --- a/contracts/tokenization/VariableDebtToken.sol +++ b/contracts/tokenization/VariableDebtToken.sol @@ -38,27 +38,15 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { * @return the debt balance of the user **/ function balanceOf(address user) public virtual override view returns (uint256) { - uint256 userBalance = principalBalanceOf(user); - uint256 index = _usersData[user]; - if (userBalance == 0) { + uint256 scaledBalance = principalBalanceOf(user); + + if (scaledBalance == 0) { return 0; } return - userBalance - .wadToRay() - .rayMul(POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET)) - .rayDiv(index) - .rayToWad(); - } - - /** - * @dev returns the index of the last user action - * @return the user index - **/ - - function getUserIndex(address user) external virtual override view returns (uint256) { - return _usersData[user]; + scaledBalance + .rayMul(POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET)); } /** @@ -66,20 +54,12 @@ 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) external override onlyLendingPool { - ( - uint256 previousBalance, - uint256 currentBalance, - uint256 balanceIncrease - ) = _calculateBalanceIncrease(user); + function mint(address user, uint256 amount) external override onlyLendingPool { + + uint256 index = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET); - _mint(user, amount.add(balanceIncrease)); - - uint256 newUserIndex = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET); - require(newUserIndex < (1 << 128), "Debt token: Index overflow"); - _usersData[user] = newUserIndex; - - emit MintDebt(user, amount, previousBalance, currentBalance, balanceIncrease, newUserIndex); + _mint(user, amount.rayDiv(index)); + emit MintDebt(user, amount, index); } /** @@ -88,26 +68,10 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { * @param amount the amount of debt being burned **/ function burn(address user, uint256 amount) external override onlyLendingPool { - ( - uint256 previousBalance, - uint256 currentBalance, - uint256 balanceIncrease - ) = _calculateBalanceIncrease(user); - if (balanceIncrease > amount) { - _mint(user, balanceIncrease.sub(amount)); - } else { - _burn(user, amount.sub(balanceIncrease)); - } + uint256 index = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET); + _burn(user, amount.rayDiv(index)); - uint256 newUserIndex = 0; - //if user not repaid everything - if (currentBalance != amount) { - newUserIndex = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET); - require(newUserIndex < (1 << 128), "Debt token: Index overflow"); - } - _usersData[user] = newUserIndex; - - emit BurnDebt(user, amount, previousBalance, currentBalance, balanceIncrease, newUserIndex); + emit BurnDebt(user, amount, index); } } diff --git a/contracts/tokenization/base/DebtTokenBase.sol b/contracts/tokenization/base/DebtTokenBase.sol index 04009023..ec021951 100644 --- a/contracts/tokenization/base/DebtTokenBase.sol +++ b/contracts/tokenization/base/DebtTokenBase.sol @@ -104,27 +104,4 @@ abstract contract DebtTokenBase is ERC20, VersionedInitializable { spender; subtractedValue; 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 = principalBalanceOf(user); - - 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 - ); - } } diff --git a/contracts/tokenization/interfaces/IVariableDebtToken.sol b/contracts/tokenization/interfaces/IVariableDebtToken.sol index fdd86a0d..e979b4a5 100644 --- a/contracts/tokenization/interfaces/IVariableDebtToken.sol +++ b/contracts/tokenization/interfaces/IVariableDebtToken.sol @@ -12,17 +12,11 @@ interface IVariableDebtToken { * @dev emitted when new variable debt is minted * @param user the user receiving the debt * @param amount the amount of debt being minted - * @param previousBalance the previous balance of the user - * @param currentBalance the current balance of the user - * @param balanceIncrease the debt accumulated since the last action * @param index the index of the user **/ event MintDebt( address user, uint256 amount, - uint256 previousBalance, - uint256 currentBalance, - uint256 balanceIncrease, uint256 index ); @@ -30,17 +24,11 @@ interface IVariableDebtToken { * @dev emitted when variable debt is burnt * @param user the user which debt has been burned * @param amount the amount of debt being burned - * @param previousBalance the previous balance of the user - * @param currentBalance the current balance of the user - * @param balanceIncrease the debt accumulated since the last action * @param index the index of the user **/ event BurnDebt( address user, uint256 amount, - uint256 previousBalance, - uint256 currentBalance, - uint256 balanceIncrease, uint256 index );