|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "../../utils/BaseTest.sol"; |
| 5 | + |
| 6 | +import { TWProxy } from "contracts/infra/TWProxy.sol"; |
| 7 | + |
| 8 | +contract MyTokenERC20 is TokenERC20 { |
| 9 | + function eip712NameHash() external view returns (bytes32) { |
| 10 | + return _EIP712NameHash(); |
| 11 | + } |
| 12 | + |
| 13 | + function eip712VersionHash() external view returns (bytes32) { |
| 14 | + return _EIP712VersionHash(); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +contract TokenERC20Test_Initialize is BaseTest { |
| 19 | + address public implementation; |
| 20 | + address public proxy; |
| 21 | + |
| 22 | + event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender); |
| 23 | + |
| 24 | + function setUp() public override { |
| 25 | + super.setUp(); |
| 26 | + |
| 27 | + // Deploy implementation. |
| 28 | + implementation = address(new MyTokenERC20()); |
| 29 | + |
| 30 | + // Deploy proxy pointing to implementaion. |
| 31 | + vm.prank(deployer); |
| 32 | + proxy = address( |
| 33 | + new TWProxy( |
| 34 | + implementation, |
| 35 | + abi.encodeCall( |
| 36 | + TokenERC20.initialize, |
| 37 | + ( |
| 38 | + deployer, |
| 39 | + NAME, |
| 40 | + SYMBOL, |
| 41 | + CONTRACT_URI, |
| 42 | + forwarders(), |
| 43 | + saleRecipient, |
| 44 | + platformFeeRecipient, |
| 45 | + platformFeeBps |
| 46 | + ) |
| 47 | + ) |
| 48 | + ) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + function test_initialize_initializingImplementation() public { |
| 53 | + vm.expectRevert("Initializable: contract is already initialized"); |
| 54 | + TokenERC20(implementation).initialize( |
| 55 | + deployer, |
| 56 | + NAME, |
| 57 | + SYMBOL, |
| 58 | + CONTRACT_URI, |
| 59 | + forwarders(), |
| 60 | + saleRecipient, |
| 61 | + platformFeeRecipient, |
| 62 | + platformFeeBps |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + modifier whenNotImplementation() { |
| 67 | + _; |
| 68 | + } |
| 69 | + |
| 70 | + function test_initialize_proxyAlreadyInitialized() public whenNotImplementation { |
| 71 | + vm.expectRevert("Initializable: contract is already initialized"); |
| 72 | + MyTokenERC20(proxy).initialize( |
| 73 | + deployer, |
| 74 | + NAME, |
| 75 | + SYMBOL, |
| 76 | + CONTRACT_URI, |
| 77 | + forwarders(), |
| 78 | + saleRecipient, |
| 79 | + platformFeeRecipient, |
| 80 | + platformFeeBps |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + modifier whenProxyNotInitialized() { |
| 85 | + proxy = address(new TWProxy(implementation, "")); |
| 86 | + _; |
| 87 | + } |
| 88 | + |
| 89 | + function test_initialize_exceedsMaxBps() public whenNotImplementation whenProxyNotInitialized { |
| 90 | + vm.expectRevert("exceeds MAX_BPS"); |
| 91 | + MyTokenERC20(proxy).initialize( |
| 92 | + deployer, |
| 93 | + NAME, |
| 94 | + SYMBOL, |
| 95 | + CONTRACT_URI, |
| 96 | + forwarders(), |
| 97 | + saleRecipient, |
| 98 | + platformFeeRecipient, |
| 99 | + uint128(MAX_BPS) + 1 // platformFeeBps greater than MAX_BPS |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + modifier whenPlatformFeeBpsWithinMaxBps() { |
| 104 | + _; |
| 105 | + } |
| 106 | + |
| 107 | + function test_initialize() public whenNotImplementation whenProxyNotInitialized whenPlatformFeeBpsWithinMaxBps { |
| 108 | + MyTokenERC20(proxy).initialize( |
| 109 | + deployer, |
| 110 | + NAME, |
| 111 | + SYMBOL, |
| 112 | + CONTRACT_URI, |
| 113 | + forwarders(), |
| 114 | + saleRecipient, |
| 115 | + platformFeeRecipient, |
| 116 | + platformFeeBps |
| 117 | + ); |
| 118 | + |
| 119 | + // check state |
| 120 | + MyTokenERC20 tokenContract = MyTokenERC20(proxy); |
| 121 | + |
| 122 | + assertEq(tokenContract.eip712NameHash(), keccak256(bytes(NAME))); |
| 123 | + assertEq(tokenContract.eip712VersionHash(), keccak256(bytes("1"))); |
| 124 | + |
| 125 | + address[] memory _trustedForwarders = forwarders(); |
| 126 | + for (uint256 i = 0; i < _trustedForwarders.length; i++) { |
| 127 | + assertTrue(tokenContract.isTrustedForwarder(_trustedForwarders[i])); |
| 128 | + } |
| 129 | + |
| 130 | + assertEq(tokenContract.name(), NAME); |
| 131 | + assertEq(tokenContract.symbol(), SYMBOL); |
| 132 | + assertEq(tokenContract.contractURI(), CONTRACT_URI); |
| 133 | + |
| 134 | + (address _platformFeeRecipient, uint16 _platformFeeBps) = tokenContract.getPlatformFeeInfo(); |
| 135 | + assertEq(_platformFeeBps, platformFeeBps); |
| 136 | + assertEq(_platformFeeRecipient, platformFeeRecipient); |
| 137 | + |
| 138 | + assertEq(tokenContract.primarySaleRecipient(), saleRecipient); |
| 139 | + |
| 140 | + assertTrue(tokenContract.hasRole(bytes32(0x00), deployer)); |
| 141 | + assertTrue(tokenContract.hasRole(keccak256("TRANSFER_ROLE"), deployer)); |
| 142 | + assertTrue(tokenContract.hasRole(keccak256("TRANSFER_ROLE"), address(0))); |
| 143 | + assertTrue(tokenContract.hasRole(keccak256("MINTER_ROLE"), deployer)); |
| 144 | + } |
| 145 | + |
| 146 | + function test_initialize_event_RoleGranted_DefaultAdmin() |
| 147 | + public |
| 148 | + whenNotImplementation |
| 149 | + whenProxyNotInitialized |
| 150 | + whenPlatformFeeBpsWithinMaxBps |
| 151 | + { |
| 152 | + bytes32 _defaultAdminRole = bytes32(0x00); |
| 153 | + vm.prank(deployer); |
| 154 | + vm.expectEmit(true, true, true, false); |
| 155 | + emit RoleGranted(_defaultAdminRole, deployer, deployer); |
| 156 | + MyTokenERC20(proxy).initialize( |
| 157 | + deployer, |
| 158 | + NAME, |
| 159 | + SYMBOL, |
| 160 | + CONTRACT_URI, |
| 161 | + forwarders(), |
| 162 | + saleRecipient, |
| 163 | + platformFeeRecipient, |
| 164 | + platformFeeBps |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + function test_initialize_event_RoleGranted_MinterRole() |
| 169 | + public |
| 170 | + whenNotImplementation |
| 171 | + whenProxyNotInitialized |
| 172 | + whenPlatformFeeBpsWithinMaxBps |
| 173 | + { |
| 174 | + bytes32 _minterRole = keccak256("MINTER_ROLE"); |
| 175 | + vm.prank(deployer); |
| 176 | + vm.expectEmit(true, true, true, false); |
| 177 | + emit RoleGranted(_minterRole, deployer, deployer); |
| 178 | + MyTokenERC20(proxy).initialize( |
| 179 | + deployer, |
| 180 | + NAME, |
| 181 | + SYMBOL, |
| 182 | + CONTRACT_URI, |
| 183 | + forwarders(), |
| 184 | + saleRecipient, |
| 185 | + platformFeeRecipient, |
| 186 | + platformFeeBps |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + function test_initialize_event_RoleGranted_TransferRole() |
| 191 | + public |
| 192 | + whenNotImplementation |
| 193 | + whenProxyNotInitialized |
| 194 | + whenPlatformFeeBpsWithinMaxBps |
| 195 | + { |
| 196 | + bytes32 _transferRole = keccak256("TRANSFER_ROLE"); |
| 197 | + vm.prank(deployer); |
| 198 | + vm.expectEmit(true, true, true, false); |
| 199 | + emit RoleGranted(_transferRole, deployer, deployer); |
| 200 | + MyTokenERC20(proxy).initialize( |
| 201 | + deployer, |
| 202 | + NAME, |
| 203 | + SYMBOL, |
| 204 | + CONTRACT_URI, |
| 205 | + forwarders(), |
| 206 | + saleRecipient, |
| 207 | + platformFeeRecipient, |
| 208 | + platformFeeBps |
| 209 | + ); |
| 210 | + } |
| 211 | + |
| 212 | + function test_initialize_event_RoleGranted_TransferRole_AddressZero() |
| 213 | + public |
| 214 | + whenNotImplementation |
| 215 | + whenProxyNotInitialized |
| 216 | + whenPlatformFeeBpsWithinMaxBps |
| 217 | + { |
| 218 | + bytes32 _transferRole = keccak256("TRANSFER_ROLE"); |
| 219 | + vm.prank(deployer); |
| 220 | + vm.expectEmit(true, true, true, false); |
| 221 | + emit RoleGranted(_transferRole, address(0), deployer); |
| 222 | + MyTokenERC20(proxy).initialize( |
| 223 | + deployer, |
| 224 | + NAME, |
| 225 | + SYMBOL, |
| 226 | + CONTRACT_URI, |
| 227 | + forwarders(), |
| 228 | + saleRecipient, |
| 229 | + platformFeeRecipient, |
| 230 | + platformFeeBps |
| 231 | + ); |
| 232 | + } |
| 233 | +} |
0 commit comments