Skip to content

Commit

Permalink
Merge branch 'main' into yash/btt-split
Browse files Browse the repository at this point in the history
  • Loading branch information
nkrishang authored Nov 20, 2023
2 parents 72d3973 + 0dbbeef commit 5183875
Show file tree
Hide file tree
Showing 8 changed files with 774 additions and 0 deletions.
200 changes: 200 additions & 0 deletions src/test/vote-BTT/initialize/initialize.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import "../../utils/BaseTest.sol";

import { TWProxy } from "contracts/infra/TWProxy.sol";
import { ERC20Vote } from "contracts/base/ERC20Vote.sol";

contract MyVoteERC20 is VoteERC20 {
function eip712NameHash() external view returns (bytes32) {
return _EIP712NameHash();
}

function eip712VersionHash() external view returns (bytes32) {
return _EIP712VersionHash();
}
}

contract VoteERC20Test_Initialize is BaseTest {
address payable public implementation;
address payable public proxy;
address public token;
uint256 public initialVotingDelay;
uint256 public initialVotingPeriod;
uint256 public initialProposalThreshold;
uint256 public initialVoteQuorumFraction;

event VotingDelaySet(uint256 oldVotingDelay, uint256 newVotingDelay);
event VotingPeriodSet(uint256 oldVotingPeriod, uint256 newVotingPeriod);
event ProposalThresholdSet(uint256 oldProposalThreshold, uint256 newProposalThreshold);
event QuorumNumeratorUpdated(uint256 oldQuorumNumerator, uint256 newQuorumNumerator);

function setUp() public override {
super.setUp();

// Deploy voting token
token = address(new ERC20Vote(deployer, "Voting VoteERC20", "VT"));

// Voting param initial values
initialVotingDelay = 5;
initialVotingPeriod = 10;
initialProposalThreshold = 100;
initialVoteQuorumFraction = 50;

// Deploy implementation.
implementation = payable(address(new MyVoteERC20()));

// Deploy proxy pointing to implementaion.
vm.prank(deployer);
proxy = payable(
address(
new TWProxy(
implementation,
abi.encodeCall(
VoteERC20.initialize,
(
NAME,
CONTRACT_URI,
forwarders(),
token,
initialVotingDelay,
initialVotingPeriod,
initialProposalThreshold,
initialVoteQuorumFraction
)
)
)
)
);
}

function test_initialize_initializingImplementation() public {
vm.expectRevert("Initializable: contract is already initialized");
VoteERC20(implementation).initialize(
NAME,
CONTRACT_URI,
forwarders(),
token,
initialVotingDelay,
initialVotingPeriod,
initialProposalThreshold,
initialVoteQuorumFraction
);
}

modifier whenNotImplementation() {
_;
}

function test_initialize_proxyAlreadyInitialized() public whenNotImplementation {
vm.expectRevert("Initializable: contract is already initialized");
MyVoteERC20(proxy).initialize(
NAME,
CONTRACT_URI,
forwarders(),
token,
initialVotingDelay,
initialVotingPeriod,
initialProposalThreshold,
initialVoteQuorumFraction
);
}

modifier whenProxyNotInitialized() {
proxy = payable(address(new TWProxy(implementation, "")));
_;
}

function test_initialize() public whenNotImplementation whenProxyNotInitialized {
MyVoteERC20(proxy).initialize(
NAME,
CONTRACT_URI,
forwarders(),
token,
initialVotingDelay,
initialVotingPeriod,
initialProposalThreshold,
initialVoteQuorumFraction
);

// check state
MyVoteERC20 voteContract = MyVoteERC20(proxy);

assertEq(voteContract.eip712NameHash(), keccak256(bytes(NAME)));
assertEq(voteContract.eip712VersionHash(), keccak256(bytes("1")));

address[] memory _trustedForwarders = forwarders();
for (uint256 i = 0; i < _trustedForwarders.length; i++) {
assertTrue(voteContract.isTrustedForwarder(_trustedForwarders[i]));
}

assertEq(voteContract.name(), NAME);
assertEq(voteContract.contractURI(), CONTRACT_URI);
assertEq(voteContract.votingDelay(), initialVotingDelay);
assertEq(voteContract.votingPeriod(), initialVotingPeriod);
assertEq(voteContract.proposalThreshold(), initialProposalThreshold);
assertEq(voteContract.quorumNumerator(), initialVoteQuorumFraction);
assertEq(address(voteContract.token()), token);
}

function test_initialize_event_VotingDelaySet() public whenNotImplementation whenProxyNotInitialized {
vm.expectEmit(false, false, false, true);
emit VotingDelaySet(0, initialVotingDelay);
MyVoteERC20(proxy).initialize(
NAME,
CONTRACT_URI,
forwarders(),
token,
initialVotingDelay,
initialVotingPeriod,
initialProposalThreshold,
initialVoteQuorumFraction
);
}

function test_initialize_event_VotingPeriodSet() public whenNotImplementation whenProxyNotInitialized {
vm.expectEmit(false, false, false, true);
emit VotingPeriodSet(0, initialVotingPeriod);
MyVoteERC20(proxy).initialize(
NAME,
CONTRACT_URI,
forwarders(),
token,
initialVotingDelay,
initialVotingPeriod,
initialProposalThreshold,
initialVoteQuorumFraction
);
}

function test_initialize_event_ProposalThresholdSet() public whenNotImplementation whenProxyNotInitialized {
vm.expectEmit(false, false, false, true);
emit ProposalThresholdSet(0, initialProposalThreshold);
MyVoteERC20(proxy).initialize(
NAME,
CONTRACT_URI,
forwarders(),
token,
initialVotingDelay,
initialVotingPeriod,
initialProposalThreshold,
initialVoteQuorumFraction
);
}

function test_initialize_event_QuorumNumeratorUpdated() public whenNotImplementation whenProxyNotInitialized {
vm.expectEmit(false, false, false, true);
emit QuorumNumeratorUpdated(0, initialVoteQuorumFraction);
MyVoteERC20(proxy).initialize(
NAME,
CONTRACT_URI,
forwarders(),
token,
initialVotingDelay,
initialVotingPeriod,
initialProposalThreshold,
initialVoteQuorumFraction
);
}
}
30 changes: 30 additions & 0 deletions src/test/vote-BTT/initialize/initialize.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
initialize(
string memory _name,
string memory _contractURI,
address[] memory _trustedForwarders,
address _token,
uint256 _initialVotingDelay,
uint256 _initialVotingPeriod,
uint256 _initialProposalThreshold,
uint256 _initialVoteQuorumFraction
)
├── when initializing the implementation contract (not proxy)
│ └── it should revert ✅
└── when it is a proxy to the implementation
└── when it is already initialized
│ └── it should revert ✅
└── when it is not initialized
└── it should set trustedForwarder mapping to true for all addresses in `_trustedForwarders` ✅
└── it should correctly set EIP712 name hash and version hash ✅
└── it should set name to `_name` input param ✅
└── it should set contractURI to `_contractURI` param value ✅
└── it should set votingDelay to `_initialVotingDelay` param value ✅
└── it should emit VotingDelaySet event ✅
└── it should set votingPeriod to `_initialVotingPeriod` param value ✅
└── it should emit VotingPeriodSet event ✅
└── it should set proposalThreshold to `_initialProposalThreshold` param value ✅
└── it should emit ProposalThresholdSet event ✅
└── it should set voting token address as the `_token` param value ✅
└── it should set initial quorum numerator as `_initialVoteQuorumFraction` param value ✅
└── it should emit QuorumNumeratorUpdated event ✅

90 changes: 90 additions & 0 deletions src/test/vote-BTT/other-functions/other.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import "../../utils/BaseTest.sol";
import { IStaking721 } from "contracts/extension/interface/IStaking721.sol";
import { IERC2981 } from "contracts/eip/interface/IERC2981.sol";

import "@openzeppelin/contracts-upgradeable/governance/GovernorUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/IERC721ReceiverUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC1155/IERC1155ReceiverUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/introspection/IERC165Upgradeable.sol";

import { TWProxy } from "contracts/infra/TWProxy.sol";
import { ERC20Vote } from "contracts/base/ERC20Vote.sol";

contract MyVoteERC20 is VoteERC20 {}

contract VoteERC20Test_OtherFunctions is BaseTest {
address payable public implementation;
address payable public proxy;

address public token;
uint256 public initialVotingDelay;
uint256 public initialVotingPeriod;
uint256 public initialProposalThreshold;
uint256 public initialVoteQuorumFraction;

MyVoteERC20 public voteContract;

function setUp() public override {
super.setUp();

// Deploy voting token
vm.prank(deployer);
token = address(new ERC20Vote(deployer, "Voting VoteERC20", "VT"));

// Voting param initial values
initialVotingDelay = 1;
initialVotingPeriod = 100;
initialProposalThreshold = 10;
initialVoteQuorumFraction = 1;

// Deploy implementation.
implementation = payable(address(new MyVoteERC20()));

// Deploy proxy pointing to implementaion.
vm.prank(deployer);
proxy = payable(
address(
new TWProxy(
implementation,
abi.encodeCall(
VoteERC20.initialize,
(
NAME,
CONTRACT_URI,
forwarders(),
token,
initialVotingDelay,
initialVotingPeriod,
initialProposalThreshold,
initialVoteQuorumFraction
)
)
)
)
);

voteContract = MyVoteERC20(proxy);
}

function test_contractType() public {
assertEq(voteContract.contractType(), bytes32("VoteERC20"));
}

function test_contractVersion() public {
assertEq(voteContract.contractVersion(), uint8(1));
}

function test_supportsInterface() public {
assertTrue(voteContract.supportsInterface(type(IERC165).interfaceId));
assertTrue(voteContract.supportsInterface(type(IERC165Upgradeable).interfaceId));
assertTrue(voteContract.supportsInterface(type(IERC721ReceiverUpgradeable).interfaceId));
assertTrue(voteContract.supportsInterface(type(IERC1155ReceiverUpgradeable).interfaceId));
assertTrue(voteContract.supportsInterface(type(IGovernorUpgradeable).interfaceId));

// false for other not supported interfaces
assertFalse(voteContract.supportsInterface(type(IStaking721).interfaceId));
}
}
9 changes: 9 additions & 0 deletions src/test/vote-BTT/other-functions/other.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contractType()
├── it should return bytes32("VoteERC20") ✅

contractVersion()
├── it should return uint8(1) ✅

supportsInterface(bytes4 interfaceId)
├── it should return true for supported interface ✅
├── it should return false for not supported interface ✅
Loading

0 comments on commit 5183875

Please sign in to comment.