ERC20Votes.sol 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/ERC20Votes.sol)
  3. pragma solidity ^0.8.20;
  4. import {ERC20} from "../ERC20.sol";
  5. import {Votes} from "../../../governance/utils/Votes.sol";
  6. import {Checkpoints} from "../../../utils/structs/Checkpoints.sol";
  7. /**
  8. * @dev Extension of ERC-20 to support Compound-like voting and delegation. This version is more generic than Compound's,
  9. * and supports token supply up to 2^208^ - 1, while COMP is limited to 2^96^ - 1.
  10. *
  11. * NOTE: This contract does not provide interface compatibility with Compound's COMP token.
  12. *
  13. * This extension keeps a history (checkpoints) of each account's vote power. Vote power can be delegated either
  14. * by calling the {delegate} function directly, or by providing a signature to be used with {delegateBySig}. Voting
  15. * power can be queried through the public accessors {getVotes} and {getPastVotes}.
  16. *
  17. * By default, token balance does not account for voting power. This makes transfers cheaper. The downside is that it
  18. * requires users to delegate to themselves in order to activate checkpoints and have their voting power tracked.
  19. */
  20. abstract contract ERC20Votes is ERC20, Votes {
  21. /**
  22. * @dev Total supply cap has been exceeded, introducing a risk of votes overflowing.
  23. */
  24. error ERC20ExceededSafeSupply(uint256 increasedSupply, uint256 cap);
  25. /**
  26. * @dev Maximum token supply. Defaults to `type(uint208).max` (2^208^ - 1).
  27. *
  28. * This maximum is enforced in {_update}. It limits the total supply of the token, which is otherwise a uint256,
  29. * so that checkpoints can be stored in the Trace208 structure used by {{Votes}}. Increasing this value will not
  30. * remove the underlying limitation, and will cause {_update} to fail because of a math overflow in
  31. * {_transferVotingUnits}. An override could be used to further restrict the total supply (to a lower value) if
  32. * additional logic requires it. When resolving override conflicts on this function, the minimum should be
  33. * returned.
  34. */
  35. function _maxSupply() internal view virtual returns (uint256) {
  36. return type(uint208).max;
  37. }
  38. /**
  39. * @dev Move voting power when tokens are transferred.
  40. *
  41. * Emits a {IVotes-DelegateVotesChanged} event.
  42. */
  43. function _update(address from, address to, uint256 value) internal virtual override {
  44. super._update(from, to, value);
  45. if (from == address(0)) {
  46. uint256 supply = totalSupply();
  47. uint256 cap = _maxSupply();
  48. if (supply > cap) {
  49. revert ERC20ExceededSafeSupply(supply, cap);
  50. }
  51. }
  52. _transferVotingUnits(from, to, value);
  53. }
  54. /**
  55. * @dev Returns the voting units of an `account`.
  56. *
  57. * WARNING: Overriding this function may compromise the internal vote accounting.
  58. * `ERC20Votes` assumes tokens map to voting units 1:1 and this is not easy to change.
  59. */
  60. function _getVotingUnits(address account) internal view virtual override returns (uint256) {
  61. return balanceOf(account);
  62. }
  63. /**
  64. * @dev Get number of checkpoints for `account`.
  65. */
  66. function numCheckpoints(address account) public view virtual returns (uint32) {
  67. return _numCheckpoints(account);
  68. }
  69. /**
  70. * @dev Get the `pos`-th checkpoint for `account`.
  71. */
  72. function checkpoints(address account, uint32 pos) public view virtual returns (Checkpoints.Checkpoint208 memory) {
  73. return _checkpoints(account, pos);
  74. }
  75. }