@0x:contracts-exchange Updated LibMath to use library rich errors
				
					
				
			This commit is contained in:
		
				
					committed by
					
						
						Amir Bandeali
					
				
			
			
				
	
			
			
			
						parent
						
							87bf940f89
						
					
				
				
					commit
					6384518ee1
				
			@@ -19,12 +19,12 @@
 | 
				
			|||||||
pragma solidity ^0.5.9;
 | 
					pragma solidity ^0.5.9;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
 | 
					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
 | 
					contract LibMath is
 | 
				
			||||||
    SafeMath,
 | 
					    SafeMath
 | 
				
			||||||
    MixinLibMathRichErrors
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    /// @dev Calculates partial value given a numerator and denominator rounded down.
 | 
					    /// @dev Calculates partial value given a numerator and denominator rounded down.
 | 
				
			||||||
    ///      Reverts if rounding error is >= 0.1%
 | 
					    ///      Reverts if rounding error is >= 0.1%
 | 
				
			||||||
@@ -42,7 +42,7 @@ contract LibMath is
 | 
				
			|||||||
        returns (uint256 partialAmount)
 | 
					        returns (uint256 partialAmount)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (denominator == 0) {
 | 
					        if (denominator == 0) {
 | 
				
			||||||
            _rrevert(DivisionByZeroError());
 | 
					            LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (_isRoundingErrorFloor(
 | 
					        if (_isRoundingErrorFloor(
 | 
				
			||||||
@@ -50,13 +50,13 @@ contract LibMath is
 | 
				
			|||||||
                denominator,
 | 
					                denominator,
 | 
				
			||||||
                target
 | 
					                target
 | 
				
			||||||
        )) {
 | 
					        )) {
 | 
				
			||||||
            _rrevert(RoundingError(
 | 
					            LibRichErrors._rrevert(LibMathRichErrors.RoundingError(
 | 
				
			||||||
                numerator,
 | 
					                numerator,
 | 
				
			||||||
                denominator,
 | 
					                denominator,
 | 
				
			||||||
                target
 | 
					                target
 | 
				
			||||||
            ));
 | 
					            ));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					
 | 
				
			||||||
        partialAmount = _safeDiv(
 | 
					        partialAmount = _safeDiv(
 | 
				
			||||||
            _safeMul(numerator, target),
 | 
					            _safeMul(numerator, target),
 | 
				
			||||||
            denominator
 | 
					            denominator
 | 
				
			||||||
@@ -80,7 +80,7 @@ contract LibMath is
 | 
				
			|||||||
        returns (uint256 partialAmount)
 | 
					        returns (uint256 partialAmount)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (denominator == 0) {
 | 
					        if (denominator == 0) {
 | 
				
			||||||
            _rrevert(DivisionByZeroError());
 | 
					            LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (_isRoundingErrorCeil(
 | 
					        if (_isRoundingErrorCeil(
 | 
				
			||||||
@@ -88,13 +88,13 @@ contract LibMath is
 | 
				
			|||||||
                denominator,
 | 
					                denominator,
 | 
				
			||||||
                target
 | 
					                target
 | 
				
			||||||
        )) {
 | 
					        )) {
 | 
				
			||||||
            _rrevert(RoundingError(
 | 
					            LibRichErrors._rrevert(LibMathRichErrors.RoundingError(
 | 
				
			||||||
                numerator,
 | 
					                numerator,
 | 
				
			||||||
                denominator,
 | 
					                denominator,
 | 
				
			||||||
                target
 | 
					                target
 | 
				
			||||||
            ));
 | 
					            ));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					
 | 
				
			||||||
        // _safeDiv computes `floor(a / b)`. We use the identity (a, b integer):
 | 
					        // _safeDiv computes `floor(a / b)`. We use the identity (a, b integer):
 | 
				
			||||||
        //       ceil(a / b) = floor((a + b - 1) / b)
 | 
					        //       ceil(a / b) = floor((a + b - 1) / b)
 | 
				
			||||||
        // To implement `ceil(a / b)` using _safeDiv.
 | 
					        // To implement `ceil(a / b)` using _safeDiv.
 | 
				
			||||||
@@ -123,7 +123,7 @@ contract LibMath is
 | 
				
			|||||||
        returns (uint256 partialAmount)
 | 
					        returns (uint256 partialAmount)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (denominator == 0) {
 | 
					        if (denominator == 0) {
 | 
				
			||||||
            _rrevert(DivisionByZeroError());
 | 
					            LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        partialAmount = _safeDiv(
 | 
					        partialAmount = _safeDiv(
 | 
				
			||||||
@@ -132,7 +132,7 @@ contract LibMath is
 | 
				
			|||||||
        );
 | 
					        );
 | 
				
			||||||
        return partialAmount;
 | 
					        return partialAmount;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
    /// @dev Calculates partial value given a numerator and denominator rounded down.
 | 
					    /// @dev Calculates partial value given a numerator and denominator rounded down.
 | 
				
			||||||
    /// @param numerator Numerator.
 | 
					    /// @param numerator Numerator.
 | 
				
			||||||
    /// @param denominator Denominator.
 | 
					    /// @param denominator Denominator.
 | 
				
			||||||
@@ -148,7 +148,7 @@ contract LibMath is
 | 
				
			|||||||
        returns (uint256 partialAmount)
 | 
					        returns (uint256 partialAmount)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (denominator == 0) {
 | 
					        if (denominator == 0) {
 | 
				
			||||||
            _rrevert(DivisionByZeroError());
 | 
					            LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // _safeDiv computes `floor(a / b)`. We use the identity (a, b integer):
 | 
					        // _safeDiv computes `floor(a / b)`. We use the identity (a, b integer):
 | 
				
			||||||
@@ -163,7 +163,7 @@ contract LibMath is
 | 
				
			|||||||
        );
 | 
					        );
 | 
				
			||||||
        return partialAmount;
 | 
					        return partialAmount;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
    /// @dev Checks if rounding error >= 0.1% when rounding down.
 | 
					    /// @dev Checks if rounding error >= 0.1% when rounding down.
 | 
				
			||||||
    /// @param numerator Numerator.
 | 
					    /// @param numerator Numerator.
 | 
				
			||||||
    /// @param denominator Denominator.
 | 
					    /// @param denominator Denominator.
 | 
				
			||||||
@@ -179,9 +179,9 @@ contract LibMath is
 | 
				
			|||||||
        returns (bool isError)
 | 
					        returns (bool isError)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (denominator == 0) {
 | 
					        if (denominator == 0) {
 | 
				
			||||||
            _rrevert(DivisionByZeroError());
 | 
					            LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					
 | 
				
			||||||
        // The absolute rounding error is the difference between the rounded
 | 
					        // The absolute rounding error is the difference between the rounded
 | 
				
			||||||
        // value and the ideal value. The relative rounding error is the
 | 
					        // value and the ideal value. The relative rounding error is the
 | 
				
			||||||
        // absolute rounding error divided by the absolute value of the
 | 
					        // absolute rounding error divided by the absolute value of the
 | 
				
			||||||
@@ -194,11 +194,11 @@ contract LibMath is
 | 
				
			|||||||
        // When the ideal value is zero, we require the absolute error to
 | 
					        // When the ideal value is zero, we require the absolute error to
 | 
				
			||||||
        // be zero. Fortunately, this is always the case. The ideal value is
 | 
					        // be zero. Fortunately, this is always the case. The ideal value is
 | 
				
			||||||
        // zero iff `numerator == 0` and/or `target == 0`. In this case the
 | 
					        // zero iff `numerator == 0` and/or `target == 0`. In this case the
 | 
				
			||||||
        // remainder and absolute error are also zero. 
 | 
					        // remainder and absolute error are also zero.
 | 
				
			||||||
        if (target == 0 || numerator == 0) {
 | 
					        if (target == 0 || numerator == 0) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					
 | 
				
			||||||
        // Otherwise, we want the relative rounding error to be strictly
 | 
					        // Otherwise, we want the relative rounding error to be strictly
 | 
				
			||||||
        // less than 0.1%.
 | 
					        // less than 0.1%.
 | 
				
			||||||
        // The relative error is `remainder / (numerator * target)`.
 | 
					        // The relative error is `remainder / (numerator * target)`.
 | 
				
			||||||
@@ -216,7 +216,7 @@ contract LibMath is
 | 
				
			|||||||
        isError = _safeMul(1000, remainder) >= _safeMul(numerator, target);
 | 
					        isError = _safeMul(1000, remainder) >= _safeMul(numerator, target);
 | 
				
			||||||
        return isError;
 | 
					        return isError;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
    /// @dev Checks if rounding error >= 0.1% when rounding up.
 | 
					    /// @dev Checks if rounding error >= 0.1% when rounding up.
 | 
				
			||||||
    /// @param numerator Numerator.
 | 
					    /// @param numerator Numerator.
 | 
				
			||||||
    /// @param denominator Denominator.
 | 
					    /// @param denominator Denominator.
 | 
				
			||||||
@@ -232,9 +232,9 @@ contract LibMath is
 | 
				
			|||||||
        returns (bool isError)
 | 
					        returns (bool isError)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (denominator == 0) {
 | 
					        if (denominator == 0) {
 | 
				
			||||||
            _rrevert(DivisionByZeroError());
 | 
					            LibRichErrors._rrevert(LibMathRichErrors.DivisionByZeroError());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					
 | 
				
			||||||
        // See the comments in `isRoundingError`.
 | 
					        // See the comments in `isRoundingError`.
 | 
				
			||||||
        if (target == 0 || numerator == 0) {
 | 
					        if (target == 0 || numerator == 0) {
 | 
				
			||||||
            // When either is zero, the ideal value and rounded value are zero
 | 
					            // When either is zero, the ideal value and rounded value are zero
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,13 +1,20 @@
 | 
				
			|||||||
pragma solidity ^0.5.9;
 | 
					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
 | 
					    // solhint-disable func-name-mixedcase
 | 
				
			||||||
    function DivisionByZeroError()
 | 
					    function DivisionByZeroError()
 | 
				
			||||||
        internal
 | 
					        internal
 | 
				
			||||||
@@ -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;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -25,8 +25,6 @@ import "./interfaces/IExchangeRichErrors.sol";
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
library LibExchangeRichErrors {
 | 
					library LibExchangeRichErrors {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /*** Selector Getters ***/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // bytes4(keccak256("SignatureError(uint8,bytes32,address,bytes)"))
 | 
					    // bytes4(keccak256("SignatureError(uint8,bytes32,address,bytes)"))
 | 
				
			||||||
    bytes4 internal constant SIGNATURE_ERROR_SELECTOR =
 | 
					    bytes4 internal constant SIGNATURE_ERROR_SELECTOR =
 | 
				
			||||||
        0x7e5a2318;
 | 
					        0x7e5a2318;
 | 
				
			||||||
@@ -244,9 +242,6 @@ library LibExchangeRichErrors {
 | 
				
			|||||||
        return INCOMPLETE_FILL_ERROR_SELECTOR;
 | 
					        return INCOMPLETE_FILL_ERROR_SELECTOR;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /*** Rich Error Functions ***/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // solhint-disable func-name-mixedcase
 | 
					 | 
				
			||||||
    function SignatureError(
 | 
					    function SignatureError(
 | 
				
			||||||
        IExchangeRichErrors.SignatureErrorCodes errorCode,
 | 
					        IExchangeRichErrors.SignatureErrorCodes errorCode,
 | 
				
			||||||
        bytes32 hash,
 | 
					        bytes32 hash,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user