Skip to content

Instantly share code, notes, and snippets.

@mpixelz
Created October 7, 2021 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mpixelz/572163c0f809031f0d3e1862db1dbced to your computer and use it in GitHub Desktop.
Save mpixelz/572163c0f809031f0d3e1862db1dbced to your computer and use it in GitHub Desktop.
Randomly Assigned extension with hashlips simple contract
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "https://github.com/1001-digital/erc721-extensions/blob/main/contracts/RandomlyAssigned.sol";
contract LFC_Test is ERC721, Ownable, RandomlyAssigned{
using Strings for uint256;
bool public paused = false;
string baseURI;
string public baseExtension = ".json";
uint8 public maxMintAmount = 5;
uint16 maxSupply = 20;
uint16 currentSupply = 0;
uint256 public cost = 0.02 ether;
uint256 startTime;
uint256[] private _allTokens;
mapping(uint256 => uint256) private _ownedTokensIndex;
mapping(uint256 => uint256) private _allTokensIndex;
mapping(address => mapping(uint256 => uint256)) private _ownedTokens;
constructor(
string memory _name,
string memory _symbol,
string memory _initBaseURI)
ERC721(_name, _symbol)
RandomlyAssigned(maxSupply,1)
{
setBaseURI(_initBaseURI);
mint(5);
paused = true;
}
//internal
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}
//public
function mint (uint8 _amount) public payable
{
require( tokenCount() + 1 <= totalSupply());
require( availableTokenCount() - 1 >= 0);
if (msg.sender != owner()) {
require( msg.value >= cost * _amount);
}
for(uint256 i = 1; i <= _amount; i++){
uint256 id = nextToken();
_safeMint(msg.sender, id);
if(currentSupply==9 || currentSupply==19 || currentSupply==29){
cost+=0.01 ether;
}
currentSupply++;
}
}
function walletOfOwner(address _owner) public view
returns (uint256[] memory) {
uint256 ownerTokenCount = balanceOf(_owner);
uint256[] memory tokenIds = new uint256[](ownerTokenCount);
for(uint256 i; i < ownerTokenCount; i++){
tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
}
return tokenIds;
}
function tokenURI(uint256 tokenId) public view virtual override
returns (string memory){
require(_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
string memory currentBaseURI = _baseURI();
return bytes(currentBaseURI).length >0 ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension)) : "";
}
//only owner
function setCost(uint256 _newCost) public onlyOwner() {
cost = _newCost * 1e18;
}
function setmaxMintAmount(uint8 _newmaxMintAmount) public onlyOwner() {
maxMintAmount = _newmaxMintAmount;
}
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 withdraw() public payable onlyOwner {
require(payable(msg.sender).send(address(this).balance));
}
//ENUMERABLE
function tokenOfOwnerByIndex(address owner, uint256 index) public view returns (uint256) {
require(index < ERC721.balanceOf(owner), "ERC721Enumerable: owner index out of bounds");
return _ownedTokens[owner][index];
}
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
uint256 length = ERC721.balanceOf(to);
_ownedTokens[to][length] = tokenId;
_ownedTokensIndex[tokenId] = length;
}
function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
uint256 lastTokenIndex = ERC721.balanceOf(from) - 1;
uint256 tokenIndex = _ownedTokensIndex[tokenId];
if (tokenIndex != lastTokenIndex) {
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
_ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
_ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
}
delete _ownedTokensIndex[tokenId];
delete _ownedTokens[from][lastTokenIndex];
}
function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
uint256 lastTokenIndex = _allTokens.length - 1;
uint256 tokenIndex = _allTokensIndex[tokenId];
uint256 lastTokenId = _allTokens[lastTokenIndex];
_allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
_allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
delete _allTokensIndex[tokenId];
_allTokens.pop();
}
function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
_allTokensIndex[tokenId] = _allTokens.length;
_allTokens.push(tokenId);
}
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override {
super._beforeTokenTransfer(from, to, tokenId);
if (from == address(0)) {
_addTokenToAllTokensEnumeration(tokenId);
} else if (from != to) {
_removeTokenFromOwnerEnumeration(from, tokenId);
}
if (to == address(0)) {
_removeTokenFromAllTokensEnumeration(tokenId);
} else if (to != from) {
_addTokenToOwnerEnumeration(to, tokenId);
}
}
}
@nugz420
Copy link

nugz420 commented Dec 9, 2021

This is great thank you !

How come you didn't include the "notRevealedUri" section ?

I was trying to add it but no luck i kept getting a "Declaration Error" undeclared identifier.

Would love your help on this. Great code regardless.

@MadManwithaBlueBox
Copy link

I also notice you have a section at the bottom commented to be Enumerable and you use several features from the ERC721Enumerable stuff but have only an import statement for ERC721 at the top, not Enumerable.

Can you explain how you got all that to work without importing the Enumerable code.

If you could also add in some code to use a count method instead of totalsupply() to lower gas fees that would be great too.

@MadManwithaBlueBox
Copy link

Also you have a bit adding 0.01 ether if current supply matches certain numbers, is this really needed or just something you wanted for yourself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment