Added RichErrors to SafeMath

This commit is contained in:
James Towle 2019-06-05 16:54:08 -07:00 committed by Amir Bandeali
parent a0602c8863
commit 6e4b6929d2
2 changed files with 70 additions and 13 deletions

View File

@ -0,0 +1,47 @@
pragma solidity ^0.5.9;
import "./RichErrors.sol";
contract MixinSafeMathRichErrors is
RichErrors
{
// bytes4(keccak256("Uint256OverflowError(uint256,uint256)"))
bytes4 internal constant UINT256_OVERFLOW_ERROR =
0x55101607;
// 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
);
}
function Uint256UnderflowError(
uint256 a,
uint256 b
)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
UINT256_UNDERFLOW_ERROR,
a,
b
);
}
}

View File

@ -1,7 +1,11 @@
pragma solidity ^0.5.9;
import "./MixinSafeMathRichErrors.sol";
contract SafeMath {
contract SafeMath is
MixinSafeMathRichErrors
{
function _safeMul(uint256 a, uint256 b)
internal
@ -12,10 +16,12 @@ contract SafeMath {
return 0;
}
uint256 c = a * b;
require(
c / a == b,
"UINT256_OVERFLOW"
);
if (c / a != b) {
_rrevert(Uint256OverflowError(
a,
b
));
}
return c;
}
@ -33,10 +39,12 @@ contract SafeMath {
pure
returns (uint256)
{
require(
b <= a,
"UINT256_UNDERFLOW"
);
if (b > a) {
_rrevert(Uint256UnderflowError(
a,
b
));
}
return a - b;
}
@ -46,10 +54,12 @@ contract SafeMath {
returns (uint256)
{
uint256 c = a + b;
require(
c >= a,
"UINT256_OVERFLOW"
);
if (c < a) {
_rrevert(Uint256OverflowError(
a,
b
));
}
return c;
}