Deploying the NFT (ERC-721) Contract

To deploy your ERC-721 token, do the following:

  1. In the project directory, create a contracts directory.

    mkdir contracts
    
  2. Create a new file in the contracts directory called MyNFT.sol.

  3. 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;
        }
    }
    
  4. Compile the contract by running the following command.

    npx hardhat compile
    

    You should see a message saying Compiled 1 Solidity file successfully.

  5. Create a directory called scripts

    mkdir scripts
    
  6. Create a file called deploy.js in the scripts directory 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);
    });
    
  7. Deploy the contract by running the following command.

    npx hardhat run scripts/deploy.js --network goerli
    

    You should see a message of the following form. The address will be different in your case.

    ERC-721 contract deployed to address: 0x6898E26AD18e2DeA803E578a1F29C0f86bF3276a
    
  8. IMPORTANT: Create a variable called CONTRACT_ADDRESS in your .env file with value equal to your deployed contract address. Your .env file 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"
    
  9. 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)

  1. 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
  2. *Customize your NFT by changing the values in the constructor arguments. The following line in MyNFT.sol needs to be changed.

    constructor() ERC721("Winter School NFT", "WSNFT") {}
    
  3. Run the following commands to lauch your customized token contract.

    npx hardhat compile
    npx hardhat run scripts/deploy.js --network goerli
    

    Note: If you change the name of the contract from WinterSchoolNFT to something else, remember to enter the new name as the argument to the ethers.getContractFactory in scripts/deploy.js.

  4. Go to https://goerli.etherscan.io/token/[Your Token Address] to see the token details.