Votes.sol 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.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/draft-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. // HARNESS : Hooks cannot access any information from Checkpoints yet, so I am also updating votes and fromBlock in this struct
  35. struct Ckpt {
  36. uint32 fromBlock;
  37. uint224 votes;
  38. }
  39. mapping(address => Ckpt) public _checkpoints;
  40. // HARNESSED getters
  41. function numCheckpoints(address account) public view returns (uint32) {
  42. return SafeCast.toUint32(_delegateCheckpoints[account]._checkpoints.length);
  43. }
  44. function ckptFromBlock(address account, uint32 pos) public view returns (uint32) {
  45. return _delegateCheckpoints[account]._checkpoints[pos]._blockNumber;
  46. }
  47. function ckptVotes(address account, uint32 pos) public view returns (uint224) {
  48. return _delegateCheckpoints[account]._checkpoints[pos]._value;
  49. }
  50. mapping(address => address) public _delegation;
  51. mapping(address => Checkpoints.History) private _delegateCheckpoints;
  52. Checkpoints.History private _totalCheckpoints;
  53. mapping(address => Counters.Counter) private _nonces;
  54. /**
  55. * @dev Returns the current amount of votes that `account` has.
  56. */
  57. function getVotes(address account) public view virtual override returns (uint256) {
  58. return _delegateCheckpoints[account].latest();
  59. }
  60. /**
  61. * @dev Returns the amount of votes that `account` had at the end of a past block (`blockNumber`).
  62. *
  63. * Requirements:
  64. *
  65. * - `blockNumber` must have been already mined
  66. */
  67. function getPastVotes(address account, uint256 blockNumber) public view virtual override returns (uint256) {
  68. return _delegateCheckpoints[account].getAtBlock(blockNumber);
  69. }
  70. /**
  71. * @dev Returns the total supply of votes available at the end of a past block (`blockNumber`).
  72. *
  73. * NOTE: This value is the sum of all available votes, which is not necessarily the sum of all delegated votes.
  74. * Votes that have not been delegated are still part of total supply, even though they would not participate in a
  75. * vote.
  76. *
  77. * Requirements:
  78. *
  79. * - `blockNumber` must have been already mined
  80. */
  81. function getPastTotalSupply(uint256 blockNumber) public view virtual override returns (uint256) {
  82. require(blockNumber < block.number, "Votes: block not yet mined");
  83. return _totalCheckpoints.getAtBlock(blockNumber);
  84. }
  85. /**
  86. * @dev Returns the current total supply of votes.
  87. */
  88. function _getTotalSupply() internal view virtual returns (uint256) {
  89. return _totalCheckpoints.latest();
  90. }
  91. /**
  92. * @dev Returns the delegate that `account` has chosen.
  93. */
  94. function delegates(address account) public view virtual override returns (address) {
  95. return _delegation[account];
  96. }
  97. /**
  98. * @dev Delegates votes from the sender to `delegatee`.
  99. */
  100. function delegate(address delegatee) public virtual override {
  101. address account = _msgSender();
  102. _delegate(account, delegatee);
  103. }
  104. /**
  105. * @dev Delegates votes from signer to `delegatee`.
  106. */
  107. function delegateBySig(
  108. address delegatee,
  109. uint256 nonce,
  110. uint256 expiry,
  111. uint8 v,
  112. bytes32 r,
  113. bytes32 s
  114. ) public virtual override {
  115. require(block.timestamp <= expiry, "Votes: signature expired");
  116. address signer = ECDSA.recover(
  117. _hashTypedDataV4(keccak256(abi.encode(_DELEGATION_TYPEHASH, delegatee, nonce, expiry))),
  118. v,
  119. r,
  120. s
  121. );
  122. require(nonce == _useNonce(signer), "Votes: invalid nonce");
  123. _delegate(signer, delegatee);
  124. }
  125. /**
  126. * @dev Delegate all of `account`'s voting units to `delegatee`.
  127. *
  128. * Emits events {DelegateChanged} and {DelegateVotesChanged}.
  129. */
  130. function _delegate(address account, address delegatee) public virtual {
  131. address oldDelegate = delegates(account);
  132. _delegation[account] = delegatee;
  133. emit DelegateChanged(account, oldDelegate, delegatee);
  134. _moveDelegateVotes(oldDelegate, delegatee, _getVotingUnits(account));
  135. }
  136. /**
  137. * @dev Transfers, mints, or burns voting units. To register a mint, `from` should be zero. To register a burn, `to`
  138. * should be zero. Total supply of voting units will be adjusted with mints and burns.
  139. */
  140. function _transferVotingUnits(
  141. address from,
  142. address to,
  143. uint256 amount
  144. ) internal virtual {
  145. if (from == address(0)) {
  146. _totalCheckpoints.push(_totalCheckpoints.latest() + amount); // Harnessed to remove function pointers
  147. }
  148. if (to == address(0)) {
  149. _totalCheckpoints.push(_totalCheckpoints.latest() - amount); // Harnessed to remove function pointers
  150. }
  151. _moveDelegateVotes(delegates(from), delegates(to), amount);
  152. }
  153. /**
  154. * @dev Moves delegated votes from one delegate to another.
  155. */
  156. function _moveDelegateVotes(
  157. address from,
  158. address to,
  159. uint256 amount
  160. ) private {
  161. if (from != to && amount > 0) {
  162. if (from != address(0)) {
  163. (uint256 oldValue, uint256 newValue) = _delegateCheckpoints[from].push(_delegateCheckpoints[from].latest() - amount); // HARNESSED TO REMOVE FUNCTION POINTERS
  164. _checkpoints[from] = Ckpt({fromBlock: SafeCast.toUint32(block.number), votes: SafeCast.toUint224(newValue)}); // HARNESS
  165. emit DelegateVotesChanged(from, oldValue, newValue);
  166. }
  167. if (to != address(0)) {
  168. (uint256 oldValue, uint256 newValue) = _delegateCheckpoints[to].push(_delegateCheckpoints[to].latest() + amount); // HARNESSED TO REMOVE FUNCTION POINTERS
  169. _checkpoints[to] = Ckpt({fromBlock: SafeCast.toUint32(block.number), votes: SafeCast.toUint224(newValue)}); // HARNESS
  170. emit DelegateVotesChanged(to, oldValue, newValue);
  171. }
  172. }
  173. }
  174. function _add(uint256 a, uint256 b) private pure returns (uint256) {
  175. return a + b;
  176. }
  177. function _subtract(uint256 a, uint256 b) private pure returns (uint256) {
  178. return a - b;
  179. }
  180. /**
  181. * @dev Consumes a nonce.
  182. *
  183. * Returns the current value and increments nonce.
  184. */
  185. function _useNonce(address owner) internal virtual returns (uint256 current) {
  186. Counters.Counter storage nonce = _nonces[owner];
  187. current = nonce.current();
  188. nonce.increment();
  189. }
  190. /**
  191. * @dev Returns an address nonce.
  192. */
  193. function nonces(address owner) public view virtual returns (uint256) {
  194. return _nonces[owner].current();
  195. }
  196. /**
  197. * @dev Returns the contract's {EIP712} domain separator.
  198. */
  199. // solhint-disable-next-line func-name-mixedcase
  200. function DOMAIN_SEPARATOR() external view returns (bytes32) {
  201. return _domainSeparatorV4();
  202. }
  203. /**
  204. * @dev Must return the voting units held by an account.
  205. */
  206. function _getVotingUnits(address) public virtual returns (uint256); // HARNESS: internal -> public
  207. }