|
| 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 MySplit is Split {} |
| 9 | + |
| 10 | +contract SplitTest_Initialize is BaseTest { |
| 11 | + address payable public implementation; |
| 12 | + address payable public proxy; |
| 13 | + |
| 14 | + address[] public payees; |
| 15 | + uint256[] public shares; |
| 16 | + |
| 17 | + event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender); |
| 18 | + |
| 19 | + function setUp() public override { |
| 20 | + super.setUp(); |
| 21 | + |
| 22 | + // create 5 payees and shares |
| 23 | + for (uint160 i = 0; i < 5; i++) { |
| 24 | + payees.push(getActor(i + 100)); |
| 25 | + shares.push(i + 100); |
| 26 | + } |
| 27 | + |
| 28 | + // Deploy implementation. |
| 29 | + implementation = payable(address(new MySplit())); |
| 30 | + |
| 31 | + // Deploy proxy pointing to implementaion. |
| 32 | + vm.prank(deployer); |
| 33 | + proxy = payable( |
| 34 | + address( |
| 35 | + new TWProxy( |
| 36 | + implementation, |
| 37 | + abi.encodeCall(Split.initialize, (deployer, CONTRACT_URI, forwarders(), payees, shares)) |
| 38 | + ) |
| 39 | + ) |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + function test_initialize_initializingImplementation() public { |
| 44 | + vm.expectRevert("Initializable: contract is already initialized"); |
| 45 | + Split(implementation).initialize(deployer, CONTRACT_URI, forwarders(), payees, shares); |
| 46 | + } |
| 47 | + |
| 48 | + modifier whenNotImplementation() { |
| 49 | + _; |
| 50 | + } |
| 51 | + |
| 52 | + function test_initialize_proxyAlreadyInitialized() public whenNotImplementation { |
| 53 | + vm.expectRevert("Initializable: contract is already initialized"); |
| 54 | + MySplit(proxy).initialize(deployer, CONTRACT_URI, forwarders(), payees, shares); |
| 55 | + } |
| 56 | + |
| 57 | + modifier whenProxyNotInitialized() { |
| 58 | + proxy = payable(address(new TWProxy(implementation, ""))); |
| 59 | + _; |
| 60 | + } |
| 61 | + |
| 62 | + function test_initialize_payeeLengthZero() public whenNotImplementation whenProxyNotInitialized { |
| 63 | + address[] memory _payees; |
| 64 | + uint256[] memory _shares; |
| 65 | + vm.expectRevert("PaymentSplitter: no payees"); |
| 66 | + MySplit(proxy).initialize(deployer, CONTRACT_URI, forwarders(), _payees, _shares); |
| 67 | + } |
| 68 | + |
| 69 | + modifier whenPayeeLengthNotZero() { |
| 70 | + _; |
| 71 | + } |
| 72 | + |
| 73 | + function test_initialize_payeesSharesUnequalLength() |
| 74 | + public |
| 75 | + whenNotImplementation |
| 76 | + whenProxyNotInitialized |
| 77 | + whenPayeeLengthNotZero |
| 78 | + { |
| 79 | + uint256[] memory _shares; |
| 80 | + vm.expectRevert("PaymentSplitter: payees and shares length mismatch"); |
| 81 | + MySplit(proxy).initialize(deployer, CONTRACT_URI, forwarders(), payees, _shares); |
| 82 | + } |
| 83 | + |
| 84 | + modifier whenEqualLengths() { |
| 85 | + _; |
| 86 | + } |
| 87 | + |
| 88 | + function test_initialize() |
| 89 | + public |
| 90 | + whenNotImplementation |
| 91 | + whenProxyNotInitialized |
| 92 | + whenPayeeLengthNotZero |
| 93 | + whenEqualLengths |
| 94 | + { |
| 95 | + MySplit(proxy).initialize(deployer, CONTRACT_URI, forwarders(), payees, shares); |
| 96 | + |
| 97 | + // check state |
| 98 | + MySplit splitContract = MySplit(proxy); |
| 99 | + |
| 100 | + address[] memory _trustedForwarders = forwarders(); |
| 101 | + for (uint256 i = 0; i < _trustedForwarders.length; i++) { |
| 102 | + assertTrue(splitContract.isTrustedForwarder(_trustedForwarders[i])); |
| 103 | + } |
| 104 | + |
| 105 | + uint256 totalShares; |
| 106 | + for (uint160 i = 0; i < 5; i++) { |
| 107 | + uint256 _shares = splitContract.shares(payees[i]); |
| 108 | + assertEq(_shares, shares[i]); |
| 109 | + |
| 110 | + totalShares += _shares; |
| 111 | + } |
| 112 | + assertEq(totalShares, splitContract.totalShares()); |
| 113 | + assertEq(splitContract.payeeCount(), payees.length); |
| 114 | + assertEq(splitContract.contractURI(), CONTRACT_URI); |
| 115 | + assertTrue(splitContract.hasRole(bytes32(0x00), deployer)); |
| 116 | + } |
| 117 | + |
| 118 | + function test_initialize_event_RoleGranted_DefaultAdmin() |
| 119 | + public |
| 120 | + whenNotImplementation |
| 121 | + whenProxyNotInitialized |
| 122 | + whenPayeeLengthNotZero |
| 123 | + whenEqualLengths |
| 124 | + { |
| 125 | + bytes32 _defaultAdminRole = bytes32(0x00); |
| 126 | + vm.prank(deployer); |
| 127 | + vm.expectEmit(true, true, true, false); |
| 128 | + emit RoleGranted(_defaultAdminRole, deployer, deployer); |
| 129 | + MySplit(proxy).initialize(deployer, CONTRACT_URI, forwarders(), payees, shares); |
| 130 | + } |
| 131 | +} |
0 commit comments