GovernorCompatibilityBravo.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. 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 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. require(
  121. _msgSender() == proposer || getVotes(proposer, clock() - 1) < proposalThreshold(),
  122. "GovernorBravo: proposer above threshold"
  123. );
  124. return _cancel(targets, values, calldatas, descriptionHash);
  125. }
  126. /**
  127. * @dev Encodes calldatas with optional function signature.
  128. */
  129. function _encodeCalldata(
  130. string[] memory signatures,
  131. bytes[] memory calldatas
  132. ) private pure returns (bytes[] memory) {
  133. bytes[] memory fullcalldatas = new bytes[](calldatas.length);
  134. for (uint256 i = 0; i < fullcalldatas.length; ++i) {
  135. fullcalldatas[i] = bytes(signatures[i]).length == 0
  136. ? calldatas[i]
  137. : abi.encodePacked(bytes4(keccak256(bytes(signatures[i]))), calldatas[i]);
  138. }
  139. return fullcalldatas;
  140. }
  141. /**
  142. * @dev Retrieve proposal parameters by id, with fully encoded calldatas.
  143. */
  144. function _getProposalParameters(
  145. uint256 proposalId
  146. )
  147. private
  148. view
  149. returns (address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
  150. {
  151. ProposalDetails storage details = _proposalDetails[proposalId];
  152. return (
  153. details.targets,
  154. details.values,
  155. _encodeCalldata(details.signatures, details.calldatas),
  156. details.descriptionHash
  157. );
  158. }
  159. /**
  160. * @dev Store proposal metadata (if not already present) for later lookup.
  161. */
  162. function _storeProposal(
  163. address[] memory targets,
  164. uint256[] memory values,
  165. string[] memory signatures,
  166. bytes[] memory calldatas,
  167. string memory description
  168. ) private {
  169. bytes32 descriptionHash = keccak256(bytes(description));
  170. uint256 proposalId = hashProposal(targets, values, _encodeCalldata(signatures, calldatas), descriptionHash);
  171. ProposalDetails storage details = _proposalDetails[proposalId];
  172. if (details.descriptionHash == bytes32(0)) {
  173. details.targets = targets;
  174. details.values = values;
  175. details.signatures = signatures;
  176. details.calldatas = calldatas;
  177. details.descriptionHash = descriptionHash;
  178. }
  179. }
  180. // ==================================================== Views =====================================================
  181. /**
  182. * @dev See {IGovernorCompatibilityBravo-proposals}.
  183. */
  184. function proposals(
  185. uint256 proposalId
  186. )
  187. public
  188. view
  189. virtual
  190. override
  191. returns (
  192. uint256 id,
  193. address proposer,
  194. uint256 eta,
  195. uint256 startBlock,
  196. uint256 endBlock,
  197. uint256 forVotes,
  198. uint256 againstVotes,
  199. uint256 abstainVotes,
  200. bool canceled,
  201. bool executed
  202. )
  203. {
  204. id = proposalId;
  205. proposer = proposalProposer(proposalId);
  206. eta = proposalEta(proposalId);
  207. startBlock = proposalSnapshot(proposalId);
  208. endBlock = proposalDeadline(proposalId);
  209. ProposalDetails storage details = _proposalDetails[proposalId];
  210. forVotes = details.forVotes;
  211. againstVotes = details.againstVotes;
  212. abstainVotes = details.abstainVotes;
  213. ProposalState currentState = state(proposalId);
  214. canceled = currentState == ProposalState.Canceled;
  215. executed = currentState == ProposalState.Executed;
  216. }
  217. /**
  218. * @dev See {IGovernorCompatibilityBravo-getActions}.
  219. */
  220. function getActions(
  221. uint256 proposalId
  222. )
  223. public
  224. view
  225. virtual
  226. override
  227. returns (
  228. address[] memory targets,
  229. uint256[] memory values,
  230. string[] memory signatures,
  231. bytes[] memory calldatas
  232. )
  233. {
  234. ProposalDetails storage details = _proposalDetails[proposalId];
  235. return (details.targets, details.values, details.signatures, details.calldatas);
  236. }
  237. /**
  238. * @dev See {IGovernorCompatibilityBravo-getReceipt}.
  239. */
  240. function getReceipt(uint256 proposalId, address voter) public view virtual override returns (Receipt memory) {
  241. return _proposalDetails[proposalId].receipts[voter];
  242. }
  243. /**
  244. * @dev See {IGovernorCompatibilityBravo-quorumVotes}.
  245. */
  246. function quorumVotes() public view virtual override returns (uint256) {
  247. return quorum(clock() - 1);
  248. }
  249. // ==================================================== Voting ====================================================
  250. /**
  251. * @dev See {IGovernor-hasVoted}.
  252. */
  253. function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
  254. return _proposalDetails[proposalId].receipts[account].hasVoted;
  255. }
  256. /**
  257. * @dev See {Governor-_quorumReached}. In this module, only forVotes count toward the quorum.
  258. */
  259. function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) {
  260. ProposalDetails storage details = _proposalDetails[proposalId];
  261. return quorum(proposalSnapshot(proposalId)) <= details.forVotes;
  262. }
  263. /**
  264. * @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be strictly over the againstVotes.
  265. */
  266. function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) {
  267. ProposalDetails storage details = _proposalDetails[proposalId];
  268. return details.forVotes > details.againstVotes;
  269. }
  270. /**
  271. * @dev See {Governor-_countVote}. In this module, the support follows Governor Bravo.
  272. */
  273. function _countVote(
  274. uint256 proposalId,
  275. address account,
  276. uint8 support,
  277. uint256 weight,
  278. bytes memory // params
  279. ) internal virtual override {
  280. ProposalDetails storage details = _proposalDetails[proposalId];
  281. Receipt storage receipt = details.receipts[account];
  282. require(!receipt.hasVoted, "GovernorCompatibilityBravo: vote already cast");
  283. receipt.hasVoted = true;
  284. receipt.support = support;
  285. receipt.votes = SafeCast.toUint96(weight);
  286. if (support == uint8(VoteType.Against)) {
  287. details.againstVotes += weight;
  288. } else if (support == uint8(VoteType.For)) {
  289. details.forVotes += weight;
  290. } else if (support == uint8(VoteType.Abstain)) {
  291. details.abstainVotes += weight;
  292. } else {
  293. revert("GovernorCompatibilityBravo: invalid vote type");
  294. }
  295. }
  296. }