-
-
Save rascode/9121f6fc0457e11683393bfa5a5fd3b3 to your computer and use it in GitHub Desktop.
A self-contained NFT minting contract
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: UNLICENSED | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | |
import "hardhat/console.sol"; | |
interface IContent { | |
struct NFTDetails { | |
string name; | |
string description; | |
uint256[6] magic; | |
uint256 seed; | |
} | |
} | |
contract MyNFT is ERC721, Ownable, ReentrancyGuard { | |
using Counters for Counters.Counter; | |
Counters.Counter private _tokenIds; | |
mapping(uint256 => IContent.NFTDetails) Content; | |
uint256 price = 5000000000000000; | |
address public renderingContractAddress; | |
event NewItem(address sender, uint256 tokenId, string name); | |
constructor() ERC721("MYNFT", "MYCOLLECTION") {} | |
function GenerateNFT( | |
string calldata name, | |
string calldata description, | |
uint256[6] calldata magic | |
) public payable virtual { | |
require(msg.value >= price, "Not enough ETH sent; check price!"); | |
uint256 newItemId = _tokenIds.current(); | |
if (newItemId >= 10000) { | |
revert("This NFT is sold out."); | |
} | |
IContent.NFTDetails memory Item; | |
Item.name = name; | |
Item.description = description; | |
Item.magic = magic; | |
Item.seed = uint256( | |
keccak256( | |
abi.encodePacked( | |
newItemId, | |
msg.sender, | |
block.difficulty, | |
block.timestamp | |
) | |
) | |
); | |
_safeMint(msg.sender, newItemId); | |
Content[newItemId] = Item; | |
emit NewItem(msg.sender, newItemId, name); | |
_tokenIds.increment(); | |
} | |
function setRenderingContractAddress(address _renderingContractAddress) | |
public | |
onlyOwner | |
{ | |
renderingContractAddress = _renderingContractAddress; | |
} | |
function setPrice(uint256 _price) public onlyOwner { | |
price = _price; | |
} | |
function totalContent() public view virtual returns (uint256) { | |
return _tokenIds.current(); | |
} | |
function tokenURI(uint256 _tokenId) | |
public | |
view | |
virtual | |
override | |
returns (string memory) | |
{ | |
require( | |
_exists(_tokenId), | |
"ERC721Metadata: URI query for nonexistent token" | |
); | |
IContent.NFTDetails memory item = Content[_tokenId]; | |
return string(abi.encodePacked("data:application/json;base64,", finalData(item.name, item.description))); | |
} | |
function finalData( | |
string memory name, | |
string memory description | |
) public pure returns (bytes memory) { | |
// check opensea for all the details you can return | |
// https://docs.opensea.io/docs/metadata-standards | |
// { | |
// "description": "Friendly OpenSea Creature that enjoys long swims in the ocean.", | |
// "external_url": "https://openseacreatures.io/3", | |
// "image": "https://storage.googleapis.com/opensea-prod.appspot.com/puffs/3.png", | |
// "name": "Dave Starbelly", | |
// "attributes": [ ... ] | |
// } | |
return | |
bytes( | |
abi.encodePacked( | |
'{"name":"', name, '",' | |
'"description":"', description, '"}' | |
) | |
); | |
} | |
function withdraw() public onlyOwner nonReentrant { | |
(bool success, ) = msg.sender.call{value: address(this).balance}(""); | |
require(success, "Withdrawal failed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment