|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Compatible with OpenZeppelin Contracts ^5.0.0 |
| 3 | +pragma solidity ^0.8.20; |
| 4 | + |
| 5 | +import { AccessControlEnumerable } from "@openzeppelin/contracts/access/extensions/AccessControlEnumerable.sol"; |
| 6 | +import { IERC165 } from "@openzeppelin/contracts/interfaces/IERC165.sol"; |
| 7 | +import { ERC1155 } from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; |
| 8 | + |
| 9 | +import { IERC1155Common } from "./interfaces/IERC1155Common.sol"; |
| 10 | +import { ERC1155Burnable } from "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Burnable.sol"; |
| 11 | +import { ERC1155Pausable } from "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Pausable.sol"; |
| 12 | +import { ERC1155Supply } from "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol"; |
| 13 | +import { Strings } from "@openzeppelin/contracts/utils/Strings.sol"; |
| 14 | + |
| 15 | +contract ERC1155Common is |
| 16 | + ERC1155, |
| 17 | + AccessControlEnumerable, |
| 18 | + ERC1155Pausable, |
| 19 | + ERC1155Burnable, |
| 20 | + ERC1155Supply, |
| 21 | + IERC1155Common |
| 22 | +{ |
| 23 | + using Strings for uint256; |
| 24 | + |
| 25 | + bytes32 public constant URI_SETTER_ROLE = keccak256("URI_SETTER_ROLE"); |
| 26 | + bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE"); |
| 27 | + bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); |
| 28 | + |
| 29 | + string private _name; |
| 30 | + string private _symbol; |
| 31 | + |
| 32 | + constructor(address admin, string memory name_, string memory symbol_, string memory uri_) ERC1155(uri_) { |
| 33 | + _grantRole(DEFAULT_ADMIN_ROLE, admin); |
| 34 | + _grantRole(PAUSER_ROLE, admin); |
| 35 | + _grantRole(MINTER_ROLE, admin); |
| 36 | + _grantRole(URI_SETTER_ROLE, admin); |
| 37 | + |
| 38 | + _name = name_; |
| 39 | + _symbol = symbol_; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @dev Set the URI for all token types. |
| 44 | + * Requirements: |
| 45 | + * - the caller must have the `URI_SETTER_ROLE`. |
| 46 | + */ |
| 47 | + function setURI( |
| 48 | + string memory newURI |
| 49 | + ) external onlyRole(URI_SETTER_ROLE) { |
| 50 | + _setURI(newURI); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @dev Pauses all token transfers. |
| 55 | + * Requirements: |
| 56 | + * - the caller must have the `PAUSER_ROLE`. |
| 57 | + */ |
| 58 | + function pause() external onlyRole(PAUSER_ROLE) { |
| 59 | + _pause(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @dev Unpauses all token transfers. |
| 64 | + * Requirements: |
| 65 | + * - the caller must have the `PAUSER_ROLE`. |
| 66 | + */ |
| 67 | + function unpause() external onlyRole(PAUSER_ROLE) { |
| 68 | + _unpause(); |
| 69 | + } |
| 70 | + |
| 71 | + /// @inheritdoc IERC1155Common |
| 72 | + function mint(address account, uint256 id, uint256 amount, bytes calldata data) public virtual onlyRole(MINTER_ROLE) { |
| 73 | + _mint(account, id, amount, data); |
| 74 | + } |
| 75 | + |
| 76 | + /// @inheritdoc IERC1155Common |
| 77 | + function mintBatch( |
| 78 | + address to, |
| 79 | + uint256[] calldata ids, |
| 80 | + uint256[] calldata amounts, |
| 81 | + bytes calldata data |
| 82 | + ) public virtual onlyRole(MINTER_ROLE) { |
| 83 | + _mintBatch(to, ids, amounts, data); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @dev Mint single token to multiple addresses. |
| 88 | + * Requirements: |
| 89 | + * - the caller must have the `MINTER_ROLE`. |
| 90 | + */ |
| 91 | + function bulkMint( |
| 92 | + uint256 id, |
| 93 | + address[] calldata tos, |
| 94 | + uint256[] calldata amounts, |
| 95 | + bytes[] calldata datas |
| 96 | + ) public virtual onlyRole(MINTER_ROLE) { |
| 97 | + uint256 length = tos.length; |
| 98 | + require(length != 0 && length == amounts.length && length == datas.length, "ERC1155: invalid array lengths"); |
| 99 | + |
| 100 | + for (uint256 i; i < length; ++i) { |
| 101 | + _mint(tos[i], id, amounts[i], datas[i]); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @dev See {ERC1155-uri}. |
| 107 | + */ |
| 108 | + function uri( |
| 109 | + uint256 tokenId |
| 110 | + ) public view virtual override returns (string memory) { |
| 111 | + string memory uri_ = super.uri(tokenId); |
| 112 | + return string.concat(uri_, tokenId.toString()); |
| 113 | + } |
| 114 | + |
| 115 | + /// @inheritdoc IERC1155Common |
| 116 | + function name() public view virtual returns (string memory) { |
| 117 | + return _name; |
| 118 | + } |
| 119 | + |
| 120 | + /// @inheritdoc IERC1155Common |
| 121 | + function symbol() public view virtual returns (string memory) { |
| 122 | + return _symbol; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @dev See {ERC165-supportsInterface}. |
| 127 | + */ |
| 128 | + function supportsInterface( |
| 129 | + bytes4 interfaceId |
| 130 | + ) public view virtual override(IERC165, ERC1155, AccessControlEnumerable) returns (bool) { |
| 131 | + return interfaceId == type(IERC1155Common).interfaceId || super.supportsInterface(interfaceId); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @dev See {ERC1155-_update}. |
| 136 | + */ |
| 137 | + function _update( |
| 138 | + address from, |
| 139 | + address to, |
| 140 | + uint256[] memory ids, |
| 141 | + uint256[] memory values |
| 142 | + ) internal virtual override(ERC1155, ERC1155Supply, ERC1155Pausable) { |
| 143 | + super._update(from, to, ids, values); |
| 144 | + } |
| 145 | +} |
0 commit comments