GovernorCountingFractional.sol 9.1 KB

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