aave-protocol-v2/contracts/libraries/PercentageMath.sol

42 lines
1.5 KiB
Solidity
Raw Normal View History

2020-07-27 10:32:54 +00:00
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
/**
* @title PercentageMath library
* @author Aave
2020-08-03 07:08:07 +00:00
* @notice Provides functions to calculate percentages.
* @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
2020-07-27 10:32:54 +00:00
**/
library PercentageMath {
using SafeMath for uint256;
uint256 constant PERCENTAGE_FACTOR = 1e4; //percentage plus two decimals
2020-07-27 10:32:54 +00:00
uint256 constant HALF_PERCENT = PERCENTAGE_FACTOR / 2;
2020-08-03 07:08:07 +00:00
/**
* @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);
2020-07-27 10:32:54 +00:00
}
2020-08-03 07:08:07 +00:00
/**
* @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;
2020-07-27 10:32:54 +00:00
2020-08-03 07:08:07 +00:00
return halfPercentage.add(_value.mul(PERCENTAGE_FACTOR)).div(_percentage);
2020-07-27 10:32:54 +00:00
}
}