How to Create an NFT Marketplace on the Polygon Blockchain

·

Overview

A non-fungible token (NFT) marketplace is a platform where users buy and sell unique digital assets represented as NFTs. These assets can include collectibles, digital art, in-game items, and more. This guide will walk you through creating and deploying an NFT marketplace smart contract on the Polygon Mumbai testnet using Hardhat, along with testing and interacting with the contract via Ethers.js.

Key Objectives

Prerequisites


Setting Up the Project

Initialize a Hardhat project with the following commands:

mkdir marketplace-hardhat  
cd marketplace-hardhat  
npm install --save-dev hardhat  
npx hardhat  

Select the JavaScript project template and install dependencies:

npm install @openzeppelin/contracts dotenv [email protected]  
npm install --save-dev @nomiclabs/hardhat-etherscan  

Configuring a QuickNode Polygon Endpoint

To deploy and interact with the contract, use a QuickNode Polygon Mumbai endpoint.

  1. Create a Free QuickNode Account: Sign up here.
  2. Generate MATIC Test Tokens: Use the QuickNode Faucet.

👉 Get started with QuickNode today


Creating the NFT Marketplace Smart Contract

Contract Features

Full Contract Code

// SPDX-License-Identifier: MIT  
pragma solidity ^0.8.9;  

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";  
import "@openzeppelin/contracts/utils/Counters.sol";  
import "@openzeppelin/contracts/access/Ownable.sol";  
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";  

contract Marketplace is ReentrancyGuard, Ownable {  
    // Structs, mappings, and functions here  
}

View complete code


Deploying and Testing

  1. Compile Contracts:

    npx hardhat compile  
  2. Test the Marketplace:

    npx hardhat test test/marketplace-test.js  
  3. Deploy to Mumbai:

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

Interacting with the Marketplace

Use Ethers.js to list and purchase NFTs:

const marketplace = await ethers.getContractAt("Marketplace", DEPLOYED_ADDRESS);  
await marketplace.createListing(tokenId, nftAddress, price);  
await marketplace.connect(buyer).buyListing(marketplaceId, nftAddress, { value: price });  

FAQs

1. How do I get MATIC test tokens?

Use the QuickNode Faucet to request MATIC on Mumbai.

2. Can I customize the marketplace fees?

Yes, modify the buyListing function to include commission logic.

3. How do I verify my contract on Polygonscan?

Run:

npx hardhat verify --network mumbai DEPLOYED_ADDRESS  

Conclusion

You’ve now built and deployed an NFT marketplace on Polygon! Expand functionality by adding features like auctions or royalties. Share your project on Twitter or Discord.

👉 Explore more blockchain guides


### Key SEO Keywords:  
- NFT Marketplace  
- Polygon Blockchain  
- Smart Contract Development  
- Hardhat Tutorial