GovernorCompatibilityBravo.sol 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (governance/compatibility/GovernorCompatibilityBravo.sol)
  3. pragma solidity ^0.8.0;
  4. import "../../utils/math/SafeCast.sol";
  5. import "../extensions/IGovernorTimelock.sol";
  6. import "../Governor.sol";
  7. import "./IGovernorCompatibilityBravo.sol";
  8. /**
  9. * @dev Compatibility layer that implements GovernorBravo compatibility on to of {Governor}.
  10. *
  11. * This compatibility layer includes a voting system and requires a {IGovernorTimelock} compatible module to be added
  12. * through inheritance. It does not include token bindings, not does it include any variable upgrade patterns.
  13. *
  14. * NOTE: When using this module, you may need to enable the Solidity optimizer to avoid hitting the contract size limit.
  15. *
  16. * _Available since v4.3._
  17. */
  18. abstract contract GovernorCompatibilityBravo is IGovernorTimelock, IGovernorCompatibilityBravo, Governor {
  19. enum VoteType {
  20. Against,
  21. For,
  22. Abstain
  23. }
  24. struct ProposalDetails {
  25. address proposer;
  26. address[] targets;
  27. uint256[] values;
  28. string[] signatures;
  29. bytes[] calldatas;
  30. uint256 forVotes;
  31. uint256 againstVotes;
  32. uint256 abstainVotes;
  33. mapping(address => Receipt) receipts;
  34. bytes32 descriptionHash;
  35. }
  36. mapping(uint256 => ProposalDetails) private _proposalDetails;
  37. // solhint-disable-next-line func-name-mixedcase
  38. function COUNTING_MODE() public pure virtual override returns (string memory) {
  39. return "support=bravo&quorum=bravo";
  40. }
  41. // ============================================== Proposal lifecycle ==============================================
  42. /**
  43. * @dev See {IGovernor-propose}.
  44. */
  45. function propose(
  46. address[] memory targets,
  47. uint256[] memory values,
  48. bytes[] memory calldatas,
  49. string memory description
  50. ) public virtual override(IGovernor, Governor) returns (uint256) {
  51. _storeProposal(_msgSender(), targets, values, new string[](calldatas.length), calldatas, description);
  52. return super.propose(targets, values, calldatas, description);
  53. }
  54. /**
  55. * @dev See {IGovernorCompatibilityBravo-propose}.
  56. */
  57. function propose(
  58. address[] memory targets,
  59. uint256[] memory values,
  60. string[] memory signatures,
  61. bytes[] memory calldatas,
  62. string memory description
  63. ) public virtual override returns (uint256) {
  64. require(signatures.length == calldatas.length, "GovernorBravo: invalid signatures length");
  65. // Stores the full proposal and fallback to the public (possibly overridden) propose. The fallback is done
  66. // after the full proposal is stored, so the store operation included in the fallback will be skipped. Here we
  67. // call `propose` and not `super.propose` to make sure if a child contract override `propose`, whatever code
  68. // is added their is also executed when calling this alternative interface.
  69. _storeProposal(_msgSender(), targets, values, signatures, calldatas, description);
  70. return propose(targets, values, _encodeCalldata(signatures, calldatas), description);
  71. }
  72. /**
  73. * @dev See {IGovernorCompatibilityBravo-queue}.
  74. */
  75. function queue(uint256 proposalId) public virtual override {
  76. ProposalDetails storage details = _proposalDetails[proposalId];
  77. queue(
  78. details.targets,
  79. details.values,
  80. _encodeCalldata(details.signatures, details.calldatas),
  81. details.descriptionHash
  82. );
  83. }
  84. /**
  85. * @dev See {IGovernorCompatibilityBravo-execute}.
  86. */
  87. function execute(uint256 proposalId) public payable virtual override {
  88. ProposalDetails storage details = _proposalDetails[proposalId];
  89. execute(
  90. details.targets,
  91. details.values,
  92. _encodeCalldata(details.signatures, details.calldatas),
  93. details.descriptionHash
  94. );
  95. }
  96. function cancel(uint256 proposalId) public virtual override {
  97. ProposalDetails storage details = _proposalDetails[proposalId];
  98. require(
  99. _msgSender() == details.proposer || getVotes(details.proposer, block.number - 1) < proposalThreshold(),
  100. "GovernorBravo: proposer above threshold"
  101. );
  102. _cancel(
  103. details.targets,
  104. details.values,
  105. _encodeCalldata(details.signatures, details.calldatas),
  106. details.descriptionHash
  107. );
  108. }
  109. /**
  110. * @dev Encodes calldatas with optional function signature.
  111. */
  112. function _encodeCalldata(string[] memory signatures, bytes[] memory calldatas)
  113. private
  114. pure
  115. returns (bytes[] memory)
  116. {
  117. bytes[] memory fullcalldatas = new bytes[](calldatas.length);
  118. for (uint256 i = 0; i < fullcalldatas.length; ++i) {
  119. fullcalldatas[i] = bytes(signatures[i]).length == 0
  120. ? calldatas[i]
  121. : abi.encodePacked(bytes4(keccak256(bytes(signatures[i]))), calldatas[i]);
  122. }
  123. return fullcalldatas;
  124. }
  125. /**
  126. * @dev Store proposal metadata for later lookup
  127. */
  128. function _storeProposal(
  129. address proposer,
  130. address[] memory targets,
  131. uint256[] memory values,
  132. string[] memory signatures,
  133. bytes[] memory calldatas,
  134. string memory description
  135. ) private {
  136. bytes32 descriptionHash = keccak256(bytes(description));
  137. uint256 proposalId = hashProposal(targets, values, _encodeCalldata(signatures, calldatas), descriptionHash);
  138. ProposalDetails storage details = _proposalDetails[proposalId];
  139. if (details.descriptionHash == bytes32(0)) {
  140. details.proposer = proposer;
  141. details.targets = targets;
  142. details.values = values;
  143. details.signatures = signatures;
  144. details.calldatas = calldatas;
  145. details.descriptionHash = descriptionHash;
  146. }
  147. }
  148. // ==================================================== Views =====================================================
  149. /**
  150. * @dev See {IGovernorCompatibilityBravo-proposals}.
  151. */
  152. function proposals(uint256 proposalId)
  153. public
  154. view
  155. virtual
  156. override
  157. returns (
  158. uint256 id,
  159. address proposer,
  160. uint256 eta,
  161. uint256 startBlock,
  162. uint256 endBlock,
  163. uint256 forVotes,
  164. uint256 againstVotes,
  165. uint256 abstainVotes,
  166. bool canceled,
  167. bool executed
  168. )
  169. {
  170. id = proposalId;
  171. eta = proposalEta(proposalId);
  172. startBlock = proposalSnapshot(proposalId);
  173. endBlock = proposalDeadline(proposalId);
  174. ProposalDetails storage details = _proposalDetails[proposalId];
  175. proposer = details.proposer;
  176. forVotes = details.forVotes;
  177. againstVotes = details.againstVotes;
  178. abstainVotes = details.abstainVotes;
  179. ProposalState status = state(proposalId);
  180. canceled = status == ProposalState.Canceled;
  181. executed = status == ProposalState.Executed;
  182. }
  183. /**
  184. * @dev See {IGovernorCompatibilityBravo-getActions}.
  185. */
  186. function getActions(uint256 proposalId)
  187. public
  188. view
  189. virtual
  190. override
  191. returns (
  192. address[] memory targets,
  193. uint256[] memory values,
  194. string[] memory signatures,
  195. bytes[] memory calldatas
  196. )
  197. {
  198. ProposalDetails storage details = _proposalDetails[proposalId];
  199. return (details.targets, details.values, details.signatures, details.calldatas);
  200. }
  201. /**
  202. * @dev See {IGovernorCompatibilityBravo-getReceipt}.
  203. */
  204. function getReceipt(uint256 proposalId, address voter) public view virtual override returns (Receipt memory) {
  205. return _proposalDetails[proposalId].receipts[voter];
  206. }
  207. /**
  208. * @dev See {IGovernorCompatibilityBravo-quorumVotes}.
  209. */
  210. function quorumVotes() public view virtual override returns (uint256) {
  211. return quorum(block.number - 1);
  212. }
  213. // ==================================================== Voting ====================================================
  214. /**
  215. * @dev See {IGovernor-hasVoted}.
  216. */
  217. function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
  218. return _proposalDetails[proposalId].receipts[account].hasVoted;
  219. }
  220. /**
  221. * @dev See {Governor-_quorumReached}. In this module, only forVotes count toward the quorum.
  222. */
  223. function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) {
  224. ProposalDetails storage details = _proposalDetails[proposalId];
  225. return quorum(proposalSnapshot(proposalId)) <= details.forVotes;
  226. }
  227. /**
  228. * @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be scritly over the againstVotes.
  229. */
  230. function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) {
  231. ProposalDetails storage details = _proposalDetails[proposalId];
  232. return details.forVotes > details.againstVotes;
  233. }
  234. /**
  235. * @dev See {Governor-_countVote}. In this module, the support follows Governor Bravo.
  236. */
  237. function _countVote(
  238. uint256 proposalId,
  239. address account,
  240. uint8 support,
  241. uint256 weight,
  242. bytes memory // params
  243. ) internal virtual override {
  244. ProposalDetails storage details = _proposalDetails[proposalId];
  245. Receipt storage receipt = details.receipts[account];
  246. require(!receipt.hasVoted, "GovernorCompatibilityBravo: vote already cast");
  247. receipt.hasVoted = true;
  248. receipt.support = support;
  249. receipt.votes = SafeCast.toUint96(weight);
  250. if (support == uint8(VoteType.Against)) {
  251. details.againstVotes += weight;
  252. } else if (support == uint8(VoteType.For)) {
  253. details.forVotes += weight;
  254. } else if (support == uint8(VoteType.Abstain)) {
  255. details.abstainVotes += weight;
  256. } else {
  257. revert("GovernorCompatibilityBravo: invalid vote type");
  258. }
  259. }
  260. }