GovernorVotesQuorumFraction.sol 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (governance/extensions/GovernorVotesQuorumFraction.sol)
  3. pragma solidity ^0.8.0;
  4. import "./GovernorVotes.sol";
  5. import "../../utils/Checkpoints.sol";
  6. import "../../utils/math/SafeCast.sol";
  7. /**
  8. * @dev Extension of {Governor} for voting weight extraction from an {ERC20Votes} token and a quorum expressed as a
  9. * fraction of the total supply.
  10. *
  11. * _Available since v4.3._
  12. */
  13. abstract contract GovernorVotesQuorumFraction is GovernorVotes {
  14. using Checkpoints for Checkpoints.Trace224;
  15. uint256 private _quorumNumerator; // DEPRECATED in favor of _quorumNumeratorHistory
  16. /// @custom:oz-retyped-from Checkpoints.History
  17. Checkpoints.Trace224 private _quorumNumeratorHistory;
  18. event QuorumNumeratorUpdated(uint256 oldQuorumNumerator, uint256 newQuorumNumerator);
  19. /**
  20. * @dev Initialize quorum as a fraction of the token's total supply.
  21. *
  22. * The fraction is specified as `numerator / denominator`. By default the denominator is 100, so quorum is
  23. * specified as a percent: a numerator of 10 corresponds to quorum being 10% of total supply. The denominator can be
  24. * customized by overriding {quorumDenominator}.
  25. */
  26. constructor(uint256 quorumNumeratorValue) {
  27. _updateQuorumNumerator(quorumNumeratorValue);
  28. }
  29. /**
  30. * @dev Returns the current quorum numerator. See {quorumDenominator}.
  31. */
  32. function quorumNumerator() public view virtual returns (uint256) {
  33. return _quorumNumeratorHistory._checkpoints.length == 0 ? _quorumNumerator : _quorumNumeratorHistory.latest();
  34. }
  35. /**
  36. * @dev Returns the quorum numerator at a specific timepoint. See {quorumDenominator}.
  37. */
  38. function quorumNumerator(uint256 timepoint) public view virtual returns (uint256) {
  39. // If history is empty, fallback to old storage
  40. uint256 length = _quorumNumeratorHistory._checkpoints.length;
  41. if (length == 0) {
  42. return _quorumNumerator;
  43. }
  44. // Optimistic search, check the latest checkpoint
  45. Checkpoints.Checkpoint224 memory latest = _quorumNumeratorHistory._checkpoints[length - 1];
  46. if (latest._key <= timepoint) {
  47. return latest._value;
  48. }
  49. // Otherwise, do the binary search
  50. return _quorumNumeratorHistory.upperLookupRecent(SafeCast.toUint32(timepoint));
  51. }
  52. /**
  53. * @dev Returns the quorum denominator. Defaults to 100, but may be overridden.
  54. */
  55. function quorumDenominator() public view virtual returns (uint256) {
  56. return 100;
  57. }
  58. /**
  59. * @dev Returns the quorum for a timepoint, in terms of number of votes: `supply * numerator / denominator`.
  60. */
  61. function quorum(uint256 timepoint) public view virtual override returns (uint256) {
  62. return (token.getPastTotalSupply(timepoint) * quorumNumerator(timepoint)) / quorumDenominator();
  63. }
  64. /**
  65. * @dev Changes the quorum numerator.
  66. *
  67. * Emits a {QuorumNumeratorUpdated} event.
  68. *
  69. * Requirements:
  70. *
  71. * - Must be called through a governance proposal.
  72. * - New numerator must be smaller or equal to the denominator.
  73. */
  74. function updateQuorumNumerator(uint256 newQuorumNumerator) external virtual onlyGovernance {
  75. _updateQuorumNumerator(newQuorumNumerator);
  76. }
  77. /**
  78. * @dev Changes the quorum numerator.
  79. *
  80. * Emits a {QuorumNumeratorUpdated} event.
  81. *
  82. * Requirements:
  83. *
  84. * - New numerator must be smaller or equal to the denominator.
  85. */
  86. function _updateQuorumNumerator(uint256 newQuorumNumerator) internal virtual {
  87. require(
  88. newQuorumNumerator <= quorumDenominator(),
  89. "GovernorVotesQuorumFraction: quorumNumerator over quorumDenominator"
  90. );
  91. uint256 oldQuorumNumerator = quorumNumerator();
  92. // Make sure we keep track of the original numerator in contracts upgraded from a version without checkpoints.
  93. if (oldQuorumNumerator != 0 && _quorumNumeratorHistory._checkpoints.length == 0) {
  94. _quorumNumeratorHistory._checkpoints.push(
  95. Checkpoints.Checkpoint224({_key: 0, _value: SafeCast.toUint224(oldQuorumNumerator)})
  96. );
  97. }
  98. // Set new quorum for future proposals
  99. _quorumNumeratorHistory.push(SafeCast.toUint32(clock()), SafeCast.toUint224(newQuorumNumerator));
  100. emit QuorumNumeratorUpdated(oldQuorumNumerator, newQuorumNumerator);
  101. }
  102. }