GovernorCountingFractional.sol 9.0 KB

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