ERC20Capped.sol 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (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-_transfer}.
  26. */
  27. function _update(
  28. address from,
  29. address to,
  30. uint256 amount
  31. ) internal virtual override {
  32. if (from == address(0)) {
  33. require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
  34. }
  35. super._update(from, to, amount);
  36. }
  37. }