GovernorCountingOverridable.sol 8.4 KB

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