Skip to content

Instantly share code, notes, and snippets.

@qz267
Forked from mwmwmw/MyNFT Minter.sol
Created July 28, 2022 18:07
Show Gist options
  • Select an option

  • Save qz267/077216718ff8f5bdf8db4c25a2f9da2f to your computer and use it in GitHub Desktop.

Select an option

Save qz267/077216718ff8f5bdf8db4c25a2f9da2f to your computer and use it in GitHub Desktop.

Revisions

  1. @mwmwmw mwmwmw revised this gist Feb 4, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MyNFT Minter.sol
    Original file line number Diff line number Diff line change
    @@ -96,7 +96,7 @@ contract MyNFT is ERC721, Ownable, ReentrancyGuard {
    return renderer.tokenURI(_tokenId, Content[_tokenId]);
    }

    function withdraw() public onlyOwner {
    function withdraw() public onlyOwner nonReentrant {
    (bool success, ) = msg.sender.call{value: address(this).balance}("");
    require(success, "Withdrawal failed");
    }
  2. @mwmwmw mwmwmw created this gist Feb 1, 2022.
    104 changes: 104 additions & 0 deletions MyNFT Minter.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    // 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 "./IContent.sol";

    import "hardhat/console.sol";

    contract MyNFT is ERC721, Ownable, ReentrancyGuard {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
    mapping(uint256 => IContent.Item) Content;
    uint256 price = 5000000000000000;

    address public renderingContractAddress;

    event NewItem(address sender, uint256 tokenId, string name);

    constructor() ERC721("MYNFT", "MYCOLLECTION") {

    function GenerateNFT(
    string calldata ItemName,
    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.Item memory Item;

    Item.name = ItemName;
    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, ItemName);

    _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"
    );

    if (renderingContractAddress == address(0)) {
    return "";
    }

    IItemRenderer renderer = IItemRenderer(renderingContractAddress);
    return renderer.tokenURI(_tokenId, Content[_tokenId]);
    }

    function withdraw() public onlyOwner {
    (bool success, ) = msg.sender.call{value: address(this).balance}("");
    require(success, "Withdrawal failed");
    }

    }