GovernorVotesQuorumFraction.sol 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (governance/extensions/GovernorVotesQuorumFraction.sol)
  3. pragma solidity ^0.8.20;
  4. import {GovernorVotes} from "./GovernorVotes.sol";
  5. import {Math} from "../../utils/math/Math.sol";
  6. import {SafeCast} from "../../utils/math/SafeCast.sol";
  7. import {Checkpoints} from "../../utils/structs/Checkpoints.sol";
  8. /**
  9. * @dev Extension of {Governor} for voting weight extraction from an {ERC20Votes} token and a quorum expressed as a
  10. * fraction of the total supply.
  11. */
  12. abstract contract GovernorVotesQuorumFraction is GovernorVotes {
  13. using Checkpoints for Checkpoints.Trace208;
  14. Checkpoints.Trace208 private _quorumNumeratorHistory;
  15. event QuorumNumeratorUpdated(uint256 oldQuorumNumerator, uint256 newQuorumNumerator);
  16. /**
  17. * @dev The quorum set is not a valid fraction.
  18. */
  19. error GovernorInvalidQuorumFraction(uint256 quorumNumerator, uint256 quorumDenominator);
  20. /**
  21. * @dev Initialize quorum as a fraction of the token's total supply.
  22. *
  23. * The fraction is specified as `numerator / denominator`. By default the denominator is 100, so quorum is
  24. * specified as a percent: a numerator of 10 corresponds to quorum being 10% of total supply. The denominator can be
  25. * customized by overriding {quorumDenominator}.
  26. */
  27. constructor(uint256 quorumNumeratorValue) {
  28. _updateQuorumNumerator(quorumNumeratorValue);
  29. }
  30. /**
  31. * @dev Returns the current quorum numerator. See {quorumDenominator}.
  32. */
  33. function quorumNumerator() public view virtual returns (uint256) {
  34. return _quorumNumeratorHistory.latest();
  35. }
  36. /**
  37. * @dev Returns the quorum numerator at a specific timepoint. See {quorumDenominator}.
  38. */
  39. function quorumNumerator(uint256 timepoint) public view virtual returns (uint256) {
  40. return _optimisticUpperLookupRecent(_quorumNumeratorHistory, timepoint);
  41. }
  42. /**
  43. * @dev Returns the quorum denominator. Defaults to 100, but may be overridden.
  44. */
  45. function quorumDenominator() public view virtual returns (uint256) {
  46. return 100;
  47. }
  48. /**
  49. * @dev Returns the quorum for a timepoint, in terms of number of votes: `supply * numerator / denominator`.
  50. */
  51. function quorum(uint256 timepoint) public view virtual override returns (uint256) {
  52. return Math.mulDiv(token().getPastTotalSupply(timepoint), quorumNumerator(timepoint), quorumDenominator());
  53. }
  54. /**
  55. * @dev Changes the quorum numerator.
  56. *
  57. * Emits a {QuorumNumeratorUpdated} event.
  58. *
  59. * Requirements:
  60. *
  61. * - Must be called through a governance proposal.
  62. * - New numerator must be smaller or equal to the denominator.
  63. */
  64. function updateQuorumNumerator(uint256 newQuorumNumerator) external virtual onlyGovernance {
  65. _updateQuorumNumerator(newQuorumNumerator);
  66. }
  67. /**
  68. * @dev Changes the quorum numerator.
  69. *
  70. * Emits a {QuorumNumeratorUpdated} event.
  71. *
  72. * Requirements:
  73. *
  74. * - New numerator must be smaller or equal to the denominator.
  75. */
  76. function _updateQuorumNumerator(uint256 newQuorumNumerator) internal virtual {
  77. uint256 denominator = quorumDenominator();
  78. if (newQuorumNumerator > denominator) {
  79. revert GovernorInvalidQuorumFraction(newQuorumNumerator, denominator);
  80. }
  81. uint256 oldQuorumNumerator = quorumNumerator();
  82. _quorumNumeratorHistory.push(clock(), SafeCast.toUint208(newQuorumNumerator));
  83. emit QuorumNumeratorUpdated(oldQuorumNumerator, newQuorumNumerator);
  84. }
  85. /**
  86. * @dev Returns the numerator at a specific timepoint.
  87. */
  88. function _optimisticUpperLookupRecent(
  89. Checkpoints.Trace208 storage ckpts,
  90. uint256 timepoint
  91. ) internal view returns (uint256) {
  92. // If trace is empty, key and value are both equal to 0.
  93. // In that case `key <= timepoint` is true, and it is ok to return 0.
  94. (, uint48 key, uint208 value) = ckpts.latestCheckpoint();
  95. return key <= timepoint ? value : ckpts.upperLookupRecent(SafeCast.toUint48(timepoint));
  96. }
  97. }