* Fix unnamed return variable warning This commit fixes warnings thrown by the solc 0.7.4 compiler: "Warning: Unnamed return variable can remain unassigned. Add an explicit return with value to all non-reverting code paths or name the variable." * Fix function state mutability warning This commit fixes warnings thrown by the solc 0.7.4 compiler: "Warning: Function state mutability can be restricted to pure" * Fix shadows an existing declaration warning This commit fixes warnings thrown by the solc 0.7.4 compiler: "Warning: This declaration shadows an existing declaration." 1. Arguments by default are not underscored. 2. If the name isn't available due to shadowing, use prefix underscore. 3. If prefix underscore isn't available due to shadowing, use suffix underscore.
46 lines
1.1 KiB
Solidity
46 lines
1.1 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.7.0;
|
|
|
|
import "./ERC20.sol";
|
|
|
|
/**
|
|
* @dev Extension of {ERC20} that adds a cap to the supply of tokens.
|
|
*/
|
|
abstract contract ERC20Capped is ERC20 {
|
|
using SafeMath for uint256;
|
|
|
|
uint256 private _cap;
|
|
|
|
/**
|
|
* @dev Sets the value of the `cap`. This value is immutable, it can only be
|
|
* set once during construction.
|
|
*/
|
|
constructor (uint256 cap_) {
|
|
require(cap_ > 0, "ERC20Capped: cap is 0");
|
|
_cap = cap_;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the cap on the token's total supply.
|
|
*/
|
|
function cap() public view returns (uint256) {
|
|
return _cap;
|
|
}
|
|
|
|
/**
|
|
* @dev See {ERC20-_beforeTokenTransfer}.
|
|
*
|
|
* Requirements:
|
|
*
|
|
* - minted tokens must not cause the total supply to go over the cap.
|
|
*/
|
|
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override {
|
|
super._beforeTokenTransfer(from, to, amount);
|
|
|
|
if (from == address(0)) { // When minting tokens
|
|
require(totalSupply().add(amount) <= _cap, "ERC20Capped: cap exceeded");
|
|
}
|
|
}
|
|
}
|