Skip to content

Commit

Permalink
chore: migrate GPv2Signing set pre-signature tests to Foundry (#203)
Browse files Browse the repository at this point in the history
## Description

See title.

## Test Plan

CI, compare with removed tests.

## Related Issues

#120

---------

Co-authored-by: mfw78 <[email protected]>
  • Loading branch information
fedgiac and mfw78 authored Aug 15, 2024
1 parent 8cbba5c commit 0e3dd9b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 52 deletions.
5 changes: 0 additions & 5 deletions src/ts/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ export const EIP1271_MAGICVALUE = ethers.utils.hexDataSlice(
4,
);

/**
* Marker value indicating a presignature is set.
*/
export const PRE_SIGNED = ethers.utils.id("GPv2Signing.Scheme.PreSign");

/**
* The signing scheme used to sign the order.
*/
Expand Down
47 changes: 0 additions & 47 deletions test/GPv2Signing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import {
EIP1271_MAGICVALUE,
OrderBalance,
OrderKind,
PRE_SIGNED,
SettlementEncoder,
SigningScheme,
TypedDataDomain,
computeOrderUid,
domain,
encodeEip1271SignatureData,
hashOrder,
packOrderUidParams,
signOrder,
} from "../src/ts";

Expand All @@ -38,51 +36,6 @@ describe("GPv2Signing", () => {
testDomain = domain(chainId, signing.address);
});

describe("setPreSignature", () => {
const [owner, nonOwner] = traders;
const orderUid = packOrderUidParams({
orderDigest: ethers.constants.HashZero,
owner: owner.address,
validTo: 0xffffffff,
});

it("should set the pre-signature", async () => {
await signing.connect(owner).setPreSignature(orderUid, true);
expect(await signing.preSignature(orderUid)).to.equal(PRE_SIGNED);
});

it("should unset the pre-signature", async () => {
await signing.connect(owner).setPreSignature(orderUid, true);
await signing.connect(owner).setPreSignature(orderUid, false);
expect(await signing.preSignature(orderUid)).to.equal(
ethers.constants.Zero,
);
});

it("should emit a PreSignature event", async () => {
await expect(signing.connect(owner).setPreSignature(orderUid, true))
.to.emit(signing, "PreSignature")
.withArgs(owner.address, orderUid, true);

await expect(signing.connect(owner).setPreSignature(orderUid, false))
.to.emit(signing, "PreSignature")
.withArgs(owner.address, orderUid, false);
});

it("should emit a PreSignature event even if storage doesn't change", async () => {
await signing.connect(owner).setPreSignature(orderUid, true);
await expect(signing.connect(owner).setPreSignature(orderUid, true))
.to.emit(signing, "PreSignature")
.withArgs(owner.address, orderUid, true);
});

it("should revert if the order owner is not the transaction sender", async () => {
await expect(
signing.connect(nonOwner).setPreSignature(orderUid, true),
).to.be.revertedWith("cannot presign order");
});
});

describe("recoverOrderFromTrade", () => {
it("should round-trip encode order data", async () => {
// NOTE: Pay extra attention to use all bytes for each field, and that
Expand Down
54 changes: 54 additions & 0 deletions test/GPv2Signing/SetPreSignature.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
pragma solidity ^0.8;

import {GPv2Signing} from "src/contracts/mixins/GPv2Signing.sol";

import {Helper} from "./Helper.sol";
import {Order} from "test/libraries/Order.sol";
import {Sign} from "test/libraries/Sign.sol";

contract SetPreSignature is Helper {
address private immutable owner = makeAddr("GPv2Signing.SetPreSignature owner");
bytes private orderUid =
Order.computeOrderUid(keccak256("GPv2Signing.SetPreSignature order hash"), owner, type(uint32).max);

function test_should_set_the_pre_signature() public {
vm.prank(owner);
executor.setPreSignature(orderUid, true);
assertEq(executor.preSignature(orderUid), Sign.PRE_SIGNED);
}

function test_should_unset_the_pre_signature() public {
vm.prank(owner);
executor.setPreSignature(orderUid, true);
vm.prank(owner);
executor.setPreSignature(orderUid, false);
assertEq(executor.preSignature(orderUid), 0);
}

function test_should_emit_a_pre_signature_event() public {
vm.prank(owner);
vm.expectEmit(address(executor));
emit GPv2Signing.PreSignature(owner, orderUid, true);
executor.setPreSignature(orderUid, true);

vm.prank(owner);
vm.expectEmit(address(executor));
emit GPv2Signing.PreSignature(owner, orderUid, false);
executor.setPreSignature(orderUid, false);
}

function test_should_emit_a_PreSignature_event_even_if_storage_does_not_change() public {
vm.prank(owner);
executor.setPreSignature(orderUid, true);
vm.prank(owner);
vm.expectEmit(address(executor));
emit GPv2Signing.PreSignature(owner, orderUid, true);
executor.setPreSignature(orderUid, true);
}

function test_reverts_if_the_order_owner_is_not_the_transaction_sender() public {
vm.expectRevert("GPv2: cannot presign order");
executor.setPreSignature(orderUid, true);
}
}

0 comments on commit 0e3dd9b

Please sign in to comment.