From 0e3dd9bcb772f33f3ce0444975876449b10135c4 Mon Sep 17 00:00:00 2001 From: Federico Giacon <58218759+fedgiac@users.noreply.github.com> Date: Thu, 15 Aug 2024 09:07:29 +0000 Subject: [PATCH] chore: migrate GPv2Signing set pre-signature tests to Foundry (#203) ## Description See title. ## Test Plan CI, compare with removed tests. ## Related Issues #120 --------- Co-authored-by: mfw78 <53399572+mfw78@users.noreply.github.com> --- src/ts/sign.ts | 5 --- test/GPv2Signing.test.ts | 47 ---------------------- test/GPv2Signing/SetPreSignature.t.sol | 54 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 test/GPv2Signing/SetPreSignature.t.sol diff --git a/src/ts/sign.ts b/src/ts/sign.ts index 401270f4..a41e04f2 100644 --- a/src/ts/sign.ts +++ b/src/ts/sign.ts @@ -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. */ diff --git a/test/GPv2Signing.test.ts b/test/GPv2Signing.test.ts index 1a7c9b4b..87de48f8 100644 --- a/test/GPv2Signing.test.ts +++ b/test/GPv2Signing.test.ts @@ -6,7 +6,6 @@ import { EIP1271_MAGICVALUE, OrderBalance, OrderKind, - PRE_SIGNED, SettlementEncoder, SigningScheme, TypedDataDomain, @@ -14,7 +13,6 @@ import { domain, encodeEip1271SignatureData, hashOrder, - packOrderUidParams, signOrder, } from "../src/ts"; @@ -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 diff --git a/test/GPv2Signing/SetPreSignature.t.sol b/test/GPv2Signing/SetPreSignature.t.sol new file mode 100644 index 00000000..e653a1dd --- /dev/null +++ b/test/GPv2Signing/SetPreSignature.t.sol @@ -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); + } +}