Skip to content

Instantly share code, notes, and snippets.

@iceonme
Forked from farzaa/MyEpicNFT.sol
Last active May 2, 2023 12:58
Show Gist options
  • Save iceonme/6954bd07c2eac62131e459b08d683461 to your computer and use it in GitHub Desktop.
Save iceonme/6954bd07c2eac62131e459b08d683461 to your computer and use it in GitHub Desktop.
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.0;
// We need some util functions for strings.
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "hardhat/console.sol";
contract MyEpicNFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
// This is our SVG code. All we need to change is the word that's displayed. Everything else stays the same.
// So, we make a baseSvg variable here that all our NFTs can use.
string baseSvg = "<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='black' /><text x='50%' y='50%' class='base' dominant-baseline='middle' text-anchor='middle'>";
// I create three arrays, each with their own theme of random words.
// Pick some random funny words, names of anime characters, foods you like, whatever!
string[] firstWords = ["YOUR_WORD", "YOUR_WORD", "YOUR_WORD", "YOUR_WORD", "YOUR_WORD", "YOUR_WORD"];
string[] secondWords = ["YOUR_WORD", "YOUR_WORD", "YOUR_WORD", "YOUR_WORD", "YOUR_WORD", "YOUR_WORD"];
string[] thirdWords = ["YOUR_WORD", "YOUR_WORD", "YOUR_WORD", "YOUR_WORD", "YOUR_WORD", "YOUR_WORD"];
constructor() ERC721 ("SquareNFT", "SQUARE") {
console.log("This is my NFT contract. Woah!");
}
// I create a function to randomly pick a word from each array.
function pickRandomFirstWord(uint256 tokenId) public view returns (string memory) {
// I seed the random generator. More on this in the lesson.
uint256 rand = random(string(abi.encodePacked("FIRST_WORD", Strings.toString(tokenId))));
// Squash the # between 0 and the length of the array to avoid going out of bounds.
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 random(string memory input) internal pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(input)));
}
function makeAnEpicNFT() public {
uint256 newItemId = _tokenIds.current();
// We go and randomly grab one word from each of the three arrays.
string memory first = pickRandomFirstWord(newItemId);
string memory second = pickRandomSecondWord(newItemId);
string memory third = pickRandomThirdWord(newItemId);
// I concatenate it all together, and then close the <text> and <svg> tags.
string memory finalSvg = string(abi.encodePacked(baseSvg, first, second, third, "</text></svg>"));
console.log("\n--------------------");
console.log(finalSvg);
console.log("--------------------\n");
_safeMint(msg.sender, newItemId);
// We'll be setting the tokenURI later!
_setTokenURI(newItemId, "blah");
_tokenIds.increment();
console.log("An NFT w/ ID %s has been minted to %s", newItemId, msg.sender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment