Skip to content

Instantly share code, notes, and snippets.

@lukebyrne
Last active November 8, 2021 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukebyrne/8a54c7028f2c71748e144a0aa102b955 to your computer and use it in GitHub Desktop.
Save lukebyrne/8a54c7028f2c71748e144a0aa102b955 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
// * Deploy version of contract to to Polygon, make sure you can pay and withdraw in MATIC
pragma solidity ^0.8.7;
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/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract PolygonTest is ERC721, ERC721Enumerable, ERC721URIStorage, ERC721Burnable, Ownable {
using Counters for Counters.Counter;
address public contractOwner;
Counters.Counter private _tokenIdCounter;
string baseURI;
string contractURI_;
uint public constant MAX_SUPPLY = 20000;
uint public piecesMinted;
bool public mintActive = false;
uint public mintPrice;
uint public mintAmount;
bool public baseURILocked = false;
constructor() ERC721("PolygonTest", "pTEST") {
contractOwner = msg.sender;
piecesMinted = 0;
mintPrice = 0.04 ether;
mintAmount = 8;
baseURI = "https://example.com/";
contractURI_ = "https://example.com/contract.json";
}
modifier contractOwnerOnly {
require(contractOwner == msg.sender, "ERC721: You are not the contract owner");
_;
}
/**
* ERC721 START
*/
function setMintActive(bool _mintActive) public onlyOwner {
mintActive = _mintActive;
}
function setMintPrice(uint _mintPrice) public onlyOwner {
mintPrice = _mintPrice;
}
function setMintAmount(uint _mintAmount) public onlyOwner {
mintAmount = _mintAmount;
}
function contractURI() public view returns (string memory) {
return contractURI_;
}
function setContractURI(string memory _contractURI) public onlyOwner {
contractURI_ = _contractURI;
}
function setBaseURILocked(bool _baseURILocked) public onlyOwner {
baseURILocked = _baseURILocked;
}
function setBaseURI(string memory baseURI_) public onlyOwner {
require(baseURILocked == false, "ERC721: baseURILocked is locked");
baseURI = baseURI_;
}
function _baseURI() internal view override returns (string memory) {
return baseURI;
}
function getBaseURI() public view returns (string memory) {
return baseURI;
}
function safeMint() public payable {
require(mintActive == true, "ERC721: Mint is not active");
require(msg.value >= mintPrice, "ERC721: Ether sent is not correct");
_safeMint(msg.sender);
}
function withdraw() public payable onlyOwner {
payable(msg.sender).transfer(msg.value);
}
function safeMintAdmin(address to) public onlyOwner {
_safeMint(to);
}
function setContractOwner(address _contractOwner) public onlyOwner {
contractOwner = _contractOwner;
}
function _safeMint(address to) private {
// Mint piecesMinted per time
for (uint i; i < mintAmount; i++) {
uint256 counter = _tokenIdCounter.current();
require(piecesMinted <= MAX_SUPPLY, "ERC721: Exceeds maximum supply");
_safeMint(to, counter);
_setTokenURI(counter, string(abi.encodePacked(Strings.toString(counter) , ".json")));
_tokenIdCounter.increment();
}
uint256 addition = mintAmount;
piecesMinted = piecesMinted + addition;
}
function _approvedForToken(uint256 tokenId) private {
require(
_isApprovedOrOwner(_msgSender(), tokenId),
"ERC721: transfer caller is not owner nor approved"
);
}
function safeBurn(uint256 tokenId) public {
_approvedForToken(tokenId);
_burn(tokenId);
}
function contractOwnerSafeBurn(uint256 tokenId) public contractOwnerOnly {
_burn(tokenId);
}
// The following functions are overrides required by Solidity.
function _beforeTokenTransfer(address from, address to, uint256 tokenId)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId);
}
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
super._burn(tokenId);
}
function setTokenURI(uint256 _tokenId, string memory _uri) public onlyOwner {
_setTokenURI(_tokenId, _uri);
}
function tokenURI(uint256 tokenId)
public
view
override(ERC721, ERC721URIStorage)
returns (string memory)
{
return super.tokenURI(tokenId);
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
/**
* ERC721 END
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment