ERC20Capped.sol 902 B

123456789101112131415161718192021222324252627282930313233343536
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../ERC20.sol";
  4. /**
  5. * @dev Extension of {ERC20} that adds a cap to the supply of tokens.
  6. */
  7. abstract contract ERC20Capped is ERC20 {
  8. uint256 private immutable _cap;
  9. /**
  10. * @dev Sets the value of the `cap`. This value is immutable, it can only be
  11. * set once during construction.
  12. */
  13. constructor(uint256 cap_) {
  14. require(cap_ > 0, "ERC20Capped: cap is 0");
  15. _cap = cap_;
  16. }
  17. /**
  18. * @dev Returns the cap on the token's total supply.
  19. */
  20. function cap() public view virtual returns (uint256) {
  21. return _cap;
  22. }
  23. /**
  24. * @dev See {ERC20-_mint}.
  25. */
  26. function _mint(address account, uint256 amount) internal virtual override {
  27. require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
  28. super._mint(account, amount);
  29. }
  30. }