my-nftproject folder, then run:
hardhat.config.jsfile for us which is where we’ll specify all of the set up for our project (on step 13).
contracts/is where we’ll keep our NFT smart contract code
scripts/is where we’ll keep scripts to deploy and interact with our smart contract
@openzeppelin/contracts/token/ERC721/ERC721.solcontains the implementation of the ERC721 standard, which our NFT smart contract will inherit. (To be a valid NFT, your smart contract must implement all the methods of the ERC721 standard.) To learn more about the inherited ERC721 functions, check out the interface definition here.
@openzeppelin/contracts/utils/Counters.solprovides counters that can only be incremented or decremented by one. Our smart contract uses a counter to keep track of the total number of NFTs minted and set the unique ID to our new NFT. Each NFT minted using a smart contract must be assigned a unique ID—here our unique ID is just determined by the total number of NFTs in existance. For example, the first NFT we mint with our smart contract has an ID of "1," our second NFT has an ID of "2," etc.
@openzeppelin/contracts/access/Ownable.solsets up access control on our smart contract, so only the owner of the smart contract (you) can mint NFTs. Note, including access control is entirely a preference. If you'd like anyone to be able to mint an NFT using your smart contract, remove the word
Ownableon line 10 and
onlyOwneron line 17.
ownerOf(returns the owner of the NFT) and
transferFrom(transfers ownership of the NFT).
mintNFT()that allows us to mint an NFT! You'll notice this function takes in 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 additional 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 ERC721 library, and ultimately returns a number that represents the ID of the freshly minted NFT.
.envfile in the root directory of our project, and add your Metamask private key and HTTP Alchemy API URL to it.
.envshould look like this:
hardhat.config.jsfile on step 13.
hardhat.config.jsin the next step.
hardhat.config.jsso that our project knows about all of them.
hardhat.config.jsto look like this:
compiletask is one of the built-in hardhat tasks.
SPDX license identifier not provided in source file, but no need to worry about that — hopefully everything else looks good! If not, you can always message in the Alchemy discord.
scripts/folder and create a new file called
deploy.js, adding the following contents to it:
ContractFactoryin ethers.js is an abstraction used to deploy new smart contracts, so MyNFT here is a factory for instances of our NFT contract. When using the
Contractinstances are connected to the first signer by default.
ContractFactorywill start the deployment, and return a
Promisethat resolves to a
Contract. This is the object that has a method for each of our smart contract functions.
Fromaddress should match your Metamask account address and the To address will say “Contract Creation.” If we click into the transaction, we’ll see our contract address in the