|
| 1 | +//SPDX-License-Identifier: Unlicense |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import {ERC721URIStorage} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; |
| 5 | +import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol"; |
| 6 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
| 8 | +import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol"; |
| 9 | +import {Strings} from "@openzeppelin/contracts/utils/Strings.sol"; |
| 10 | + |
| 11 | +/// @title DemoNativeNFT |
| 12 | +/// @notice |
| 13 | +/// @dev |
| 14 | +contract DemoNativeNFT is Ownable, AccessControl, ERC721URIStorage { |
| 15 | + using Strings for uint256; |
| 16 | + |
| 17 | + uint256 public tokenIdCounter; |
| 18 | + string public baseTokenURI; |
| 19 | + uint256 public mintPrice; |
| 20 | + |
| 21 | + event PaymentTokenSet(address indexed paymentToken); |
| 22 | + event MintPriceSet(uint256 indexed mintPrice); |
| 23 | + event BaseURISet(string indexed baseURI); |
| 24 | + |
| 25 | + constructor(address _owner, string memory _baseTokenURI, uint256 _mintPrice) |
| 26 | + Ownable(_owner) |
| 27 | + ERC721("DemoNFT", "DMON") |
| 28 | + { |
| 29 | + tokenIdCounter = 0; |
| 30 | + baseTokenURI = _baseTokenURI; |
| 31 | + mintPrice = _mintPrice; |
| 32 | + } |
| 33 | + |
| 34 | + //////////////////////////////////////////////////////////////////////// |
| 35 | + /// Internal functions |
| 36 | + //////////////////////////////////////////////////////////////////////// |
| 37 | + |
| 38 | + function _baseURI() internal view override returns (string memory) { |
| 39 | + return baseTokenURI; |
| 40 | + } |
| 41 | + |
| 42 | + //////////////////////////////////////////////////////////////////////// |
| 43 | + /// Owner actions |
| 44 | + //////////////////////////////////////////////////////////////////////// |
| 45 | + |
| 46 | + function _transferOwnership(address newOwner) internal virtual override { |
| 47 | + super._transferOwnership(newOwner); |
| 48 | + _grantRole(DEFAULT_ADMIN_ROLE, newOwner); |
| 49 | + } |
| 50 | + |
| 51 | + /// @notice set a new baseTokenURI |
| 52 | + /// @param _baseTokenURI the new base token URI |
| 53 | + function setBaseURI(string memory _baseTokenURI) public onlyOwner { |
| 54 | + baseTokenURI = _baseTokenURI; |
| 55 | + emit BaseURISet(_baseTokenURI); |
| 56 | + } |
| 57 | + |
| 58 | + function setMintPrice(uint256 _mintPrice) public onlyOwner { |
| 59 | + mintPrice = _mintPrice; |
| 60 | + emit MintPriceSet(_mintPrice); |
| 61 | + } |
| 62 | + |
| 63 | + //////////////////////////////////////////////////////////////////////// |
| 64 | + /// Minter action |
| 65 | + //////////////////////////////////////////////////////////////////////// |
| 66 | + |
| 67 | + /// @notice mint a NFT |
| 68 | + |
| 69 | + function mint(string memory _tokenURI) public payable returns (uint256) { |
| 70 | + require(msg.value == mintPrice, "Insufficient ETH sent"); |
| 71 | + |
| 72 | + unchecked { |
| 73 | + ++tokenIdCounter; |
| 74 | + } |
| 75 | + uint256 newItemId = tokenIdCounter; |
| 76 | + _safeMint(msg.sender, newItemId); |
| 77 | + _setTokenURI(newItemId, _tokenURI); |
| 78 | + return newItemId; |
| 79 | + } |
| 80 | + |
| 81 | + //////////////////////////////////////////////////////////////////////// |
| 82 | + /// Interface functions |
| 83 | + //////////////////////////////////////////////////////////////////////// |
| 84 | + |
| 85 | + function supportsInterface(bytes4 interfaceId) |
| 86 | + public |
| 87 | + view |
| 88 | + override(ERC721URIStorage, AccessControl) |
| 89 | + returns (bool) |
| 90 | + { |
| 91 | + return ERC721URIStorage.supportsInterface(interfaceId) || AccessControl.supportsInterface(interfaceId); |
| 92 | + } |
| 93 | +} |
0 commit comments