VotesUpgradeable.sol 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. }
  33. function __Votes_init_unchained() internal onlyInitializing {
  34. }
  35. using CheckpointsUpgradeable for CheckpointsUpgradeable.History;
  36. using CountersUpgradeable for CountersUpgradeable.Counter;
  37. bytes32 private constant _DELEGATION_TYPEHASH =
  38. keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");
  39. mapping(address => address) private _delegation;
  40. mapping(address => CheckpointsUpgradeable.History) private _delegateCheckpoints;
  41. CheckpointsUpgradeable.History private _totalCheckpoints;
  42. mapping(address => CountersUpgradeable.Counter) private _nonces;
  43. /**
  44. * @dev Returns the current amount of votes that `account` has.
  45. */
  46. function getVotes(address account) public view virtual override returns (uint256) {
  47. return _delegateCheckpoints[account].latest();
  48. }
  49. /**
  50. * @dev Returns the amount of votes that `account` had at the end of a past block (`blockNumber`).
  51. *
  52. * Requirements:
  53. *
  54. * - `blockNumber` must have been already mined
  55. */
  56. function getPastVotes(address account, uint256 blockNumber) public view virtual override returns (uint256) {
  57. return _delegateCheckpoints[account].getAtBlock(blockNumber);
  58. }
  59. /**
  60. * @dev Returns the total supply of votes available at the end of a past block (`blockNumber`).
  61. *
  62. * NOTE: This value is the sum of all available votes, which is not necessarily the sum of all delegated votes.
  63. * Votes that have not been delegated are still part of total supply, even though they would not participate in a
  64. * vote.
  65. *
  66. * Requirements:
  67. *
  68. * - `blockNumber` must have been already mined
  69. */
  70. function getPastTotalSupply(uint256 blockNumber) public view virtual override returns (uint256) {
  71. require(blockNumber < block.number, "Votes: block not yet mined");
  72. return _totalCheckpoints.getAtBlock(blockNumber);
  73. }
  74. /**
  75. * @dev Returns the current total supply of votes.
  76. */
  77. function _getTotalSupply() internal view virtual returns (uint256) {
  78. return _totalCheckpoints.latest();
  79. }
  80. /**
  81. * @dev Returns the delegate that `account` has chosen.
  82. */
  83. function delegates(address account) public view virtual override returns (address) {
  84. return _delegation[account];
  85. }
  86. /**
  87. * @dev Delegates votes from the sender to `delegatee`.
  88. */
  89. function delegate(address delegatee) public virtual override {
  90. address account = _msgSender();
  91. _delegate(account, delegatee);
  92. }
  93. /**
  94. * @dev Delegates votes from signer to `delegatee`.
  95. */
  96. function delegateBySig(
  97. address delegatee,
  98. uint256 nonce,
  99. uint256 expiry,
  100. uint8 v,
  101. bytes32 r,
  102. bytes32 s
  103. ) public virtual override {
  104. require(block.timestamp <= expiry, "Votes: signature expired");
  105. address signer = ECDSAUpgradeable.recover(
  106. _hashTypedDataV4(keccak256(abi.encode(_DELEGATION_TYPEHASH, delegatee, nonce, expiry))),
  107. v,
  108. r,
  109. s
  110. );
  111. require(nonce == _useNonce(signer), "Votes: invalid nonce");
  112. _delegate(signer, delegatee);
  113. }
  114. /**
  115. * @dev Delegate all of `account`'s voting units to `delegatee`.
  116. *
  117. * Emits events {DelegateChanged} and {DelegateVotesChanged}.
  118. */
  119. function _delegate(address account, address delegatee) internal virtual {
  120. address oldDelegate = delegates(account);
  121. _delegation[account] = delegatee;
  122. emit DelegateChanged(account, oldDelegate, delegatee);
  123. _moveDelegateVotes(oldDelegate, delegatee, _getVotingUnits(account));
  124. }
  125. /**
  126. * @dev Transfers, mints, or burns voting units. To register a mint, `from` should be zero. To register a burn, `to`
  127. * should be zero. Total supply of voting units will be adjusted with mints and burns.
  128. */
  129. function _transferVotingUnits(
  130. address from,
  131. address to,
  132. uint256 amount
  133. ) internal virtual {
  134. if (from == address(0)) {
  135. _totalCheckpoints.push(_add, amount);
  136. }
  137. if (to == address(0)) {
  138. _totalCheckpoints.push(_subtract, amount);
  139. }
  140. _moveDelegateVotes(delegates(from), delegates(to), amount);
  141. }
  142. /**
  143. * @dev Moves delegated votes from one delegate to another.
  144. */
  145. function _moveDelegateVotes(
  146. address from,
  147. address to,
  148. uint256 amount
  149. ) private {
  150. if (from != to && amount > 0) {
  151. if (from != address(0)) {
  152. (uint256 oldValue, uint256 newValue) = _delegateCheckpoints[from].push(_subtract, amount);
  153. emit DelegateVotesChanged(from, oldValue, newValue);
  154. }
  155. if (to != address(0)) {
  156. (uint256 oldValue, uint256 newValue) = _delegateCheckpoints[to].push(_add, amount);
  157. emit DelegateVotesChanged(to, oldValue, newValue);
  158. }
  159. }
  160. }
  161. function _add(uint256 a, uint256 b) private pure returns (uint256) {
  162. return a + b;
  163. }
  164. function _subtract(uint256 a, uint256 b) private pure returns (uint256) {
  165. return a - b;
  166. }
  167. /**
  168. * @dev Consumes a nonce.
  169. *
  170. * Returns the current value and increments nonce.
  171. */
  172. function _useNonce(address owner) internal virtual returns (uint256 current) {
  173. CountersUpgradeable.Counter storage nonce = _nonces[owner];
  174. current = nonce.current();
  175. nonce.increment();
  176. }
  177. /**
  178. * @dev Returns an address nonce.
  179. */
  180. function nonces(address owner) public view virtual returns (uint256) {
  181. return _nonces[owner].current();
  182. }
  183. /**
  184. * @dev Returns the contract's {EIP712} domain separator.
  185. */
  186. // solhint-disable-next-line func-name-mixedcase
  187. function DOMAIN_SEPARATOR() external view returns (bytes32) {
  188. return _domainSeparatorV4();
  189. }
  190. /**
  191. * @dev Must return the voting units held by an account.
  192. */
  193. function _getVotingUnits(address) internal virtual returns (uint256);
  194. /**
  195. * This empty reserved space is put in place to allow future versions to add new
  196. * variables without shifting down storage in the inheritance chain.
  197. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  198. */
  199. uint256[46] private __gap;
  200. }