Skip to content

Commit

Permalink
More reactor tests, address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
codyborn committed Sep 3, 2024
1 parent 29f6ed9 commit 13ca0a9
Show file tree
Hide file tree
Showing 24 changed files with 622 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192981
192980
2 changes: 1 addition & 1 deletion .forge-snapshots/Base-V3DutchOrder-ExecuteBatch.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
219434
219432
Original file line number Diff line number Diff line change
@@ -1 +1 @@
231444
231439
Original file line number Diff line number Diff line change
@@ -1 +1 @@
287371
287363
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212953
212951
2 changes: 1 addition & 1 deletion .forge-snapshots/Base-V3DutchOrder-ExecuteSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
159369
159368
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144927
144926
Original file line number Diff line number Diff line change
@@ -1 +1 @@
168675
168674
2 changes: 1 addition & 1 deletion .forge-snapshots/Base-V3DutchOrder-RevertInvalidNonce.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37963
37962
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayBounded.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5778
1070
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayFullyDecayed.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14782
14810
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayFullyDecayedNegative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14634
14662
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-ExtendedMultiPointDutchDecay.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
219186
208112
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-LocateCurvePositionMulti.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24798
24086
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-LocateCurvePositionSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8820
8758
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-MultiPointDutchDecay.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51976
51174
7 changes: 4 additions & 3 deletions src/lib/NonlinearDutchDecayLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {OutputToken, InputToken} from "../base/ReactorStructs.sol";
import {V3DutchOutput, V3DutchInput, NonlinearDutchDecay} from "../lib/V3DutchOrderLib.sol";
import {FixedPointMathLib} from "solmate/src/utils/FixedPointMathLib.sol";
import {sub} from "./MathExt.sol";
import {Uint16Array, fromUnderlying} from "../types/Uint16Array.sol";
import {Uint16ArrayLibrary, Uint16Array, fromUnderlying} from "../types/Uint16Array.sol";

/// @notice thrown when the decay curve is invalid
error InvalidDecayCurve();
Expand All @@ -14,6 +14,7 @@ error InvalidDecayCurve();
library NonlinearDutchDecayLib {
using FixedPointMathLib for uint256;
using {sub} for uint256;
using Uint16ArrayLibrary for Uint16Array;

/// @notice locates the current position on the curve and calculates the decay
/// @param curve The curve to search
Expand Down Expand Up @@ -61,12 +62,12 @@ library NonlinearDutchDecayLib {
returns (uint16 prev, uint16 next)
{
Uint16Array relativeBlocks = fromUnderlying(curve.relativeBlocks);
while (next < curve.relativeAmounts.length) {
uint16 curveLength = uint16(curve.relativeAmounts.length);
for (; next < curveLength; next++) {
if (relativeBlocks.getElement(next) >= currentRelativeBlock) {
return (prev, next);
}
prev = next;
next++;
}
return (next - 1, next - 1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/V3DutchOrderLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ library V3DutchOrderLib {
function hash(V3DutchOutput[] memory outputs) internal pure returns (bytes32) {
unchecked {
bytes memory packedHashes = new bytes(32 * outputs.length);

for (uint256 i = 0; i < outputs.length; i++) {
uint256 outputsLength = outputs.length;
for (uint256 i = 0; i < outputsLength; i++) {
bytes32 outputHash = hash(outputs[i]);
assembly {
mstore(add(add(packedHashes, 0x20), mul(i, 0x20)), outputHash)
Expand Down
9 changes: 5 additions & 4 deletions src/reactors/V3DutchOrderReactor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ contract V3DutchOrderReactor is BaseReactor {
// hash the order _before_ overriding amounts, as this is the hash the user would have signed
bytes32 orderHash = order.hash();

if (order.info.deadline < block.timestamp) {
revert DeadlineReached();
}
_validateOrder(orderHash, order);
_updateWithCosignerAmounts(order);

Expand Down Expand Up @@ -108,8 +105,12 @@ contract V3DutchOrderReactor is BaseReactor {

/// @notice validate the dutch order fields
/// - deadline must have not passed
/// - cosigner is valid if specified
/// @dev Throws if the order is invalid
function _validateOrder(bytes32 orderHash, V3DutchOrder memory order) internal pure {
function _validateOrder(bytes32 orderHash, V3DutchOrder memory order) internal view {
if (order.info.deadline < block.timestamp) {
revert DeadlineReached();
}
(bytes32 r, bytes32 s) = abi.decode(order.cosignature, (bytes32, bytes32));
uint8 v = uint8(order.cosignature[64]);
// cosigner signs over (orderHash || cosignerData)
Expand Down
2 changes: 0 additions & 2 deletions src/types/Uint16Array.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ pragma solidity ^0.8.0;
/// @dev An uint16 array of max 16 values packed into a single uint256
type Uint16Array is uint256;

using Uint16ArrayLibrary for Uint16Array global;

error IndexOutOfBounds();
error InvalidArrLength();

Expand Down
1 change: 0 additions & 1 deletion test/lib/NonLinearDutchDecayLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ contract NonlinearDutchDecayLibTest is Test, GasSnapshot {
vm.assume(decayAmount > 0);
vm.assume(decayAmount < 2 ** 255 - 1);
vm.assume(startAmount <= UINT256_MAX - decayAmount);
vm.assume(decayDuration > 0);

NonlinearDutchDecay memory curve = CurveBuilder.singlePointCurve(decayDuration, 0 - int256(decayAmount));
snapStart("V3-DutchDecayBounded");
Expand Down
4 changes: 3 additions & 1 deletion test/lib/Uint16Array.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {Test} from "forge-std/Test.sol";
import {NonlinearDutchDecayLib} from "../../src/lib/NonlinearDutchDecayLib.sol";
import {V3DutchOutput, V3DutchInput, NonlinearDutchDecay} from "../../src/lib/V3DutchOrderLib.sol";
import {ArrayBuilder} from "../util/ArrayBuilder.sol";
import {Uint16Array, toUint16Array, InvalidArrLength, IndexOutOfBounds} from "../../src/types/Uint16Array.sol";
import {Uint16ArrayLibrary, Uint16Array, toUint16Array, InvalidArrLength, IndexOutOfBounds} from "../../src/types/Uint16Array.sol";

contract Uint16ArrayTest is Test {
using Uint16ArrayLibrary for Uint16Array;

function testGetElement(uint16 value, uint16 length) public {
vm.assume(length <= 16);
Uint16Array packedArr = toUint16Array(ArrayBuilder.fillUint16(length, value));
Expand Down
Loading

0 comments on commit 13ca0a9

Please sign in to comment.