mirror of
				https://github.com/Instadapp/aave-protocol-v2.git
				synced 2024-07-29 21:47:30 +00:00 
			
		
		
		
	Initial commit
This commit is contained in:
		
							parent
							
								
									23f99d30f0
								
							
						
					
					
						commit
						3df87a8e5d
					
				|  | @ -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 | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -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)); | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|  | @ -67,19 +55,11 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken { | |||
|    * @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); | ||||
|      | ||||
|     _mint(user, amount.add(balanceIncrease)); | ||||
|     uint256 index = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET); | ||||
| 
 | ||||
|     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); | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -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 | ||||
|     ); | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -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 | ||||
|   ); | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 The3D
						The3D