Add ERC721Votes for NFT-based governance (#2944)

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
Co-authored-by: Hadrien Croubois <hadrien@openzeppelin.com>
This commit is contained in:
JulissaDantes
2021-12-10 17:58:45 -04:00
committed by GitHub
parent 9a7e4a0112
commit b42b05311b
31 changed files with 1346 additions and 36 deletions

View File

@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../utils/Checkpoints.sol";
contract CheckpointsImpl {
using Checkpoints for Checkpoints.History;
Checkpoints.History private _totalCheckpoints;
function latest() public view returns (uint256) {
return _totalCheckpoints.latest();
}
function getAtBlock(uint256 blockNumber) public view returns (uint256) {
return _totalCheckpoints.getAtBlock(blockNumber);
}
function push(uint256 value) public returns (uint256, uint256) {
return _totalCheckpoints.push(value);
}
}

View File

@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../token/ERC721/extensions/draft-ERC721Votes.sol";
contract ERC721VotesMock is ERC721Votes {
constructor(string memory name, string memory symbol) ERC721(name, symbol) EIP712(name, "1") {}
function getTotalSupply() public view returns (uint256) {
return _getTotalSupply();
}
function mint(address account, uint256 tokenId) public {
_mint(account, tokenId);
}
function burn(uint256 tokenId) public {
_burn(tokenId);
}
function getChainId() external view returns (uint256) {
return block.chainid;
}
}

View File

@ -15,7 +15,7 @@ contract GovernorMock is
{
constructor(
string memory name_,
ERC20Votes token_,
IVotes token_,
uint256 votingDelay_,
uint256 votingPeriod_,
uint256 quorumNumerator_

View File

@ -17,7 +17,7 @@ contract GovernorPreventLateQuorumMock is
constructor(
string memory name_,
ERC20Votes token_,
IVotes token_,
uint256 votingDelay_,
uint256 votingPeriod_,
uint256 quorum_,

View File

@ -15,7 +15,7 @@ contract GovernorTimelockCompoundMock is
{
constructor(
string memory name_,
ERC20Votes token_,
IVotes token_,
uint256 votingDelay_,
uint256 votingPeriod_,
ICompoundTimelock timelock_,

View File

@ -15,7 +15,7 @@ contract GovernorTimelockControlMock is
{
constructor(
string memory name_,
ERC20Votes token_,
IVotes token_,
uint256 votingDelay_,
uint256 votingPeriod_,
TimelockController timelock_,

View File

@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../governance/extensions/GovernorCountingSimple.sol";
import "../governance/extensions/GovernorVotes.sol";
contract GovernorVoteMocks is GovernorVotes, GovernorCountingSimple {
constructor(string memory name_, IVotes token_) Governor(name_) GovernorVotes(token_) {}
function quorum(uint256) public pure override returns (uint256) {
return 0;
}
function votingDelay() public pure override returns (uint256) {
return 4;
}
function votingPeriod() public pure override returns (uint256) {
return 16;
}
function cancel(
address[] memory targets,
uint256[] memory values,
bytes[] memory calldatas,
bytes32 salt
) public returns (uint256 proposalId) {
return _cancel(targets, values, calldatas, salt);
}
function getVotes(address account, uint256 blockNumber)
public
view
virtual
override(IGovernor, GovernorVotes)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}
}

View File

@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../governance/utils/Votes.sol";
contract VotesMock is Votes {
mapping(address => uint256) private _balances;
mapping(uint256 => address) private _owners;
constructor(string memory name) EIP712(name, "1") {}
function getTotalSupply() public view returns (uint256) {
return _getTotalSupply();
}
function delegate(address account, address newDelegation) public {
return _delegate(account, newDelegation);
}
function _getVotingUnits(address account) internal virtual override returns (uint256) {
return _balances[account];
}
function mint(address account, uint256 voteId) external {
_balances[account] += 1;
_owners[voteId] = account;
_transferVotingUnits(address(0), account, 1);
}
function burn(uint256 voteId) external {
address owner = _owners[voteId];
_balances[owner] -= 1;
_transferVotingUnits(owner, address(0), 1);
}
function getChainId() external view returns (uint256) {
return block.chainid;
}
}

View File

@ -14,7 +14,7 @@ contract MyGovernor1 is
GovernorVotesQuorumFraction,
GovernorCountingSimple
{
constructor(ERC20Votes _token, TimelockController _timelock)
constructor(IVotes _token, TimelockController _timelock)
Governor("MyGovernor")
GovernorVotes(_token)
GovernorVotesQuorumFraction(4)

View File

@ -16,7 +16,7 @@ contract MyGovernor2 is
GovernorVotesQuorumFraction,
GovernorCountingSimple
{
constructor(ERC20Votes _token, TimelockController _timelock)
constructor(IVotes _token, TimelockController _timelock)
Governor("MyGovernor")
GovernorVotes(_token)
GovernorVotesQuorumFraction(4)

View File

@ -14,7 +14,7 @@ contract MyGovernor is
GovernorVotes,
GovernorVotesQuorumFraction
{
constructor(ERC20Votes _token, TimelockController _timelock)
constructor(IVotes _token, TimelockController _timelock)
Governor("MyGovernor")
GovernorVotes(_token)
GovernorVotesQuorumFraction(4)