GovernorCompatibilityBravo.sol 12 KB

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