|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import {Governor} from "../Governor.sol"; |
| 5 | +import {GovernorSuperQuorum} from "./GovernorSuperQuorum.sol"; |
| 6 | +import {GovernorVotesQuorumFraction} from "./GovernorVotesQuorumFraction.sol"; |
| 7 | +import {Math} from "../../utils/math/Math.sol"; |
| 8 | +import {SafeCast} from "../../utils/math/SafeCast.sol"; |
| 9 | +import {Checkpoints} from "../../utils/structs/Checkpoints.sol"; |
| 10 | + |
| 11 | +/** |
| 12 | + * @dev Extension of {GovernorVotesQuorumFraction} with a super quorum expressed as a |
| 13 | + * fraction of the total supply. Proposals that meet the super quorum (and have a majority of for votes) advance to |
| 14 | + * the `Succeeded` state before the proposal deadline. |
| 15 | + */ |
| 16 | +abstract contract GovernorVotesSuperQuorumFraction is GovernorVotesQuorumFraction, GovernorSuperQuorum { |
| 17 | + using Checkpoints for Checkpoints.Trace208; |
| 18 | + |
| 19 | + Checkpoints.Trace208 private _superQuorumNumeratorHistory; |
| 20 | + |
| 21 | + event SuperQuorumNumeratorUpdated(uint256 oldSuperQuorumNumerator, uint256 newSuperQuorumNumerator); |
| 22 | + |
| 23 | + /** |
| 24 | + * @dev The super quorum set is not valid as it exceeds the quorum denominator. |
| 25 | + */ |
| 26 | + error GovernorInvalidSuperQuorumFraction(uint256 superQuorumNumerator, uint256 denominator); |
| 27 | + |
| 28 | + /** |
| 29 | + * @dev The super quorum set is not valid as it is smaller or equal to the quorum. |
| 30 | + */ |
| 31 | + error GovernorInvalidSuperQuorumTooSmall(uint256 superQuorumNumerator, uint256 quorumNumerator); |
| 32 | + |
| 33 | + /** |
| 34 | + * @dev The quorum set is not valid as it exceeds the super quorum. |
| 35 | + */ |
| 36 | + error GovernorInvalidQuorumTooLarge(uint256 quorumNumerator, uint256 superQuorumNumerator); |
| 37 | + |
| 38 | + /** |
| 39 | + * @dev Initialize super quorum as a fraction of the token's total supply. |
| 40 | + * |
| 41 | + * The super quorum is specified as a fraction of the token's total supply and has to |
| 42 | + * be greater than the quorum. |
| 43 | + */ |
| 44 | + constructor(uint256 superQuorumNumeratorValue) { |
| 45 | + _updateSuperQuorumNumerator(superQuorumNumeratorValue); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @dev Returns the current super quorum numerator. |
| 50 | + */ |
| 51 | + function superQuorumNumerator() public view virtual returns (uint256) { |
| 52 | + return _superQuorumNumeratorHistory.latest(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @dev Returns the super quorum numerator at a specific `timepoint`. |
| 57 | + */ |
| 58 | + function superQuorumNumerator(uint256 timepoint) public view virtual returns (uint256) { |
| 59 | + return _optimisticUpperLookupRecent(_superQuorumNumeratorHistory, timepoint); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @dev Returns the super quorum for a `timepoint`, in terms of number of votes: `supply * numerator / denominator`. |
| 64 | + */ |
| 65 | + function superQuorum(uint256 timepoint) public view virtual override returns (uint256) { |
| 66 | + return Math.mulDiv(token().getPastTotalSupply(timepoint), superQuorumNumerator(timepoint), quorumDenominator()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @dev Changes the super quorum numerator. |
| 71 | + * |
| 72 | + * Emits a {SuperQuorumNumeratorUpdated} event. |
| 73 | + * |
| 74 | + * Requirements: |
| 75 | + * |
| 76 | + * - Must be called through a governance proposal. |
| 77 | + * - New super quorum numerator must be smaller or equal to the denominator. |
| 78 | + * - New super quorum numerator must be greater than or equal to the quorum numerator. |
| 79 | + */ |
| 80 | + function updateSuperQuorumNumerator(uint256 newSuperQuorumNumerator) public virtual onlyGovernance { |
| 81 | + _updateSuperQuorumNumerator(newSuperQuorumNumerator); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @dev Changes the super quorum numerator. |
| 86 | + * |
| 87 | + * Emits a {SuperQuorumNumeratorUpdated} event. |
| 88 | + * |
| 89 | + * Requirements: |
| 90 | + * |
| 91 | + * - New super quorum numerator must be smaller or equal to the denominator. |
| 92 | + * - New super quorum numerator must be greater than or equal to the quorum numerator. |
| 93 | + */ |
| 94 | + function _updateSuperQuorumNumerator(uint256 newSuperQuorumNumerator) internal virtual { |
| 95 | + uint256 denominator = quorumDenominator(); |
| 96 | + if (newSuperQuorumNumerator > denominator) { |
| 97 | + revert GovernorInvalidSuperQuorumFraction(newSuperQuorumNumerator, denominator); |
| 98 | + } |
| 99 | + |
| 100 | + uint256 quorumNumerator = quorumNumerator(); |
| 101 | + if (newSuperQuorumNumerator < quorumNumerator) { |
| 102 | + revert GovernorInvalidSuperQuorumTooSmall(newSuperQuorumNumerator, quorumNumerator); |
| 103 | + } |
| 104 | + |
| 105 | + uint256 oldSuperQuorumNumerator = _superQuorumNumeratorHistory.latest(); |
| 106 | + _superQuorumNumeratorHistory.push(clock(), SafeCast.toUint208(newSuperQuorumNumerator)); |
| 107 | + |
| 108 | + emit SuperQuorumNumeratorUpdated(oldSuperQuorumNumerator, newSuperQuorumNumerator); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @dev Overrides {GovernorVotesQuorumFraction-_updateQuorumNumerator} to ensure the super |
| 113 | + * quorum numerator is greater than or equal to the quorum numerator. |
| 114 | + */ |
| 115 | + function _updateQuorumNumerator(uint256 newQuorumNumerator) internal virtual override { |
| 116 | + // Ignoring check when the superQuorum was never set (construction sets quorum before superQuorum) |
| 117 | + if (_superQuorumNumeratorHistory.length() > 0) { |
| 118 | + uint256 superQuorumNumerator_ = superQuorumNumerator(); |
| 119 | + if (newQuorumNumerator > superQuorumNumerator_) { |
| 120 | + revert GovernorInvalidQuorumTooLarge(newQuorumNumerator, superQuorumNumerator_); |
| 121 | + } |
| 122 | + } |
| 123 | + super._updateQuorumNumerator(newQuorumNumerator); |
| 124 | + } |
| 125 | + |
| 126 | + /// @inheritdoc GovernorSuperQuorum |
| 127 | + function state( |
| 128 | + uint256 proposalId |
| 129 | + ) public view virtual override(Governor, GovernorSuperQuorum) returns (ProposalState) { |
| 130 | + return super.state(proposalId); |
| 131 | + } |
| 132 | +} |
0 commit comments