Created
March 24, 2022 09:55
-
-
Save manelephant/10982d943c1d47b8a87152acd540006c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/utils/math/SafeMath.sol"; | |
import "@openzeppelin/contracts/utils/math/SafeMath.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; | |
import "@openzeppelin/contracts/access/AccessControl.sol"; | |
import "@openzeppelin/contracts/security/Pausable.sol"; | |
import "./Ixor1155.sol"; | |
contract GalleryERC1155 is Ixor1155, ERC1155, Ownable, AccessControl, Pausable { | |
using SafeMath for uint256; | |
string public name; | |
string public symbol; | |
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); | |
string private _baseMetadataURI; | |
uint256 private _currentTokenID = 0; | |
mapping(uint256 => string) private _uris; | |
mapping(uint256 => uint256) public tokenSupply; | |
constructor(string memory _name, string memory _symbol, string memory _uri) ERC1155(_uri) { | |
require(bytes(_name).length > 0, "Name is mandatory"); | |
require(bytes(_symbol).length > 0, "Symbol is mandatory"); | |
require(bytes(_uri).length > 0, "BaseMetaDataUri is mandatory"); | |
name = _name; | |
symbol = _symbol; | |
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender); | |
setBaseMetadataURI(_uri); | |
} | |
function setMinterRole(address newMinter) external onlyOwner { | |
_setupRole(MINTER_ROLE, newMinter); | |
} | |
function setBaseMetadataURI(string memory _newBaseMetadataURI) public onlyOwner { | |
_baseMetadataURI = _newBaseMetadataURI; | |
} | |
function uri(uint256 tokenId) override public view returns (string memory) { | |
if (bytes(_uris[tokenId]).length > 0) { | |
return (_uris[tokenId]); | |
} | |
return string(abi.encodePacked( | |
_baseMetadataURI, | |
Strings.toString(tokenId), | |
".json" | |
)); | |
} | |
function setTokenUri(uint256 tokenId, string calldata tokenUri) external onlyOwner { | |
require(bytes(tokenUri).length > 0, "uri cant be empty"); | |
require(bytes(_uris[tokenId]).length == 0, "Cant set uri twice"); | |
_uris[tokenId] = tokenUri; | |
emit URI(tokenUri, tokenId); | |
} | |
function mint(address to, uint256 id, uint256 quantity) public whenNotPaused { | |
require(hasRole(MINTER_ROLE, msg.sender) || hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "Caller is not a minter"); | |
require(quantity != 0, "_number need to be > 0 "); | |
require(id == 0 || (exists(id) || (id == getNextTokenID())), "Token id must exists or be the next available token ID"); | |
_mint(to, id, quantity, ""); | |
/** If this token id does not exists then its a new token */ | |
if (exists(id) == false && id != 0) { | |
_incrementTokenTypeId(); | |
} | |
tokenSupply[id] = tokenSupply[id].add(quantity); | |
} | |
function isXorContract() external pure returns (bool) { | |
return true; | |
} | |
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC1155, AccessControl) returns (bool) { | |
return super.supportsInterface(interfaceId); | |
} | |
function _beforeTokenTransfer( | |
address operator, | |
address from, | |
address to, | |
uint256[] memory ids, | |
uint256[] memory amounts, | |
bytes memory data | |
) internal whenNotPaused override { | |
super._beforeTokenTransfer(operator, from, to, ids, amounts, data); | |
} | |
function pause() public onlyOwner { | |
_pause(); | |
} | |
function unpause() public onlyOwner { | |
_unpause(); | |
} | |
function contractURI() external view returns (string memory) { | |
return string(abi.encodePacked( | |
_baseMetadataURI, | |
"contract.json" | |
)); | |
} | |
/** | |
* @dev Returns the total quantity for a token ID | |
* @param _id uint256 ID of the token to query | |
* @return true or false if the token exists or not | |
*/ | |
function exists(uint256 _id) public view returns (bool) { | |
return tokenSupply[_id] > 0; | |
} | |
/** | |
* @dev calculates the next token ID based on value of _currentTokenID | |
* @return uint256 for the next token ID | |
*/ | |
function getNextTokenID() public view returns (uint256) { | |
return _currentTokenID.add(1); | |
} | |
/** | |
* @dev increments the value of _currentTokenID | |
*/ | |
function _incrementTokenTypeId() private { | |
_currentTokenID++; | |
} | |
function batchMint(address[] calldata accounts, uint256[] calldata ids, uint256[] calldata quantities) external whenNotPaused { | |
require(hasRole(MINTER_ROLE, msg.sender) || hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "Caller is not a minter"); | |
require(accounts.length < 500, "Cannot have more than 500 addresses"); | |
require(accounts.length == ids.length, "Accounts and ids length mismatch"); | |
require(accounts.length == quantities.length, "Accounts and quantities length mismatch"); | |
for (uint256 i = 0; i < ids.length; i++) { | |
mint(accounts[i], ids[i], quantities[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment