Added comments

This commit is contained in:
emilio 2020-08-03 09:08:07 +02:00
parent c86884ef28
commit d8424d43a5

View File

@ -6,8 +6,9 @@ import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
/** /**
* @title PercentageMath library * @title PercentageMath library
* @author Aave * @author Aave
* @notice Provides functions to calculate percentages. Percentages needs to be defined by default with 2 decimals of precision (100.00) * @notice Provides functions to calculate percentages.
* @dev Operations are rounded up * @dev Percentages are defined by default with 2 decimals of precision (100.00). The precision is indicated by PERCENTAGE_FACTOR
* @dev Operations are rounded half up
**/ **/
library PercentageMath { library PercentageMath {
@ -16,13 +17,25 @@ library PercentageMath {
uint256 constant PERCENTAGE_FACTOR = 1e4; //percentage plus two decimals uint256 constant PERCENTAGE_FACTOR = 1e4; //percentage plus two decimals
uint256 constant HALF_PERCENT = PERCENTAGE_FACTOR / 2; uint256 constant HALF_PERCENT = PERCENTAGE_FACTOR / 2;
function percentMul(uint256 value, uint256 percentage) internal pure returns (uint256) { /**
return HALF_PERCENT.add(value.mul(percentage)).div(PERCENTAGE_FACTOR); * @dev executes a percentage multiplication
* @param _value the value of which the percentage needs to be calculated
* @param _percentage the percentage of the value to be calculated
* @return the _percentage of _value
**/
function percentMul(uint256 _value, uint256 _percentage) internal pure returns (uint256) {
return HALF_PERCENT.add(_value.mul(_percentage)).div(PERCENTAGE_FACTOR);
} }
function percentDiv(uint256 value, uint256 percentage) internal pure returns (uint256) { /**
uint256 halfPercentage = percentage / 2; * @dev executes a percentage division
* @param _value the value of which the percentage needs to be calculated
* @param _percentage the percentage of the value to be calculated
* @return the _value divided the _percentage
**/
function percentDiv(uint256 _value, uint256 _percentage) internal pure returns (uint256) {
uint256 halfPercentage = _percentage / 2;
return halfPercentage.add(value.mul(PERCENTAGE_FACTOR)).div(percentage); return halfPercentage.add(_value.mul(PERCENTAGE_FACTOR)).div(_percentage);
} }
} }