Skip to content

Commit

Permalink
Merge pull request #16 from liarco/lower-gas-fees
Browse files Browse the repository at this point in the history
Change implementation to enable lower gas fees
  • Loading branch information
HashLips authored Jan 6, 2022
2 parents 975d6d6 + 7c2922c commit 70c3c7f
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions contract/SimpleNftLowerGas.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// SPDX-License-Identifier: MIT

// Amended by HashLips
/**
!Disclaimer!
These contracts have been used to create tutorials,
and was created for the purpose to teach people
how to create smart contracts on the blockchain.
please review this code on your own before using any of
the following code for production.
The developer will not be responsible or liable for all loss or
damage whatsoever caused by you participating in any way in the
experimental code, whether putting money into the contract or
using the code for your own project.
*/

pragma solidity >=0.7.0 <0.9.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract SimpleNftLowerGas is ERC721, Ownable {
using Strings for uint256;
using Counters for Counters.Counter;

Counters.Counter private supply;

string public uriPrefix = "";
string public uriSuffix = ".json";
string public hiddenMetadataUri;

uint256 public cost = 0.01 ether;
uint256 public maxSupply = 10000;
uint256 public maxMintAmountPerTx = 5;

bool public paused = true;
bool public revealed = false;

constructor() ERC721("NAME", "SYMBOL") {
setHiddenMetadataUri("ipfs://__CID__/hidden.json");
}

modifier mintCompliance(uint256 _mintAmount) {
require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
require(supply.current() + _mintAmount <= maxSupply, "Max supply exceeded!");
_;
}

function totalSupply() public view returns (uint256) {
return supply.current();
}

function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
require(!paused, "The contract is paused!");
require(msg.value >= cost * _mintAmount, "Insufficient funds!");

_mintLoop(msg.sender, _mintAmount);
}

function mintForAddress(uint256 _mintAmount, address _receiver) public mintCompliance(_mintAmount) onlyOwner {
_mintLoop(_receiver, _mintAmount);
}

function walletOfOwner(address _owner)
public
view
returns (uint256[] memory)
{
uint256 ownerTokenCount = balanceOf(_owner);
uint256[] memory ownedTokenIds = new uint256[](ownerTokenCount);
uint256 currentTokenId = 1;
uint256 ownedTokenIndex = 0;

while (ownedTokenIndex < ownerTokenCount && currentTokenId <= maxSupply) {
address currentTokenOwner = ownerOf(currentTokenId);

if (currentTokenOwner == _owner) {
ownedTokenIds[ownedTokenIndex] = currentTokenId;

ownedTokenIndex++;
}

currentTokenId++;
}

return ownedTokenIds;
}

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 setRevealed(bool _state) public onlyOwner {
revealed = _state;
}

function setCost(uint256 _cost) public onlyOwner {
cost = _cost;
}

function setMaxMintAmountPerTx(uint256 _maxMintAmountPerTx) public onlyOwner {
maxMintAmountPerTx = _maxMintAmountPerTx;
}

function setHiddenMetadataUri(string memory _hiddenMetadataUri) public onlyOwner {
hiddenMetadataUri = _hiddenMetadataUri;
}

function setUriPrefix(string memory _uriPrefix) public onlyOwner {
uriPrefix = _uriPrefix;
}

function setUriSuffix(string memory _uriSuffix) public onlyOwner {
uriSuffix = _uriSuffix;
}

function setPaused(bool _state) public onlyOwner {
paused = _state;
}

function withdraw() public onlyOwner {
// This will pay HashLips 5% of the initial sale.
// You can remove this if you want, or keep it in to support HashLips and his channel.
// =============================================================================
(bool hs, ) = payable(0x943590A42C27D08e3744202c4Ae5eD55c2dE240D).call{value: address(this).balance * 5 / 100}("");
require(hs);
// =============================================================================

// This will transfer the remaining contract balance to the owner.
// 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);
// =============================================================================
}

function _mintLoop(address _receiver, uint256 _mintAmount) internal {
for (uint256 i = 0; i < _mintAmount; i++) {
supply.increment();
_safeMint(_receiver, supply.current());
}
}

function _baseURI() internal view virtual override returns (string memory) {
return uriPrefix;
}
}

0 comments on commit 70c3c7f

Please sign in to comment.