GovernorCompatibilityBravo.sol 12 KB

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