GovernorVotesSuperQuorumFraction.sol 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. */
  54. function superQuorum(uint256 timepoint) public view virtual override returns (uint256) {
  55. return Math.mulDiv(token().getPastTotalSupply(timepoint), superQuorumNumerator(timepoint), quorumDenominator());
  56. }
  57. /**
  58. * @dev Changes the super quorum numerator.
  59. *
  60. * Emits a {SuperQuorumNumeratorUpdated} event.
  61. *
  62. * Requirements:
  63. *
  64. * - Must be called through a governance proposal.
  65. * - New super quorum numerator must be smaller or equal to the denominator.
  66. * - New super quorum numerator must be greater than or equal to the quorum numerator.
  67. */
  68. function updateSuperQuorumNumerator(uint256 newSuperQuorumNumerator) public virtual onlyGovernance {
  69. _updateSuperQuorumNumerator(newSuperQuorumNumerator);
  70. }
  71. /**
  72. * @dev Changes the super quorum numerator.
  73. *
  74. * Emits a {SuperQuorumNumeratorUpdated} event.
  75. *
  76. * Requirements:
  77. *
  78. * - New super quorum numerator must be smaller or equal to the denominator.
  79. * - New super quorum numerator must be greater than or equal to the quorum numerator.
  80. */
  81. function _updateSuperQuorumNumerator(uint256 newSuperQuorumNumerator) internal virtual {
  82. uint256 denominator = quorumDenominator();
  83. if (newSuperQuorumNumerator > denominator) {
  84. revert GovernorInvalidSuperQuorumFraction(newSuperQuorumNumerator, denominator);
  85. }
  86. uint256 quorumNumerator = quorumNumerator();
  87. if (newSuperQuorumNumerator < quorumNumerator) {
  88. revert GovernorInvalidSuperQuorumTooSmall(newSuperQuorumNumerator, quorumNumerator);
  89. }
  90. uint256 oldSuperQuorumNumerator = _superQuorumNumeratorHistory.latest();
  91. _superQuorumNumeratorHistory.push(clock(), SafeCast.toUint208(newSuperQuorumNumerator));
  92. emit SuperQuorumNumeratorUpdated(oldSuperQuorumNumerator, newSuperQuorumNumerator);
  93. }
  94. /**
  95. * @dev Overrides {GovernorVotesQuorumFraction-_updateQuorumNumerator} to ensure the super
  96. * quorum numerator is greater than or equal to the quorum numerator.
  97. */
  98. function _updateQuorumNumerator(uint256 newQuorumNumerator) internal virtual override {
  99. // Ignoring check when the superQuorum was never set (construction sets quorum before superQuorum)
  100. if (_superQuorumNumeratorHistory.length() > 0) {
  101. uint256 superQuorumNumerator_ = superQuorumNumerator();
  102. if (newQuorumNumerator > superQuorumNumerator_) {
  103. revert GovernorInvalidQuorumTooLarge(newQuorumNumerator, superQuorumNumerator_);
  104. }
  105. }
  106. super._updateQuorumNumerator(newQuorumNumerator);
  107. }
  108. /// @inheritdoc GovernorSuperQuorum
  109. function state(
  110. uint256 proposalId
  111. ) public view virtual override(Governor, GovernorSuperQuorum) returns (ProposalState) {
  112. return super.state(proposalId);
  113. }
  114. }