|
| 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 | +import { ERC20Vote } from "contracts/base/ERC20Vote.sol"; |
| 8 | + |
| 9 | +contract MyVoteERC20 is VoteERC20 { |
| 10 | + function eip712NameHash() external view returns (bytes32) { |
| 11 | + return _EIP712NameHash(); |
| 12 | + } |
| 13 | + |
| 14 | + function eip712VersionHash() external view returns (bytes32) { |
| 15 | + return _EIP712VersionHash(); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +contract VoteERC20Test_Initialize is BaseTest { |
| 20 | + address payable public implementation; |
| 21 | + address payable public proxy; |
| 22 | + address public token; |
| 23 | + uint256 public initialVotingDelay; |
| 24 | + uint256 public initialVotingPeriod; |
| 25 | + uint256 public initialProposalThreshold; |
| 26 | + uint256 public initialVoteQuorumFraction; |
| 27 | + |
| 28 | + event VotingDelaySet(uint256 oldVotingDelay, uint256 newVotingDelay); |
| 29 | + event VotingPeriodSet(uint256 oldVotingPeriod, uint256 newVotingPeriod); |
| 30 | + event ProposalThresholdSet(uint256 oldProposalThreshold, uint256 newProposalThreshold); |
| 31 | + event QuorumNumeratorUpdated(uint256 oldQuorumNumerator, uint256 newQuorumNumerator); |
| 32 | + |
| 33 | + function setUp() public override { |
| 34 | + super.setUp(); |
| 35 | + |
| 36 | + // Deploy voting token |
| 37 | + token = address(new ERC20Vote(deployer, "Voting VoteERC20", "VT")); |
| 38 | + |
| 39 | + // Voting param initial values |
| 40 | + initialVotingDelay = 5; |
| 41 | + initialVotingPeriod = 10; |
| 42 | + initialProposalThreshold = 100; |
| 43 | + initialVoteQuorumFraction = 50; |
| 44 | + |
| 45 | + // Deploy implementation. |
| 46 | + implementation = payable(address(new MyVoteERC20())); |
| 47 | + |
| 48 | + // Deploy proxy pointing to implementaion. |
| 49 | + vm.prank(deployer); |
| 50 | + proxy = payable( |
| 51 | + address( |
| 52 | + new TWProxy( |
| 53 | + implementation, |
| 54 | + abi.encodeCall( |
| 55 | + VoteERC20.initialize, |
| 56 | + ( |
| 57 | + NAME, |
| 58 | + CONTRACT_URI, |
| 59 | + forwarders(), |
| 60 | + token, |
| 61 | + initialVotingDelay, |
| 62 | + initialVotingPeriod, |
| 63 | + initialProposalThreshold, |
| 64 | + initialVoteQuorumFraction |
| 65 | + ) |
| 66 | + ) |
| 67 | + ) |
| 68 | + ) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + function test_initialize_initializingImplementation() public { |
| 73 | + vm.expectRevert("Initializable: contract is already initialized"); |
| 74 | + VoteERC20(implementation).initialize( |
| 75 | + NAME, |
| 76 | + CONTRACT_URI, |
| 77 | + forwarders(), |
| 78 | + token, |
| 79 | + initialVotingDelay, |
| 80 | + initialVotingPeriod, |
| 81 | + initialProposalThreshold, |
| 82 | + initialVoteQuorumFraction |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + modifier whenNotImplementation() { |
| 87 | + _; |
| 88 | + } |
| 89 | + |
| 90 | + function test_initialize_proxyAlreadyInitialized() public whenNotImplementation { |
| 91 | + vm.expectRevert("Initializable: contract is already initialized"); |
| 92 | + MyVoteERC20(proxy).initialize( |
| 93 | + NAME, |
| 94 | + CONTRACT_URI, |
| 95 | + forwarders(), |
| 96 | + token, |
| 97 | + initialVotingDelay, |
| 98 | + initialVotingPeriod, |
| 99 | + initialProposalThreshold, |
| 100 | + initialVoteQuorumFraction |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + modifier whenProxyNotInitialized() { |
| 105 | + proxy = payable(address(new TWProxy(implementation, ""))); |
| 106 | + _; |
| 107 | + } |
| 108 | + |
| 109 | + function test_initialize() public whenNotImplementation whenProxyNotInitialized { |
| 110 | + MyVoteERC20(proxy).initialize( |
| 111 | + NAME, |
| 112 | + CONTRACT_URI, |
| 113 | + forwarders(), |
| 114 | + token, |
| 115 | + initialVotingDelay, |
| 116 | + initialVotingPeriod, |
| 117 | + initialProposalThreshold, |
| 118 | + initialVoteQuorumFraction |
| 119 | + ); |
| 120 | + |
| 121 | + // check state |
| 122 | + MyVoteERC20 voteContract = MyVoteERC20(proxy); |
| 123 | + |
| 124 | + assertEq(voteContract.eip712NameHash(), keccak256(bytes(NAME))); |
| 125 | + assertEq(voteContract.eip712VersionHash(), keccak256(bytes("1"))); |
| 126 | + |
| 127 | + address[] memory _trustedForwarders = forwarders(); |
| 128 | + for (uint256 i = 0; i < _trustedForwarders.length; i++) { |
| 129 | + assertTrue(voteContract.isTrustedForwarder(_trustedForwarders[i])); |
| 130 | + } |
| 131 | + |
| 132 | + assertEq(voteContract.name(), NAME); |
| 133 | + assertEq(voteContract.contractURI(), CONTRACT_URI); |
| 134 | + assertEq(voteContract.votingDelay(), initialVotingDelay); |
| 135 | + assertEq(voteContract.votingPeriod(), initialVotingPeriod); |
| 136 | + assertEq(voteContract.proposalThreshold(), initialProposalThreshold); |
| 137 | + assertEq(voteContract.quorumNumerator(), initialVoteQuorumFraction); |
| 138 | + assertEq(address(voteContract.token()), token); |
| 139 | + } |
| 140 | + |
| 141 | + function test_initialize_event_VotingDelaySet() public whenNotImplementation whenProxyNotInitialized { |
| 142 | + vm.expectEmit(false, false, false, true); |
| 143 | + emit VotingDelaySet(0, initialVotingDelay); |
| 144 | + MyVoteERC20(proxy).initialize( |
| 145 | + NAME, |
| 146 | + CONTRACT_URI, |
| 147 | + forwarders(), |
| 148 | + token, |
| 149 | + initialVotingDelay, |
| 150 | + initialVotingPeriod, |
| 151 | + initialProposalThreshold, |
| 152 | + initialVoteQuorumFraction |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + function test_initialize_event_VotingPeriodSet() public whenNotImplementation whenProxyNotInitialized { |
| 157 | + vm.expectEmit(false, false, false, true); |
| 158 | + emit VotingPeriodSet(0, initialVotingPeriod); |
| 159 | + MyVoteERC20(proxy).initialize( |
| 160 | + NAME, |
| 161 | + CONTRACT_URI, |
| 162 | + forwarders(), |
| 163 | + token, |
| 164 | + initialVotingDelay, |
| 165 | + initialVotingPeriod, |
| 166 | + initialProposalThreshold, |
| 167 | + initialVoteQuorumFraction |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + function test_initialize_event_ProposalThresholdSet() public whenNotImplementation whenProxyNotInitialized { |
| 172 | + vm.expectEmit(false, false, false, true); |
| 173 | + emit ProposalThresholdSet(0, initialProposalThreshold); |
| 174 | + MyVoteERC20(proxy).initialize( |
| 175 | + NAME, |
| 176 | + CONTRACT_URI, |
| 177 | + forwarders(), |
| 178 | + token, |
| 179 | + initialVotingDelay, |
| 180 | + initialVotingPeriod, |
| 181 | + initialProposalThreshold, |
| 182 | + initialVoteQuorumFraction |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + function test_initialize_event_QuorumNumeratorUpdated() public whenNotImplementation whenProxyNotInitialized { |
| 187 | + vm.expectEmit(false, false, false, true); |
| 188 | + emit QuorumNumeratorUpdated(0, initialVoteQuorumFraction); |
| 189 | + MyVoteERC20(proxy).initialize( |
| 190 | + NAME, |
| 191 | + CONTRACT_URI, |
| 192 | + forwarders(), |
| 193 | + token, |
| 194 | + initialVotingDelay, |
| 195 | + initialVotingPeriod, |
| 196 | + initialProposalThreshold, |
| 197 | + initialVoteQuorumFraction |
| 198 | + ); |
| 199 | + } |
| 200 | +} |
0 commit comments