ERC20Capped.sol 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. pragma solidity ^0.6.0;
  2. import "./ERC20.sol";
  3. /**
  4. * @dev Extension of {ERC20} that adds a cap to the supply of tokens.
  5. */
  6. abstract contract ERC20Capped is ERC20 {
  7. uint256 private _cap;
  8. /**
  9. * @dev Sets the value of the `cap`. This value is immutable, it can only be
  10. * set once during construction.
  11. */
  12. constructor (uint256 cap) public {
  13. require(cap > 0, "ERC20Capped: cap is 0");
  14. _cap = cap;
  15. }
  16. /**
  17. * @dev Returns the cap on the token's total supply.
  18. */
  19. function cap() public view returns (uint256) {
  20. return _cap;
  21. }
  22. /**
  23. * @dev See {ERC20-_beforeTokenTransfer}.
  24. *
  25. * Requirements:
  26. *
  27. * - minted tokens must not cause the total supply to go over the cap.
  28. */
  29. function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override {
  30. super._beforeTokenTransfer(from, to, amount);
  31. if (from == address(0)) { // When minting tokens
  32. require(totalSupply().add(amount) <= _cap, "ERC20Capped: cap exceeded");
  33. }
  34. }
  35. }