Setting up the ERC-721 Project

To setup a project that uses Hardhat for ERC-721 contract development, do the following:

  1. Create a new directory and enter it.

    mkdir my-nft
    cd my-nft
    
  2. Initialize a new Node.js project.

    npm init -y
    

    The directory should contain a single file called package.json.

  3. Install Hardhat by running the following command in the my-nft directory.

    npm install --save-dev hardhat
    

    The package.json file will now have a hardhat section under devDependencies.

  4. Create a Hardhat project by running the following command. Choose the Create an empty hardhat.config.js option.

    npx hardhat
    

    The directory will have a file called hardhat.config.js with the following contents.

    /** @type import('hardhat/config').HardhatUserConfig */
    module.exports = {
        solidity: "0.8.17",
    };
    

    The number 0.8.17 specifies the version of the Solidity compiler.

  5. Install the dotenv package in your project directory.

    npm install dotenv --save
    
  6. Create a file called .env in the project directory with the following contents.

    API_URL = "https://eth-goerli.g.alchemy.com/v2/your-api-key"
    API_KEY = "your-api-key"
    PRIVATE_KEY = "your-metamask-private-key"
    

    Follow these instructions to export your private key from Metamask. The API_URL and API_KEY values need to be copied from your Alchemy account.

    NOTE: If you are going to push the project code to a public Github/Gitlab repository, remember to add the .env file to your .gitignore.

  7. Install Ethers.js by running the following command

    npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"
    
  8. Update the hardhat.config.js file to have the following content.

    /**
    * @type import('hardhat/config').HardhatUserConfig
    */
    
    require('dotenv').config();
    require("@nomiclabs/hardhat-ethers");
    
    const { API_URL, PRIVATE_KEY } = process.env;
    
    module.exports = {
    solidity: "0.8.17",
    defaultNetwork: "goerli",
    networks: {
        hardhat: {},
        goerli: {
            url: API_URL,
            accounts: [`0x${PRIVATE_KEY}`]
        }
    },
    }
    
  9. Your ERC-721 token will be based on the implementation by OpenZeppelin. Install the Node.js package containing OpenZeppelin's contracts by running the following command in the project directory.

    npm install @openzeppelin/contracts
    

    The installed contracts can be found in the node_modules directory in your project directory. The path will be node_modules/@openzeppelin/contracts/. We will be inheriting the ERC-20 implementation at node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol.