GovernorVotesQuorumFraction.sol 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.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 SafeCast for *;
  15. using Checkpoints for Checkpoints.Trace224;
  16. uint256 private _quorumNumerator; // DEPRECATED in favor of _quorumNumeratorHistory
  17. /// @custom:oz-retyped-from Checkpoints.History
  18. Checkpoints.Trace224 private _quorumNumeratorHistory;
  19. event QuorumNumeratorUpdated(uint256 oldQuorumNumerator, uint256 newQuorumNumerator);
  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._checkpoints.length == 0 ? _quorumNumerator : _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. // If history is empty, fallback to old storage
  41. uint256 length = _quorumNumeratorHistory._checkpoints.length;
  42. if (length == 0) {
  43. return _quorumNumerator;
  44. }
  45. // Optimistic search, check the latest checkpoint
  46. Checkpoints.Checkpoint224 memory latest = _quorumNumeratorHistory._checkpoints[length - 1];
  47. if (latest._key <= timepoint) {
  48. return latest._value;
  49. }
  50. // Otherwise, do the binary search
  51. return _quorumNumeratorHistory.upperLookupRecent(timepoint.toUint32());
  52. }
  53. /**
  54. * @dev Returns the quorum denominator. Defaults to 100, but may be overridden.
  55. */
  56. function quorumDenominator() public view virtual returns (uint256) {
  57. return 100;
  58. }
  59. /**
  60. * @dev Returns the quorum for a timepoint, in terms of number of votes: `supply * numerator / denominator`.
  61. */
  62. function quorum(uint256 timepoint) public view virtual override returns (uint256) {
  63. return (token.getPastTotalSupply(timepoint) * quorumNumerator(timepoint)) / quorumDenominator();
  64. }
  65. /**
  66. * @dev Changes the quorum numerator.
  67. *
  68. * Emits a {QuorumNumeratorUpdated} event.
  69. *
  70. * Requirements:
  71. *
  72. * - Must be called through a governance proposal.
  73. * - New numerator must be smaller or equal to the denominator.
  74. */
  75. function updateQuorumNumerator(uint256 newQuorumNumerator) external virtual onlyGovernance {
  76. _updateQuorumNumerator(newQuorumNumerator);
  77. }
  78. /**
  79. * @dev Changes the quorum numerator.
  80. *
  81. * Emits a {QuorumNumeratorUpdated} event.
  82. *
  83. * Requirements:
  84. *
  85. * - New numerator must be smaller or equal to the denominator.
  86. */
  87. function _updateQuorumNumerator(uint256 newQuorumNumerator) internal virtual {
  88. require(
  89. newQuorumNumerator <= quorumDenominator(),
  90. "GovernorVotesQuorumFraction: quorumNumerator over quorumDenominator"
  91. );
  92. uint256 oldQuorumNumerator = quorumNumerator();
  93. // Make sure we keep track of the original numerator in contracts upgraded from a version without checkpoints.
  94. if (oldQuorumNumerator != 0 && _quorumNumeratorHistory._checkpoints.length == 0) {
  95. _quorumNumeratorHistory._checkpoints.push(
  96. Checkpoints.Checkpoint224({_key: 0, _value: oldQuorumNumerator.toUint224()})
  97. );
  98. }
  99. // Set new quorum for future proposals
  100. _quorumNumeratorHistory.push(clock().toUint32(), newQuorumNumerator.toUint224());
  101. emit QuorumNumeratorUpdated(oldQuorumNumerator, newQuorumNumerator);
  102. }
  103. }