Skip to content

Instantly share code, notes, and snippets.

@oliverjumpertz
Last active June 3, 2021 09:22
Show Gist options
  • Save oliverjumpertz/d4de07b2010b456784a50b9ee23d1312 to your computer and use it in GitHub Desktop.
Save oliverjumpertz/d4de07b2010b456784a50b9ee23d1312 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