GovernorCountingFractional.sol 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0) (governance/extensions/GovernorCountingFractional.sol)
  3. pragma solidity ^0.8.24;
  4. import {IGovernor, Governor} from "../Governor.sol";
  5. import {GovernorCountingSimple} from "./GovernorCountingSimple.sol";
  6. import {Math} from "../../utils/math/Math.sol";
  7. /**
  8. * @dev Extension of {Governor} for fractional voting.
  9. *
  10. * Similar to {GovernorCountingSimple}, this contract is a votes counting module for {Governor} that supports 3 options:
  11. * Against, For, Abstain. Additionally, it includes a fourth option: Fractional, which allows voters to split their voting
  12. * power amongst the other 3 options.
  13. *
  14. * Votes cast with the Fractional support must be accompanied by a `params` argument that is three packed `uint128` values
  15. * representing the weight the delegate assigns to Against, For, and Abstain respectively. For those votes cast for the other
  16. * 3 options, the `params` argument must be empty.
  17. *
  18. * This is mostly useful when the delegate is a contract that implements its own rules for voting. These delegate-contracts
  19. * can cast fractional votes according to the preferences of multiple entities delegating their voting power.
  20. *
  21. * Some example use cases include:
  22. *
  23. * * Voting from tokens that are held by a DeFi pool
  24. * * Voting from an L2 with tokens held by a bridge
  25. * * Voting privately from a shielded pool using zero knowledge proofs.
  26. *
  27. * Based on ScopeLift's https://github.com/ScopeLift/flexible-voting/blob/e5de2efd1368387b840931f19f3c184c85842761/src/GovernorCountingFractional.sol[`GovernorCountingFractional`]
  28. *
  29. * _Available since v5.1._
  30. */
  31. abstract contract GovernorCountingFractional is Governor {
  32. using Math for *;
  33. uint8 internal constant VOTE_TYPE_FRACTIONAL = 255;
  34. struct ProposalVote {
  35. uint256 againstVotes;
  36. uint256 forVotes;
  37. uint256 abstainVotes;
  38. mapping(address voter => uint256) usedVotes;
  39. }
  40. /**
  41. * @dev Mapping from proposal ID to vote tallies for that proposal.
  42. */
  43. mapping(uint256 proposalId => ProposalVote) private _proposalVotes;
  44. /**
  45. * @dev A fractional vote params uses more votes than are available for that user.
  46. */
  47. error GovernorExceedRemainingWeight(address voter, uint256 usedVotes, uint256 remainingWeight);
  48. /// @inheritdoc IGovernor
  49. // solhint-disable-next-line func-name-mixedcase
  50. function COUNTING_MODE() public pure virtual override returns (string memory) {
  51. return "support=bravo,fractional&quorum=for,abstain&params=fractional";
  52. }
  53. /// @inheritdoc IGovernor
  54. function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
  55. return usedVotes(proposalId, account) > 0;
  56. }
  57. /**
  58. * @dev Get the number of votes already cast by `account` for a proposal with `proposalId`. Useful for
  59. * integrations that allow delegates to cast rolling, partial votes.
  60. */
  61. function usedVotes(uint256 proposalId, address account) public view virtual returns (uint256) {
  62. return _proposalVotes[proposalId].usedVotes[account];
  63. }
  64. /**
  65. * @dev Get current distribution of votes for a given proposal.
  66. */
  67. function proposalVotes(
  68. uint256 proposalId
  69. ) public view virtual returns (uint256 againstVotes, uint256 forVotes, uint256 abstainVotes) {
  70. ProposalVote storage proposalVote = _proposalVotes[proposalId];
  71. return (proposalVote.againstVotes, proposalVote.forVotes, proposalVote.abstainVotes);
  72. }
  73. /// @inheritdoc Governor
  74. function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) {
  75. ProposalVote storage proposalVote = _proposalVotes[proposalId];
  76. return quorum(proposalSnapshot(proposalId)) <= proposalVote.forVotes + proposalVote.abstainVotes;
  77. }
  78. /**
  79. * @dev See {Governor-_voteSucceeded}. In this module, forVotes must be > againstVotes.
  80. */
  81. function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) {
  82. ProposalVote storage proposalVote = _proposalVotes[proposalId];
  83. return proposalVote.forVotes > proposalVote.againstVotes;
  84. }
  85. /**
  86. * @dev See {Governor-_countVote}. Function that records the delegate's votes.
  87. *
  88. * Executing this function consumes (part of) the delegate's weight on the proposal. This weight can be
  89. * distributed amongst the 3 options (Against, For, Abstain) by specifying a fractional `support`.
  90. *
  91. * This counting module supports two vote casting modes: nominal and fractional.
  92. *
  93. * - Nominal: A nominal vote is cast by setting `support` to one of the 3 bravo options (Against, For, Abstain).
  94. * - Fractional: A fractional vote is cast by setting `support` to `type(uint8).max` (255).
  95. *
  96. * Casting a nominal vote requires `params` to be empty and consumes the delegate's full remaining weight on the
  97. * proposal for the specified `support` option. This is similar to the {GovernorCountingSimple} module and follows
  98. * the `VoteType` enum from Governor Bravo. As a consequence, no vote weight remains unspent so no further voting
  99. * is possible (for this `proposalId` and this `account`).
  100. *
  101. * Casting a fractional vote consumes a fraction of the delegate's remaining weight on the proposal according to the
  102. * weights the delegate assigns to each support option (Against, For, Abstain respectively). The sum total of the
  103. * three decoded vote weights _must_ be less than or equal to the delegate's remaining weight on the proposal (i.e.
  104. * their checkpointed total weight minus votes already cast on the proposal). This format can be produced using:
  105. *
  106. * `abi.encodePacked(uint128(againstVotes), uint128(forVotes), uint128(abstainVotes))`
  107. *
  108. * NOTE: Consider that fractional voting restricts the number of casted votes (in each category) to 128 bits.
  109. * Depending on how many decimals the underlying token has, a single voter may require to split their vote into
  110. * multiple vote operations. For precision higher than ~30 decimals, large token holders may require a
  111. * potentially large number of calls to cast all their votes. The voter has the possibility to cast all the
  112. * remaining votes in a single operation using the traditional "bravo" vote.
  113. */
  114. // slither-disable-next-line cyclomatic-complexity
  115. function _countVote(
  116. uint256 proposalId,
  117. address account,
  118. uint8 support,
  119. uint256 totalWeight,
  120. bytes memory params
  121. ) internal virtual override returns (uint256) {
  122. // Compute number of remaining votes. Returns 0 on overflow.
  123. (, uint256 remainingWeight) = totalWeight.trySub(usedVotes(proposalId, account));
  124. if (remainingWeight == 0) {
  125. revert GovernorAlreadyCastVote(account);
  126. }
  127. uint256 againstVotes = 0;
  128. uint256 forVotes = 0;
  129. uint256 abstainVotes = 0;
  130. uint256 usedWeight = 0;
  131. // For clarity of event indexing, fractional voting must be clearly advertised in the "support" field.
  132. //
  133. // Supported `support` value must be:
  134. // - "Full" voting: `support = 0` (Against), `1` (For) or `2` (Abstain), with empty params.
  135. // - "Fractional" voting: `support = 255`, with 48 bytes params.
  136. if (support == uint8(GovernorCountingSimple.VoteType.Against)) {
  137. if (params.length != 0) revert GovernorInvalidVoteParams();
  138. usedWeight = againstVotes = remainingWeight;
  139. } else if (support == uint8(GovernorCountingSimple.VoteType.For)) {
  140. if (params.length != 0) revert GovernorInvalidVoteParams();
  141. usedWeight = forVotes = remainingWeight;
  142. } else if (support == uint8(GovernorCountingSimple.VoteType.Abstain)) {
  143. if (params.length != 0) revert GovernorInvalidVoteParams();
  144. usedWeight = abstainVotes = remainingWeight;
  145. } else if (support == VOTE_TYPE_FRACTIONAL) {
  146. // The `params` argument is expected to be three packed `uint128`:
  147. // `abi.encodePacked(uint128(againstVotes), uint128(forVotes), uint128(abstainVotes))`
  148. if (params.length != 0x30) revert GovernorInvalidVoteParams();
  149. assembly ("memory-safe") {
  150. againstVotes := shr(128, mload(add(params, 0x20)))
  151. forVotes := shr(128, mload(add(params, 0x30)))
  152. abstainVotes := shr(128, mload(add(params, 0x40)))
  153. usedWeight := add(add(againstVotes, forVotes), abstainVotes) // inputs are uint128: cannot overflow
  154. }
  155. // check parsed arguments are valid
  156. if (usedWeight > remainingWeight) {
  157. revert GovernorExceedRemainingWeight(account, usedWeight, remainingWeight);
  158. }
  159. } else {
  160. revert GovernorInvalidVoteType();
  161. }
  162. // update votes tracking
  163. ProposalVote storage details = _proposalVotes[proposalId];
  164. if (againstVotes > 0) details.againstVotes += againstVotes;
  165. if (forVotes > 0) details.forVotes += forVotes;
  166. if (abstainVotes > 0) details.abstainVotes += abstainVotes;
  167. details.usedVotes[account] += usedWeight;
  168. return usedWeight;
  169. }
  170. }