Skip to content

Commit 70c3c7f

Browse files
authored
Merge pull request #16 from liarco/lower-gas-fees
Change implementation to enable lower gas fees
2 parents 975d6d6 + 7c2922c commit 70c3c7f

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

contract/SimpleNftLowerGas.sol

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
// Amended by HashLips
4+
/**
5+
!Disclaimer!
6+
7+
These contracts have been used to create tutorials,
8+
and was created for the purpose to teach people
9+
how to create smart contracts on the blockchain.
10+
please review this code on your own before using any of
11+
the following code for production.
12+
The developer will not be responsible or liable for all loss or
13+
damage whatsoever caused by you participating in any way in the
14+
experimental code, whether putting money into the contract or
15+
using the code for your own project.
16+
*/
17+
18+
pragma solidity >=0.7.0 <0.9.0;
19+
20+
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
21+
import "@openzeppelin/contracts/utils/Counters.sol";
22+
import "@openzeppelin/contracts/access/Ownable.sol";
23+
24+
contract SimpleNftLowerGas is ERC721, Ownable {
25+
using Strings for uint256;
26+
using Counters for Counters.Counter;
27+
28+
Counters.Counter private supply;
29+
30+
string public uriPrefix = "";
31+
string public uriSuffix = ".json";
32+
string public hiddenMetadataUri;
33+
34+
uint256 public cost = 0.01 ether;
35+
uint256 public maxSupply = 10000;
36+
uint256 public maxMintAmountPerTx = 5;
37+
38+
bool public paused = true;
39+
bool public revealed = false;
40+
41+
constructor() ERC721("NAME", "SYMBOL") {
42+
setHiddenMetadataUri("ipfs://__CID__/hidden.json");
43+
}
44+
45+
modifier mintCompliance(uint256 _mintAmount) {
46+
require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
47+
require(supply.current() + _mintAmount <= maxSupply, "Max supply exceeded!");
48+
_;
49+
}
50+
51+
function totalSupply() public view returns (uint256) {
52+
return supply.current();
53+
}
54+
55+
function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
56+
require(!paused, "The contract is paused!");
57+
require(msg.value >= cost * _mintAmount, "Insufficient funds!");
58+
59+
_mintLoop(msg.sender, _mintAmount);
60+
}
61+
62+
function mintForAddress(uint256 _mintAmount, address _receiver) public mintCompliance(_mintAmount) onlyOwner {
63+
_mintLoop(_receiver, _mintAmount);
64+
}
65+
66+
function walletOfOwner(address _owner)
67+
public
68+
view
69+
returns (uint256[] memory)
70+
{
71+
uint256 ownerTokenCount = balanceOf(_owner);
72+
uint256[] memory ownedTokenIds = new uint256[](ownerTokenCount);
73+
uint256 currentTokenId = 1;
74+
uint256 ownedTokenIndex = 0;
75+
76+
while (ownedTokenIndex < ownerTokenCount && currentTokenId <= maxSupply) {
77+
address currentTokenOwner = ownerOf(currentTokenId);
78+
79+
if (currentTokenOwner == _owner) {
80+
ownedTokenIds[ownedTokenIndex] = currentTokenId;
81+
82+
ownedTokenIndex++;
83+
}
84+
85+
currentTokenId++;
86+
}
87+
88+
return ownedTokenIds;
89+
}
90+
91+
function tokenURI(uint256 _tokenId)
92+
public
93+
view
94+
virtual
95+
override
96+
returns (string memory)
97+
{
98+
require(
99+
_exists(_tokenId),
100+
"ERC721Metadata: URI query for nonexistent token"
101+
);
102+
103+
if (revealed == false) {
104+
return hiddenMetadataUri;
105+
}
106+
107+
string memory currentBaseURI = _baseURI();
108+
return bytes(currentBaseURI).length > 0
109+
? string(abi.encodePacked(currentBaseURI, _tokenId.toString(), uriSuffix))
110+
: "";
111+
}
112+
113+
function setRevealed(bool _state) public onlyOwner {
114+
revealed = _state;
115+
}
116+
117+
function setCost(uint256 _cost) public onlyOwner {
118+
cost = _cost;
119+
}
120+
121+
function setMaxMintAmountPerTx(uint256 _maxMintAmountPerTx) public onlyOwner {
122+
maxMintAmountPerTx = _maxMintAmountPerTx;
123+
}
124+
125+
function setHiddenMetadataUri(string memory _hiddenMetadataUri) public onlyOwner {
126+
hiddenMetadataUri = _hiddenMetadataUri;
127+
}
128+
129+
function setUriPrefix(string memory _uriPrefix) public onlyOwner {
130+
uriPrefix = _uriPrefix;
131+
}
132+
133+
function setUriSuffix(string memory _uriSuffix) public onlyOwner {
134+
uriSuffix = _uriSuffix;
135+
}
136+
137+
function setPaused(bool _state) public onlyOwner {
138+
paused = _state;
139+
}
140+
141+
function withdraw() public onlyOwner {
142+
// This will pay HashLips 5% of the initial sale.
143+
// You can remove this if you want, or keep it in to support HashLips and his channel.
144+
// =============================================================================
145+
(bool hs, ) = payable(0x943590A42C27D08e3744202c4Ae5eD55c2dE240D).call{value: address(this).balance * 5 / 100}("");
146+
require(hs);
147+
// =============================================================================
148+
149+
// This will transfer the remaining contract balance to the owner.
150+
// Do not remove this otherwise you will not be able to withdraw the funds.
151+
// =============================================================================
152+
(bool os, ) = payable(owner()).call{value: address(this).balance}("");
153+
require(os);
154+
// =============================================================================
155+
}
156+
157+
function _mintLoop(address _receiver, uint256 _mintAmount) internal {
158+
for (uint256 i = 0; i < _mintAmount; i++) {
159+
supply.increment();
160+
_safeMint(_receiver, supply.current());
161+
}
162+
}
163+
164+
function _baseURI() internal view virtual override returns (string memory) {
165+
return uriPrefix;
166+
}
167+
}

0 commit comments

Comments
 (0)