GovernorVotesSuperQuorumFraction.sol 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Governor} from "../Governor.sol";
  4. import {GovernorSuperQuorum} from "./GovernorSuperQuorum.sol";
  5. import {GovernorVotesQuorumFraction} from "./GovernorVotesQuorumFraction.sol";
  6. import {Math} from "../../utils/math/Math.sol";
  7. import {SafeCast} from "../../utils/math/SafeCast.sol";
  8. import {Checkpoints} from "../../utils/structs/Checkpoints.sol";
  9. /**
  10. * @dev Extension of {GovernorVotesQuorumFraction} with a super quorum expressed as a
  11. * fraction of the total supply. Proposals that meet the super quorum (and have a majority of for votes) advance to
  12. * the `Succeeded` state before the proposal deadline.
  13. */
  14. abstract contract GovernorVotesSuperQuorumFraction is GovernorVotesQuorumFraction, GovernorSuperQuorum {
  15. using Checkpoints for Checkpoints.Trace208;
  16. Checkpoints.Trace208 private _superQuorumNumeratorHistory;
  17. event SuperQuorumNumeratorUpdated(uint256 oldSuperQuorumNumerator, uint256 newSuperQuorumNumerator);
  18. /**
  19. * @dev The super quorum set is not valid as it exceeds the quorum denominator.
  20. */
  21. error GovernorInvalidSuperQuorumFraction(uint256 superQuorumNumerator, uint256 denominator);
  22. /**
  23. * @dev The super quorum set is not valid as it is smaller or equal to the quorum.
  24. */
  25. error GovernorInvalidSuperQuorumTooSmall(uint256 superQuorumNumerator, uint256 quorumNumerator);
  26. /**
  27. * @dev The quorum set is not valid as it exceeds the super quorum.
  28. */
  29. error GovernorInvalidQuorumTooLarge(uint256 quorumNumerator, uint256 superQuorumNumerator);
  30. /**
  31. * @dev Initialize super quorum as a fraction of the token's total supply.
  32. *
  33. * The super quorum is specified as a fraction of the token's total supply and has to
  34. * be greater than the quorum.
  35. */
  36. constructor(uint256 superQuorumNumeratorValue) {
  37. _updateSuperQuorumNumerator(superQuorumNumeratorValue);
  38. }
  39. /**
  40. * @dev Returns the current super quorum numerator.
  41. */
  42. function superQuorumNumerator() public view virtual returns (uint256) {
  43. return _superQuorumNumeratorHistory.latest();
  44. }
  45. /**
  46. * @dev Returns the super quorum numerator at a specific `timepoint`.
  47. */
  48. function superQuorumNumerator(uint256 timepoint) public view virtual returns (uint256) {
  49. return _optimisticUpperLookupRecent(_superQuorumNumeratorHistory, timepoint);
  50. }
  51. /**
  52. * @dev Returns the super quorum for a `timepoint`, in terms of number of votes: `supply * numerator / denominator`.
  53. * See {GovernorSuperQuorum-superQuorum} for more details.
  54. */
  55. function superQuorum(uint256 timepoint) public view virtual override returns (uint256) {
  56. return Math.mulDiv(token().getPastTotalSupply(timepoint), superQuorumNumerator(timepoint), quorumDenominator());
  57. }
  58. /**
  59. * @dev Changes the super quorum numerator.
  60. *
  61. * Emits a {SuperQuorumNumeratorUpdated} event.
  62. *
  63. * Requirements:
  64. *
  65. * - Must be called through a governance proposal.
  66. * - New super quorum numerator must be smaller or equal to the denominator.
  67. * - New super quorum numerator must be greater than or equal to the quorum numerator.
  68. */
  69. function updateSuperQuorumNumerator(uint256 newSuperQuorumNumerator) public virtual onlyGovernance {
  70. _updateSuperQuorumNumerator(newSuperQuorumNumerator);
  71. }
  72. /**
  73. * @dev Changes the super quorum numerator.
  74. *
  75. * Emits a {SuperQuorumNumeratorUpdated} event.
  76. *
  77. * Requirements:
  78. *
  79. * - New super quorum numerator must be smaller or equal to the denominator.
  80. * - New super quorum numerator must be greater than or equal to the quorum numerator.
  81. */
  82. function _updateSuperQuorumNumerator(uint256 newSuperQuorumNumerator) internal virtual {
  83. uint256 denominator = quorumDenominator();
  84. if (newSuperQuorumNumerator > denominator) {
  85. revert GovernorInvalidSuperQuorumFraction(newSuperQuorumNumerator, denominator);
  86. }
  87. uint256 quorumNumerator = quorumNumerator();
  88. if (newSuperQuorumNumerator < quorumNumerator) {
  89. revert GovernorInvalidSuperQuorumTooSmall(newSuperQuorumNumerator, quorumNumerator);
  90. }
  91. uint256 oldSuperQuorumNumerator = _superQuorumNumeratorHistory.latest();
  92. _superQuorumNumeratorHistory.push(clock(), SafeCast.toUint208(newSuperQuorumNumerator));
  93. emit SuperQuorumNumeratorUpdated(oldSuperQuorumNumerator, newSuperQuorumNumerator);
  94. }
  95. /**
  96. * @dev Overrides {GovernorVotesQuorumFraction-_updateQuorumNumerator} to ensure the super
  97. * quorum numerator is greater than or equal to the quorum numerator.
  98. */
  99. function _updateQuorumNumerator(uint256 newQuorumNumerator) internal virtual override {
  100. // Ignoring check when the superQuorum was never set (construction sets quorum before superQuorum)
  101. if (_superQuorumNumeratorHistory.length() > 0) {
  102. uint256 superQuorumNumerator_ = superQuorumNumerator();
  103. if (newQuorumNumerator > superQuorumNumerator_) {
  104. revert GovernorInvalidQuorumTooLarge(newQuorumNumerator, superQuorumNumerator_);
  105. }
  106. }
  107. super._updateQuorumNumerator(newQuorumNumerator);
  108. }
  109. /// @inheritdoc GovernorSuperQuorum
  110. function state(
  111. uint256 proposalId
  112. ) public view virtual override(Governor, GovernorSuperQuorum) returns (ProposalState) {
  113. return super.state(proposalId);
  114. }
  115. }