GovernorCompatibilityBravo.sol 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../../utils/Counters.sol";
  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 to 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, not does it include any variable upgrade patterns.
  13. *
  14. * _Available since v4.3._
  15. */
  16. abstract contract GovernorCompatibilityBravo is IGovernorTimelock, IGovernorCompatibilityBravo, Governor {
  17. using Counters for Counters.Counter;
  18. using Timers for Timers.BlockNumber;
  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. // public for hooking
  38. function proposalThreshold() public view virtual override returns (uint256);
  39. // public for hooking
  40. function proposalEta(uint256 proposalId) public view virtual override returns (uint256);
  41. // public for hooking
  42. function queue(
  43. address[] memory targets,
  44. uint256[] memory values,
  45. bytes[] memory calldatas,
  46. bytes32 descriptionHash
  47. ) public virtual override returns (uint256);
  48. // solhint-disable-next-line func-name-mixedcase
  49. function COUNTING_MODE() public pure virtual override returns (string memory) {
  50. return "support=bravo&quorum=bravo";
  51. }
  52. // ============================================== Proposal lifecycle ==============================================
  53. /**
  54. * @dev See {IGovernor-propose}.
  55. */
  56. function propose(
  57. address[] memory targets,
  58. uint256[] memory values,
  59. bytes[] memory calldatas,
  60. string memory description
  61. ) public virtual override(IGovernor, Governor) returns (uint256) {
  62. return propose(targets, values, new string[](calldatas.length), calldatas, description);
  63. }
  64. /**
  65. * @dev See {IGovernorCompatibilityBravo-propose}.
  66. */
  67. function propose(
  68. address[] memory targets,
  69. uint256[] memory values,
  70. string[] memory signatures,
  71. bytes[] memory calldatas,
  72. string memory description
  73. ) public virtual override returns (uint256) {
  74. require(
  75. getVotes(msg.sender, block.number - 1) >= proposalThreshold(),
  76. "GovernorCompatibilityBravo: proposer votes below proposal threshold"
  77. );
  78. uint256 proposalId = super.propose(targets, values, _encodeCalldata(signatures, calldatas), description);
  79. _storeProposal(proposalId, _msgSender(), targets, values, signatures, calldatas, description);
  80. return proposalId;
  81. }
  82. /**
  83. * @dev See {IGovernorCompatibilityBravo-queue}.
  84. */
  85. function queue(uint256 proposalId) public virtual override {
  86. ProposalDetails storage details = _proposalDetails[proposalId];
  87. queue(
  88. details.targets,
  89. details.values,
  90. _encodeCalldata(details.signatures, details.calldatas),
  91. details.descriptionHash
  92. );
  93. }
  94. /**
  95. * @dev See {IGovernorCompatibilityBravo-execute}.
  96. */
  97. function execute(uint256 proposalId) public payable virtual override {
  98. ProposalDetails storage details = _proposalDetails[proposalId];
  99. execute(
  100. details.targets,
  101. details.values,
  102. _encodeCalldata(details.signatures, details.calldatas),
  103. details.descriptionHash
  104. );
  105. }
  106. /**
  107. * @dev Encodes calldatas with optional function signature.
  108. */
  109. function _encodeCalldata(string[] memory signatures, bytes[] memory calldatas)
  110. private
  111. pure
  112. returns (bytes[] memory)
  113. {
  114. bytes[] memory fullcalldatas = new bytes[](calldatas.length);
  115. for (uint256 i = 0; i < signatures.length; ++i) {
  116. fullcalldatas[i] = bytes(signatures[i]).length == 0
  117. ? calldatas[i]
  118. : abi.encodePacked(bytes4(keccak256(bytes(signatures[i]))), calldatas[i]);
  119. }
  120. return fullcalldatas;
  121. }
  122. /**
  123. * @dev Store proposal metadata for later lookup
  124. */
  125. function _storeProposal(
  126. uint256 proposalId,
  127. address proposer,
  128. address[] memory targets,
  129. uint256[] memory values,
  130. string[] memory signatures,
  131. bytes[] memory calldatas,
  132. string memory description
  133. ) private {
  134. ProposalDetails storage details = _proposalDetails[proposalId];
  135. details.proposer = proposer;
  136. details.targets = targets;
  137. details.values = values;
  138. details.signatures = signatures;
  139. details.calldatas = calldatas;
  140. details.descriptionHash = keccak256(bytes(description));
  141. }
  142. // ==================================================== Views =====================================================
  143. /**
  144. * @dev See {IGovernorCompatibilityBravo-proposals}.
  145. */
  146. function proposals(uint256 proposalId)
  147. public
  148. view
  149. virtual
  150. override
  151. returns (
  152. uint256 id,
  153. address proposer,
  154. uint256 eta,
  155. uint256 startBlock,
  156. uint256 endBlock,
  157. uint256 forVotes,
  158. uint256 againstVotes,
  159. uint256 abstainVotes,
  160. bool canceled,
  161. bool executed
  162. )
  163. {
  164. id = proposalId;
  165. eta = proposalEta(proposalId);
  166. startBlock = proposalSnapshot(proposalId);
  167. endBlock = proposalDeadline(proposalId);
  168. ProposalDetails storage details = _proposalDetails[proposalId];
  169. proposer = details.proposer;
  170. forVotes = details.forVotes;
  171. againstVotes = details.againstVotes;
  172. abstainVotes = details.abstainVotes;
  173. ProposalState status = state(proposalId);
  174. canceled = status == ProposalState.Canceled;
  175. executed = status == ProposalState.Executed;
  176. }
  177. /**
  178. * @dev See {IGovernorCompatibilityBravo-getActions}.
  179. */
  180. function getActions(uint256 proposalId)
  181. public
  182. view
  183. virtual
  184. override
  185. returns (
  186. address[] memory targets,
  187. uint256[] memory values,
  188. string[] memory signatures,
  189. bytes[] memory calldatas
  190. )
  191. {
  192. ProposalDetails storage details = _proposalDetails[proposalId];
  193. return (details.targets, details.values, details.signatures, details.calldatas);
  194. }
  195. /**
  196. * @dev See {IGovernorCompatibilityBravo-getReceipt}.
  197. */
  198. function getReceipt(uint256 proposalId, address voter) public view virtual override returns (Receipt memory) {
  199. return _proposalDetails[proposalId].receipts[voter];
  200. }
  201. /**
  202. * @dev See {IGovernorCompatibilityBravo-quorumVotes}.
  203. */
  204. function quorumVotes() public view virtual override returns (uint256) {
  205. return quorum(block.number - 1);
  206. }
  207. // ==================================================== Voting ====================================================
  208. /**
  209. * @dev See {IGovernor-hasVoted}.
  210. */
  211. function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
  212. return _proposalDetails[proposalId].receipts[account].hasVoted;
  213. }
  214. /**
  215. * @dev See {Governor-_quorumReached}. In this module, only forVotes count toward the quorum.
  216. */
  217. function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) {
  218. ProposalDetails storage details = _proposalDetails[proposalId];
  219. return quorum(proposalSnapshot(proposalId)) < details.forVotes;
  220. }
  221. /**
  222. * @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be scritly over the againstVotes.
  223. */
  224. function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) {
  225. ProposalDetails storage details = _proposalDetails[proposalId];
  226. return details.forVotes > details.againstVotes;
  227. }
  228. /**
  229. * @dev See {Governor-_countVote}. In this module, the support follows Governor Bravo.
  230. */
  231. function _countVote(
  232. uint256 proposalId,
  233. address account,
  234. uint8 support,
  235. uint256 weight
  236. ) internal virtual override {
  237. ProposalDetails storage details = _proposalDetails[proposalId];
  238. Receipt storage receipt = details.receipts[account];
  239. require(!receipt.hasVoted, "GovernorCompatibilityBravo: vote already casted");
  240. receipt.hasVoted = true;
  241. receipt.support = support;
  242. receipt.votes = SafeCast.toUint96(weight);
  243. if (support == uint8(VoteType.Against)) {
  244. details.againstVotes += weight;
  245. } else if (support == uint8(VoteType.For)) {
  246. details.forVotes += weight;
  247. } else if (support == uint8(VoteType.Abstain)) {
  248. details.abstainVotes += weight;
  249. } else {
  250. revert("GovernorCompatibilityBravo: invalid vote type");
  251. }
  252. }
  253. }