Skip to content

demonstration of deploying an ERC-721 Smart Contract to Polygon with Ankr and Hardhat

License

Notifications You must be signed in to change notification settings

jcstein/la-hacks

Repository files navigation

Ankr + Polygon + Hardhat: Deploy Your First ERC-721 Smart Contract and NFT

If you’re looking to learn how to deploy your first smart contract and mint your first NFT, you’ve come to the right place. This is a tutorial on how to deploy an ERC-721 smart contract with unlimited minting functionality to the Polygon main network using Ankr’s public RPC.

🧱 Prerequisites

🏗 Let’s Get Started

  1. Take a look at the ERC-721 page below ⬇️

    ERC-721

    • Here are the key points:

      1. “ERC-721 is a free, open standard that describes how to build non-fungible or unique tokens on the Ethereum blockchain.”

      2. “ERC-721 defines a minimum interface a smart contract must implement to allow unique tokens to be managed, owned, and traded. It does not mandate a standard for token metadata or restrict adding supplemental functions.”

      3. Most tokens are fungible, but ERC-721 tokens are all unique. ERC-20 tokens are fungible = every token is the same as every other token

        Screen Shot 2022-04-10 at 3 17 17 PM

  2. Take a look at the Ankr integration with Ethers.js

  3. Add Polygon to your MetaMask wallet as a network at https://ankr.com/protocol/public by clicking the MetaMask button

    Screen Shot 2022-04-11 at 2 52 47 PM
  4. In your terminal, make a new directory called ankr-polygon-nft

    mkdir ankr-polygon-nft
  5. Change directories into ankr-polygon-nft

    cd ankr-polygon-nft
  6. Initialize the directory with npm

    npm init -y
  7. Add the dependencies using Yarn

    yarn add dotenv hardhat @nomiclabs/hardhat-etherscan @openzeppelin/contracts
  8. Open up VS Code and run Command + Shift + P

    1. Type shell into the command pallette and hit enter, hit enter again

    2. Open up VS Code from your terminal

      code .
  9. Initiate Hardhat

    1. What is Hardhat? An Ethereum development environment for testing and deploying smart contracts to the blockchain
    2. Pick Create a basic sample project and select yes or press enter to the default values
    npx hardhat init
  10. Delete Greeter.sol in the Contracts folder and sample-script.js in the Scripts folder. You can also delete the Tests folder.

  11. Create a new file in Contracts called AnkrPolygonNFT.sol

    1. Add the following code

      // SPDX-License-Identifier: MIT
      pragma solidity ^0.8.0;
      
      import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
      import "@openzeppelin/contracts/utils/Counters.sol";
      1. Line 1 - declares the type of license associated with the contract, in this case, an MIT license
      2. Line 2 - declare the version of solidity to compile this contract
      3. Lines 4-5 - import ERC721 URI storage extension standard and Counters.sol which will help set token IDs from OpenZepplin
  12. Now, we’ll write our contract ⬇️

    // ...the code above...
    // name your contract and set the storage type (inherited from OpenZeppelin)
    contract AnkrPolygonNFT is ERC721URIStorage {
    
    // set up our counters
        using Counters for Counters.Counter;
    // use counter to store tokenIds
        Counters.Counter private _tokenIds;
    
    // pass arguments for name and symbol
        constructor() ERC721("AnkrPolygonNFT", "ANKRPG") {}
    
    // create mint function with argument for tokenURI which will be a JSON file on IPFS
        function mint(string memory tokenURI) public returns (uint256){
    
    // use token increment function to count up
            _tokenIds.increment();
    
    // fetch current tokenId
            uint256 newItemId = _tokenIds.current();
    
    // safeMint requires address of who is interacting with contract (msg.sender) and tokenId from the line above
            _safeMint(msg.sender, newItemId);
    
    // set newItemId and tokenURI
            _setTokenURI(newItemId, tokenURI);
    
    // return newItemId
            return newItemId;
        }
    }
  13. Next, clear your hardhat.config.js file and add the following

    require("dotenv").config();
    require("@nomiclabs/hardhat-waffle");
    require("@nomiclabs/hardhat-etherscan");
    
    module.exports = {
      solidity: "0.8.4",
    };
  14. You can now specify the network, account and polygonscan key in module.exports

    1. Go to ankr.com/protocol/public and copy the RPC for Polygon: https://rpc.ankr.com/polygon
    // imports
    
    module.exports = {
      solidity: "0.8.4",
      networks: {
        matic: {
          url: "https://rpc.ankr.com/polygon",
          accounts: [process.env.WALLET_PRIVATE_KEY],
        },
      },
      etherscan: {
        apiKey: process.env.POLYGONSCAN_API_KEY,
      },
    };
  15. Create a .env file in your root directory

    1. Copy the private key from your MetaMask wallet with $MATIC in it

      Screen Shot 2022-04-11 at 3 03 07 PM Screen Shot 2022-04-11 at 3 04 12 PM
    2. Register a new account, add a new API key, and copy the key from polygonscan.com. This will allow us to verify our contract on polygonscan once it has been deployed.

      Screen Shot 2022-04-11 at 2 48 16 PM
    3. Paste these keys into your .env file

      WALLET_PRIVATE_KEY=[YOUR_PRIVATE_METAMASK_KEY]
      POLYGONSCAN_API_KEY=[YOUR_POLYGONSCAN_API_KEY]
  16. Create a new Deploy.js file in the scripts directory

    // open main asynchronous function will handle deployment
    const main = async () => {
      try {
        // use hre object that allows us to pass the name of our contract to getContractFactory
        const nftContractFactory = await hre.ethers.getContractFactory(
          "AnkrPolygonNFT"
        );
    
        // create variable to allow us to use the deploy function of getContractFactory
        const nftContract = await nftContractFactory.deploy();
    
        // await deployment of contract
        await nftContract.deployed();
    
        // log the address of the Contract in our console
        console.log("Contract deployed to:", nftContract.address);
        process.exit(0);
    
        // catch error, if any, and log in console
      } catch (error) {
        console.log(error);
        process.exit(1);
      }
    };
    
    main();
  17. Run the following command in your terminal to deploy your smart contract to the Polygon Blockchain, a L2 on Ethereum

    npx hardhat run scripts/Deploy.js --network matic
    • If the contract was successfully deployed to Polygon’s blockchain, the output should look like this ⬇️
    Downloading compiler 0.8.4
    Compiled 12 Solidity files successfully
    Contract deployed to: 0x66248349aa6Ef98792c61b7C625F992bB5E7Fbd2
  18. Copy the contract address and find it in polygonscan at polygonscan.com

Screen Shot 2022-04-11 at 3 33 23 PM

  1. Be sure your Polygon Scan API key is in your .env file and then run the following command to verify your contract on polygonscan. Be sure to replace the address with your contract address from Step 18. ⬇️

    npx hardhat verify 0x66248349aa6Ef98792c61b7C625F992bB5E7Fbd2 --network matic
    // your output will look similar to the below
    joshstein@Joshs-MacBook-Pro ankr-polygon-nft % npx hardhat verify 0x66248349aa6Ef98792c61b7C625F992bB5E7Fbd2 --network matic
    Nothing to compile
    Successfully submitted source code for contract
    contracts/AnkrPolygonNFT.sol:AnkrPolygonNFT at 0x66248349aa6Ef98792c61b7C625F992bB5E7Fbd2
    for verification on the block explorer. Waiting for verification result...
    
    Successfully verified contract AnkrPolygonNFT on Etherscan.
    https://polygonscan.com/address/0x66248349aa6Ef98792c61b7C625F992bB5E7Fbd2#code
  2. Our contract is now verified on polygonscan, along with 11 other contracts from Open Zeppelin included in the compilation

Screen Shot 2022-04-11 at 3 39 01 PM

  1. In polygonscan, we are ready to call our function. First, select Contract ✅and then Write Contract. Locate the mint function (this should be #2)

    1. Next, click 🔴 Connect to Web3 to connect your MetaMask wallet

      Untitled

    2. If you connect to your wallet, it will now look like this ⬇️

      Screen Shot 2022-04-11 at 3 52 57 PM
  2. We will now need to set the Token URI and can use web3.storage to store our image and data. This will be in the form of a URL to a JSON file. I’ve gone ahead and made an image with the tools we’ve used on this tutorial. You can use whatever you’d like!

    1. You can learn more about how to structure your JSON file from OpenSea’s metadata standards
    {
      "name": "Ankr x Polygon ERC-721 Smart Contract",
      "description": "Ankr x Polygon ERC-721 Smart Contract",
      "image": "https://ipfs.io/ipfs/bafybeidtawmsmymozum2ndgjsnzf4pgct3rt4p5x6ywcrkiun7sogcsoi4/Ankr-Polygon.svg",
      "attributes": [
        {
          "trait_type": "Developer",
          "value": "Josh CS"
        },
        {
          "trait_type": "Website",
          "value": "https://ankr.com"
        }
      ]
    }
  3. After uploading to web3.storage, paste the URL to your JSON file and select Mint and then Confirm the MetaMask transaction

    Screen Shot 2022-04-11 at 4 58 08 PM
  4. Check out your OpenSea gallery to see your NFT!

Screen Shot 2022-04-14 at 9 22 13 PM

About

demonstration of deploying an ERC-721 Smart Contract to Polygon with Ankr and Hardhat

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published