Skip to content

Instantly share code, notes, and snippets.

@nully0x
Forked from oliverjumpertz/MyNFT.sol
Created June 3, 2021 09:22
Show Gist options
  • Save nully0x/97786ded81326ec696bbb5543f2c67f0 to your computer and use it in GitHub Desktop.
Save nully0x/97786ded81326ec696bbb5543f2c67f0 to your computer and use it in GitHub Desktop.
A basic ERC721 (NFT) implementation. Instantly runnable in remix.ethereum.org
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
import "https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-contracts/master/contracts/token/ERC721/ERC721.sol";
import "https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-contracts/master/contracts/utils/Counters.sol";
import "https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-contracts/master/contracts/access/AccessControl.sol";
import "https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-contracts/master/contracts/utils/Context.sol";
contract MyNFT is Context, AccessControl, ERC721 {
using Counters for Counters.Counter;
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
Counters.Counter private _tokenIdTracker;
mapping (uint256 => uint256) private _tokenData;
constructor() public ERC721("My NFT", "NFT") {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
}
function mint(address to, uint256 tokenData) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "MyNFT: must have minter role to mint");
_mint(to, _tokenIdTracker.current());
_tokenData[_tokenIdTracker.current()] = tokenData;
_tokenIdTracker.increment();
}
function tokenData(uint256 tokenId) public view returns (uint256) {
require(_exists(tokenId), "MyNFT: URI query for nonexistent token");
return _tokenData[tokenId];
}
function supportsInterface(bytes4 interfaceId) public view override(ERC721, AccessControl) returns (bool) {
return super.supportsInterface(interfaceId);
}
function _baseURI() internal view override returns (string memory) {
return "https://example.com/api/token/";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment