Skip to content

Commit

Permalink
I remove useAfterTokenTransfersHook to simplify afterTokenTransferHook.
Browse files Browse the repository at this point in the history
  • Loading branch information
SweetmanTech committed Aug 6, 2023
1 parent cf4b016 commit ee6aee3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 59 deletions.
12 changes: 2 additions & 10 deletions src/ERC721ACH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,8 @@ contract ERC721ACH is IERC721ACH, ERC721AC {
IAfterTokenTransfersHook hook = IAfterTokenTransfersHook(
hooks[HookType.AfterTokenTransfers]
);
if (
address(hook) != address(0) &&
hook.useAfterTokenTransfersHook(from, to, startTokenId, quantity)
) {
hook.afterTokenTransfersOverrideHook(
from,
to,
startTokenId,
quantity
);
if (address(hook) != address(0)) {
hook.afterTokenTransfersHook(from, to, startTokenId, quantity);
}
}

Expand Down
19 changes: 1 addition & 18 deletions src/interfaces/IAfterTokenTransfersHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.15;
/// @title IAfterTokenTransfersHook
/// @dev Interface that defines hooks to be executed After token transfers.
interface IAfterTokenTransfersHook {

/**
@notice Emitted when the after token transfers hook is used.
@param from Address from which the tokens are being transferred.
Expand All @@ -20,22 +19,6 @@ interface IAfterTokenTransfersHook {
uint256 quantity
);

/**
@notice Checks if the token transfers function should use the custom hook.
@param from Address from which the tokens are being transferred.
@param to Address to which the tokens are being transferred.
@param startTokenId The starting ID of the tokens being transferred.
@param quantity The number of tokens being transferred.
@return A boolean indicating whether or not to use the custom hook for the token transfers function.
*/
function useAfterTokenTransfersHook(
address from,
address to,
uint256 startTokenId,
uint256 quantity
) external view returns (bool);

/**
@notice Provides a custom implementation for the token transfers process.
@param from Address from which the tokens are being transferred.
Expand All @@ -44,7 +27,7 @@ interface IAfterTokenTransfersHook {
@param quantity The number of tokens being transferred.
*/
function afterTokenTransfersOverrideHook(
function afterTokenTransfersHook(
address from,
address to,
uint256 startTokenId,
Expand Down
31 changes: 14 additions & 17 deletions test/hooks/AfterTokenTransfers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ import {AfterTokenTransfersHookMock} from "../utils/hooks/AfterTokenTransfersHoo

import {IERC721ACH} from "../../src/interfaces/IERC721ACH.sol";


contract AfterTokenTransfersHookTest is DSTest {
Vm public constant vm = Vm(HEVM_ADDRESS);
address public constant DEFAULT_OWNER_ADDRESS = address(0xC0FFEE);
address public constant DEFAULT_BUYER_ADDRESS = address(0xBABE);
ERC721ACHMock erc721Mock;
AfterTokenTransfersHookMock hookMock;

IERC721ACH.HookType constant AfterTokenTransfers = IERC721ACH.HookType.AfterTokenTransfers;

IERC721ACH.HookType constant AfterTokenTransfers =
IERC721ACH.HookType.AfterTokenTransfers;

function setUp() public {
erc721Mock = new ERC721ACHMock(DEFAULT_OWNER_ADDRESS);
Expand All @@ -32,31 +31,31 @@ contract AfterTokenTransfersHookTest is DSTest {
function test_setAfterTokenTransfersHook() public {
assertEq(address(0), address(erc721Mock.getHook(AfterTokenTransfers)));

// calling an admin function without being the contract owner should revert
// calling an admin function without being the contract owner should revert
vm.expectRevert();
erc721Mock.setHook(AfterTokenTransfers, address(hookMock));

vm.prank(DEFAULT_OWNER_ADDRESS);
erc721Mock.setHook(AfterTokenTransfers, address(hookMock));

assertEq(address(hookMock), address(erc721Mock.getHook(AfterTokenTransfers)));
assertEq(
address(hookMock),
address(erc721Mock.getHook(AfterTokenTransfers))
);
}

function test_afterTokenTransfersHook(
function test_afterTokenTransfersHook(
uint256 startTokenId,
uint256 quantity
) public {
vm.assume(quantity > 0);
vm.assume(startTokenId > 0);
vm.assume(quantity < 10_000);
vm.assume(quantity >= startTokenId);


// Mint some tokens first
test_setAfterTokenTransfersHook();
erc721Mock.mint(DEFAULT_BUYER_ADDRESS, quantity);


vm.prank(DEFAULT_BUYER_ADDRESS);
erc721Mock.transferFrom(
DEFAULT_BUYER_ADDRESS,
Expand All @@ -67,21 +66,19 @@ contract AfterTokenTransfersHookTest is DSTest {
assertEq(DEFAULT_OWNER_ADDRESS, erc721Mock.ownerOf(startTokenId));

// Verify hook override
test_setAfterTokenTransfersHook();

hookMock.setHooksEnabled(true);
vm.expectRevert(
AfterTokenTransfersHookMock.AfterTokenTransfersHook_Executed.selector
AfterTokenTransfersHookMock
.AfterTokenTransfersHook_Executed
.selector
);
vm.prank(DEFAULT_OWNER_ADDRESS);
erc721Mock.transferFrom(
DEFAULT_OWNER_ADDRESS,
DEFAULT_BUYER_ADDRESS,
startTokenId
);


}




}
16 changes: 2 additions & 14 deletions test/utils/hooks/AfterTokenTransfersHookMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,14 @@ contract AfterTokenTransfersHookMock is IAfterTokenTransfersHook {
function setHooksEnabled(bool _enabled) public {
hooksEnabled = _enabled;
}

/// @notice Check if the AfterTokenTransfers function should use hook.
/// @dev Returns whether or not to use the hook for AfterTokenTransfers function
function useAfterTokenTransfersHook(
address,
address,
uint256,
uint256
) external view override returns (bool) {
return hooksEnabled;
}


/// @notice custom implementation for AfterTokenTransfers Hook.
function afterTokenTransfersOverrideHook(
function afterTokenTransfersHook(
address,
address,
uint256,
uint256
) external pure override {
) external pure override {
revert AfterTokenTransfersHook_Executed();
}
}

0 comments on commit ee6aee3

Please sign in to comment.