ERC20Votes.sol 3.0 KB

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