Last active
July 17, 2023 07:45
-
-
Save farzaa/b788ba3a8dbaf6f1ef9af57eefa63c27 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Don't forget to add the first line: // SPDX-License-Identifier: UNLICENSED
Correct, i think it's good to add // SPDX-License-Identifier: UNLICENSED
at the top of the file, but for learning purposes, it's okay to omit.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.1;
Why did we step down to pragma solidity 0.8.0; from pragma solidity ^0.8.1; ?
Change pragma solidity 0.8.0;
=> pragma solidity ^0.8.0;
Also add: // SPDX-License-Identifier: MIT
to first line
Why use keccak256 instead of sha256?
Perfect for use with above changes >
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pragma solidity 0.8.0;
=>pragma solidity ^0.8.0;