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
- Deploy an NFT Marketplace to the Polygon Mumbai testnet using Hardhat.
- Mint an ERC-721 NFT for testing on the marketplace.
- Interact with the marketplace contract using Ethers.js.
Prerequisites
- Node.js and NPM installed
- A code editor (e.g., VSCode)
- MetaMask configured with two test accounts
- MATIC tokens from the QuickNode Faucet
- Hardhat and Ethers.js installed
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.
- Create a Free QuickNode Account: Sign up here.
- Generate MATIC Test Tokens: Use the QuickNode Faucet.
👉 Get started with QuickNode today
Creating the NFT Marketplace Smart Contract
Contract Features
- Store NFT details (token ID, address, price, seller).
- List NFTs for sale via
createListing. - Facilitate purchases via
buyNFT. - Track listed/purchased NFTs with helper functions.
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
}Deploying and Testing
Compile Contracts:
npx hardhat compileTest the Marketplace:
npx hardhat test test/marketplace-test.jsDeploy 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