Skip to content

Commit

Permalink
Merge branch 'main' into migrate-test-trade-extract-flags
Browse files Browse the repository at this point in the history
  • Loading branch information
fedgiac committed Aug 12, 2024
2 parents ef895e7 + a2e719d commit 0ab732e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 134 deletions.
90 changes: 0 additions & 90 deletions test/GPv2Order.test.ts

This file was deleted.

31 changes: 31 additions & 0 deletions test/GPv2Order/ExtractOrderUidParams.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
pragma solidity ^0.8;

import {GPv2Order} from "src/contracts/libraries/GPv2Order.sol";

import {Helper} from "./Helper.sol";

contract ExtractOrderUidParams is Helper {
function test_round_trip_encode_decode() public view {
bytes32 orderDigest = keccak256("order digest");
address owner = address(uint160(uint256(keccak256("owner"))));
uint32 validTo = uint32(uint256(keccak256("valid to")));
bytes memory orderUid = executor.packOrderUidParamsTest(GPv2Order.UID_LENGTH, orderDigest, owner, validTo);

(bytes32 extractedOrderDigest, address extractedOwner, uint32 extractedValidTo) =
executor.extractOrderUidParamsTest(orderUid);
assertEq(extractedOrderDigest, orderDigest);
assertEq(extractedOwner, owner);
assertEq(extractedValidTo, validTo);
}

function test_reverts_with_uid_longer_than_expected() public {
vm.expectRevert("GPv2: invalid uid");
executor.extractOrderUidParamsTest(new bytes(GPv2Order.UID_LENGTH + 1));
}

function test_reverts_with_uid_shorter_than_expected() public {
vm.expectRevert("GPv2: invalid uid");
executor.extractOrderUidParamsTest(new bytes(GPv2Order.UID_LENGTH - 1));
}
}
38 changes: 34 additions & 4 deletions test/GPv2Order/Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,41 @@ pragma solidity ^0.8;

import {Test} from "forge-std/Test.sol";

import {GPv2OrderTestInterface} from "test/src/GPv2OrderTestInterface.sol";
import {GPv2Order} from "src/contracts/libraries/GPv2Order.sol";

// TODO: move the content of `GPv2OrderTestInterface` here once all tests have been removed.
// solhint-disable-next-line no-empty-blocks
contract Harness is GPv2OrderTestInterface {}
contract Harness {
using GPv2Order for GPv2Order.Data;
using GPv2Order for bytes;

function typeHashTest() external pure returns (bytes32) {
return GPv2Order.TYPE_HASH;
}

function hashTest(GPv2Order.Data memory order, bytes32 domainSeparator)
external
pure
returns (bytes32 orderDigest)
{
orderDigest = order.hash(domainSeparator);
}

function packOrderUidParamsTest(uint256 bufferLength, bytes32 orderDigest, address owner, uint32 validTo)
external
pure
returns (bytes memory orderUid)
{
orderUid = new bytes(bufferLength);
orderUid.packOrderUidParams(orderDigest, owner, validTo);
}

function extractOrderUidParamsTest(bytes calldata orderUid)
external
pure
returns (bytes32 orderDigest, address owner, uint32 validTo)
{
return orderUid.extractOrderUidParams();
}
}

contract Helper is Test {
Harness internal executor;
Expand Down
23 changes: 23 additions & 0 deletions test/GPv2Order/PackOrderUidParams.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
pragma solidity ^0.8;

import {GPv2Order} from "src/contracts/libraries/GPv2Order.sol";

import {Helper} from "./Helper.sol";

contract PackOrderUidParams is Helper {
function test_packOrderUidParams_packs_the_order_UID() public view {
bytes32 orderDigest = keccak256("order digest");
address owner = address(uint160(uint256(keccak256("owner"))));
uint32 validTo = uint32(uint256(keccak256("valid to")));
assertEq(
executor.packOrderUidParamsTest(GPv2Order.UID_LENGTH, orderDigest, owner, validTo),
abi.encodePacked(orderDigest, owner, validTo)
);
}

function test_reverts_if_the_buffer_length_is_wrong() public {
vm.expectRevert("GPv2: uid buffer overflow");
executor.packOrderUidParamsTest(GPv2Order.UID_LENGTH + 1, bytes32(0), address(0), uint32(0));
}
}
40 changes: 0 additions & 40 deletions test/src/GPv2OrderTestInterface.sol

This file was deleted.

0 comments on commit 0ab732e

Please sign in to comment.