Deploying the NFT (ERC-721) Contract
To deploy your ERC-721 token, do the following:
-
In the project directory, create a
contractsdirectory.mkdir contracts -
Create a new file in the
contractsdirectory calledMyNFT.sol. -
Copy and paste the following code into
MyNFT.sol.// SPDX-License-Identifier: MIT pragma solidity ^0.8.17; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; import "@openzeppelin/contracts/utils/Counters.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract WinterSchoolNFT is ERC721URIStorage, Ownable { using Counters for Counters.Counter; Counters.Counter private _tokenIds; constructor() ERC721("Winter School NFT", "WSNFT") {} function mintNFT(address recipient, string memory tokenURI) public onlyOwner returns (uint256) { _tokenIds.increment(); uint256 newItemId = _tokenIds.current(); _mint(recipient, newItemId); _setTokenURI(newItemId, tokenURI); return newItemId; } } -
Compile the contract by running the following command.
npx hardhat compileYou should see a message saying
Compiled 1 Solidity file successfully. -
Create a directory called
scriptsmkdir scripts -
Create a file called
deploy.jsin thescriptsdirectory with the following content.async function main() { // Grab the contract factory const MyNFT = await ethers.getContractFactory("WinterSchoolNFT"); // Start deployment, returning a promise that resolves to a contract object const myNFT = await MyNFT.deploy(); // Instance of the contract console.log("ERC-721 contract deployed to address:", myNFT.address); } main() .then(() => process.exit(0)) .catch(error => { console.error(error); process.exit(1); }); -
Deploy the contract by running the following command.
npx hardhat run scripts/deploy.js --network goerliYou should see a message of the following form. The address will be different in your case.
ERC-721 contract deployed to address: 0x6898E26AD18e2DeA803E578a1F29C0f86bF3276a -
IMPORTANT: Create a variable called
CONTRACT_ADDRESSin your.envfile with value equal to your deployed contract address. Your.envfile should look like this.API_URL = "https://eth-goerli.g.alchemy.com/v2/your-api-key" API_KEY = "your-api-key" PRIVATE_KEY = "your-metamask-private-key" CONTRACT_ADDRESS = "your-deployed-contract-address" -
Go to https://goerli.etherscan.io/token/[Your Token Address] to see the token details. Notice that you have to enter the address of the newly created token in the URL.
Customize and Launch Your Token (optional)
-
The Winter School NFT launched in the previous section has two characteristics that can be customized.
- NFT Collection name: Winter School NFT
- NFT Collection symbol: WSNST
-
*Customize your NFT by changing the values in the constructor arguments. The following line in
MyNFT.solneeds to be changed.constructor() ERC721("Winter School NFT", "WSNFT") {} -
Run the following commands to lauch your customized token contract.
npx hardhat compile npx hardhat run scripts/deploy.js --network goerliNote: If you change the name of the contract from
WinterSchoolNFTto something else, remember to enter the new name as the argument to theethers.getContractFactoryinscripts/deploy.js. -
Go to https://goerli.etherscan.io/token/[Your Token Address] to see the token details.