GovernorCountingFractional.sol 9.1 KB

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