Skip to content

Commit

Permalink
I clean up and add additional tests for transfers.
Browse files Browse the repository at this point in the history
  • Loading branch information
SweetmanTech committed Aug 6, 2023
1 parent b742345 commit 308b18c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
31 changes: 24 additions & 7 deletions test/hooks/AfterTokenTransfers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,8 @@ contract AfterTokenTransfersHookTest is DSTest, HookUtils {
vm.expectRevert();
erc721Mock.setHook(AfterTokenTransfers, address(hookMock));

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

assertEq(
address(hookMock),
address(erc721Mock.getHook(AfterTokenTransfers))
);
// Verify Success
_setHook(address(erc721Mock), AfterTokenTransfers, address(hookMock));
}

function test_afterTokenTransfersHook(
Expand Down Expand Up @@ -76,4 +71,26 @@ contract AfterTokenTransfersHookTest is DSTest, HookUtils {
.selector
);
}

function test_turn_off_hook(
address _firstOwner,
address _secondOwner,
uint256 startTokenId,
uint256 quantity
) public {
test_afterTokenTransfersHook(
_firstOwner,
_secondOwner,
startTokenId,
quantity
);

_setHook(address(erc721Mock), AfterTokenTransfers, address(0));
_assertNormalTransfer(
address(erc721Mock),
_secondOwner,
_firstOwner,
startTokenId
);
}
}
38 changes: 21 additions & 17 deletions test/hooks/BeforeTokenTransfers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ contract BeforeTokenTransfersHookTest is DSTest, HookUtils {
vm.expectRevert();
erc721Mock.setHook(BeforeTokenTransfers, address(hookMock));

vm.prank(DEFAULT_OWNER_ADDRESS);
erc721Mock.setHook(BeforeTokenTransfers, address(hookMock));
assertEq(
address(hookMock),
address(erc721Mock.getHook(BeforeTokenTransfers))
);
// set hook
_setHook(address(erc721Mock), BeforeTokenTransfers, address(hookMock));
}

function test_beforeTokenTransfersHook(
Expand Down Expand Up @@ -73,17 +69,25 @@ contract BeforeTokenTransfersHookTest is DSTest, HookUtils {
);
}

function _assertHookRevert(
address _from,
address _to,
uint256 _tokenId
) internal {
vm.prank(_from);
vm.expectRevert(
BeforeTokenTransfersHookMock
.BeforeTokenTransfersHook_Executed
.selector
function test_turn_off_hook(
address _firstOwner,
address _secondOwner,
uint256 startTokenId,
uint256 quantity
) public {
test_beforeTokenTransfersHook(
_firstOwner,
_secondOwner,
startTokenId,
quantity
);

_setHook(address(erc721Mock), BeforeTokenTransfers, address(0));
_assertNormalTransfer(
address(erc721Mock),
_secondOwner,
_firstOwner,
startTokenId
);
erc721Mock.transferFrom(_from, _to, _tokenId);
}
}
14 changes: 13 additions & 1 deletion test/utils/HookUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {Vm} from "forge-std/Vm.sol";
import {DSTest} from "ds-test/test.sol";
import {ERC721ACHMock} from "./ERC721ACHMock.sol";
import {IERC721A} from "lib/ERC721A/contracts/IERC721A.sol";
import {BeforeTokenTransfersHookMock} from "../utils/hooks/BeforeTokenTransfersHookMock.sol";
import {IERC721ACH} from "../../src/interfaces/IERC721ACH.sol";

contract HookUtils is DSTest {
Expand Down Expand Up @@ -53,4 +52,17 @@ contract HookUtils is DSTest {
vm.expectRevert(_err);
ERC721ACHMock(_target).transferFrom(_from, _to, _tokenId);
}

function _setHook(
address _target,
IERC721ACH.HookType _type,
address _hook
) internal {
vm.prank(DEFAULT_OWNER_ADDRESS);
ERC721ACHMock(_target).setHook(_type, address(_hook));
assertEq(
address(_hook),
address(ERC721ACHMock(_target).getHook(_type))
);
}
}

0 comments on commit 308b18c

Please sign in to comment.