Votes.sol 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (governance/utils/Votes.sol)
  3. pragma solidity ^0.8.0;
  4. import "../../utils/Context.sol";
  5. import "../../utils/Counters.sol";
  6. import "../../utils/Checkpoints.sol";
  7. import "../../utils/cryptography/EIP712.sol";
  8. import "./IVotes.sol";
  9. /**
  10. * @dev This is a base abstract contract that tracks voting units, which are a measure of voting power that can be
  11. * transferred, and provides a system of vote delegation, where an account can delegate its voting units to a sort of
  12. * "representative" that will pool delegated voting units from different accounts and can then use it to vote in
  13. * decisions. In fact, voting units _must_ be delegated in order to count as actual votes, and an account has to
  14. * delegate those votes to itself if it wishes to participate in decisions and does not have a trusted representative.
  15. *
  16. * This contract is often combined with a token contract such that voting units correspond to token units. For an
  17. * example, see {ERC721Votes}.
  18. *
  19. * The full history of delegate votes is tracked on-chain so that governance protocols can consider votes as distributed
  20. * at a particular block number to protect against flash loans and double voting. The opt-in delegate system makes the
  21. * cost of this history tracking optional.
  22. *
  23. * When using this module the derived contract must implement {_getVotingUnits} (for example, make it return
  24. * {ERC721-balanceOf}), and can use {_transferVotingUnits} to track a change in the distribution of those units (in the
  25. * previous example, it would be included in {ERC721-_beforeTokenTransfer}).
  26. *
  27. * _Available since v4.5._
  28. */
  29. abstract contract Votes is IVotes, Context, EIP712 {
  30. using Checkpoints for Checkpoints.History;
  31. using Counters for Counters.Counter;
  32. bytes32 private constant _DELEGATION_TYPEHASH =
  33. keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");
  34. mapping(address => address) private _delegation;
  35. mapping(address => Checkpoints.History) private _delegateCheckpoints;
  36. Checkpoints.History private _totalCheckpoints;
  37. mapping(address => Counters.Counter) private _nonces;
  38. /**
  39. * @dev Returns the current amount of votes that `account` has.
  40. */
  41. function getVotes(address account) public view virtual override returns (uint256) {
  42. return _delegateCheckpoints[account].latest();
  43. }
  44. /**
  45. * @dev Returns the amount of votes that `account` had at the end of a past block (`blockNumber`).
  46. *
  47. * Requirements:
  48. *
  49. * - `blockNumber` must have been already mined
  50. */
  51. function getPastVotes(address account, uint256 blockNumber) public view virtual override returns (uint256) {
  52. return _delegateCheckpoints[account].getAtProbablyRecentBlock(blockNumber);
  53. }
  54. /**
  55. * @dev Returns the total supply of votes available at the end of a past block (`blockNumber`).
  56. *
  57. * NOTE: This value is the sum of all available votes, which is not necessarily the sum of all delegated votes.
  58. * Votes that have not been delegated are still part of total supply, even though they would not participate in a
  59. * vote.
  60. *
  61. * Requirements:
  62. *
  63. * - `blockNumber` must have been already mined
  64. */
  65. function getPastTotalSupply(uint256 blockNumber) public view virtual override returns (uint256) {
  66. require(blockNumber < block.number, "Votes: block not yet mined");
  67. return _totalCheckpoints.getAtProbablyRecentBlock(blockNumber);
  68. }
  69. /**
  70. * @dev Returns the current total supply of votes.
  71. */
  72. function _getTotalSupply() internal view virtual returns (uint256) {
  73. return _totalCheckpoints.latest();
  74. }
  75. /**
  76. * @dev Returns the delegate that `account` has chosen.
  77. */
  78. function delegates(address account) public view virtual override returns (address) {
  79. return _delegation[account];
  80. }
  81. /**
  82. * @dev Delegates votes from the sender to `delegatee`.
  83. */
  84. function delegate(address delegatee) public virtual override {
  85. address account = _msgSender();
  86. _delegate(account, delegatee);
  87. }
  88. /**
  89. * @dev Delegates votes from signer to `delegatee`.
  90. */
  91. function delegateBySig(
  92. address delegatee,
  93. uint256 nonce,
  94. uint256 expiry,
  95. uint8 v,
  96. bytes32 r,
  97. bytes32 s
  98. ) public virtual override {
  99. require(block.timestamp <= expiry, "Votes: signature expired");
  100. address signer = ECDSA.recover(
  101. _hashTypedDataV4(keccak256(abi.encode(_DELEGATION_TYPEHASH, delegatee, nonce, expiry))),
  102. v,
  103. r,
  104. s
  105. );
  106. require(nonce == _useNonce(signer), "Votes: invalid nonce");
  107. _delegate(signer, delegatee);
  108. }
  109. /**
  110. * @dev Delegate all of `account`'s voting units to `delegatee`.
  111. *
  112. * Emits events {IVotes-DelegateChanged} and {IVotes-DelegateVotesChanged}.
  113. */
  114. function _delegate(address account, address delegatee) internal virtual {
  115. address oldDelegate = delegates(account);
  116. _delegation[account] = delegatee;
  117. emit DelegateChanged(account, oldDelegate, delegatee);
  118. _moveDelegateVotes(oldDelegate, delegatee, _getVotingUnits(account));
  119. }
  120. /**
  121. * @dev Transfers, mints, or burns voting units. To register a mint, `from` should be zero. To register a burn, `to`
  122. * should be zero. Total supply of voting units will be adjusted with mints and burns.
  123. */
  124. function _transferVotingUnits(address from, address to, uint256 amount) internal virtual {
  125. if (from == address(0)) {
  126. _totalCheckpoints.push(_add, amount);
  127. }
  128. if (to == address(0)) {
  129. _totalCheckpoints.push(_subtract, amount);
  130. }
  131. _moveDelegateVotes(delegates(from), delegates(to), amount);
  132. }
  133. /**
  134. * @dev Moves delegated votes from one delegate to another.
  135. */
  136. function _moveDelegateVotes(address from, address to, uint256 amount) private {
  137. if (from != to && amount > 0) {
  138. if (from != address(0)) {
  139. (uint256 oldValue, uint256 newValue) = _delegateCheckpoints[from].push(_subtract, amount);
  140. emit DelegateVotesChanged(from, oldValue, newValue);
  141. }
  142. if (to != address(0)) {
  143. (uint256 oldValue, uint256 newValue) = _delegateCheckpoints[to].push(_add, amount);
  144. emit DelegateVotesChanged(to, oldValue, newValue);
  145. }
  146. }
  147. }
  148. function _add(uint256 a, uint256 b) private pure returns (uint256) {
  149. return a + b;
  150. }
  151. function _subtract(uint256 a, uint256 b) private pure returns (uint256) {
  152. return a - b;
  153. }
  154. /**
  155. * @dev Consumes a nonce.
  156. *
  157. * Returns the current value and increments nonce.
  158. */
  159. function _useNonce(address owner) internal virtual returns (uint256 current) {
  160. Counters.Counter storage nonce = _nonces[owner];
  161. current = nonce.current();
  162. nonce.increment();
  163. }
  164. /**
  165. * @dev Returns an address nonce.
  166. */
  167. function nonces(address owner) public view virtual returns (uint256) {
  168. return _nonces[owner].current();
  169. }
  170. /**
  171. * @dev Returns the contract's {EIP712} domain separator.
  172. */
  173. // solhint-disable-next-line func-name-mixedcase
  174. function DOMAIN_SEPARATOR() external view returns (bytes32) {
  175. return _domainSeparatorV4();
  176. }
  177. /**
  178. * @dev Must return the voting units held by an account.
  179. */
  180. function _getVotingUnits(address) internal view virtual returns (uint256);
  181. }