GovernorCompatibilityBravo.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. _storeProposal(_msgSender(), 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. _storeProposal(_msgSender(), targets, values, signatures, calldatas, description);
  65. return propose(targets, values, _encodeCalldata(signatures, calldatas), description);
  66. }
  67. /**
  68. * @dev See {IGovernorCompatibilityBravo-queue}.
  69. */
  70. function queue(uint256 proposalId) public virtual override {
  71. (
  72. address[] memory targets,
  73. uint256[] memory values,
  74. bytes[] memory calldatas,
  75. bytes32 descriptionHash
  76. ) = _getProposalParameters(proposalId);
  77. queue(targets, values, calldatas, descriptionHash);
  78. }
  79. /**
  80. * @dev See {IGovernorCompatibilityBravo-execute}.
  81. */
  82. function execute(uint256 proposalId) public payable virtual override {
  83. (
  84. address[] memory targets,
  85. uint256[] memory values,
  86. bytes[] memory calldatas,
  87. bytes32 descriptionHash
  88. ) = _getProposalParameters(proposalId);
  89. execute(targets, values, calldatas, descriptionHash);
  90. }
  91. /**
  92. * @dev Cancel a proposal with GovernorBravo logic.
  93. */
  94. function cancel(uint256 proposalId) public virtual {
  95. (
  96. address[] memory targets,
  97. uint256[] memory values,
  98. bytes[] memory calldatas,
  99. bytes32 descriptionHash
  100. ) = _getProposalParameters(proposalId);
  101. cancel(targets, values, calldatas, descriptionHash);
  102. }
  103. /**
  104. * @dev Cancel a proposal with GovernorBravo logic. At any moment a proposal can be cancelled, either by the
  105. * proposer, or by third parties if the proposer's voting power has dropped below the proposal threshold.
  106. */
  107. function cancel(
  108. address[] memory targets,
  109. uint256[] memory values,
  110. bytes[] memory calldatas,
  111. bytes32 descriptionHash
  112. ) public virtual override(IGovernor, Governor) returns (uint256) {
  113. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  114. address proposer = _proposalDetails[proposalId].proposer;
  115. require(
  116. _msgSender() == proposer || getVotes(proposer, clock() - 1) < proposalThreshold(),
  117. "GovernorBravo: proposer above threshold"
  118. );
  119. return _cancel(targets, values, calldatas, descriptionHash);
  120. }
  121. /**
  122. * @dev Encodes calldatas with optional function signature.
  123. */
  124. function _encodeCalldata(
  125. string[] memory signatures,
  126. bytes[] memory calldatas
  127. ) private pure returns (bytes[] memory) {
  128. bytes[] memory fullcalldatas = new bytes[](calldatas.length);
  129. for (uint256 i = 0; i < signatures.length; ++i) {
  130. fullcalldatas[i] = bytes(signatures[i]).length == 0
  131. ? calldatas[i]
  132. : abi.encodePacked(bytes4(keccak256(bytes(signatures[i]))), calldatas[i]);
  133. }
  134. return fullcalldatas;
  135. }
  136. /**
  137. * @dev Retrieve proposal parameters by id, with fully encoded calldatas.
  138. */
  139. function _getProposalParameters(
  140. uint256 proposalId
  141. )
  142. private
  143. view
  144. returns (address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
  145. {
  146. ProposalDetails storage details = _proposalDetails[proposalId];
  147. return (
  148. details.targets,
  149. details.values,
  150. _encodeCalldata(details.signatures, details.calldatas),
  151. details.descriptionHash
  152. );
  153. }
  154. /**
  155. * @dev Store proposal metadata for later lookup
  156. */
  157. function _storeProposal(
  158. address proposer,
  159. address[] memory targets,
  160. uint256[] memory values,
  161. string[] memory signatures,
  162. bytes[] memory calldatas,
  163. string memory description
  164. ) private {
  165. bytes32 descriptionHash = keccak256(bytes(description));
  166. uint256 proposalId = hashProposal(targets, values, _encodeCalldata(signatures, calldatas), descriptionHash);
  167. ProposalDetails storage details = _proposalDetails[proposalId];
  168. if (details.descriptionHash == bytes32(0)) {
  169. details.proposer = proposer;
  170. details.targets = targets;
  171. details.values = values;
  172. details.signatures = signatures;
  173. details.calldatas = calldatas;
  174. details.descriptionHash = descriptionHash;
  175. }
  176. }
  177. // ==================================================== Views =====================================================
  178. /**
  179. * @dev See {IGovernorCompatibilityBravo-proposals}.
  180. */
  181. function proposals(
  182. uint256 proposalId
  183. )
  184. public
  185. view
  186. virtual
  187. override
  188. returns (
  189. uint256 id,
  190. address proposer,
  191. uint256 eta,
  192. uint256 startBlock,
  193. uint256 endBlock,
  194. uint256 forVotes,
  195. uint256 againstVotes,
  196. uint256 abstainVotes,
  197. bool canceled,
  198. bool executed
  199. )
  200. {
  201. id = proposalId;
  202. eta = proposalEta(proposalId);
  203. startBlock = proposalSnapshot(proposalId);
  204. endBlock = proposalDeadline(proposalId);
  205. ProposalDetails storage details = _proposalDetails[proposalId];
  206. proposer = details.proposer;
  207. forVotes = details.forVotes;
  208. againstVotes = details.againstVotes;
  209. abstainVotes = details.abstainVotes;
  210. ProposalState status = state(proposalId);
  211. canceled = status == ProposalState.Canceled;
  212. executed = status == ProposalState.Executed;
  213. }
  214. /**
  215. * @dev See {IGovernorCompatibilityBravo-getActions}.
  216. */
  217. function getActions(
  218. uint256 proposalId
  219. )
  220. public
  221. view
  222. virtual
  223. override
  224. returns (
  225. address[] memory targets,
  226. uint256[] memory values,
  227. string[] memory signatures,
  228. bytes[] memory calldatas
  229. )
  230. {
  231. ProposalDetails storage details = _proposalDetails[proposalId];
  232. return (details.targets, details.values, details.signatures, details.calldatas);
  233. }
  234. /**
  235. * @dev See {IGovernorCompatibilityBravo-getReceipt}.
  236. */
  237. function getReceipt(uint256 proposalId, address voter) public view virtual override returns (Receipt memory) {
  238. return _proposalDetails[proposalId].receipts[voter];
  239. }
  240. /**
  241. * @dev See {IGovernorCompatibilityBravo-quorumVotes}.
  242. */
  243. function quorumVotes() public view virtual override returns (uint256) {
  244. return quorum(clock() - 1);
  245. }
  246. // ==================================================== Voting ====================================================
  247. /**
  248. * @dev See {IGovernor-hasVoted}.
  249. */
  250. function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
  251. return _proposalDetails[proposalId].receipts[account].hasVoted;
  252. }
  253. /**
  254. * @dev See {Governor-_quorumReached}. In this module, only forVotes count toward the quorum.
  255. */
  256. function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) {
  257. ProposalDetails storage details = _proposalDetails[proposalId];
  258. return quorum(proposalSnapshot(proposalId)) <= details.forVotes;
  259. }
  260. /**
  261. * @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be strictly over the againstVotes.
  262. */
  263. function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) {
  264. ProposalDetails storage details = _proposalDetails[proposalId];
  265. return details.forVotes > details.againstVotes;
  266. }
  267. /**
  268. * @dev See {Governor-_countVote}. In this module, the support follows Governor Bravo.
  269. */
  270. function _countVote(
  271. uint256 proposalId,
  272. address account,
  273. uint8 support,
  274. uint256 weight,
  275. bytes memory // params
  276. ) internal virtual override {
  277. ProposalDetails storage details = _proposalDetails[proposalId];
  278. Receipt storage receipt = details.receipts[account];
  279. require(!receipt.hasVoted, "GovernorCompatibilityBravo: vote already cast");
  280. receipt.hasVoted = true;
  281. receipt.support = support;
  282. receipt.votes = SafeCast.toUint96(weight);
  283. if (support == uint8(VoteType.Against)) {
  284. details.againstVotes += weight;
  285. } else if (support == uint8(VoteType.For)) {
  286. details.forVotes += weight;
  287. } else if (support == uint8(VoteType.Abstain)) {
  288. details.abstainVotes += weight;
  289. } else {
  290. revert("GovernorCompatibilityBravo: invalid vote type");
  291. }
  292. }
  293. }