diff --git a/contracts/lib/AmountDeriver.sol b/contracts/lib/AmountDeriver.sol index 60c25c22c..a41747d3e 100644 --- a/contracts/lib/AmountDeriver.sol +++ b/contracts/lib/AmountDeriver.sol @@ -6,6 +6,8 @@ import { AmountDerivationErrors } from "../interfaces/AmountDerivationErrors.sol"; +import "./ConsiderationConstants.sol"; + /** * @title AmountDeriver * @author 0age @@ -98,16 +100,13 @@ contract AmountDeriver is AmountDerivationErrors { // Ensure fraction can be applied to the value with no remainder. Note // that the denominator cannot be zero. - bool exact; assembly { // Ensure new value contains no remainder via mulmod operator. // Credit to @hrkrshnn + @axic for proposing this optimal solution. - exact := iszero(mulmod(value, numerator, denominator)) - } - - // Ensure that division gave a final result with no remainder. - if (!exact) { - revert InexactFraction(); + if mulmod(value, numerator, denominator) { + mstore(0, InexactFraction_error_signature) + revert(0, InexactFraction_error_len) + } } // Multiply the numerator by the value and ensure no overflow occurs. diff --git a/contracts/lib/ConsiderationConstants.sol b/contracts/lib/ConsiderationConstants.sol index a727ebf0c..cfbbc54d8 100644 --- a/contracts/lib/ConsiderationConstants.sol +++ b/contracts/lib/ConsiderationConstants.sol @@ -344,3 +344,10 @@ uint256 constant Conduit_transferItem_from_ptr = 0x40; uint256 constant Conduit_transferItem_to_ptr = 0x60; uint256 constant Conduit_transferItem_identifier_ptr = 0x80; uint256 constant Conduit_transferItem_amount_ptr = 0xa0; + +// Declare constant for errors related to amount derivation. +// error InexactFraction() @ AmountDerivationErrors.sol +uint256 constant InexactFraction_error_signature = ( + 0xc63cf08900000000000000000000000000000000000000000000000000000000 +); +uint256 constant InexactFraction_error_len = 0x20;