GovernorCompatibilityBravo.sol 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * 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. using Counters for Counters.Counter;
  20. using Timers for Timers.BlockNumber;
  21. enum VoteType {
  22. Against,
  23. For,
  24. Abstain
  25. }
  26. struct ProposalDetails {
  27. address proposer;
  28. address[] targets;
  29. uint256[] values;
  30. string[] signatures;
  31. bytes[] calldatas;
  32. uint256 forVotes;
  33. uint256 againstVotes;
  34. uint256 abstainVotes;
  35. mapping(address => Receipt) receipts;
  36. bytes32 descriptionHash;
  37. }
  38. mapping(uint256 => ProposalDetails) private _proposalDetails;
  39. // solhint-disable-next-line func-name-mixedcase
  40. function COUNTING_MODE() public pure virtual override returns (string memory) {
  41. return "support=bravo&quorum=bravo";
  42. }
  43. // ============================================== Proposal lifecycle ==============================================
  44. /**
  45. * @dev See {IGovernor-propose}.
  46. */
  47. function propose(
  48. address[] memory targets,
  49. uint256[] memory values,
  50. bytes[] memory calldatas,
  51. string memory description
  52. ) public virtual override(IGovernor, Governor) returns (uint256) {
  53. _storeProposal(_msgSender(), targets, values, new string[](calldatas.length), calldatas, description);
  54. return super.propose(targets, values, calldatas, description);
  55. }
  56. /**
  57. * @dev See {IGovernorCompatibilityBravo-propose}.
  58. */
  59. function propose(
  60. address[] memory targets,
  61. uint256[] memory values,
  62. string[] memory signatures,
  63. bytes[] memory calldatas,
  64. string memory description
  65. ) public virtual override returns (uint256) {
  66. _storeProposal(_msgSender(), targets, values, signatures, calldatas, description);
  67. return propose(targets, values, _encodeCalldata(signatures, calldatas), description);
  68. }
  69. /**
  70. * @dev See {IGovernorCompatibilityBravo-queue}.
  71. */
  72. function queue(uint256 proposalId) public virtual override {
  73. ProposalDetails storage details = _proposalDetails[proposalId];
  74. queue(
  75. details.targets,
  76. details.values,
  77. _encodeCalldata(details.signatures, details.calldatas),
  78. details.descriptionHash
  79. );
  80. }
  81. /**
  82. * @dev See {IGovernorCompatibilityBravo-execute}.
  83. */
  84. function execute(uint256 proposalId) public payable virtual override {
  85. ProposalDetails storage details = _proposalDetails[proposalId];
  86. execute(
  87. details.targets,
  88. details.values,
  89. _encodeCalldata(details.signatures, details.calldatas),
  90. details.descriptionHash
  91. );
  92. }
  93. function cancel(uint256 proposalId) public virtual override {
  94. ProposalDetails storage details = _proposalDetails[proposalId];
  95. require(
  96. _msgSender() == details.proposer || getVotes(details.proposer, block.number - 1) < proposalThreshold(),
  97. "GovernorBravo: proposer above threshold"
  98. );
  99. _cancel(
  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.encodeWithSignature(signatures[i], calldatas[i]);
  119. }
  120. return fullcalldatas;
  121. }
  122. /**
  123. * @dev Store proposal metadata for later lookup
  124. */
  125. function _storeProposal(
  126. address proposer,
  127. address[] memory targets,
  128. uint256[] memory values,
  129. string[] memory signatures,
  130. bytes[] memory calldatas,
  131. string memory description
  132. ) private {
  133. bytes32 descriptionHash = keccak256(bytes(description));
  134. uint256 proposalId = hashProposal(targets, values, _encodeCalldata(signatures, calldatas), descriptionHash);
  135. ProposalDetails storage details = _proposalDetails[proposalId];
  136. if (details.descriptionHash == bytes32(0)) {
  137. details.proposer = proposer;
  138. details.targets = targets;
  139. details.values = values;
  140. details.signatures = signatures;
  141. details.calldatas = calldatas;
  142. details.descriptionHash = descriptionHash;
  143. }
  144. }
  145. // ==================================================== Views =====================================================
  146. /**
  147. * @dev See {IGovernorCompatibilityBravo-proposals}.
  148. */
  149. function proposals(uint256 proposalId)
  150. public
  151. view
  152. virtual
  153. override
  154. returns (
  155. uint256 id,
  156. address proposer,
  157. uint256 eta,
  158. uint256 startBlock,
  159. uint256 endBlock,
  160. uint256 forVotes,
  161. uint256 againstVotes,
  162. uint256 abstainVotes,
  163. bool canceled,
  164. bool executed
  165. )
  166. {
  167. id = proposalId;
  168. eta = proposalEta(proposalId);
  169. startBlock = proposalSnapshot(proposalId);
  170. endBlock = proposalDeadline(proposalId);
  171. ProposalDetails storage details = _proposalDetails[proposalId];
  172. proposer = details.proposer;
  173. forVotes = details.forVotes;
  174. againstVotes = details.againstVotes;
  175. abstainVotes = details.abstainVotes;
  176. ProposalState status = state(proposalId);
  177. canceled = status == ProposalState.Canceled;
  178. executed = status == ProposalState.Executed;
  179. }
  180. /**
  181. * @dev See {IGovernorCompatibilityBravo-getActions}.
  182. */
  183. function getActions(uint256 proposalId)
  184. public
  185. view
  186. virtual
  187. override
  188. returns (
  189. address[] memory targets,
  190. uint256[] memory values,
  191. string[] memory signatures,
  192. bytes[] memory calldatas
  193. )
  194. {
  195. ProposalDetails storage details = _proposalDetails[proposalId];
  196. return (details.targets, details.values, details.signatures, details.calldatas);
  197. }
  198. /**
  199. * @dev See {IGovernorCompatibilityBravo-getReceipt}.
  200. */
  201. function getReceipt(uint256 proposalId, address voter) public view virtual override returns (Receipt memory) {
  202. return _proposalDetails[proposalId].receipts[voter];
  203. }
  204. /**
  205. * @dev See {IGovernorCompatibilityBravo-quorumVotes}.
  206. */
  207. function quorumVotes() public view virtual override returns (uint256) {
  208. return quorum(block.number - 1);
  209. }
  210. // ==================================================== Voting ====================================================
  211. /**
  212. * @dev See {IGovernor-hasVoted}.
  213. */
  214. function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
  215. return _proposalDetails[proposalId].receipts[account].hasVoted;
  216. }
  217. /**
  218. * @dev See {Governor-_quorumReached}. In this module, only forVotes count toward the quorum.
  219. */
  220. function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) {
  221. ProposalDetails storage details = _proposalDetails[proposalId];
  222. return quorum(proposalSnapshot(proposalId)) < details.forVotes;
  223. }
  224. /**
  225. * @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be scritly over the againstVotes.
  226. */
  227. function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) {
  228. ProposalDetails storage details = _proposalDetails[proposalId];
  229. return details.forVotes > details.againstVotes;
  230. }
  231. /**
  232. * @dev See {Governor-_countVote}. In this module, the support follows Governor Bravo.
  233. */
  234. function _countVote(
  235. uint256 proposalId,
  236. address account,
  237. uint8 support,
  238. uint256 weight
  239. ) internal virtual override {
  240. ProposalDetails storage details = _proposalDetails[proposalId];
  241. Receipt storage receipt = details.receipts[account];
  242. require(!receipt.hasVoted, "GovernorCompatibilityBravo: vote already cast");
  243. receipt.hasVoted = true;
  244. receipt.support = support;
  245. receipt.votes = SafeCast.toUint96(weight);
  246. if (support == uint8(VoteType.Against)) {
  247. details.againstVotes += weight;
  248. } else if (support == uint8(VoteType.For)) {
  249. details.forVotes += weight;
  250. } else if (support == uint8(VoteType.Abstain)) {
  251. details.abstainVotes += weight;
  252. } else {
  253. revert("GovernorCompatibilityBravo: invalid vote type");
  254. }
  255. }
  256. }