Refactored the SafeMath errors

This commit is contained in:
James Towle
2019-06-17 10:57:33 -07:00
committed by Amir Bandeali
parent e916daf5fd
commit a46b13967a
6 changed files with 51 additions and 70 deletions

View File

@@ -6,40 +6,28 @@ import "./RichErrors.sol";
contract MixinSafeMathRichErrors is
RichErrors
{
// bytes4(keccak256("Uint256OverflowError(uint256,uint256)"))
bytes4 internal constant UINT256_OVERFLOW_ERROR =
0x55101607;
// bytes4(keccak256("SafeMathError(uint8,uint256,uint256)"))
bytes4 internal constant SAFE_MATH_ERROR =
0x35a51a70;
// bytes4(keccak256("Uint256UnderflowError(uint256,uint256)"))
bytes4 internal constant UINT256_UNDERFLOW_ERROR =
0x60ee612f;
// solhint-disable func-name-mixedcase
function Uint256OverflowError(
uint256 a,
uint256 b
)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
UINT256_OVERFLOW_ERROR,
a,
b
);
enum SafeMathErrorCodes {
UINT256_ADDITION_OVERFLOW,
UINT256_MULTIPLICATION_OVERFLOW,
UINT256_SUBTRACTION_UNDERFLOW
}
function Uint256UnderflowError(
// solhint-disable func-name-mixedcase
function SafeMathError(
SafeMathErrorCodes errorCode,
uint256 a,
uint256 b
)
internal
pure
returns (bytes memory)
pure returns (bytes memory)
{
return abi.encodeWithSelector(
UINT256_UNDERFLOW_ERROR,
SAFE_MATH_ERROR,
errorCode,
a,
b
);

View File

@@ -17,7 +17,8 @@ contract SafeMath is
}
uint256 c = a * b;
if (c / a != b) {
_rrevert(Uint256OverflowError(
_rrevert(SafeMathError(
SafeMathErrorCodes.UINT256_MULTIPLICATION_OVERFLOW,
a,
b
));
@@ -40,7 +41,8 @@ contract SafeMath is
returns (uint256)
{
if (b > a) {
_rrevert(Uint256UnderflowError(
_rrevert(SafeMathError(
SafeMathErrorCodes.UINT256_SUBTRACTION_UNDERFLOW,
a,
b
));
@@ -55,7 +57,8 @@ contract SafeMath is
{
uint256 c = a + b;
if (c < a) {
_rrevert(Uint256OverflowError(
_rrevert(SafeMathError(
SafeMathErrorCodes.UINT256_ADDITION_OVERFLOW,
a,
b
));