GovernorCountingSimpleUpgradeable.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (governance/extensions/GovernorCountingSimple.sol)
  3. pragma solidity ^0.8.0;
  4. import "../GovernorUpgradeable.sol";
  5. import "../../proxy/utils/Initializable.sol";
  6. /**
  7. * @dev Extension of {Governor} for simple, 3 options, vote counting.
  8. *
  9. * _Available since v4.3._
  10. */
  11. abstract contract GovernorCountingSimpleUpgradeable is Initializable, GovernorUpgradeable {
  12. function __GovernorCountingSimple_init() internal onlyInitializing {
  13. }
  14. function __GovernorCountingSimple_init_unchained() internal onlyInitializing {
  15. }
  16. /**
  17. * @dev Supported vote types. Matches Governor Bravo ordering.
  18. */
  19. enum VoteType {
  20. Against,
  21. For,
  22. Abstain
  23. }
  24. struct ProposalVote {
  25. uint256 againstVotes;
  26. uint256 forVotes;
  27. uint256 abstainVotes;
  28. mapping(address => bool) hasVoted;
  29. }
  30. mapping(uint256 => ProposalVote) private _proposalVotes;
  31. /**
  32. * @dev See {IGovernor-COUNTING_MODE}.
  33. */
  34. // solhint-disable-next-line func-name-mixedcase
  35. function COUNTING_MODE() public pure virtual override returns (string memory) {
  36. return "support=bravo&quorum=for,abstain";
  37. }
  38. /**
  39. * @dev See {IGovernor-hasVoted}.
  40. */
  41. function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
  42. return _proposalVotes[proposalId].hasVoted[account];
  43. }
  44. /**
  45. * @dev Accessor to the internal vote counts.
  46. */
  47. function proposalVotes(uint256 proposalId)
  48. public
  49. view
  50. virtual
  51. returns (
  52. uint256 againstVotes,
  53. uint256 forVotes,
  54. uint256 abstainVotes
  55. )
  56. {
  57. ProposalVote storage proposalvote = _proposalVotes[proposalId];
  58. return (proposalvote.againstVotes, proposalvote.forVotes, proposalvote.abstainVotes);
  59. }
  60. /**
  61. * @dev See {Governor-_quorumReached}.
  62. */
  63. function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) {
  64. ProposalVote storage proposalvote = _proposalVotes[proposalId];
  65. return quorum(proposalSnapshot(proposalId)) <= proposalvote.forVotes + proposalvote.abstainVotes;
  66. }
  67. /**
  68. * @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be strictly over the againstVotes.
  69. */
  70. function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) {
  71. ProposalVote storage proposalvote = _proposalVotes[proposalId];
  72. return proposalvote.forVotes > proposalvote.againstVotes;
  73. }
  74. /**
  75. * @dev See {Governor-_countVote}. In this module, the support follows the `VoteType` enum (from Governor Bravo).
  76. */
  77. function _countVote(
  78. uint256 proposalId,
  79. address account,
  80. uint8 support,
  81. uint256 weight
  82. ) internal virtual override {
  83. ProposalVote storage proposalvote = _proposalVotes[proposalId];
  84. require(!proposalvote.hasVoted[account], "GovernorVotingSimple: vote already cast");
  85. proposalvote.hasVoted[account] = true;
  86. if (support == uint8(VoteType.Against)) {
  87. proposalvote.againstVotes += weight;
  88. } else if (support == uint8(VoteType.For)) {
  89. proposalvote.forVotes += weight;
  90. } else if (support == uint8(VoteType.Abstain)) {
  91. proposalvote.abstainVotes += weight;
  92. } else {
  93. revert("GovernorVotingSimple: invalid value for enum VoteType");
  94. }
  95. }
  96. /**
  97. * @dev This empty reserved space is put in place to allow future versions to add new
  98. * variables without shifting down storage in the inheritance chain.
  99. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  100. */
  101. uint256[49] private __gap;
  102. }