Skip to content

Commit

Permalink
Merge pull request #58 from ProjectOpenSea/gas-updateStatus-scaledown
Browse files Browse the repository at this point in the history
Improve `_updateStatus` scale-down
  • Loading branch information
0age authored Mar 5, 2024
2 parents 51dc0b5 + a564382 commit fbb182b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
8 changes: 2 additions & 6 deletions reference/lib/ReferenceOrderValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,9 @@ contract ReferenceOrderValidator is
|| denominator > type(uint120).max
) {
// Derive greatest common divisor using euclidean algorithm.
uint256 scaleDown = _greatestCommonDivisor(
numerator,
_greatestCommonDivisor(filledNumerator, denominator)
);
uint256 scaleDown = _greatestCommonDivisor(filledNumerator, denominator);

// Scale all fractional values down by gcd.
numerator = numerator / scaleDown;
// Scale new filled fractional values down by gcd.
filledNumerator = filledNumerator / scaleDown;
denominator = denominator / scaleDown;

Expand Down
8 changes: 3 additions & 5 deletions src/core/lib/OrderValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,13 @@ contract OrderValidator is Executor, ZoneInteraction {
out := _a
}

// Determine the amount to scale down the fill fractions.
let scaleDown :=
gcd(numerator, gcd(filledNumerator, denominator))
// Determine the amount to scale down the new filled fraction.
let scaleDown := gcd(filledNumerator, denominator)

// Ensure that the divisor is at least one.
let safeScaleDown := add(scaleDown, iszero(scaleDown))

// Scale all fractional values down by gcd.
numerator := div(numerator, safeScaleDown)
// Scale new filled fractional values down by gcd.
filledNumerator := div(filledNumerator, safeScaleDown)
denominator := div(denominator, safeScaleDown)

Expand Down
6 changes: 3 additions & 3 deletions test/foundry/FulfillAdvancedOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ contract FulfillAdvancedOrder is BaseOrderTest {
address(0)
);

// Assert six-tenths of the order has been fulfilled.
// Assert three-fifths of the order has been fulfilled.
{
(
bool isValidated,
Expand All @@ -497,9 +497,9 @@ contract FulfillAdvancedOrder is BaseOrderTest {
) = context.consideration.getOrderStatus(orderHash);
assertTrue(isValidated);
assertFalse(isCancelled);
assertEq(totalFilled, 6);
assertEq(totalFilled, 3);

assertEq(totalSize, 10);
assertEq(totalSize, 5);
assertEq(60, test1155_1.balanceOf(address(this), 1));
}
}
Expand Down
12 changes: 6 additions & 6 deletions test/foundry/FulfillAvailableAdvancedOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ contract FulfillAvailableAdvancedOrder is BaseOrderTest {
100
);

// Assert six-tenths of the offer has been fulfilled.
// Assert three-fifths of the offer has been fulfilled.
{
(
bool isValidated,
Expand All @@ -1006,9 +1006,9 @@ contract FulfillAvailableAdvancedOrder is BaseOrderTest {
) = context.consideration.getOrderStatus(orderHash);
assertTrue(isValidated);
assertFalse(isCancelled);
assertEq(totalFilled, 6);
assertEq(totalFilled, 3);

assertEq(totalSize, 10);
assertEq(totalSize, 5);
assertEq(60, test1155_1.balanceOf(address(this), 1));
}
}
Expand Down Expand Up @@ -1093,7 +1093,7 @@ contract FulfillAvailableAdvancedOrder is BaseOrderTest {
100
);

// Assert six-tenths of the offer has been fulfilled.
// Assert three-fifths of the offer has been fulfilled.
{
(
bool isValidated,
Expand All @@ -1103,9 +1103,9 @@ contract FulfillAvailableAdvancedOrder is BaseOrderTest {
) = context.consideration.getOrderStatus(orderHash);
assertTrue(isValidated);
assertFalse(isCancelled);
assertEq(totalFilled, 6);
assertEq(totalFilled, 3);

assertEq(totalSize, 10);
assertEq(totalSize, 5);
assertEq(60, test1155_1.balanceOf(address(this), 1));
}
}
Expand Down

0 comments on commit fbb182b

Please sign in to comment.