From ac1371adabab75574b75ea030d282e4be1eeb0c2 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Wed, 4 Dec 2024 16:40:03 +0700 Subject: [PATCH] Create NFTContract.sol --- .../smart_contracts/NFTContract.sol | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 coin/QuantumCoin/smart_contracts/NFTContract.sol diff --git a/coin/QuantumCoin/smart_contracts/NFTContract.sol b/coin/QuantumCoin/smart_contracts/NFTContract.sol new file mode 100644 index 000000000..0de2992c4 --- /dev/null +++ b/coin/QuantumCoin/smart_contracts/NFTContract.sol @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; +import "@openzeppelin/contracts/access/Ownable.sol"; +import "@openzeppelin/contracts/utils/Counters.sol"; + +contract NFTContract is ERC721, Ownable { + using Counters for Counters.Counter; + Counters.Counter private _tokenIdCounter; + + // Mapping from token ID to token metadata URI + mapping(uint256 => string) private _tokenURIs; + + // Event emitted when a new NFT is minted + event NFTMinted(address indexed owner, uint256 indexed tokenId, string tokenURI); + + constructor() ERC721("QuantumNFT", "QNFT") {} + + // Mint a new NFT + function mintNFT(address to, string memory tokenURI) external onlyOwner { + uint256 tokenId = _tokenIdCounter.current(); + _mint(to, tokenId); + _setTokenURI(tokenId, tokenURI); + _tokenIdCounter.increment(); + + emit NFTMinted(to, tokenId, tokenURI); + } + + // Set the token URI for a specific token ID + function _setTokenURI(uint256 tokenId, string memory tokenURI) internal { + require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token"); + _tokenURIs[tokenId] = tokenURI; + } + + // Override the base URI function + function tokenURI(uint256 tokenId) public view override returns (string memory) { + require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); + return _tokenURIs[tokenId]; + } + + // Burn an NFT + function burn(uint256 tokenId) external { + require(ownerOf(tokenId) == msg.sender, "You are not the owner of this token"); + _burn(tokenId); + delete _tokenURIs[tokenId]; + } +}