Smart Contract With Polygon

Steps in creating a smart contract in Polygon.

mkdir mycollection && cd mycollection && npm init -y
npm install — save-dev hardhat
npx hardhat
npx hardhat run scripts/sample-script.js
npm install @openzeppelin/contracts
npm install dotenv
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";contract mycollection is ERC721Enumerable, Ownable {
using SafeMath for uint256;
using Counters for Counters.Counter;

Counters.Counter private _tokenIds;
uint public constant MAX_SUPPLY = 400;
uint public constant PRICE = 0.01 ether;
uint public constant MAX_PER_MINT = 3;

string public baseTokenURI;
constructor(string memory baseURI) ERC721("mycollection", "MCN") {
function reserveNFTs() public onlyOwner {
uint totalMinted = _tokenIds.current(); require(
totalMinted.add(10) < MAX_SUPPLY, "Not enough NFTs"
); for (uint i = 0; i < 10; i++) {
function _baseURI() internal 
returns (string memory) {
return baseTokenURI;

function setBaseURI(string memory _baseTokenURI) public onlyOwner {
baseTokenURI = _baseTokenURI;
function mintNFTs(uint _count) public payable {
uint totalMinted = _tokenIds.current(); require(
totalMinted.add(_count) <= MAX_SUPPLY, "Not enough NFTs!"
); require(
_count > 0 && _count <= MAX_PER_MINT,
"Cannot mint specified number of NFTs."
); require(
msg.value >= PRICE.mul(_count),
"Not enough ether to purchase NFTs."
); for (uint i = 0; i < _count; i++) {
  1. There are enough NFTs left in the collection for the caller to mint the requested amount.
  2. The caller has requested to mint more than 0 and less than the maximum number of NFTs allowed per transaction.
  3. The caller has sent enough ether to mint the requested number of NFTs.
function _mintSingleNFT() private {
uint newTokenID = _tokenIds.current();
_safeMint(msg.sender, newTokenID);
  1. Getting the current ID that hasn’t been minted yet.
  2. Using the _safeMint() function already defined by OpenZeppelin to assign the NFT ID to the account that called the function.
  3. Incrementing the token IDs counter by 1
function tokensOfOwner(address _owner) 
returns (uint[] memory) { uint tokenCount = balanceOf(_owner);
uint[] memory tokensId = new uint256[](tokenCount); for (uint i = 0; i < tokenCount; i++) {
tokensId[i] = tokenOfOwnerByIndex(_owner, i);

return tokensId;
function withdraw() public payable onlyOwner {
uint balance = address(this).balance;
require(balance > 0, "No ether left to withdraw"); (bool success, ) = (msg.sender).call{value: balance}("");
require(success, "Transfer failed.");
  1. We get the address of the deployer/owner (us)
  2. We get the contract that we want to deploy.
  3. We send a request for the contract to be deployed and wait for a miner to pick this request and add it to the blockchain.
  4. Once mined, we get the contract address.
  5. We then call public functions of our contract. We reserve 10 NFTs, mint 3 NFTs by sending 0.03 ETH to the contract, and check the NFTs owned by us. Note that the first two calls require gas (because they’re writing to the blockchain) whereas the third simply reads from the blockchain.
npx hardhat run scripts/run
API_URL = ""
PRIVATE_KEY = "<-- Metamask wallet private key -->"
npm install @nomiclabs/hardhat-etherscan
npx hardhat run scripts/run.js — network mumbai
  1. Signup for an account on Polygonscan.
  2. Create your API key.
  3. Update your .env file with the API key for POLYGONSCAN_KEY
API_URL = ""
PRIVATE_KEY = "<-- Metamask wallet private key -->"
POLYGON_URL = "<-- Alchemy Polygon URL -->"
npx hardhat clean
npx hardhat verify --network mumbai DEPLOYED_CONTRACT_ADDRESS "constraint"
npx hardhat verify — network mumbai 0x357044B12f31Bf68c5cAc9f48d5f69648a3a48f8 "ipfs://QmUZJxtWp5xJc9YFd87E9dZkuMTTTrLCL7vcakHAgae7FW/"


  1. This is a test environment, to mint using the real environment, you have to use real MATIC currency by purchasing one.
  2. You will need a user interface for your users to mint by a click of a button.




Data Scientist || Machine Learning enthusiast and hobbyist

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What Is A Good And Free API Using Python For Getting A Website Classified?

Spring Boot Admin Server and Client —implementation with example


From laptop to reality: why MLOps’ biggest challenges are organizational

Technical Analysis Library: Solving the Legacy Code Issue

How to connect Quickbooks Desktop From Php/Laravel

How To Create A Temporary Email For Playstation Plus

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nwosu Rosemary

Nwosu Rosemary

Data Scientist || Machine Learning enthusiast and hobbyist

More from Medium

Create an App like Uniswap and Execute Trades in a Decentralized Manner

Smart Contract Tutorial (1) — Ethereum Solidity

Ownership Exploitation in Solidity

Creating a Blockchain and using a REST API to insert blocks and retrieve the chain.