|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.17; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | +import {TestPlus} from "solady/test/utils/TestPlus.sol"; |
| 6 | +import {Ownable} from "solady/src/auth/Ownable.sol"; |
| 7 | +import {ICreatorToken} from "src/interfaces/transfer-validated/ICreatorToken.sol"; |
| 8 | +import {ITransferValidator1155} from "src/interfaces/transfer-validated/ITransferValidator.sol"; |
| 9 | +import {MockTransferValidator} from "./mock/MockTransferValidator.sol"; |
| 10 | +import {ERC1155ShipyardTransferValidated} from "src/transfer-validated/ERC1155ShipyardTransferValidated.sol"; |
| 11 | + |
| 12 | +contract ERC1155ShipyardTransferValidatedWithMint is ERC1155ShipyardTransferValidated { |
| 13 | + constructor(address initialTransferValidator) ERC1155ShipyardTransferValidated(initialTransferValidator) {} |
| 14 | + |
| 15 | + function mint(address to, uint256 id, uint256 amount) public onlyOwner { |
| 16 | + _mint(to, id, amount, ""); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +contract TestERC1155ShipyardTransferValidated is Test, TestPlus { |
| 21 | + MockTransferValidator transferValidatorAlwaysSucceeds = new MockTransferValidator(false); |
| 22 | + MockTransferValidator transferValidatorAlwaysReverts = new MockTransferValidator(true); |
| 23 | + |
| 24 | + event TransferValidatorUpdated(address oldValidator, address newValidator); |
| 25 | + |
| 26 | + ERC1155ShipyardTransferValidatedWithMint token; |
| 27 | + |
| 28 | + function setUp() public { |
| 29 | + token = new ERC1155ShipyardTransferValidatedWithMint(address(0)); |
| 30 | + } |
| 31 | + |
| 32 | + function testOnlyOwnerCanSetTransferValidator() public { |
| 33 | + assertEq(token.getTransferValidator(), address(0)); |
| 34 | + |
| 35 | + vm.prank(address(token)); |
| 36 | + vm.expectRevert(Ownable.Unauthorized.selector); |
| 37 | + token.setTransferValidator(address(transferValidatorAlwaysSucceeds)); |
| 38 | + |
| 39 | + token.setTransferValidator(address(transferValidatorAlwaysSucceeds)); |
| 40 | + assertEq(token.getTransferValidator(), address(transferValidatorAlwaysSucceeds)); |
| 41 | + } |
| 42 | + |
| 43 | + function testTransferValidatedSetInConstructor() public { |
| 44 | + ERC1155ShipyardTransferValidatedWithMint token2 = |
| 45 | + new ERC1155ShipyardTransferValidatedWithMint(address(transferValidatorAlwaysSucceeds)); |
| 46 | + |
| 47 | + assertEq(token2.getTransferValidator(), address(transferValidatorAlwaysSucceeds)); |
| 48 | + } |
| 49 | + |
| 50 | + function testTransferValidatorIsCalledOnTransfer() public { |
| 51 | + token.mint(address(this), 1, 10); |
| 52 | + token.mint(address(this), 2, 10); |
| 53 | + |
| 54 | + vm.expectEmit(true, true, true, true); |
| 55 | + emit TransferValidatorUpdated(address(0), address(transferValidatorAlwaysSucceeds)); |
| 56 | + token.setTransferValidator(address(transferValidatorAlwaysSucceeds)); |
| 57 | + token.safeTransferFrom(address(this), msg.sender, 1, 1, ""); |
| 58 | + uint256[] memory ids = new uint256[](2); |
| 59 | + uint256[] memory amounts = new uint256[](2); |
| 60 | + ids[0] = 1; |
| 61 | + ids[1] = 2; |
| 62 | + amounts[0] = 2; |
| 63 | + amounts[1] = 2; |
| 64 | + token.safeBatchTransferFrom(address(this), msg.sender, ids, amounts, ""); |
| 65 | + |
| 66 | + vm.expectEmit(true, true, true, true); |
| 67 | + emit TransferValidatorUpdated(address(transferValidatorAlwaysSucceeds), address(transferValidatorAlwaysReverts)); |
| 68 | + token.setTransferValidator(address(transferValidatorAlwaysReverts)); |
| 69 | + vm.expectRevert("MockTransferValidator: always reverts"); |
| 70 | + token.safeTransferFrom(address(this), msg.sender, 1, 1, ""); |
| 71 | + vm.expectRevert("MockTransferValidator: always reverts"); |
| 72 | + token.safeBatchTransferFrom(address(this), msg.sender, ids, amounts, ""); |
| 73 | + |
| 74 | + // When set to null address, transfer should succeed without calling the validator |
| 75 | + vm.expectEmit(true, true, true, true); |
| 76 | + emit TransferValidatorUpdated(address(transferValidatorAlwaysReverts), address(0)); |
| 77 | + token.setTransferValidator(address(0)); |
| 78 | + token.safeTransferFrom(address(this), msg.sender, 1, 1, ""); |
| 79 | + token.safeBatchTransferFrom(address(this), msg.sender, ids, amounts, ""); |
| 80 | + } |
| 81 | + |
| 82 | + function testGetTransferValidationFunction() public { |
| 83 | + (bytes4 functionSignature, bool isViewFunction) = token.getTransferValidationFunction(); |
| 84 | + assertEq(functionSignature, ITransferValidator1155.validateTransfer.selector); |
| 85 | + assertEq(isViewFunction, true); |
| 86 | + } |
| 87 | + |
| 88 | + function testSupportsInterface() public { |
| 89 | + assertEq(token.supportsInterface(type(ICreatorToken).interfaceId), true); |
| 90 | + } |
| 91 | + |
| 92 | + function onERC1155Received( |
| 93 | + address, /* operator */ |
| 94 | + address, /* from */ |
| 95 | + uint256, /* id */ |
| 96 | + uint256, /* value */ |
| 97 | + bytes calldata /* data */ |
| 98 | + ) external pure returns (bytes4) { |
| 99 | + return this.onERC1155Received.selector; |
| 100 | + } |
| 101 | +} |
0 commit comments