@0x:contracts-exchange Updated LibMath to use library rich errors

This commit is contained in:
James Towle
2019-07-11 17:34:35 -05:00
committed by Amir Bandeali
parent 87bf940f89
commit 6384518ee1
4 changed files with 33 additions and 48 deletions

View File

@@ -19,12 +19,12 @@
pragma solidity ^0.5.9;
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
import "./MixinLibMathRichErrors.sol";
import "@0x/contracts-utils/contracts/src/LibRichErrors.sol";
import "./LibMathRichErrors.sol";
contract LibMath is
SafeMath,
MixinLibMathRichErrors
SafeMath
{
/// @dev Calculates partial value given a numerator and denominator rounded down.
/// Reverts if rounding error is >= 0.1%
@@ -42,7 +42,7 @@ contract LibMath is
returns (uint256 partialAmount)
{
if (denominator == 0) {
_rrevert(DivisionByZeroError());
LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
}
if (_isRoundingErrorFloor(
@@ -50,7 +50,7 @@ contract LibMath is
denominator,
target
)) {
_rrevert(RoundingError(
LibRichErrors._rrevert(LibMathRichErrors.RoundingError(
numerator,
denominator,
target
@@ -80,7 +80,7 @@ contract LibMath is
returns (uint256 partialAmount)
{
if (denominator == 0) {
_rrevert(DivisionByZeroError());
LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
}
if (_isRoundingErrorCeil(
@@ -88,7 +88,7 @@ contract LibMath is
denominator,
target
)) {
_rrevert(RoundingError(
LibRichErrors._rrevert(LibMathRichErrors.RoundingError(
numerator,
denominator,
target
@@ -123,7 +123,7 @@ contract LibMath is
returns (uint256 partialAmount)
{
if (denominator == 0) {
_rrevert(DivisionByZeroError());
LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
}
partialAmount = _safeDiv(
@@ -148,7 +148,7 @@ contract LibMath is
returns (uint256 partialAmount)
{
if (denominator == 0) {
_rrevert(DivisionByZeroError());
LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
}
// _safeDiv computes `floor(a / b)`. We use the identity (a, b integer):
@@ -179,7 +179,7 @@ contract LibMath is
returns (bool isError)
{
if (denominator == 0) {
_rrevert(DivisionByZeroError());
LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
}
// The absolute rounding error is the difference between the rounded
@@ -232,7 +232,7 @@ contract LibMath is
returns (bool isError)
{
if (denominator == 0) {
_rrevert(DivisionByZeroError());
LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
}
// See the comments in `isRoundingError`.

View File

@@ -1,13 +1,20 @@
pragma solidity ^0.5.9;
import "@0x/contracts-utils/contracts/src/RichErrors.sol";
import "./interfaces/IMixinLibMathRichErrors.sol";
library LibMathRichErrors {
// bytes4(keccak256("DivisionByZeroError()"))
bytes internal constant DIVISION_BY_ZERO_ERROR =
hex"a791837c";
// bytes4(keccak256("DivisionByZeroError()"))
bytes4 internal constant DIVISION_BY_ZERO_ERROR_SELECTOR =
0xa791837c;
// bytes4(keccak256("RoundingError(uint256,uint256,uint256)"))
bytes4 internal constant ROUNDING_ERROR_SELECTOR =
0x339f3de2;
contract MixinLibMathRichErrors is
IMixinLibMathRichErrors,
RichErrors
{
// solhint-disable func-name-mixedcase
function DivisionByZeroError()
internal

View File

@@ -1,17 +0,0 @@
pragma solidity ^0.5.9;
contract IMixinLibMathRichErrors {
// bytes4(keccak256("DivisionByZeroError()"))
bytes internal constant DIVISION_BY_ZERO_ERROR =
hex"a791837c";
// bytes4(keccak256("DivisionByZeroError()"))
bytes4 internal constant DIVISION_BY_ZERO_ERROR_SELECTOR =
0xa791837c;
// bytes4(keccak256("RoundingError(uint256,uint256,uint256)"))
bytes4 internal constant ROUNDING_ERROR_SELECTOR =
0x339f3de2;
}

View File

@@ -25,8 +25,6 @@ import "./interfaces/IExchangeRichErrors.sol";
library LibExchangeRichErrors {
/*** Selector Getters ***/
// bytes4(keccak256("SignatureError(uint8,bytes32,address,bytes)"))
bytes4 internal constant SIGNATURE_ERROR_SELECTOR =
0x7e5a2318;
@@ -244,9 +242,6 @@ library LibExchangeRichErrors {
return INCOMPLETE_FILL_ERROR_SELECTOR;
}
/*** Rich Error Functions ***/
// solhint-disable func-name-mixedcase
function SignatureError(
IExchangeRichErrors.SignatureErrorCodes errorCode,
bytes32 hash,