GovernorVotesQuorumFraction.sol 4.3 KB

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