GovernorCompatibilityBravo.sol 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 top 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, nor 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. _storeProposal(_msgSender(), targets, values, signatures, calldatas, description);
  65. return propose(targets, values, _encodeCalldata(signatures, calldatas), description);
  66. }
  67. /**
  68. * @dev See {IGovernorCompatibilityBravo-queue}.
  69. */
  70. function queue(uint256 proposalId) public virtual override {
  71. ProposalDetails storage details = _proposalDetails[proposalId];
  72. queue(
  73. details.targets,
  74. details.values,
  75. _encodeCalldata(details.signatures, details.calldatas),
  76. details.descriptionHash
  77. );
  78. }
  79. /**
  80. * @dev See {IGovernorCompatibilityBravo-execute}.
  81. */
  82. function execute(uint256 proposalId) public payable virtual override {
  83. ProposalDetails storage details = _proposalDetails[proposalId];
  84. execute(
  85. details.targets,
  86. details.values,
  87. _encodeCalldata(details.signatures, details.calldatas),
  88. details.descriptionHash
  89. );
  90. }
  91. function cancel(uint256 proposalId) public virtual override {
  92. ProposalDetails storage details = _proposalDetails[proposalId];
  93. require(
  94. _msgSender() == details.proposer || getVotes(details.proposer, block.number - 1) < proposalThreshold(),
  95. "GovernorBravo: proposer above threshold"
  96. );
  97. _cancel(
  98. details.targets,
  99. details.values,
  100. _encodeCalldata(details.signatures, details.calldatas),
  101. details.descriptionHash
  102. );
  103. }
  104. /**
  105. * @dev Encodes calldatas with optional function signature.
  106. */
  107. function _encodeCalldata(
  108. string[] memory signatures,
  109. bytes[] memory calldatas
  110. ) private pure returns (bytes[] memory) {
  111. bytes[] memory fullcalldatas = new bytes[](calldatas.length);
  112. for (uint256 i = 0; i < signatures.length; ++i) {
  113. fullcalldatas[i] = bytes(signatures[i]).length == 0
  114. ? calldatas[i]
  115. : abi.encodePacked(bytes4(keccak256(bytes(signatures[i]))), calldatas[i]);
  116. }
  117. return fullcalldatas;
  118. }
  119. /**
  120. * @dev Store proposal metadata for later lookup
  121. */
  122. function _storeProposal(
  123. address proposer,
  124. address[] memory targets,
  125. uint256[] memory values,
  126. string[] memory signatures,
  127. bytes[] memory calldatas,
  128. string memory description
  129. ) private {
  130. bytes32 descriptionHash = keccak256(bytes(description));
  131. uint256 proposalId = hashProposal(targets, values, _encodeCalldata(signatures, calldatas), descriptionHash);
  132. ProposalDetails storage details = _proposalDetails[proposalId];
  133. if (details.descriptionHash == bytes32(0)) {
  134. details.proposer = proposer;
  135. details.targets = targets;
  136. details.values = values;
  137. details.signatures = signatures;
  138. details.calldatas = calldatas;
  139. details.descriptionHash = descriptionHash;
  140. }
  141. }
  142. // ==================================================== Views =====================================================
  143. /**
  144. * @dev See {IGovernorCompatibilityBravo-proposals}.
  145. */
  146. function proposals(
  147. uint256 proposalId
  148. )
  149. public
  150. view
  151. virtual
  152. override
  153. returns (
  154. uint256 id,
  155. address proposer,
  156. uint256 eta,
  157. uint256 startBlock,
  158. uint256 endBlock,
  159. uint256 forVotes,
  160. uint256 againstVotes,
  161. uint256 abstainVotes,
  162. bool canceled,
  163. bool executed
  164. )
  165. {
  166. id = proposalId;
  167. eta = proposalEta(proposalId);
  168. startBlock = proposalSnapshot(proposalId);
  169. endBlock = proposalDeadline(proposalId);
  170. ProposalDetails storage details = _proposalDetails[proposalId];
  171. proposer = details.proposer;
  172. forVotes = details.forVotes;
  173. againstVotes = details.againstVotes;
  174. abstainVotes = details.abstainVotes;
  175. ProposalState status = state(proposalId);
  176. canceled = status == ProposalState.Canceled;
  177. executed = status == ProposalState.Executed;
  178. }
  179. /**
  180. * @dev See {IGovernorCompatibilityBravo-getActions}.
  181. */
  182. function getActions(
  183. uint256 proposalId
  184. )
  185. public
  186. view
  187. virtual
  188. override
  189. returns (
  190. address[] memory targets,
  191. uint256[] memory values,
  192. string[] memory signatures,
  193. bytes[] memory calldatas
  194. )
  195. {
  196. ProposalDetails storage details = _proposalDetails[proposalId];
  197. return (details.targets, details.values, details.signatures, details.calldatas);
  198. }
  199. /**
  200. * @dev See {IGovernorCompatibilityBravo-getReceipt}.
  201. */
  202. function getReceipt(uint256 proposalId, address voter) public view virtual override returns (Receipt memory) {
  203. return _proposalDetails[proposalId].receipts[voter];
  204. }
  205. /**
  206. * @dev See {IGovernorCompatibilityBravo-quorumVotes}.
  207. */
  208. function quorumVotes() public view virtual override returns (uint256) {
  209. return quorum(block.number - 1);
  210. }
  211. // ==================================================== Voting ====================================================
  212. /**
  213. * @dev See {IGovernor-hasVoted}.
  214. */
  215. function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
  216. return _proposalDetails[proposalId].receipts[account].hasVoted;
  217. }
  218. /**
  219. * @dev See {Governor-_quorumReached}. In this module, only forVotes count toward the quorum.
  220. */
  221. function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) {
  222. ProposalDetails storage details = _proposalDetails[proposalId];
  223. return quorum(proposalSnapshot(proposalId)) <= details.forVotes;
  224. }
  225. /**
  226. * @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be strictly over the againstVotes.
  227. */
  228. function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) {
  229. ProposalDetails storage details = _proposalDetails[proposalId];
  230. return details.forVotes > details.againstVotes;
  231. }
  232. /**
  233. * @dev See {Governor-_countVote}. In this module, the support follows Governor Bravo.
  234. */
  235. function _countVote(
  236. uint256 proposalId,
  237. address account,
  238. uint8 support,
  239. uint256 weight,
  240. bytes memory // params
  241. ) internal virtual override {
  242. ProposalDetails storage details = _proposalDetails[proposalId];
  243. Receipt storage receipt = details.receipts[account];
  244. require(!receipt.hasVoted, "GovernorCompatibilityBravo: vote already cast");
  245. receipt.hasVoted = true;
  246. receipt.support = support;
  247. receipt.votes = SafeCast.toUint96(weight);
  248. if (support == uint8(VoteType.Against)) {
  249. details.againstVotes += weight;
  250. } else if (support == uint8(VoteType.For)) {
  251. details.forVotes += weight;
  252. } else if (support == uint8(VoteType.Abstain)) {
  253. details.abstainVotes += weight;
  254. } else {
  255. revert("GovernorCompatibilityBravo: invalid vote type");
  256. }
  257. }
  258. }