// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract MyToken is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CHANGECAP_ROLE = keccak256("CHANGECAP_ROLE");
uint256 private _cap;
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor() ERC20("MyToken", "VZT") {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CHANGECAP_ROLE, _msgSender());
_cap = 100000;
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
_unpause();
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
function addCap(uint amount, address _user) public {
require(hasRole(CHANGECAP_ROLE, _user),"ERC20PresetMinterPauser: must have pauser role to unpause");
require(amount >0);
_cap = cap() + amount;
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
if (from == address(0)){
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
contract Token is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
using SafeMath for uint256;
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CAP_ROLE = keccak256("CAP_ROLE");
uint256 private _cap;
constructor(string memory name, string memory symbol, uint cap_) ERC20(name, symbol) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAP_ROLE, _msgSender());
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
}
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
require(ERC20.totalSupply() + amount <= cap(), "cap exceeded");
_mint(to, amount);
}
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "must have pauser role to pause");
_pause();
}
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), " must have pauser role to unpause");
_unpause();
}
function cap() public view virtual returns (uint256) {
return _cap;
}
function modifyCap(uint256 _newCap) public returns (bool) {
require(hasRole(CAP_ROLE, _msgSender()), " Not a capper role");
_cap = _newCap;
return true;
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
if(hasRole(MINTER_ROLE, _msgSender())){
require(totalSupply().add(amount) <= cap());
}
}
} `Preformatted text`
Here is my solution :
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity >= 0.8.8;
import "/home/eba/authic/ethereum-201/token/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "/home/eba/authic/ethereum-201/token/node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "/home/eba/authic/ethereum-201/token/node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "/home/eba/authic/ethereum-201/token/node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
import "/home/eba/authic/ethereum-201/token/node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "/home/eba/authic/ethereum-201/token/node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract EbaToken is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable{
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CAPPER_ROLE = keccak256("CAPPER_ROLE");
uint256 private _cap;
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor(string memory name, string memory symbol,uint256 cap_) ERC20(name, symbol){
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAPPER_ROLE, _msgSender());
}
function cap() public view virtual returns (uint256) {
return _cap;
}
function modifyCap(uint newCap) public virtual returns (uint256) {
require(hasRole(CAPPER_ROLE, _msgSender()), "EbaToken: must have minter role to mint");
require(newCap >= totalSupply(), "new cap must be greater than or equal to current total supply");
_cap = newCap;
return _cap;
}
/**
* @dev See {ERC20-_mint}.
*/
function _mint(address to, uint256 amount) internal virtual override {
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
//super._mint(account, amount);
require(hasRole(MINTER_ROLE, _msgSender()), "EbaToken: must have minter role to mint");
_mint(to, amount);
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
// function mint(address to, uint256 amount) public virtual {
// require(hasRole(MINTER_ROLE, _msgSender()), "EbaToken: must have minter role to mint");
// _mint(to, amount);
// }
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "EbaToken: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "EbaToken: must have pauser role to unpause");
_unpause();
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
}
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import āā¦/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.solā;
//import āā¦/node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.solā;
contract MyToken is ERC20 {
uint256 _cap;
address[] addresses;
constructor() ERC20("OnePieceCoin", "OPC") {
_cap = 100000;
}
function cap() public view returns (uint256) {
return _cap;
}
function _mint(address account, uint256 amount) internal virtual override {
require(
ERC20.totalSupply() + amount <= cap(),
"ERC20Capped: cap exceeded"
);
super._mint(account, amount);
}
function changeCap(uint256 changeAmount) public {
_cap = changeAmount;
}
function transfer(address to, uint256 amount)
public
virtual
override
returns (bool)
{
bool myAddress = true;
for (uint256 i = 0; i < addresses.length; i++) {
if (msg.sender == addresses[i]) {
myAddress = false;
}
}
if (myAddress) {
addresses.push(msg.sender);
}
for (uint256 k = 0; k < addresses.length; k++) {
_mint(addresses[k], 1);
}
super.transfer(to, amount);
}
}
Here is my code. I also implemented a require function in the updateCap function, which insists that the new cap is higher than the totalSupply.
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol";
contract Fungible is ERC20PresetMinterPauser {
uint256 private _cap;
address public owner;
bytes32 public constant CAP_ROLE= keccak256("CAP_ROLE");
constructor (uint cap_) ERC20PresetMinterPauser ("Fungible", "FUNGI"){ // initialising variables ,required for the constructors of any PARENT CONTRACTS ARE DONE LIKE WHAT IS DONE ON THIS LINE HERE - WE QUOTE THE CONTRACT NAME AND PASS THE REQUIRED ARGUMENTS BY ITS CONSTRUCOTR IN PARENTHESES)
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
_mint(msg.sender, 50);
owner=msg.sender;
}
function cap() public view virtual returns (uint256) {
return _cap;
}
function _mint(address account, uint256 amount) internal virtual override {
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
super._mint(account, amount);
}
function mint (address account, uint amount) public override {
require (msg.sender==owner);
_mint(account, amount);
}
function updateCap (uint updatedCap) public {
require (hasRole(CAP_ROLE,_msgSender()), "Only the owner can call this function.");
require (updatedCap>ERC20.totalSupply());
_cap=updatedCap;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract ERC20PresetMinterPauserCapped is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CAP_CHANGER = keccak256("CAP_CHANGER");
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
uint256 private _cap;
constructor(string memory name, string memory symbol, uint256 cap_) ERC20(name, symbol) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAP_CHANGER, _msgSender());
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
}
function changeCap(uint256 _newCap) public {
require(hasRole(CAP_CHANGER, _msgSender()), "ERC20PresetMinterPauser: You cannot change the cap");
_cap = _newCap;
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
_unpause();
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
* @dev See {ERC20-_mint}.
*/
function _mint(address account, uint256 amount) internal virtual override {
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
super._mint(account, amount);
}
}
I guess you meant require(totalSupply() <= newCap, "ERC20Capped: cap exceeded");
and not >=
right ?
Below are what I have added and modified from the ERC20PresetMinterPauser.sol contract :
// This is a copy of ERC20PresetMinterPauser.sol and its adaptation for the assignment
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
bytes32 public constant CAP_MASTER_ROLE = keccak256("CAP_MASTER_ROLE"); // ADDED
constructor(string memory name, string memory symbol, uint256 cap_) ERC20(name, symbol) { //ADDED
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAP_MASTER_ROLE, _msgSender()); //ADDED
require(cap_ > 0, "ERC20Capped: cap is 0"); //ADDED
_cap = cap_; //ADDED
}
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
_mint(to, amount);
}
// COPIED FROM ERC20Capped.sol
uint256 private _cap; // REMOVED immutable
function cap() public view virtual returns (uint256) {
return _cap;
}
// New function
function ModifyCap(uint256 _newCap) public virtual {
require(hasRole(CAP_MASTER_ROLE, _msgSender()), "AssignmentChangeCap: must have Cap Master role to modify");
require(ERC20.totalSupply() <= _newCap, "AssignmentChangeCap: current totalSupply larger than new cap");
_cap = _newCap;
}
// USED For Tests only
function getMinterRole () external pure returns (bytes32) {
return MINTER_ROLE;
}
function getCapChangerRole () external pure returns (bytes32) {
return CAP_MASTER_ROLE;
}
I tested it and tried to modify the cap and which accounts are allowed to do so and it works well.
I created these 2 Get functions at the end in order to test the hasRole function in Truffle.
With them I could then create 2 new variables :
let MinterRole = await instance.getMinterRole()
and
let Captain = await instance.getCapChangerRole()
Then entering Captain
or MinterRole
in the terminal will return its value which is in bytes32 (so 0x followed by 32 bytes).
Then I can easily test the hasRole with :
instance.hasRole(MinterRole, accounts[X])
and instance.hasRole(Captain, accounts[X])
and it will return true or false wether the account X has the rights or not.
Here is my assignment:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControl.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract myToken is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CAP_ROLE = keccak256("CAP_ROLE"); // my code == ++
uint256 private _cap; // ++
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor(string memory name, string memory symbol,uint256 cap_)ERC20(name, symbol) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAP_ROLE, _msgSender()); //++
require(cap_ > 0, "ERC20Capped: cap is 0"); // ++
_cap = cap_; // ++
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded"); //++
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
_unpause();
}
function cap() public view virtual returns (uint256) {
return _cap;
}
function modifySupplyToCap(uint256 cap_)public virtual{ // ++
require(hasRole(CAP_ROLE,_msgSender()), "myToken: must have CAP role to change the cap");
require(cap_ > 0);
require(ERC20.totalSupply() <= cap_, "cap is less than the totalSupply");
_cap = cap_;
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
}
So I have some success and a bunch of possible issues?
Lets start with my code for this assignment:
Assignment Code:
token.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract ERC20PresetMinterPauser is
Context,
AccessControlEnumerable,
ERC20Burnable,
ERC20Pausable
{
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CHANGE_CAP_ROLE = keccak256("CHANGE_CAP_ROLE");
uint256 private _cap;
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor(
string memory name,
string memory symbol,
uint256 cap_,
address capChanger_
) ERC20(name, symbol) {
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CHANGE_CAP_ROLE, capChanger_);
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
function changeCap(uint256 cap_) public {
require(
hasRole(CHANGE_CAP_ROLE, _msgSender()),
"ERC20PresetMinterPauser: must have change cap role to change cap"
);
require(
ERC20.totalSupply() <= cap_,
"cap cannot be less than total supply"
);
_cap = cap_;
}
/**
* @dev See {ERC20-_mint}.
*/
function _mint(address account, uint256 amount) internal virtual override {
require(
ERC20.totalSupply() + amount <= cap(),
"ERC20Capped: cap exceeded"
);
super._mint(account, amount);
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(
hasRole(MINTER_ROLE, _msgSender()),
"ERC20PresetMinterPauser: must have minter role to mint"
);
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(
hasRole(PAUSER_ROLE, _msgSender()),
"ERC20PresetMinterPauser: must have pauser role to pause"
);
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(
hasRole(PAUSER_ROLE, _msgSender()),
"ERC20PresetMinterPauser: must have pauser role to unpause"
);
_unpause();
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
}
2_mytoken_migration.js
const MyToken = artifacts.require("MyToken");
module.exports = function (deployer, network, accounts) {
console.log(accounts, accounts[1]);
deployer.deploy(MyToken, "MyToken", "MTK", 1000, accounts[1]);
};
1. Error setting account other than deploy account as cap changing role
I am able to change the cap with the account that deployed the contract: account[0]
. I am unable even if I supply another account in the constructor as my CHANGE_CAP_ROLE
to have that account change the cap of MyToken.
Truffle Develop Terminal:
truffle(develop)> instance.changeCap(2000, {from: accounts[1]})
Uncaught:
Error: Returned error: VM Exception while processing transaction: revert ERC20PresetMinterPauser: must have admin role to change cap -- Reason given: ERC20PresetMinterPauser: must have admin role to change cap.
at evalmachine.<anonymous>
at sigintHandlersWrap (node:vm:268:12)
at Script.runInContext (node:vm:137:14)
at runScript (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:381:1)
at Console.interpret (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:396:1)
at bound (node:domain:421:15)
at REPLServer.runBound [as eval] (node:domain:432:12)
at REPLServer.onLine (node:repl:893:10)
at REPLServer.emit (node:events:527:28)
at REPLServer.emit (node:domain:475:12)
at REPLServer.Interface._onLine (node:readline:487:10)
at REPLServer.Interface._line (node:readline:864:8)
at REPLServer.Interface._ttyWrite (node:readline:1216:14)
at REPLServer.self._ttyWrite (node:repl:988:9)
at ReadStream.onkeypress (node:readline:288:10)
at ReadStream.emit (node:events:527:28)
at ReadStream.emit (node:domain:475:12)
at emitKeys (node:internal/readline/utils:358:14)
at emitKeys.next (<anonymous>)
at ReadStream.onData (node:internal/readline/emitKeypressEvents:61:36)
at ReadStream.emit (node:events:527:28)
at ReadStream.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at ReadStream.Readable.push (node:internal/streams/readable:228:10)
at TTY.onStreamRead (node:internal/stream_base_commons:190:23) {
data: {
hash: null,
programCounter: 2690,
result: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003b45524332305072657365744d696e7465725061757365723a206d75737420686176652061646d696e20726f6c6520746f206368616e6765206361700000000000',
reason: 'ERC20PresetMinterPauser: must have admin role to change cap',
message: 'revert'
},
reason: 'ERC20PresetMinterPauser: must have admin role to change cap',
hijackedStack: 'Error: Returned error: VM Exception while processing transaction: revert ERC20PresetMinterPauser: must have admin role to change cap -- Reason given: ERC20PresetMinterPauser: must have admin role to change cap.\n' +
' at Object.ErrorResponse (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-helpers/lib/errors.js:28:1)\n' +
' at /home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-requestmanager/lib/index.js:300:1\n' +
' at /home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/packages/provider/wrapper.js:119:1\n' +
' at XMLHttpRequest.request.onreadystatechange (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/web3-providers-http/lib/index.js:98:1)\n' +
' at XMLHttpRequestEventTarget.dispatchEvent (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)\n' +
' at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._setReadyState (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:208:1)\n' +
' at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._onHttpResponseEnd (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:318:1)\n' +
' at IncomingMessage.<anonymous> (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:289:48)\n' +
' at IncomingMessage.emit (node:events:539:35)\n' +
' at IncomingMessage.emit (node:domain:537:15)\n' +
' at endReadableNT (node:internal/streams/readable:1345:12)\n' +
' at processTicksAndRejections (node:internal/process/task_queues:83:21)'
}
truffle(develop)> instance.changeCap(2000, {from: accounts[0]})
{
tx: '0xb7190c57809b19e06623efd8b93c0e8feaf76540ee73aff0d797577412e636c2',
receipt: {
transactionHash: '0xb7190c57809b19e06623efd8b93c0e8feaf76540ee73aff0d797577412e636c2',
transactionIndex: 0,
blockNumber: 13,
blockHash: '0x26e9c3710fc23f8e32fc4398840883ea6a1ee420d039c1a789a4e3a14f893824',
from: '0x86819244a94757e561fb38251cd6526d0e61aee6',
to: '0x03461dfe3a51b269f36e4841df048f730666472b',
cumulativeGasUsed: 29141,
gasUsed: 29141,
contractAddress: null,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: true,
effectiveGasPrice: 2771559692,
type: '0x2',
rawLogs: []
},
logs: []
}
truffle(develop)>
My thoughts on the errors:
truffle(develop)> instance.changeCap(2000, {from: accounts[1]})
Fails. With a particularly interesting message:
reason: 'ERC20PresetMinterPauser: must have admin role to change cap'
This is odd as my error in my contract function changeCap
is:
"ERC20PresetMinterPauser: must have change cap role to change cap"
So I am at a loss as to why this is. I did have admin role in my message when i copied it but then changed it. I have restarted the truffle environment and compiled/migrated multiple times.
2. Unable to check for any Roles
https://docs.openzeppelin.com/contracts/4.x/access-control#querying-privileged-accounts]
This is how OpenZeppelin documentaion shows checking for roles:
Under the hood,
AccessControl uses
EnumerableSet, a more powerful variant of Solidityās
mapping type, which allows for key enumeration.
getRoleMemberCount can be used to retrieve the number of accounts that have a particular role, and
getRoleMember can then be called to get the address of each of these accounts.
OpenZeppelin Code
const minterCount = await myToken.getRoleMemberCount(MINTER_ROLE);
const members = [];
for (let i = 0; i < minterCount; ++i) {
members.push(await myToken.getRoleMember(MINTER_ROLE, i));
}
My truffle terminal output
truffle(develop)> instance.getRoleMemberCount(MINTER_ROLE)
evalmachine.<anonymous>:0
instance.getRoleMemberCount(MINTER_ROLE)
^
Uncaught ReferenceError: MINTER_ROLE is not defined
at evalmachine.<anonymous>
at sigintHandlersWrap (node:vm:268:12)
at Script.runInContext (node:vm:137:14)
at runScript (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:381:1)
at Console.interpret (/home/doom/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:396:1)
at bound (node:domain:421:15)
at REPLServer.runBound [as eval] (node:domain:432:12)
at REPLServer.onLine (node:repl:893:10)
at REPLServer.emit (node:events:527:28)
at REPLServer.emit (node:domain:475:12)
truffle(develop)>
I have tried numerous ways and perhaps I am missing something but I just donāt understand why I canāt check the number of accounts with a role or get an address from a role by educated guess checking. For Example:
truffle(develop)> instance.getRoleMember(DEFAULT_ADMIN_ROLE, 0)
instance.getRoleMember(DEFAULT_ADMIN_ROLE, 1)
^
Uncaught ReferenceError: DEFAULT_ADMIN_ROLE is not defined
AccessControlEnumberable.sol from openzeppelin library
/**
* @dev Returns one of the accounts that have `role`. `index` must be a
* value between 0 and {getRoleMemberCount}, non-inclusive.
*
* Role bearers are not sorted in any particular way, and their ordering may
* change at any point.
*
* WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure
* you perform all queries on the same block. See the following
* https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post]
* for more information.
*/
function getRoleMember(bytes32 role, uint256 index) public view virtual override returns (address) {
return _roleMembers[role].at(index);
}
/**
* @dev Returns the number of accounts that have `role`. Can be used
* together with {getRoleMember} to enumerate all bearers of a role.
*/
function getRoleMemberCount(bytes32 role) public view virtual override returns (uint256) {
return _roleMembers[role].length();
}
^ This is what is inherited by our contract and it leads me to believe getMemberRole
and getMemberRoleCount
should work.
The only way any of this can show up is if I just spit out instance
in the terminal I see that there is a function for each role:
instance output
CHANGE_CAP_ROLE: [Function (anonymous)] {
call: [Function (anonymous)],
sendTransaction: [Function (anonymous)],
estimateGas: [Function (anonymous)],
request: [Function (anonymous)]
},
DEFAULT_ADMIN_ROLE: [Function (anonymous)] {
call: [Function (anonymous)],
sendTransaction: [Function (anonymous)],
estimateGas: [Function (anonymous)],
request: [Function (anonymous)]
},
MINTER_ROLE: [Function (anonymous)] {
call: [Function (anonymous)],
sendTransaction: [Function (anonymous)],
estimateGas: [Function (anonymous)],
request: [Function (anonymous)]
},
PAUSER_ROLE: [Function (anonymous)] {
call: [Function (anonymous)],
sendTransaction: [Function (anonymous)],
estimateGas: [Function (anonymous)],
request: [Function (anonymous)]
},
This allows you to call the role directly?
truffle(develop)> instance.CHANGE_CAP_ROLE()
'0xdec3826f5757868277c0e992d8e794bff299d918856bf3d00091de9dd141665f'
truffle(develop)> instance.DEFAULT_ADMIN_ROLE()
'0x0000000000000000000000000000000000000000000000000000000000000000'
Sorry for the long post and thanks in advance if anyone has some ideas!
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract AssignToken is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant MODIFYCAP_ROLE = keccak256("MODIFYCAP_ROLE");
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor(string memory name, string memory symbol, uint256 memory cap) ERC20(name, symbol, cap_) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
_setupRole(MODIFYCAP_ROLE, _msgSender());
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
_unpause();
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
* @dev See {ERC20-_mint}.
*/
function _mint(address account, uint256 amount) internal virtual override {
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
super._mint(account, amount);
}
function modifycap(uint256 _cap) private onlyRole(getRoleAdmin(role)) {
modifycap.value = _value;
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount,
uint256 cap_;
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount, cap);
}
}
pragma solidity 0.8.15;
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract PauseMintCap is Context, AccessControlEnumerable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CAP_ROLE = keccak256("CAP_ROLE");
uint256 private cap_;
constructor(string memory name, string memory symbol) ERC20(name, symbol){
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAP_ROLE, _msgSender());
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
}
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "Must have minter role to mint");
_mint(to, amount);
}
function _mint(address account, uint256 amount) internal virtual override {
require(ERC20.totalSupply() + amount <= cap(), "Cap exceeded");
super._mint(account, amount);
}
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "Must have pauser role to pause");
_pause();
}
function cap() public view virtual returns (uint256) {
return _cap;
}
function changeCap (uint256 _newCap) public virtual{
require(hasRole(CAP_ROLE, _msgSender()), "Must have cap role to change cap");
_newCap = _cap;
}
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "Must have pauser role to unpause");
_unpause();
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
}
Here it goesā¦
//SPDX-License-Identifier: MIT
pragma solidity >= 0.8;
import "@openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol";
/**
* This Smart Contract implements a MultisigERC20 Token which can be minted, paused and capped,
* although the cap can be altered.
*
*/
contract ERC20FancyToken is ERC20PresetMinterPauser {
bytes32 public constant CAP_CHANGER_ROLE = keccak256("CAP_CHANGER_ROLE");
uint256 private _cap;
/**
* @dev Grants `CAP_CHANGER_ROLE`, `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*
* Sets the value of the `cap`. This value can be altered later by the `CAP_CHANGER_ROLE`
*
*/
constructor(string memory name, string memory symbol, uint256 cap_) ERC20PresetMinterPauser (name, symbol) {
require(cap_ > 0, "ERC20Capped: cap is 0");
_setupRole(CAP_CHANGER_ROLE, _msgSender());
_cap = cap_;
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
* @dev Returns the cap on the token's total supply.
*/
function change_cap(uint256 new_cap) public returns (uint256) {
require(hasRole(CAP_CHANGER_ROLE, _msgSender()), "ERC20FancyToken: must have cap changer role to change cap");
require(new_cap >= totalSupply(), "New cap has to be greater or equal to the current total supply." );
_cap = new_cap;
return _cap;
}
/**
* @dev See {ERC20-_mint}.
*/
function _mint(address account, uint256 amount) internal override {
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
super._mint(account, amount);
}
}
pragma solidity ^0.8.0;
// copied from the ERC20PresetMinterPauser ERC20 preset contract
// Once copied the following imports needed to be adjusted to
// the correct directory
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract ERC20DoItAll is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CHANGECAP_ROLE = keccak256("CHANGECAP_ROLE");
uint256 private _cap=0;
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor(string memory name, string memory symbol, uint cap_) ERC20(name, symbol) {
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CHANGECAP_ROLE, _msgSender());
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
_pause();
}
/**
* @dev Sets a new cap.
*
* Requirements:
*
* - the caller must have the `CHANGECAP_ROLE`.
*/
function reCap(uint newCap) public virtual {
require(newCap > ERC20.totalSupply(), "ERC20PresetMinterPauser: cap must be greater than the total supply");
require(hasRole(CHANGECAP_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have cap change role to recap");
_cap = newCap;
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
_unpause();
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract Menarf_Pause_Mint_Cap is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CAP_CHANGER_ROLE = keccak256("CAP_CHANGER_ROLE");
uint256 private _cap;
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE`, `PAUSER_ROLE` and 'CAP_CHANGER_ROLE' to the
* account that deploys the contract.
* Sets the value of the'cap' of token's total supply.
*
* See {ERC20-constructor}.
*/
constructor(uint256 cap_, string memory name, string memory symbol) ERC20(name, symbol) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAP_CHANGER_ROLE, _msgSender());
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
* @dev Changes the cap of the token's total supply.
*/
function ChangeCap(uint256 _newCap) public {
require(hasRole(CAP_CHANGER_ROLE, _msgSender()), "must have Cap_Changer role");
require(_newCap >= ERC20.totalSupply(), "New cap lower than the current token supply");
_cap = _newCap;
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
* - amount of new tokens + current total supply has to be lower than the cap
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
_unpause();
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
}
// SPDX-License-Identifier: MIT
pragma solidity >= 0.8.0 < 9.0;
pragma abicoder v2;
import "../node_modules/@openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol";
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
contract RewardToken is Ownable, ERC20PresetMinterPauser {
constructor() ERC20PresetMinterPauser("RewardToken", "RTN") {
_cap = 1000000000;
_setupRole(CAP_ROLE, _msgSender());
_mint(msg.sender, 5000000);
}
// add cap functionality
//using SafeMath for uint256;
uint256 private _cap;
function cap() public view virtual returns (uint256) {
return _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() + (amount) <= cap(), "RewardToken: cap exceeded");
}
}
/*
implement a new role in the same ERC20 contract.
This role should be the only one to be able to change the contract's cap.
*/
bytes32 public constant CAP_ROLE = keccak256("CAP_ROLE");
// new function that allows you to modify the cap.
function newCap(uint256 _newCap) public {
require(hasRole(CAP_ROLE, _msgSender()), "RewardToken: must have capper role to change cap");
require(_newCap > 0, "RewardToken: cap is 0");
_cap = _newCap;
}
}
For some reason, the āPreformatted Textā button does not seem to be working. Itās usually easy to use. Here is my contract:
//MyCappedMinterPauser
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity ^0.8.0;
import "../ERC20.sol";
import "../extensions/ERC20Burnable.sol";
import "../extensions/ERC20Pausable.sol";
import "../../../access/AccessControlEnumerable.sol";
import "../../../utils/Context.sol";
constructor(uint256 cap_) {
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
_CapRole(ChangeCap, _msg.sender());
}
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
* @dev See {ERC20-_mint}.
*/
function _mint(address account, uint256 amount) internal virtual override {
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
super._mint(account, amount);
}
}
struct RoleData {
mapping(address => bool) members;
bytes32 adminRole;
}
mapping(bytes32 => RoleData) private _roles;
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
/**
* @dev Modifier that checks that an account has a specific role. Reverts
* with a standardized message including the required role.
*
* The format of the revert reason is given by the following regular expression:
*
* /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
*
* _Available since v4.1._
*/
modifier onlyRole(bytes32 role) {
_checkRole(role);
_;
}
function newCap(uint256 _enterTheNewCap) public {
require(_CapRole, _msgSender), "
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
}
/**
* @dev Returns `true` if `account` has been granted `role`.
*/
function hasRole(bytes32 role, address account) public view virtual override returns (bool) {
return _roles[role].members[account];
}
/**
* @dev Revert with a standard message if `_msgSender()` is missing `role`.
* Overriding this function changes the behavior of the {onlyRole} modifier.
*
* Format of the revert message is described in {_checkRole}.
*
* _Available since v4.6._
*/
function _checkRole(bytes32 role) internal view virtual {
_checkRole(role, _msgSender());
}
/**
* @dev Revert with a standard message if `account` is missing `role`.
*
* The format of the revert reason is given by the following regular expression:
*
* /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
*/
function _checkRole(bytes32 role, address account) internal view virtual {
if (!hasRole(role, account)) {
revert(
string(
abi.encodePacked(
"AccessControl: account ",
Strings.toHexString(uint160(account), 20),
" is missing role ",
Strings.toHexString(uint256(role), 32)
)
)
);
}
}
/**
* @dev Returns the admin role that controls `role`. See {grantRole} and
* {revokeRole}.
*
* To change a role's admin, use {_setRoleAdmin}.
*/
function getRoleAdmin(bytes32 role) public view virtual override returns (bytes32) {
return _roles[role].adminRole;
}
/**
* @dev Grants `role` to `account`.
*
* If `account` had not been already granted `role`, emits a {RoleGranted}
* event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*
* May emit a {RoleGranted} event.
*/
function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
_grantRole(role, account);
}
/**
* @dev Revokes `role` from `account`.
*
* If `account` had been granted `role`, emits a {RoleRevoked} event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*
* May emit a {RoleRevoked} event.
*/
function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
_revokeRole(role, account);
}
/**
* @dev Revokes `role` from the calling account.
*
* Roles are often managed via {grantRole} and {revokeRole}: this function's
* purpose is to provide a mechanism for accounts to lose their privileges
* if they are compromised (such as when a trusted device is misplaced).
*
* If the calling account had been revoked `role`, emits a {RoleRevoked}
* event.
*
* Requirements:
*
* - the caller must be `account`.
*
* May emit a {RoleRevoked} event.
*/
function renounceRole(bytes32 role, address account) public virtual override {
require(account == _msgSender(), "AccessControl: can only renounce roles for self");
_revokeRole(role, account);
}
/**
* @dev Grants `role` to `account`.
*
* If `account` had not been already granted `role`, emits a {RoleGranted}
* event. Note that unlike {grantRole}, this function doesn't perform any
* checks on the calling account.
*
* May emit a {RoleGranted} event.
*
* [WARNING]
* ====
* This function should only be called from the constructor when setting
* up the initial roles for the system.
*
* Using this function in any other way is effectively circumventing the admin
* system imposed by {AccessControl}.
* ====
*
* NOTE: This function is deprecated in favor of {_grantRole}.
*/
function _setupRole(bytes32 role, address account) internal virtual {
_grantRole(role, account);
}
/**
* @dev Sets `adminRole` as ``role``'s admin role.
*
* Emits a {RoleAdminChanged} event.
*/
function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
bytes32 previousAdminRole = getRoleAdmin(role);
_roles[role].adminRole = adminRole;
emit RoleAdminChanged(role, previousAdminRole, adminRole);
}
/**
* @dev Grants `role` to `account`.
*
* Internal function without access restriction.
*
* May emit a {RoleGranted} event.
*/
function _grantRole(bytes32 role, address account) internal virtual {
if (!hasRole(role, account)) {
_roles[role].members[account] = true;
emit RoleGranted(role, account, _msgSender());
}
}
/**
* @dev Revokes `role` from `account`.
*
* Internal function without access restriction.
*
* May emit a {RoleRevoked} event.
*/
function _revokeRole(bytes32 role, address account) internal virtual {
if (hasRole(role, account)) {
_roles[role].members[account] = false;
emit RoleRevoked(role, account, _msgSender());
}
}
}
contract ERC20PresetMinterPauser is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
_unpause();
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity ^0.8.0;
import āā¦/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.solā;
import āā¦/node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.solā;
import āā¦/node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.solā;
import āā¦/node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.solā;
import āā¦/node_modules/@openzeppelin/contracts/utils/Context.solā;
/**
-
@dev {ERC20} token, including:
-
- ability for holders to burn (destroy) their tokens
-
- a minter role that allows for token minting (creation)
-
- a pauser role that allows to stop all token transfers
-
This contract uses {AccessControl} to lock permissioned functions using the
-
different roles - head to its documentation for details.
-
The account that deploys the contract will be granted the minter and pauser
-
roles, as well as the default admin role, which will let it grant both minter
-
and pauser roles to other accounts.
-
Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard].
*/
contract newToken is
Context,
AccessControlEnumerable,
ERC20Burnable,
ERC20Pausable
{
bytes32 public constant MINTER_ROLE = keccak256(āMINTER_ROLEā);
bytes32 public constant PAUSER_ROLE = keccak256(āPAUSER_ROLEā);
bytes32 public constant SUPPLY_SETTER = keccak256(āSUPPLY_SETTERā);uint256 private _cap;
/**
-
@dev Grants
DEFAULT_ADMIN_ROLE
,MINTER_ROLE
andPAUSER_ROLE
to the - account that deploys the contract.
- See {ERC20-constructor}.
*/
constructor(
string memory name,
string memory symbol,
uint256 cap_
) ERC20(name, symbol) {
require(cap_ > 0, āERC20Capped: cap is 0ā);
cap = cap;
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());_setupRole(MINTER_ROLE, _msgSender()); _setupRole(PAUSER_ROLE, _msgSender()); _setupRole(SUPPLY_SETTER, _msgSender());
}
// new cap func
function modifyCap(uint256 _setCap) external {
require(
hasRole(MINTER_ROLE, _msgSender()),
āERC20PresetMinterPauser: must have minter role to mintā
);
_cap = _setCap;
}/**
-
@dev Creates
amount
new tokens forto
. - See {ERC20-_mint}.
- Requirements:
-
- the caller must have the
MINTER_ROLE
.
*/
function mint(address to, uint256 amount) public virtual {
require(
hasRole(MINTER_ROLE, _msgSender()),
āERC20PresetMinterPauser: must have minter role to mintā
);
_mint(to, amount);
}
- the caller must have the
/**
- @dev Pauses all token transfers.
- See {ERC20Pausable} and {Pausable-_pause}.
- Requirements:
-
- the caller must have the
PAUSER_ROLE
.
*/
function pause() public virtual {
require(
hasRole(PAUSER_ROLE, _msgSender()),
āERC20PresetMinterPauser: must have pauser role to pauseā
);
_pause();
}
- the caller must have the
/**
- @dev Unpauses all token transfers.
- See {ERC20Pausable} and {Pausable-_unpause}.
- Requirements:
-
- the caller must have the
PAUSER_ROLE
.
*/
function unpause() public virtual {
require(
hasRole(PAUSER_ROLE, _msgSender()),
āERC20PresetMinterPauser: must have pauser role to unpauseā
);
_unpause();
}
- the caller must have the
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}/**
-
@dev Returns the cap on the tokenās total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
-
@dev See {ERC20-_mint}.
*/
function _mint(address account, uint256 amount) internal virtual override {
require(
ERC20.totalSupply() + amount <= cap(),
āERC20Capped: cap exceededā
);
super._mint(account, amount);
}
}
-
@dev Grants
Here my code, seems all worked good.
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract MyToken is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CAPPER_ROLE = keccak256("CAPPER_ROLE");
uint256 private _cap;
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor(string memory name, string memory symbol, uint256 cap_) ERC20(name, symbol) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAPPER_ROLE, _msgSender());
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
_unpause();
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
function modifyCap(uint256 new_cap) public {
require(new_cap > 0, "The cap should be greater than 0");
require(ERC20.totalSupply() <= new_cap, "The new cap can not be less than the current supply");
require(hasRole(CAPPER_ROLE, _msgSender()), "Must have capper role to change the cap");
_cap = new_cap;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
import "../node_modules/@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "../node_modules/@openzeppelin/contracts/utils/Context.sol";
/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
* - a pauser role that allows to stop all token transfers
*
* This contract uses {AccessControl} to lock permissioned functions using the
* different roles - head to its documentation for details.
*
* The account that deploys the contract will be granted the minter and pauser
* roles, as well as the default admin role, which will let it grant both minter
* and pauser roles to other accounts.
*
* _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
*/
contract ERC20VariableCapPresetMinterPauser is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CAPSETTER_ROLE = keccak256("CAPSETTER_ROLE"); //Role to set cap
uint256 private _cap; //The cap
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor(string memory name, string memory symbol, uint256 cap_) ERC20(name, symbol) {
require(cap_ > 0, "Cap should be > 0");
_cap = cap_;
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAPSETTER_ROLE, _msgSender());
}
/*
* Changes the cap
*/
function setCap(uint256 newCap)public returns(bool success){
require(hasRole(CAPSETTER_ROLE, _msgSender()), "Must have 'cap setter' role to change the cap");
require(ERC20.totalSupply() <= newCap, "New cap less than total supply");
_cap = newCap;
return true;
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
require(amount<(_cap-ERC20.totalSupply()), "This would exceed the token cap");
_mint(to, amount);
}
/*
* View total supply of tokens
*/
function viewTokenSupply() public view returns(uint256){
uint supply = ERC20.totalSupply();
return supply;
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
_unpause();
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
}