Skip to content

Instantly share code, notes, and snippets.

@mwmwmw
Created January 27, 2023 17:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mwmwmw/eb5142d1819111a7ccf8a33d0fc93775 to your computer and use it in GitHub Desktop.
Save mwmwmw/eb5142d1819111a7ccf8a33d0fc93775 to your computer and use it in GitHub Desktop.
A self-contained NFT minting contract
// 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