ReasonJun

Openzeppelin : ERC20PresetMinterPauser 본문

Blockchain/Solidity

Openzeppelin : ERC20PresetMinterPauser

ReasonJun 2023. 10. 22. 21:06
728x90
// 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";

/**
 * @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");

    /**
     * @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);
    }
}

This is the source code for the ERC20PresetMinterPauser contract. This contract is a preset implementation of the ERC-20 standard that includes the following features:

  • The 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

The contract uses the AccessControl library to lock permissioned functions using the different roles. 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.

 

The mint() function mints a new token and assigns it to the specified address. The caller must have the MINTER_ROLE in order to call this function.

 

The pause() and unpause() functions pause and unpause all token transfers, respectively. The caller must have the PAUSER_ROLE in order to call these functions.

 

The _beforeTokenTransfer() function is called before any token transfer. This function is used to implement the pausable functionality. If the contract is paused, then the transfer will be cancelled.

 

The supportsInterface() function returns true if the contract supports the specified interface. The contract supports the following interfaces:

  • IERC165
  • IERC20
  • IERC20Metadata
  • AccessControlEnumerable

This contract is a good starting point for anyone who wants to create an ERC-20 token with the features listed above.

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : abi.encodePacked(), super.uri()  (0) 2023.10.22
Ethereum : ERC1155  (0) 2023.10.22
Openzeppelin : ERC721PresetMinterPauserAutoId  (0) 2023.10.22
Solidity : sol2uml  (0) 2023.10.20
Solidity : deposit code example  (0) 2023.10.18
Comments