-
Notifications
You must be signed in to change notification settings - Fork 5
/
CustomNft.sol
171 lines (145 loc) · 5.18 KB
/
CustomNft.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
/// @title Custom NFT Contract (ERC721 compliant)
/// @author Manav Vagdoda (vagdonic.github.io)
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
contract CustomNft is ERC721, Ownable, ERC721URIStorage, ERC721Burnable{
using Counters for Counters.Counter;
Counters.Counter public _tokenIdCounter;
address Owner;
constructor() ERC721("Evolving Pandas Token", "EPT") {
Owner = msg.sender;
}
// function _baseURI() internal pure override returns (string memory) {
// return "https://api.mnft.com/tokens/";
// }
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);
}
mapping(address => bool) public whitelist;
uint256 public openingTime;
uint256 public closingTime;
uint public mintRate = 0.001 ether;
string public myTokenURI = "ipfs://QmSb9KiZKkST5p4cm1bSNJKbD2EtEvv9NmArULNqjoxwpm/";
string public commonEndpoint = "commonURI";
string public rareEndpoint = "rareURI";
string public baseExtension = ".json";
enum Stage {locked, presale, publicsale}
function uint2strk(uint256 _i) internal pure returns (string memory str) {
if (_i == 0)
{
return "0";
}
uint256 j = _i;
uint256 length;
while (j != 0)
{
length++;
j /= 10;
}
bytes memory bstr = new bytes(length);
uint256 k = length;
j = _i;
while (j != 0)
{
bstr[--k] = bytes1(uint8(48 + j % 10));
j /= 10;
}
str = string(bstr);
}
function addToWhitelist(address _beneficiary) public onlyOwner {
whitelist[_beneficiary] = true;
}
function TimedCrowdsale(uint256 _openingTime, uint256 _closingTime) public onlyOwner{
require(_openingTime >= block.timestamp);
require(_closingTime >= _openingTime);
openingTime = _openingTime;
closingTime = _closingTime;
}
function addManyToWhitelist(address[] memory _beneficiaries) public onlyOwner {
for (uint256 i = 0; i < _beneficiaries.length; i++) {
whitelist[_beneficiaries[i]] = true;
}
}
function checkStage() public view returns (Stage stage){
if(block.timestamp < openingTime) {
stage = Stage.locked;
return stage;
}
else if(block.timestamp >= openingTime && block.timestamp <= closingTime) {
stage = Stage.presale;
return stage;
}
else if(block.timestamp >= closingTime) {
stage = Stage.publicsale;
return stage;
}
}
modifier isPresale {
require(checkStage() == Stage.presale);
_;
}
function iswhitelisted(address xyz) public view isPresale returns (bool) {
if(whitelist[xyz]) return true;
else return false;
}
modifier buffer(address abc) {
require(openingTime != 0);
require(checkStage() != Stage.locked);
require((checkStage() == Stage.publicsale || iswhitelisted(abc)));
_;
}
modifier checkAmount(uint _mintAmt) {
require(_mintAmt < 8 && _mintAmt > 0);
_;
}
function random() public view returns (uint256) {
return uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty)))%10;
}
function safeMint(address to, uint _amtOfTokensToMint) public payable checkAmount(_amtOfTokensToMint) buffer(to){
require(msg.value == (_amtOfTokensToMint * mintRate));
for (uint i = 0; i < _amtOfTokensToMint; i++) {
_tokenIdCounter.increment();
uint256 tokenId = _tokenIdCounter.current();
_safeMint(to, tokenId);
string memory finalURI = uint2strk(tokenId);
if(random() == 5) {
finalURI = string(abi.encodePacked(rareEndpoint, finalURI, baseExtension));
}
else {
finalURI = string(abi.encodePacked(commonEndpoint, finalURI, baseExtension));
}
_setTokenURI(tokenId, finalURI);
}
}
function reserveMint(address _owner, uint _reserveAmt) public onlyOwner{
for (uint i = 0; i < _reserveAmt; i++) {
_tokenIdCounter.increment();
uint256 tokenId = _tokenIdCounter.current();
_safeMint(_owner, tokenId);
string memory finalURI = uint2strk(tokenId);
finalURI = string(abi.encodePacked(commonEndpoint, finalURI, baseExtension));
_setTokenURI(tokenId, finalURI);
}
}
function giveAway(address winner, uint256 _tokenIdToGiveaway) public onlyOwner {
safeTransferFrom(msg.sender, winner, _tokenIdToGiveaway);
}
function getMintedTknsAmt() public view returns(uint256) {
uint256 currentItem = _tokenIdCounter.current();
return currentItem;
}
}