Created
October 31, 2021 18:13
-
-
Save rounakbanik/a0429cf3227abc4bde8c18ac71fff76d to your computer and use it in GitHub Desktop.
NFT Collectible Smart Contract
This file contains 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: MIT | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/utils/math/SafeMath.sol"; | |
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol"; | |
contract NFTCollectible is ERC721Enumerable, Ownable { | |
using SafeMath for uint256; | |
using Counters for Counters.Counter; | |
Counters.Counter private _tokenIds; | |
uint public constant MAX_SUPPLY = 100; | |
uint public constant PRICE = 0.01 ether; | |
uint public constant MAX_PER_MINT = 5; | |
string public baseTokenURI; | |
constructor(string memory baseURI) ERC721("NFT Collectible", "NFTC") { | |
setBaseURI(baseURI); | |
} | |
function reserveNFTs() public onlyOwner { | |
uint totalMinted = _tokenIds.current(); | |
require(totalMinted.add(10) < MAX_SUPPLY, "Not enough NFTs left to reserve"); | |
for (uint i = 0; i < 10; i++) { | |
_mintSingleNFT(); | |
} | |
} | |
function _baseURI() internal view virtual override returns (string memory) { | |
return baseTokenURI; | |
} | |
function setBaseURI(string memory _baseTokenURI) public onlyOwner { | |
baseTokenURI = _baseTokenURI; | |
} | |
function mintNFTs(uint _count) public payable { | |
uint totalMinted = _tokenIds.current(); | |
require(totalMinted.add(_count) <= MAX_SUPPLY, "Not enough NFTs left!"); | |
require(_count >0 && _count <= MAX_PER_MINT, "Cannot mint specified number of NFTs."); | |
require(msg.value >= PRICE.mul(_count), "Not enough ether to purchase NFTs."); | |
for (uint i = 0; i < _count; i++) { | |
_mintSingleNFT(); | |
} | |
} | |
function _mintSingleNFT() private { | |
uint newTokenID = _tokenIds.current(); | |
_safeMint(msg.sender, newTokenID); | |
_tokenIds.increment(); | |
} | |
function tokensOfOwner(address _owner) external view returns (uint[] memory) { | |
uint tokenCount = balanceOf(_owner); | |
uint[] memory tokensId = new uint256[](tokenCount); | |
for (uint i = 0; i < tokenCount; i++) { | |
tokensId[i] = tokenOfOwnerByIndex(_owner, i); | |
} | |
return tokensId; | |
} | |
function withdraw() public payable onlyOwner { | |
uint balance = address(this).balance; | |
require(balance > 0, "No ether left to withdraw"); | |
(bool success, ) = (msg.sender).call{value: balance}(""); | |
require(success, "Transfer failed."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment