NFT Marketplace Clone Step-by-Step
Creating an NFT marketplace clone is a great way to learn about blockchain, smart contracts, web3 development, and full-stack DApps. Here's a step-by-step guide to help you build a basic NFT marketplace clone (like OpenSea) using Ethereum, Solidity, and JavaScript (React + Web3.js or Ethers.js).
π§± What You’ll Build
A minimal NFT marketplace where users can:
Mint NFTs (ERC-721 tokens)
List NFTs for sale
Buy NFTs
View owned NFTs
π§° Tech Stack
Layer Tools
Smart Contracts Solidity, OpenZeppelin, Hardhat/Remix
Blockchain Ethereum (testnet like Goerli or local using Hardhat)
Frontend React.js, Web3.js or Ethers.js
Wallet Integration MetaMask
Storage (optional) IPFS via Pinata or NFT.Storage (for images/metadata)
π Step 1: Set Up Your Development Environment
Install Node.js & npm:
node -v
npm -v
Create a project folder:
mkdir nft-marketplace-clone
cd nft-marketplace-clone
Install Hardhat (for smart contract development):
npm init -y
npm install --save-dev hardhat
npx hardhat
Choose: “Create a basic sample project”
π Step 2: Write the NFT Smart Contract
Use OpenZeppelin to quickly build a secure ERC-721 contract.
Install OpenZeppelin:
npm install @openzeppelin/contracts
NFT.sol — Your NFT Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract NFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("MyNFT", "MNFT") {}
function mintNFT(string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
}
π Step 3: Write the Marketplace Smart Contract
Marketplace.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IERC721 {
function transferFrom(address from, address to, uint256 tokenId) external;
}
contract NFTMarketplace {
struct Listing {
address seller;
uint256 price;
}
mapping(address => mapping(uint256 => Listing)) public listings;
function listItem(address nftAddress, uint256 tokenId, uint256 price) public {
IERC721(nftAddress).transferFrom(msg.sender, address(this), tokenId);
listings[nftAddress][tokenId] = Listing(msg.sender, price);
}
function buyItem(address nftAddress, uint256 tokenId) public payable {
Listing memory item = listings[nftAddress][tokenId];
require(msg.value == item.price, "Incorrect price");
delete listings[nftAddress][tokenId];
payable(item.seller).transfer(msg.value);
IERC721(nftAddress).transferFrom(address(this), msg.sender, tokenId);
}
}
π§ͺ Step 4: Compile and Deploy
Compile contracts:
npx hardhat compile
Deploy Script (scripts/deploy.js)
const hre = require("hardhat");
async function main() {
const NFT = await hre.ethers.getContractFactory("NFT");
const nft = await NFT.deploy();
await nft.deployed();
console.log("NFT Contract deployed to:", nft.address);
const Marketplace = await hre.ethers.getContractFactory("NFTMarketplace");
const marketplace = await Marketplace.deploy();
await marketplace.deployed();
console.log("Marketplace Contract deployed to:", marketplace.address);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Run deployment:
npx hardhat run scripts/deploy.js --network hardhat
Or use --network goerli if you want to deploy to testnet.
π Step 5: Build the Frontend (React)
Set up React:
npx create-react-app client
cd client
npm install ethers web3modal
Connect MetaMask & Contracts
Use ethers.js to connect to the blockchain and interact with your smart contracts.
Create a file like utils/contracts.js to store contract ABIs and addresses.
πΈ Step 6: Add NFT Storage
Use Pinata
or NFT.Storage
to upload:
Image
Metadata JSON
Return the tokenURI and pass it to mintNFT(tokenURI) when minting.
π§π» Step 7: Implement Frontend Features
Feature Description
Mint NFT Upload image + metadata to IPFS, call mintNFT
List NFT Approve and call listItem on Marketplace
Buy NFT Display listed NFTs and call buyItem with correct ETH
My NFTs Query owned NFTs using tokenOfOwnerByIndex or balanceOf + tokenURI
Use React components to display NFTs with images and metadata.
✅ Step 8: Test It!
Test in local Hardhat network or deploy to Goerli.
Connect MetaMask, mint a few NFTs, list them, and try buying from another account.
π§ Bonus Ideas
Add filtering/search features
Add auction or bidding system
Enable royalties
Connect with third-party wallets (WalletConnect)
Make it mobile-friendly
π¦ Final Notes
This is a minimal NFT marketplace clone to help you learn:
Smart contract development
Web3 integration
NFT minting and trading
For a production-ready app, you’d need user authentication, metadata pinning, gas optimizations, and security audits.
Learn Blockchain Course in Hyderabad
Read More
Create a Personal Blockchain on Python
Build a Decentralized To-Do List App
Learning Path: From Beginner to Blockchain Pro
From Web2 to Web3: What Developers Should Know
Comments
Post a Comment