-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNFTwPreSale.sol
More file actions
166 lines (138 loc) · 4.55 KB
/
NFTwPreSale.sol
File metadata and controls
166 lines (138 loc) · 4.55 KB
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
// SPDX-License-Identifier: MIT
// Created by @0mdur - Khalid Hamid
// Learned from @Hashlipsnft - Hashlips
// gas optimization Inspired by @0xFloop
pragma solidity >=0.7.0 <0.9.0;
//import "ERC721.sol";
//import "Ownable.sol";
//import "SafeMath.sol";
//import "Counters.sol";
contract NFT is ERC721, Ownable {
using SafeMath for uint256;
using Strings for uint256;
using Counters for Counters.Counter;
Counters.Counter private tokenSupply;
string public baseURI;
string public baseExtension = ".json";
string public notRevealedUri;
uint256 public cost = 0.05 ether;
uint256 public maxSupply = 10000;
uint256 public maxMintAmount = 10;
uint256 public mintLimit = 3;
bool public paused = false;
bool public revealed = false;
bool public presale = true;
address[] public whitelist;
constructor(
string memory _name,
string memory _symbol,
string memory _initBaseURI,
string memory _initNotRevealedUri
) ERC721(_name, _symbol) {
setBaseURI(_initBaseURI);
setNotRevealedURI(_initNotRevealedUri);
}
// internal
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}
// public
function mint(uint256 _amount) public payable {
require(!paused);
require(_amount > 0);
require(_amount <= maxMintAmount);
require(tokenSupply.current().add(_amount) <= maxSupply);
if (msg.sender != owner()) {
if (presale == true) {
require(isWhiteListed(msg.sender), "user is not whitelisted");
uint256 tokenCount = balanceOf(msg.sender);
require(tokenCount < mintLimit);
}
}
for (uint256 i = 1; i <= _amount; i++) {
//ERC721Enumerable totalSupply()
//_safeMint(msg.sender, supply + i);
//using counters.sol to reduce gas
tokenSupply.increment();
uint256 newId = tokenSupply.current();
_safeMint(msg.sender, newId);
}
}
function isWhiteListed(address _user) public view returns (bool) {
for (uint256 i = 0; i < whitelist.length; i++) {
if (whitelist[i] == _user) {
return true;
}
}
return false;
}
function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
require(
_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
if (revealed == false) {
return notRevealedUri;
}
string memory currentBaseURI = _baseURI();
return
bytes(currentBaseURI).length > 0
? string(
abi.encodePacked(
currentBaseURI,
tokenId.toString(),
baseExtension
)
)
: "";
}
//only owner
function reveal() public onlyOwner {
revealed = true;
}
function setCost(uint256 _newCost) public onlyOwner {
cost = _newCost;
}
function setLimit(uint256 _newLimit) public onlyOwner {
mintLimit = _newLimit;
}
function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
maxMintAmount = _newmaxMintAmount;
}
function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
notRevealedUri = _notRevealedURI;
}
function setBaseURI(string memory _newBaseURI) public onlyOwner {
baseURI = _newBaseURI;
}
function setBaseExtension(string memory _newBaseExtension)
public
onlyOwner
{
baseExtension = _newBaseExtension;
}
function pause(bool _state) public onlyOwner {
paused = _state;
}
function setOnlyPresale(bool _state) public onlyOwner {
presale = _state;
}
function addToWhiteList(address[] calldata _users) public onlyOwner {
delete whitelist;
whitelist = _users;
}
function withdraw() public payable onlyOwner {
// This will payout the owner the balance in the contract.
// Do not remove this otherwise you will not be able to withdraw the funds.
// =============================================================================
(bool os, ) = payable(owner()).call{value: address(this).balance}("");
require(os);
// =============================================================================
}
}