-
Notifications
You must be signed in to change notification settings - Fork 742
/
PercentageMath.sol
60 lines (45 loc) · 1.84 KB
/
PercentageMath.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
import {Errors} from '../helpers/Errors.sol';
/**
* @title PercentageMath library
* @author Aave
* @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
**/
library PercentageMath {
uint256 constant PERCENTAGE_FACTOR = 1e4; //percentage plus two decimals
uint256 constant HALF_PERCENT = PERCENTAGE_FACTOR / 2;
/**
* @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) {
if (value == 0) {
return 0;
}
uint256 result = value * percentage;
require(result / value == percentage, Errors.MULTIPLICATION_OVERFLOW);
result += HALF_PERCENT;
require(result >= HALF_PERCENT, Errors.ADDITION_OVERFLOW);
return result / PERCENTAGE_FACTOR;
}
/**
* @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) {
require(percentage != 0, Errors.DIVISION_BY_ZERO);
uint256 halfPercentage = percentage / 2;
uint256 result = value * PERCENTAGE_FACTOR;
require(result / PERCENTAGE_FACTOR == value, Errors.MULTIPLICATION_OVERFLOW);
result += halfPercentage;
require(result >= halfPercentage, Errors.ADDITION_OVERFLOW);
return result / percentage;
}
}