hardhat.config.jsfile which is where we’ll set up our project in step 14.
contracts/is where we’ll keep your NFT smart contract code2.
scripts/is where we’ll keep scripts to deploy and interact with your smart contract
@openzeppelin/contracts/token/ERC721/ERC721.solcontains the implementation of the ERC-721 standard, which your NFT smart contract will inherit. Your smart contract must implement all the methods of the ERC-721 standard to be a valid NFT. To learn more about the inherited ERC-721 functions, check out the interface definition.
@openzeppelin/contracts/utils/Counters.solprovides counters that can only be incremented or decremented by one. Your smart contract uses a counter to keep track of the total number of NFTs minted and sets the unique ID on your new NFT. Each NFT minted using a smart contract must be assigned a unique ID—here your unique ID is just determined by the total number of NFTs in existence. For example, the first NFT we mint with your smart contract has an ID of "1," your second NFT has an ID of "2," etc.
@openzeppelin/contracts/access/Ownable.solsets up access control on your smart contract, so only the owner of the smart contract (you) can mint NFTs. (
Ownableon line 10 and
onlyOwneron line 17.)
ownerOf, which returns the owner of the NFT, and
transferFrom, which transfers ownership of the NFT from one account to another.
MyNFT” and “
NFT.” The first variable is the smart contract’s name, and the second is its symbol. You can name each of these variables whatever you wish!
mintNFT(address recipient, string memory tokenURI)that allows us to mint an NFT! You'll notice this function takes two variables:
address recipientspecifies the address that will receive your freshly minted NFT
string memory tokenURIis a string that should resolve to a JSON document that describes the NFT's metadata. An NFT's metadata is really what brings it to life, allowing it to have configurable properties, such as a name, description, image, and other attributes. In part 2 of this tutorial, we will describe how to configure this metadata.
mintNFTcalls some methods from the inherited ERC-721 library and ultimately returns a number that represents the ID of the freshly minted NFT.
.envfile (just name it “.env”) in the root directory of your project, and add your MetaMask private key and HTTP Alchemy API URL.
.envshould now look like this:
hardhat.config.jsfile in step 14.
hardhat.config.jsin the next step.
hardhat.config.jsso that your project knows about them.
hardhat.config.jsto look like this:
deploy.jswith the following contents:
const MyNFT = await ethers.getContractFactory("MyNFT");
const myNFT = await MyNFT.deploy();
deploy()on a ContractFactory will start the deployment, and return a Promise that resolves to a Contract. This is the object that has a method for each of your smart contract functions.