Skip to content

Instantly share code, notes, and snippets.

@Deveshb15
Last active October 11, 2021 08:44
Show Gist options
  • Save Deveshb15/147f24d6256d83398c00ab31c380a20f to your computer and use it in GitHub Desktop.
Save Deveshb15/147f24d6256d83398c00ab31c380a20f to your computer and use it in GitHub Desktop.
NFT Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "hardhat/console.sol";
import { Base64 } from "./libraries/Base64.sol";
contract MyEpicNFT is ERC721URIStorage {
event NewEpicNFTMinted(address sender, uint256 tokenId, uint256 nftCount);
// OpenZeppelin helps us keep track of tokenIds.
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
uint256 nftCount;
string svgPartOne = "<svg xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='xMinYMin meet' viewBox='0 0 350 350'><style>.base { fill: white; font-family: serif; font-size: 24px; }</style><rect width='100%' height='100%' fill='";
string svgPartTwo = "'/><text x='50%' y='50%' class='base' dominant-baseline='middle' text-anchor='middle'>";
string[] firstWords = ["Amazingly", "Tremendously", "Excellentlly", "Weakly", "Fabulously", "Epicly", "Crazily", "Wildly", "Terryfyingly", "Startlingly", "Stunningly", "Astonishingly", "Coolest", "Dumbest"];
string[] secondWords = ["Eating", "Dancing", "Jerking", "Singing", "Crying", "Laughing", "Running", "Crawling", "Fighting", "Sleeping", "Walking", "Flying", "Falling", "Pushing" ];
string[] thirdWords = ["Naruto", "Kakashi" ,"Sasuke", "Jiraiya", "Hinata", "Sakura", "Itachi" ,"Tsunade", "Madara", "Obito", "Minato", "Gaara", "Orochimaru", "RockLee"];
string[] colors = ["#FF5C58", "#0CECDD", "black", "#5C7AEA", "#8236CB", "#F037A5", "#FDE49C"];
constructor() ERC721 ("NarutoKunV1", "NARUTO") {
console.log("This is my NFT. OOF!");
}
function pickRandomFirstWord(uint256 tokenId) public view returns (string memory) {
uint256 rand = random(string(abi.encodePacked("FIRST_WORD", Strings.toString(tokenId))));
rand = rand % firstWords.length;
return firstWords[rand];
}
function pickRandomSecondWord(uint256 tokenId) public view returns (string memory) {
uint256 rand = random(string(abi.encodePacked("SECOND_WORD", Strings.toString(tokenId))));
rand = rand % secondWords.length;
return secondWords[rand];
}
function pickRandomThirdWord(uint256 tokenId) public view returns (string memory) {
uint256 rand = random(string(abi.encodePacked("THIRD_WORD", Strings.toString(tokenId))));
rand = rand % thirdWords.length;
return thirdWords[rand];
}
function pickRandomColor(uint256 tokenId) public view returns (string memory) {
uint256 rand = random(string(abi.encodePacked("COLOR", Strings.toString(tokenId))));
rand = rand % colors.length;
return colors[rand];
}
function random(string memory input) internal pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(input)));
}
function makeAnEpicNFT() public {
require(nftCount < 50, "Can't mint more than 50 NFTs");
uint256 newItemId = _tokenIds.current();
string memory first = pickRandomFirstWord(newItemId);
string memory second = pickRandomSecondWord(newItemId);
string memory third = pickRandomThirdWord(newItemId);
string memory combinedWord = string(abi.encodePacked(first, second, third));
string memory randomColor = pickRandomColor(newItemId);
string memory finalSvg = string(abi.encodePacked(svgPartOne, randomColor, svgPartTwo, combinedWord, "</text></svg>"));
string memory json = Base64.encode(
bytes(
string(
abi.encodePacked(
'{"name" : "',
combinedWord,
'","description":"Premium Naruto Funny Squares.", "image":"data:image/svg+xml;base64,',
Base64.encode(bytes(finalSvg)),
'"}'
)
)
)
);
string memory finalTokenUri = string(
abi.encodePacked("data:application/json;base64,", json)
);
console.log("\n________________");
console.log(finalTokenUri);
console.log("________________");
_safeMint(msg.sender, _tokenIds.current());
_setTokenURI(_tokenIds.current(), finalTokenUri);
console.log("An NFT w/ ID %s has been minted to %s", newItemId, msg.sender);
_tokenIds.increment();
nftCount++;
emit NewEpicNFTMinted(msg.sender , newItemId, nftCount);
}
function getTotalNFTsMintedSoFar() public view returns(uint256) {
return nftCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment