VotesUpgradeable.sol 8.0 KB

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