ERC20Capped.sol 981 B

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