How To Programmatically Deploy and Mint a Simple NFT on the Polygon Blockchain

mbvissers.eth
Geek Culture
Published in
4 min readSep 2, 2021

A quick guide to deploying your first NFT without an NFT marketplace while still being able to trade it on OpenSea.

Photo by Austin Distel on Unsplash

The blockchain is an interesting piece of software at this time. It's a very hot topic, and developers are slowly rising to the top. Ethereum, the biggest smart-contract blockchain is rising rapidly. The problem, however, its gas fees are rising with it. A simple NFT smart contract would cost me $1300 to deploy.

That is why we’re going to use Polygon’s blockchain. It is an Ethereum compatible blockchain with many projects adopting it as a secondary option. Its gas fees are substantially lower while still having the same functionalities. We’ll also be using Hardhat. A way of connecting to the blockchain in JavaScript.

Project Setup

We’re going to deploy and mint our NFTs on the Polygon Testnet (Mumbai). We will use Polygon for its lower gas fees and its ability to still connect with a platform such as OpenSea.

Setting up Your Wallet

For starters, you’d need to set up MetaMask, get some testnet MATIC and make it appear in your wallet. You can follow another article I wrote to achieve this.

After you have created your wallet and set up everything you need for Polygon to work, you can continue.

Setting up the project

We are going to deploy our smart contract using Hardhat. Hardhat is an Ethereum development environment, but thankfully it also works great for Polygon and other chains. We’ll also use NPM to set everything up.

npm install --save-dev hardhat
npx hardhat

The second command will prompt some options for you. You can choose the default options in this case. You can learn more about them in the official docs.

You should now have a directory filled with a few files and folders like contracts, scripts, and test.

Making a Smart Contract

We will use OpenZeppelin’s contract library to quickly create an ERC721 smart contract that is compliant with the specifications specified in the ERC721 standard which is the default for NFTs.

npm install @openzeppelin/contracts

Next, we’re going to create our own contract with these helpful tools in the Contracts folder. You can copy and paste the code from the Gist below in a file called MyToken.sol . The code is generated with OpenZeppelin’s token wizard which is a useful tool for making a simple contract.

This contract has the ability to be minted by the owner of the contract. It will automatically keep track of the TokenId using Counters , and by using Enumerable we can easily keep track of how many tokens there are and to who they belong. The bottom two functions are simple overrides necessary for Enumerable .

It can be very useful to read the abilities of the contract we import on OpenZeppelin. Just read the functions and what they do. If you need more knowledge of Solidity, I suggest doing the CryptoZombies tutorial.

Deploying

We can create a simple script that we can call to deploy our script to the network of our choice. Polygon’s Mumbai testnet. For this, we also need to add some small options in the Hardhat config.

Inside the scripts folder, create a new file called deploy-script.js and add the following code to it.

Also, modify the hardhat.config.js to include the following code.

The private key should be the private key from your MetaMask wallet. We now have everything we should need to deploy a contract if you have testnet MATIC tokens in your wallet from which the private key is by running:

npx hardhat run scripts/deploy-script.js --network matic_testnet

Copy the address it will return in the console, you can even check it out on the Mumbai PolygonScan website.

Minting

If the contract is deployed, you should be able to call the mint function in quite some ways. You could connect your wallet using Web3 and let a button call mint , but we can also do it with another script.

Which you can call with

npx hardhat run scripts/mint-script.js --network matic_testnet

Your first NFT should now be minted and you should be able to view them on OpenSea.

This NFT however, has no URI, no image, and no metadata. That is something for another article.

Conclusion

If you want to mint a ton of NFT’s, you can run the npx command in a loop using a library that executes commands in your cmd. Let me know on Twitter what NFT project you’ve created!

Thank you very much for reading and have a wonderful day.

--

--

mbvissers.eth
Geek Culture

I occasionally write about programming. Follow me on Twitter @0xmbvissers