GovernorCompatibilityBravo.sol 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (governance/compatibility/GovernorCompatibilityBravo.sol)
  3. pragma solidity ^0.8.19;
  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[] targets;
  26. uint256[] values;
  27. string[] signatures;
  28. bytes[] calldatas;
  29. uint256 forVotes;
  30. uint256 againstVotes;
  31. uint256 abstainVotes;
  32. mapping(address => Receipt) receipts;
  33. bytes32 descriptionHash;
  34. }
  35. mapping(uint256 => ProposalDetails) private _proposalDetails;
  36. // solhint-disable-next-line func-name-mixedcase
  37. function COUNTING_MODE() public pure virtual override returns (string memory) {
  38. return "support=bravo&quorum=bravo";
  39. }
  40. // ============================================== Proposal lifecycle ==============================================
  41. /**
  42. * @dev See {IGovernor-propose}.
  43. */
  44. function propose(
  45. address[] memory targets,
  46. uint256[] memory values,
  47. bytes[] memory calldatas,
  48. string memory description
  49. ) public virtual override(IGovernor, Governor) returns (uint256) {
  50. // Stores the proposal details (if not already present) and executes the propose logic from the core.
  51. _storeProposal(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. if (signatures.length != calldatas.length) {
  65. revert GovernorInvalidSignaturesLength(signatures.length, calldatas.length);
  66. }
  67. // Stores the full proposal and fallback to the public (possibly overridden) propose. The fallback is done
  68. // after the full proposal is stored, so the store operation included in the fallback will be skipped. Here we
  69. // call `propose` and not `super.propose` to make sure if a child contract override `propose`, whatever code
  70. // is added there is also executed when calling this alternative interface.
  71. _storeProposal(targets, values, signatures, calldatas, description);
  72. return propose(targets, values, _encodeCalldata(signatures, calldatas), description);
  73. }
  74. /**
  75. * @dev See {IGovernorCompatibilityBravo-queue}.
  76. */
  77. function queue(uint256 proposalId) public virtual override {
  78. (
  79. address[] memory targets,
  80. uint256[] memory values,
  81. bytes[] memory calldatas,
  82. bytes32 descriptionHash
  83. ) = _getProposalParameters(proposalId);
  84. queue(targets, values, calldatas, descriptionHash);
  85. }
  86. /**
  87. * @dev See {IGovernorCompatibilityBravo-execute}.
  88. */
  89. function execute(uint256 proposalId) public payable virtual override {
  90. (
  91. address[] memory targets,
  92. uint256[] memory values,
  93. bytes[] memory calldatas,
  94. bytes32 descriptionHash
  95. ) = _getProposalParameters(proposalId);
  96. execute(targets, values, calldatas, descriptionHash);
  97. }
  98. /**
  99. * @dev Cancel a proposal with GovernorBravo logic.
  100. */
  101. function cancel(uint256 proposalId) public virtual override {
  102. (
  103. address[] memory targets,
  104. uint256[] memory values,
  105. bytes[] memory calldatas,
  106. bytes32 descriptionHash
  107. ) = _getProposalParameters(proposalId);
  108. cancel(targets, values, calldatas, descriptionHash);
  109. }
  110. /**
  111. * @dev Cancel a proposal with GovernorBravo logic. At any moment a proposal can be cancelled, either by the
  112. * proposer, or by third parties if the proposer's voting power has dropped below the proposal threshold.
  113. */
  114. function cancel(
  115. address[] memory targets,
  116. uint256[] memory values,
  117. bytes[] memory calldatas,
  118. bytes32 descriptionHash
  119. ) public virtual override(IGovernor, Governor) returns (uint256) {
  120. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  121. address proposer = proposalProposer(proposalId);
  122. uint256 proposerVotes = getVotes(proposer, clock() - 1);
  123. uint256 votesThreshold = proposalThreshold();
  124. if (_msgSender() != proposer && proposerVotes >= votesThreshold) {
  125. revert GovernorInsufficientProposerVotes(proposer, proposerVotes, votesThreshold);
  126. }
  127. return _cancel(targets, values, calldatas, descriptionHash);
  128. }
  129. /**
  130. * @dev Encodes calldatas with optional function signature.
  131. */
  132. function _encodeCalldata(
  133. string[] memory signatures,
  134. bytes[] memory calldatas
  135. ) private pure returns (bytes[] memory) {
  136. bytes[] memory fullcalldatas = new bytes[](calldatas.length);
  137. for (uint256 i = 0; i < fullcalldatas.length; ++i) {
  138. fullcalldatas[i] = bytes(signatures[i]).length == 0
  139. ? calldatas[i]
  140. : abi.encodePacked(bytes4(keccak256(bytes(signatures[i]))), calldatas[i]);
  141. }
  142. return fullcalldatas;
  143. }
  144. /**
  145. * @dev Retrieve proposal parameters by id, with fully encoded calldatas.
  146. */
  147. function _getProposalParameters(
  148. uint256 proposalId
  149. )
  150. private
  151. view
  152. returns (address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
  153. {
  154. ProposalDetails storage details = _proposalDetails[proposalId];
  155. return (
  156. details.targets,
  157. details.values,
  158. _encodeCalldata(details.signatures, details.calldatas),
  159. details.descriptionHash
  160. );
  161. }
  162. /**
  163. * @dev Store proposal metadata (if not already present) for later lookup.
  164. */
  165. function _storeProposal(
  166. address[] memory targets,
  167. uint256[] memory values,
  168. string[] memory signatures,
  169. bytes[] memory calldatas,
  170. string memory description
  171. ) private {
  172. bytes32 descriptionHash = keccak256(bytes(description));
  173. uint256 proposalId = hashProposal(targets, values, _encodeCalldata(signatures, calldatas), descriptionHash);
  174. ProposalDetails storage details = _proposalDetails[proposalId];
  175. if (details.descriptionHash == bytes32(0)) {
  176. details.targets = targets;
  177. details.values = values;
  178. details.signatures = signatures;
  179. details.calldatas = calldatas;
  180. details.descriptionHash = descriptionHash;
  181. }
  182. }
  183. // ==================================================== Views =====================================================
  184. /**
  185. * @dev See {IGovernorCompatibilityBravo-proposals}.
  186. */
  187. function proposals(
  188. uint256 proposalId
  189. )
  190. public
  191. view
  192. virtual
  193. override
  194. returns (
  195. uint256 id,
  196. address proposer,
  197. uint256 eta,
  198. uint256 startBlock,
  199. uint256 endBlock,
  200. uint256 forVotes,
  201. uint256 againstVotes,
  202. uint256 abstainVotes,
  203. bool canceled,
  204. bool executed
  205. )
  206. {
  207. id = proposalId;
  208. proposer = proposalProposer(proposalId);
  209. eta = proposalEta(proposalId);
  210. startBlock = proposalSnapshot(proposalId);
  211. endBlock = proposalDeadline(proposalId);
  212. ProposalDetails storage details = _proposalDetails[proposalId];
  213. forVotes = details.forVotes;
  214. againstVotes = details.againstVotes;
  215. abstainVotes = details.abstainVotes;
  216. ProposalState currentState = state(proposalId);
  217. canceled = currentState == ProposalState.Canceled;
  218. executed = currentState == ProposalState.Executed;
  219. }
  220. /**
  221. * @dev See {IGovernorCompatibilityBravo-getActions}.
  222. */
  223. function getActions(
  224. uint256 proposalId
  225. )
  226. public
  227. view
  228. virtual
  229. override
  230. returns (
  231. address[] memory targets,
  232. uint256[] memory values,
  233. string[] memory signatures,
  234. bytes[] memory calldatas
  235. )
  236. {
  237. ProposalDetails storage details = _proposalDetails[proposalId];
  238. return (details.targets, details.values, details.signatures, details.calldatas);
  239. }
  240. /**
  241. * @dev See {IGovernorCompatibilityBravo-getReceipt}.
  242. */
  243. function getReceipt(uint256 proposalId, address voter) public view virtual override returns (Receipt memory) {
  244. return _proposalDetails[proposalId].receipts[voter];
  245. }
  246. /**
  247. * @dev See {IGovernorCompatibilityBravo-quorumVotes}.
  248. */
  249. function quorumVotes() public view virtual override returns (uint256) {
  250. return quorum(clock() - 1);
  251. }
  252. // ==================================================== Voting ====================================================
  253. /**
  254. * @dev See {IGovernor-hasVoted}.
  255. */
  256. function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
  257. return _proposalDetails[proposalId].receipts[account].hasVoted;
  258. }
  259. /**
  260. * @dev See {Governor-_quorumReached}. In this module, only forVotes count toward the quorum.
  261. */
  262. function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) {
  263. ProposalDetails storage details = _proposalDetails[proposalId];
  264. return quorum(proposalSnapshot(proposalId)) <= details.forVotes;
  265. }
  266. /**
  267. * @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be strictly over the againstVotes.
  268. */
  269. function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) {
  270. ProposalDetails storage details = _proposalDetails[proposalId];
  271. return details.forVotes > details.againstVotes;
  272. }
  273. /**
  274. * @dev See {Governor-_countVote}. In this module, the support follows Governor Bravo.
  275. */
  276. function _countVote(
  277. uint256 proposalId,
  278. address account,
  279. uint8 support,
  280. uint256 weight,
  281. bytes memory // params
  282. ) internal virtual override {
  283. ProposalDetails storage details = _proposalDetails[proposalId];
  284. Receipt storage receipt = details.receipts[account];
  285. if (receipt.hasVoted) {
  286. revert GovernorAlreadyCastVote(account);
  287. }
  288. receipt.hasVoted = true;
  289. receipt.support = support;
  290. receipt.votes = SafeCast.toUint96(weight);
  291. if (support == uint8(VoteType.Against)) {
  292. details.againstVotes += weight;
  293. } else if (support == uint8(VoteType.For)) {
  294. details.forVotes += weight;
  295. } else if (support == uint8(VoteType.Abstain)) {
  296. details.abstainVotes += weight;
  297. } else {
  298. revert GovernorInvalidVoteType();
  299. }
  300. }
  301. }