-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Percentage calculation moved to MathUtils library
- Loading branch information
Showing
10 changed files
with
146 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pragma solidity ^0.4.17; | ||
|
||
import "zeppelin-solidity/contracts/math/SafeMath.sol"; | ||
|
||
|
||
library MathUtils { | ||
using SafeMath for uint256; | ||
|
||
// Divisor used for representing percentages | ||
uint256 public constant PERC_DIVISOR = 1000000; | ||
|
||
/* | ||
* @dev Returns whether an amount is a valid percentage out of PERC_DIVISOR | ||
* @param _amount Amount that is supposed to be a percentage | ||
*/ | ||
function validPerc(uint256 _amount) internal view returns (bool) { | ||
return _amount <= PERC_DIVISOR; | ||
} | ||
|
||
/* | ||
* @dev Compute percentage of a value with the percentage represented by a fraction | ||
* @param _amount Amount to take the percentage of | ||
* @param _fracNum Numerator of fraction representing the percentage | ||
* @param _fracDenom Denominator of fraction representing the percentage | ||
*/ | ||
function percOf(uint256 _amount, uint256 _fracNum, uint256 _fracDenom) internal view returns (uint256) { | ||
return _amount.mul(percPoints(_fracNum, _fracDenom)).div(PERC_DIVISOR); | ||
} | ||
|
||
/* | ||
* @dev Compute percentage of a value with the percentage represented by a fraction over PERC_DIVISOR | ||
* @param _amount Amount to take the percentage of | ||
* @param _fracNum Numerator of fraction representing the percentage with PERC_DIVISOR as the denominator | ||
*/ | ||
function percOf(uint256 _amount, uint256 _fracNum) internal view returns (uint256) { | ||
return _amount.mul(_fracNum).div(PERC_DIVISOR); | ||
} | ||
|
||
/* | ||
* @dev Compute percentage representation of a fraction | ||
* @param _fracNum Numerator of fraction represeting the percentage | ||
* @param _fracDenom Denominator of fraction represeting the percentage | ||
*/ | ||
function percPoints(uint256 _fracNum, uint256 _fracDenom) internal view returns (uint256) { | ||
return _fracNum.mul(PERC_DIVISOR).div(_fracDenom); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.