From 30479c695429acfc7015cea313c4e91d10324779 Mon Sep 17 00:00:00 2001 From: Tina Zheng Date: Tue, 21 Nov 2023 14:55:06 -0500 Subject: [PATCH] use custom errors and IER1271.isValidSignature.selector --- contracts/base/ERC721Permit.sol | 19 +++++++++++++------ contracts/base/PeripheryPayments.sol | 4 +++- contracts/base/PeripheryValidation.sol | 4 +++- contracts/libraries/TransferHelper.sol | 10 +++++++--- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/contracts/base/ERC721Permit.sol b/contracts/base/ERC721Permit.sol index 015f8394..f9af9273 100644 --- a/contracts/base/ERC721Permit.sol +++ b/contracts/base/ERC721Permit.sol @@ -1,13 +1,18 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.19; -import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; +import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import {Address} from "@openzeppelin/contracts/utils/Address.sol"; import {ChainId} from "../libraries/ChainId.sol"; import {IERC1271} from "../interfaces/external/IERC1271.sol"; import {IERC721Permit} from "../interfaces/IERC721Permit.sol"; +error PermitExpired(); +error InvalidSignature(); +error ApprovalToOwner(); +error Unauthorized(); + /// @title ERC721 with permit /// @notice Nonfungible tokens that support an approve via signature, i.e. permit abstract contract ERC721Permit is ERC721, IERC721Permit { @@ -51,7 +56,7 @@ abstract contract ERC721Permit is ERC721, IERC721Permit { payable override { - require(block.timestamp <= deadline, "Permit expired"); + if (block.timestamp > deadline) revert PermitExpired(); bytes32 digest = keccak256( abi.encodePacked( @@ -61,14 +66,16 @@ abstract contract ERC721Permit is ERC721, IERC721Permit { ) ); address owner = ownerOf(tokenId); - require(spender != owner, "ERC721Permit: approval to current owner"); + if (spender == owner) revert ApprovalToOwner(); if (owner.code.length > 0) { - require(IERC1271(owner).isValidSignature(digest, abi.encodePacked(r, s, v)) == 0x1626ba7e, "Unauthorized"); + if (IERC1271(owner).isValidSignature(digest, abi.encodePacked(r, s, v)) != IERC1271.isValidSignature.selector) { + revert Unauthorized(); + } } else { address recoveredAddress = ecrecover(digest, v, r, s); - require(recoveredAddress != address(0), "Invalid signature"); - require(recoveredAddress == owner, "Unauthorized"); + if (recoveredAddress == address(0)) revert InvalidSignature(); + if (recoveredAddress != owner) revert Unauthorized(); } _approve(spender, tokenId); diff --git a/contracts/base/PeripheryPayments.sol b/contracts/base/PeripheryPayments.sol index 08b54d04..2fa42cd0 100644 --- a/contracts/base/PeripheryPayments.sol +++ b/contracts/base/PeripheryPayments.sol @@ -8,11 +8,13 @@ import {IPeripheryPayments} from "../interfaces/IPeripheryPayments.sol"; import "../libraries/TransferHelper.sol"; +error InsufficientToken(); + abstract contract PeripheryPayments is IPeripheryPayments { /// @inheritdoc IPeripheryPayments function sweepToken(address token, uint256 amountMinimum, address recipient) public payable override { uint256 balanceToken = IERC20(token).balanceOf(address(this)); - require(balanceToken >= amountMinimum, "Insufficient token"); + if (balanceToken < amountMinimum) revert InsufficientToken(); if (balanceToken > 0) { TransferHelper.safeTransfer(IERC20Minimal(token), recipient, balanceToken); diff --git a/contracts/base/PeripheryValidation.sol b/contracts/base/PeripheryValidation.sol index 3f8a8b6d..009c0129 100644 --- a/contracts/base/PeripheryValidation.sol +++ b/contracts/base/PeripheryValidation.sol @@ -1,9 +1,11 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.19; +error TransactionTooOld(); + abstract contract PeripheryValidation { modifier checkDeadline(uint256 deadline) { - require(block.timestamp <= deadline, "Transaction too old"); + if (block.timestamp > deadline) revert TransactionTooOld(); _; } } diff --git a/contracts/libraries/TransferHelper.sol b/contracts/libraries/TransferHelper.sol index 21209410..f7a27e04 100644 --- a/contracts/libraries/TransferHelper.sol +++ b/contracts/libraries/TransferHelper.sol @@ -3,6 +3,10 @@ pragma solidity ^0.8.15; import {IERC20Minimal} from "@uniswap/v4-core/contracts/interfaces/external/IERC20Minimal.sol"; +error TransferFailed(); +error STF(); +error STE(); + /// @title TransferHelper /// @notice Contains helper methods for interacting with ERC20 tokens that do not consistently return true/false /// @dev implementation from https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeTransferLib.sol#L63 @@ -37,7 +41,7 @@ library TransferHelper { ) } - require(success, "TRANSFER_FAILED"); + if (!success) revert TransferFailed(); } /// @notice Transfers tokens from from to a recipient @@ -49,7 +53,7 @@ library TransferHelper { function safeTransferFrom(IERC20Minimal token, address from, address to, uint256 value) internal { (bool success, bytes memory data) = address(token).call(abi.encodeWithSelector(IERC20Minimal.transferFrom.selector, from, to, value)); - require(success && (data.length == 0 || abi.decode(data, (bool))), "STF"); + if (!success || !(data.length == 0 || abi.decode(data, (bool)))) revert STF(); } /// @notice Transfers ETH to the recipient address @@ -58,6 +62,6 @@ library TransferHelper { /// @param value The value to be transferred function safeTransferETH(address to, uint256 value) internal { (bool success,) = to.call{value: value}(new bytes(0)); - require(success, "STE"); + if (!success) revert STE(); } }