Skip to content

Instantly share code, notes, and snippets.

@superswan
Created August 10, 2022 00:27
Show Gist options
  • Save superswan/5b5b23823b265986abe91088032ae30e to your computer and use it in GitHub Desktop.
Save superswan/5b5b23823b265986abe91088032ae30e to your computer and use it in GitHub Desktop.
NFT Contract Template - Pay to Mint
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract TREEMINTTEST is ERC721, ERC721URIStorage, ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;
// CUSTOM PARAMETERS
uint256 MAX_SUPPLY = 9999; // Set max supply
mapping(string => uint8) existingURIs; // Mappping to ensure unique URI
constructor() ERC721("NFT Contract", "NFT") {}
function isOwned(string memory uri) public view returns (bool) {
return existingURIs[uri] == 1;
}
function _baseURI() internal pure override returns (string memory) {
return "ipfs://";
}
// Pay to mint function
function payToMint(
address recipient,
string memory metadataURI
) public payable returns (uint256) {
require(existingURIs[metadataURI] != 1, "This token has already been minted");
require (msg.value >= 0.0555 ether, "This contract requires a payment to mint");
uint256 newToken = _tokenIdCounter.current();
_tokenIdCounter.increment();
existingURIs[metadataURI] = 1;
_mint(recipient, newItemId);
_setTokenURI(newItemId, metadataURI);
return newToken;
}
function safeMint(address to, string memory uri) public onlyOwner {
uint256 tokenId = _tokenIdCounter.current();
require(tokenId <= MAX_SUPPLY, "Minted Out");
_tokenIdCounter.increment();
_safeMint(to, tokenId);
_setTokenURI(tokenId, uri);
}
// The following functions are overrides required by Solidity.
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
super._burn(tokenId);
}
function tokenURI(uint256 tokenId)
public
view
override(ERC721, ERC721URIStorage)
returns (string memory)
{
return super.tokenURI(tokenId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment