GovernorCompatibilityBravo.sol 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. // Stores the proposal details (if not already present) and executes the propose logic from the core.
  52. _storeProposal(_msgSender(), targets, values, new string[](calldatas.length), calldatas, description);
  53. return super.propose(targets, values, calldatas, description);
  54. }
  55. /**
  56. * @dev See {IGovernorCompatibilityBravo-propose}.
  57. */
  58. function propose(
  59. address[] memory targets,
  60. uint256[] memory values,
  61. string[] memory signatures,
  62. bytes[] memory calldatas,
  63. string memory description
  64. ) public virtual override returns (uint256) {
  65. require(signatures.length == calldatas.length, "GovernorBravo: invalid signatures length");
  66. // Stores the full proposal and fallback to the public (possibly overridden) propose. The fallback is done
  67. // after the full proposal is stored, so the store operation included in the fallback will be skipped. Here we
  68. // call `propose` and not `super.propose` to make sure if a child contract override `propose`, whatever code
  69. // is added there is also executed when calling this alternative interface.
  70. _storeProposal(_msgSender(), targets, values, signatures, calldatas, description);
  71. return propose(targets, values, _encodeCalldata(signatures, calldatas), description);
  72. }
  73. /**
  74. * @dev See {IGovernorCompatibilityBravo-queue}.
  75. */
  76. function queue(uint256 proposalId) public virtual override {
  77. (
  78. address[] memory targets,
  79. uint256[] memory values,
  80. bytes[] memory calldatas,
  81. bytes32 descriptionHash
  82. ) = _getProposalParameters(proposalId);
  83. queue(targets, values, calldatas, descriptionHash);
  84. }
  85. /**
  86. * @dev See {IGovernorCompatibilityBravo-execute}.
  87. */
  88. function execute(uint256 proposalId) public payable virtual override {
  89. (
  90. address[] memory targets,
  91. uint256[] memory values,
  92. bytes[] memory calldatas,
  93. bytes32 descriptionHash
  94. ) = _getProposalParameters(proposalId);
  95. execute(targets, values, calldatas, descriptionHash);
  96. }
  97. /**
  98. * @dev Cancel a proposal with GovernorBravo logic.
  99. */
  100. function cancel(uint256 proposalId) public virtual override {
  101. (
  102. address[] memory targets,
  103. uint256[] memory values,
  104. bytes[] memory calldatas,
  105. bytes32 descriptionHash
  106. ) = _getProposalParameters(proposalId);
  107. cancel(targets, values, calldatas, descriptionHash);
  108. }
  109. /**
  110. * @dev Cancel a proposal with GovernorBravo logic. At any moment a proposal can be cancelled, either by the
  111. * proposer, or by third parties if the proposer's voting power has dropped below the proposal threshold.
  112. */
  113. function cancel(
  114. address[] memory targets,
  115. uint256[] memory values,
  116. bytes[] memory calldatas,
  117. bytes32 descriptionHash
  118. ) public virtual override(IGovernor, Governor) returns (uint256) {
  119. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  120. address proposer = _proposalDetails[proposalId].proposer;
  121. require(
  122. _msgSender() == proposer || getVotes(proposer, clock() - 1) < proposalThreshold(),
  123. "GovernorBravo: proposer above threshold"
  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. : abi.encodePacked(bytes4(keccak256(bytes(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 proposer,
  165. address[] memory targets,
  166. uint256[] memory values,
  167. string[] memory signatures,
  168. bytes[] memory calldatas,
  169. string memory description
  170. ) private {
  171. bytes32 descriptionHash = keccak256(bytes(description));
  172. uint256 proposalId = hashProposal(targets, values, _encodeCalldata(signatures, calldatas), descriptionHash);
  173. ProposalDetails storage details = _proposalDetails[proposalId];
  174. if (details.descriptionHash == bytes32(0)) {
  175. details.proposer = proposer;
  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. eta = proposalEta(proposalId);
  209. startBlock = proposalSnapshot(proposalId);
  210. endBlock = proposalDeadline(proposalId);
  211. ProposalDetails storage details = _proposalDetails[proposalId];
  212. proposer = details.proposer;
  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. require(!receipt.hasVoted, "GovernorCompatibilityBravo: vote already cast");
  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("GovernorCompatibilityBravo: invalid vote type");
  297. }
  298. }
  299. }