|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.20; |
| 4 | + |
| 5 | +import {SignatureChecker} from "../../utils/cryptography/SignatureChecker.sol"; |
| 6 | +import {SafeCast} from "../../utils/math/SafeCast.sol"; |
| 7 | +import {VotesExtended} from "../utils/VotesExtended.sol"; |
| 8 | +import {GovernorVotes} from "./GovernorVotes.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @dev Extension of {Governor} which enables delegatees to override the vote of their delegates. This module requires a |
| 12 | + * token token that inherits `VotesExtended`. |
| 13 | + */ |
| 14 | +abstract contract GovernorCountingOverridable is GovernorVotes { |
| 15 | + bytes32 public constant OVERRIDE_BALLOT_TYPEHASH = |
| 16 | + keccak256("OverrideBallot(uint256 proposalId,uint8 support,address voter,uint256 nonce,string reason)"); |
| 17 | + |
| 18 | + /** |
| 19 | + * @dev Supported vote types. Matches Governor Bravo ordering. |
| 20 | + */ |
| 21 | + enum VoteType { |
| 22 | + Against, |
| 23 | + For, |
| 24 | + Abstain |
| 25 | + } |
| 26 | + |
| 27 | + struct VoteReceipt { |
| 28 | + uint8 casted; // 0 if vote was not casted. Otherwise: support + 1 |
| 29 | + bool hasOverriden; |
| 30 | + uint208 overridenWeight; |
| 31 | + } |
| 32 | + |
| 33 | + struct ProposalVote { |
| 34 | + uint256[3] votes; |
| 35 | + mapping(address voter => VoteReceipt) voteReceipt; |
| 36 | + } |
| 37 | + |
| 38 | + event VoteReduced(address indexed voter, uint256 proposalId, uint8 support, uint256 weight); |
| 39 | + event OverrideVoteCast(address indexed voter, uint256 proposalId, uint8 support, uint256 weight, string reason); |
| 40 | + |
| 41 | + error GovernorAlreadyOverridenVote(address account); |
| 42 | + |
| 43 | + mapping(uint256 proposalId => ProposalVote) private _proposalVotes; |
| 44 | + |
| 45 | + /** |
| 46 | + * @dev See {IGovernor-COUNTING_MODE}. |
| 47 | + */ |
| 48 | + // solhint-disable-next-line func-name-mixedcase |
| 49 | + function COUNTING_MODE() public pure virtual override returns (string memory) { |
| 50 | + return "support=bravo,override&quorum=for,abstain&overridable=true"; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @dev See {IGovernor-hasVoted}. |
| 55 | + */ |
| 56 | + function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) { |
| 57 | + return _proposalVotes[proposalId].voteReceipt[account].casted != 0; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dev Check if an `account` has overridden their delegate for a proposal. |
| 62 | + */ |
| 63 | + function hasVotedOverride(uint256 proposalId, address account) public view virtual returns (bool) { |
| 64 | + return _proposalVotes[proposalId].voteReceipt[account].hasOverriden; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @dev Accessor to the internal vote counts. |
| 69 | + */ |
| 70 | + function proposalVotes( |
| 71 | + uint256 proposalId |
| 72 | + ) public view virtual returns (uint256 againstVotes, uint256 forVotes, uint256 abstainVotes) { |
| 73 | + uint256[3] storage votes = _proposalVotes[proposalId].votes; |
| 74 | + return (votes[uint8(VoteType.Against)], votes[uint8(VoteType.For)], votes[uint8(VoteType.Abstain)]); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @dev See {Governor-_quorumReached}. |
| 79 | + */ |
| 80 | + function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) { |
| 81 | + uint256[3] storage votes = _proposalVotes[proposalId].votes; |
| 82 | + return quorum(proposalSnapshot(proposalId)) <= votes[uint8(VoteType.For)] + votes[uint8(VoteType.Abstain)]; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be strictly over the againstVotes. |
| 87 | + */ |
| 88 | + function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) { |
| 89 | + uint256[3] storage votes = _proposalVotes[proposalId].votes; |
| 90 | + return votes[uint8(VoteType.For)] > votes[uint8(VoteType.Against)]; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @dev See {Governor-_countVote}. In this module, the support follows the `VoteType` enum (from Governor Bravo). |
| 95 | + * |
| 96 | + * NOTE: called by {Governor-_castVote} which emits the {IGovernor-VoteCast} (or {IGovernor-VoteCastWithParams}) |
| 97 | + * event. |
| 98 | + */ |
| 99 | + function _countVote( |
| 100 | + uint256 proposalId, |
| 101 | + address account, |
| 102 | + uint8 support, |
| 103 | + uint256 totalWeight, |
| 104 | + bytes memory /*params*/ |
| 105 | + ) internal virtual override returns (uint256) { |
| 106 | + ProposalVote storage proposalVote = _proposalVotes[proposalId]; |
| 107 | + |
| 108 | + if (support > uint8(VoteType.Abstain)) { |
| 109 | + revert GovernorInvalidVoteType(); |
| 110 | + } |
| 111 | + |
| 112 | + if (proposalVote.voteReceipt[account].casted != 0) { |
| 113 | + revert GovernorAlreadyCastVote(account); |
| 114 | + } |
| 115 | + |
| 116 | + totalWeight -= proposalVote.voteReceipt[account].overridenWeight; |
| 117 | + proposalVote.votes[support] += totalWeight; |
| 118 | + proposalVote.voteReceipt[account].casted = support + 1; |
| 119 | + |
| 120 | + return totalWeight; |
| 121 | + } |
| 122 | + |
| 123 | + /// @dev Variant of {Governor-_countVote} that deals with vote overrides. |
| 124 | + function _countOverride(uint256 proposalId, address account, uint8 support) internal virtual returns (uint256) { |
| 125 | + ProposalVote storage proposalVote = _proposalVotes[proposalId]; |
| 126 | + |
| 127 | + if (support > uint8(VoteType.Abstain)) { |
| 128 | + revert GovernorInvalidVoteType(); |
| 129 | + } |
| 130 | + |
| 131 | + if (proposalVote.voteReceipt[account].hasOverriden) { |
| 132 | + revert GovernorAlreadyOverridenVote(account); |
| 133 | + } |
| 134 | + |
| 135 | + uint256 proposalSnapshot = proposalSnapshot(proposalId); |
| 136 | + uint256 overridenWeight = VotesExtended(address(token())).getPastBalanceOf(account, proposalSnapshot); |
| 137 | + address delegate = VotesExtended(address(token())).getPastDelegate(account, proposalSnapshot); |
| 138 | + uint8 delegateCasted = proposalVote.voteReceipt[delegate].casted; |
| 139 | + |
| 140 | + proposalVote.voteReceipt[account].hasOverriden = true; |
| 141 | + proposalVote.votes[support] += overridenWeight; |
| 142 | + if (delegateCasted == 0) { |
| 143 | + proposalVote.voteReceipt[delegate].overridenWeight += SafeCast.toUint208(overridenWeight); |
| 144 | + } else { |
| 145 | + uint8 delegateSupport = delegateCasted - 1; |
| 146 | + proposalVote.votes[delegateSupport] -= overridenWeight; |
| 147 | + emit VoteReduced(delegate, proposalId, delegateSupport, overridenWeight); |
| 148 | + } |
| 149 | + |
| 150 | + return overridenWeight; |
| 151 | + } |
| 152 | + |
| 153 | + /// @dev variant of {Governor-_castVote} that deals with vote overrides. |
| 154 | + function _castOverride( |
| 155 | + uint256 proposalId, |
| 156 | + address account, |
| 157 | + uint8 support, |
| 158 | + string calldata reason |
| 159 | + ) internal virtual returns (uint256) { |
| 160 | + _validateStateBitmap(proposalId, _encodeStateBitmap(ProposalState.Active)); |
| 161 | + |
| 162 | + uint256 overridenWeight = _countOverride(proposalId, account, support); |
| 163 | + |
| 164 | + emit OverrideVoteCast(account, proposalId, support, overridenWeight, reason); |
| 165 | + |
| 166 | + _tallyUpdated(proposalId); |
| 167 | + |
| 168 | + return overridenWeight; |
| 169 | + } |
| 170 | + |
| 171 | + /// @dev Public function for casting an override vote |
| 172 | + function castOverrideVote( |
| 173 | + uint256 proposalId, |
| 174 | + uint8 support, |
| 175 | + string calldata reason |
| 176 | + ) public virtual returns (uint256) { |
| 177 | + address voter = _msgSender(); |
| 178 | + return _castOverride(proposalId, voter, support, reason); |
| 179 | + } |
| 180 | + |
| 181 | + /// @dev Public function for casting an override vote using a voter's signature |
| 182 | + function castOverrideVoteBySig( |
| 183 | + uint256 proposalId, |
| 184 | + uint8 support, |
| 185 | + address voter, |
| 186 | + string calldata reason, |
| 187 | + bytes calldata signature |
| 188 | + ) public virtual returns (uint256) { |
| 189 | + bool valid = SignatureChecker.isValidSignatureNow( |
| 190 | + voter, |
| 191 | + _hashTypedDataV4( |
| 192 | + keccak256( |
| 193 | + abi.encode( |
| 194 | + OVERRIDE_BALLOT_TYPEHASH, |
| 195 | + proposalId, |
| 196 | + support, |
| 197 | + voter, |
| 198 | + _useNonce(voter), |
| 199 | + keccak256(bytes(reason)) |
| 200 | + ) |
| 201 | + ) |
| 202 | + ), |
| 203 | + signature |
| 204 | + ); |
| 205 | + |
| 206 | + if (!valid) { |
| 207 | + revert GovernorInvalidSignature(voter); |
| 208 | + } |
| 209 | + |
| 210 | + return _castOverride(proposalId, voter, support, reason); |
| 211 | + } |
| 212 | +} |
0 commit comments