From da36ee104f7538c8e0fd31d7e9b2c9e535f41b5f Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Mon, 1 Apr 2024 15:38:36 -0700 Subject: [PATCH] simplify getTransferValidationFunction(), add test --- .../ERC1155ContractMetadataCloneable.sol | 10 ++--- src/interfaces/ITransferValidator.sol | 9 ++-- src/lib/ERC1155ContractMetadata.sol | 10 ++--- src/lib/ERC721ContractMetadata.sol | 8 ++-- src/test/MockTransferValidator.sol | 18 +++++--- test/foundry/TokenTransferValidator.t.sol | 45 ++++++++++++++++--- 6 files changed, 64 insertions(+), 36 deletions(-) diff --git a/src/clones/ERC1155ContractMetadataCloneable.sol b/src/clones/ERC1155ContractMetadataCloneable.sol index 8bbe42d6..9c404fa3 100644 --- a/src/clones/ERC1155ContractMetadataCloneable.sol +++ b/src/clones/ERC1155ContractMetadataCloneable.sol @@ -14,7 +14,7 @@ import { ILegacyCreatorToken } from "../interfaces/ICreatorToken.sol"; -import { ITransferValidator } from "../interfaces/ITransferValidator.sol"; +import { ITransferValidator1155 } from "../interfaces/ITransferValidator.sol"; import { TokenTransferValidator } from "../lib/TokenTransferValidator.sol"; @@ -324,11 +324,7 @@ contract ERC1155ContractMetadataCloneable is pure returns (bytes4 functionSignature, bool isViewFunction) { - functionSignature = bytes4( - keccak256( - "validateTransfer(address,address,address,uint256,uint256)" - ) - ); + functionSignature = ITransferValidator1155.validateTransfer.selector; isViewFunction = true; } @@ -360,7 +356,7 @@ contract ERC1155ContractMetadataCloneable is // Call the transfer validator if one is set. if (_transferValidator != address(0)) { for (uint256 i = 0; i < ids.length; i++) { - ITransferValidator(_transferValidator).validateTransfer( + ITransferValidator1155(_transferValidator).validateTransfer( msg.sender, from, to, diff --git a/src/interfaces/ITransferValidator.sol b/src/interfaces/ITransferValidator.sol index 6d5ba6a1..6ecdab3d 100644 --- a/src/interfaces/ITransferValidator.sol +++ b/src/interfaces/ITransferValidator.sol @@ -1,17 +1,18 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.17; -interface ITransferValidator { - /// @notice Ensure that a transfer has been authorized for a specific tokenId. +interface ITransferValidator721 { + /// @notice Ensure that a transfer has been authorized for a specific tokenId function validateTransfer( address caller, address from, address to, uint256 tokenId ) external view; +} - /// @notice Ensure that a transfer has been authorized for a specific amount of - /// a specific tokenId, and reduce the transferable amount remaining. +interface ITransferValidator1155 { + /// @notice Ensure that a transfer has been authorized for a specific amount of a specific tokenId, and reduce the transferable amount remaining function validateTransfer( address caller, address from, diff --git a/src/lib/ERC1155ContractMetadata.sol b/src/lib/ERC1155ContractMetadata.sol index 98d76233..83aab9cd 100644 --- a/src/lib/ERC1155ContractMetadata.sol +++ b/src/lib/ERC1155ContractMetadata.sol @@ -14,7 +14,7 @@ import { ILegacyCreatorToken } from "../interfaces/ICreatorToken.sol"; -import { ITransferValidator } from "../interfaces/ITransferValidator.sol"; +import { ITransferValidator1155 } from "../interfaces/ITransferValidator.sol"; import { TokenTransferValidator } from "./TokenTransferValidator.sol"; @@ -322,11 +322,7 @@ contract ERC1155ContractMetadata is pure returns (bytes4 functionSignature, bool isViewFunction) { - functionSignature = bytes4( - keccak256( - "validateTransfer(address,address,address,uint256,uint256)" - ) - ); + functionSignature = ITransferValidator1155.validateTransfer.selector; isViewFunction = true; } @@ -358,7 +354,7 @@ contract ERC1155ContractMetadata is // Call the transfer validator if one is set. if (_transferValidator != address(0)) { for (uint256 i = 0; i < ids.length; i++) { - ITransferValidator(_transferValidator).validateTransfer( + ITransferValidator1155(_transferValidator).validateTransfer( msg.sender, from, to, diff --git a/src/lib/ERC721ContractMetadata.sol b/src/lib/ERC721ContractMetadata.sol index aa668a27..ee086380 100644 --- a/src/lib/ERC721ContractMetadata.sol +++ b/src/lib/ERC721ContractMetadata.sol @@ -12,7 +12,7 @@ import { ILegacyCreatorToken } from "../interfaces/ICreatorToken.sol"; -import { ITransferValidator } from "../interfaces/ITransferValidator.sol"; +import { ITransferValidator721 } from "../interfaces/ITransferValidator.sol"; import { TokenTransferValidator } from "./TokenTransferValidator.sol"; @@ -290,9 +290,7 @@ contract ERC721ContractMetadata is pure returns (bytes4 functionSignature, bool isViewFunction) { - functionSignature = bytes4( - keccak256("validateTransfer(address,address,address,uint256)") - ); + functionSignature = ITransferValidator721.validateTransfer.selector; isViewFunction = false; } @@ -317,7 +315,7 @@ contract ERC721ContractMetadata is if (from != address(0) && to != address(0)) { // Call the transfer validator if one is set. if (_transferValidator != address(0)) { - ITransferValidator(_transferValidator).validateTransfer( + ITransferValidator721(_transferValidator).validateTransfer( msg.sender, from, to, diff --git a/src/test/MockTransferValidator.sol b/src/test/MockTransferValidator.sol index 78f16dba..30e8d1d8 100644 --- a/src/test/MockTransferValidator.sol +++ b/src/test/MockTransferValidator.sol @@ -1,9 +1,15 @@ // SPDX-License-Identifier: Unlicense pragma solidity ^0.8.17; -import { ITransferValidator } from "../interfaces/ITransferValidator.sol"; +import { + ITransferValidator721, + ITransferValidator1155 +} from "../interfaces/ITransferValidator.sol"; -contract MockTransferValidator is ITransferValidator { +contract MockTransferValidator is + ITransferValidator721, + ITransferValidator1155 +{ bool internal _revertOnValidate; constructor(bool revertOnValidate) { @@ -25,10 +31,10 @@ contract MockTransferValidator is ITransferValidator { } function validateTransfer( - address, /* caller */ - address, /* from */ - address, /* to */ - uint256, /* tokenId */ + address /* caller */, + address /* from */, + address /* to */, + uint256 /* tokenId */, uint256 /* amount */ ) external view { if (_revertOnValidate) { diff --git a/test/foundry/TokenTransferValidator.t.sol b/test/foundry/TokenTransferValidator.t.sol index d79464f7..7d049bea 100644 --- a/test/foundry/TokenTransferValidator.t.sol +++ b/test/foundry/TokenTransferValidator.t.sol @@ -7,6 +7,11 @@ import { ERC721SeaDrop } from "seadrop/ERC721SeaDrop.sol"; import { ERC1155SeaDrop } from "seadrop/ERC1155SeaDrop.sol"; +import { + ITransferValidator721, + ITransferValidator1155 +} from "seadrop/interfaces/ITransferValidator.sol"; + import { MockTransferValidator } from "seadrop/test/MockTransferValidator.sol"; import { Ownable } from "solady/src/auth/Ownable.sol"; @@ -32,11 +37,7 @@ contract ERC1155SeaDropWithMint is ERC1155SeaDrop { string memory symbol_ ) ERC1155SeaDrop(allowedConfigurer, allowedSeaport, name_, symbol_) {} - function mint( - address to, - uint256 id, - uint256 amount - ) public onlyOwner { + function mint(address to, uint256 id, uint256 amount) public onlyOwner { _mint(to, id, amount, ""); } } @@ -55,10 +56,20 @@ contract TokenTransferValidatorTest is SeaDrop721Test { function setUp() public override { super.setUp(); - token721 = new ERC721SeaDropWithMint(address(0), allowedSeaport, "", ""); + token721 = new ERC721SeaDropWithMint( + address(0), + allowedSeaport, + "", + "" + ); token721.setMaxSupply(10); - token1155 = new ERC1155SeaDropWithMint(address(0), allowedSeaport, "", ""); + token1155 = new ERC1155SeaDropWithMint( + address(0), + allowedSeaport, + "", + "" + ); token1155.setMaxSupply(1, 10); token1155.setMaxSupply(2, 10); } @@ -185,4 +196,24 @@ contract TokenTransferValidatorTest is SeaDrop721Test { "" ); } + + function testERC721GetTransferValidationFunction() public { + (bytes4 functionSignature, bool isViewFunction) = token721 + .getTransferValidationFunction(); + assertEq( + functionSignature, + ITransferValidator721.validateTransfer.selector + ); + assertEq(isViewFunction, false); + } + + function testERC1155GetTransferValidationFunction() public { + (bytes4 functionSignature, bool isViewFunction) = token1155 + .getTransferValidationFunction(); + assertEq( + functionSignature, + ITransferValidator1155.validateTransfer.selector + ); + assertEq(isViewFunction, true); + } }