GovernorCountingFractional.sol 9.2 KB

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