A decentralized NFT marketplace built with Solidity, leveraging the latest smart contract development practices and tools. This marketplace enables users to mint, list, buy, sell, and auction NFTs with support for royalties and marketplace fees.
-
NFT Management
- Mint single and batch NFTs
- Built-in royalty support (ERC2981)
- Token URI management
- Burn functionality
-
Marketplace Operations
- List NFTs for direct sale
- Create timed auctions
- Place and withdraw bids
- Update listing prices
- Cancel listings
- Withdraw proceeds
- Clone the repository:
git clone git@github.com:am-miracle/nftmarketplace.git
cd nftmarketplace- Install Foundry dependencies:
forge install- Install Node.js dependencies:
npm install- ERC721 implementation with ERC2981 royalty support
- Supports single and batch minting
- Configurable base URI
- Built-in royalty management
- Core marketplace functionality
- Supports direct sales and auctions
- Handles royalty distribution
- Manages marketplace fees
- Tracks bid history
Run the test suite:
forge testRun tests with gas reporting:
forge test --gas-reportRun tests with verbosity:
forge test -vvv- Set up environment variables:
cp .env.example .envEdit .env with your configuration:
PRIVATE_KEY=your_private_key
ETHERSCAN_API_KEY=your_etherscan_api_key
SEPOLIA_RPC_URL=your_sepolia_rpc_url- Deploy to testnet (Sepolia):
forge script script/Deploy.s.sol:Deploy --rpc-url $SEPOLIA_RPC_URL --broadcast --verify -vvvv- Sepolia Testnet:
- NFTCollection:
0x2e70EBbefc792bB3b842363771F7a3d0E3223721 - NFTMarketplace:
0x15053f4bf121a516C5454D9A69638B281907Fe71
- NFTCollection:
// 1. Approve the marketplace
IERC721(nftAddress).setApprovalForAll(marketplaceAddress, true);
// 2. Mint NFT with royalty
nftCollection.mint(recipient, tokenURI, royaltyFee);// List for direct sale
marketplace.listItem(nftAddress, tokenId, price, false);
// List for auction
marketplace.listItem(nftAddress, tokenId, startingPrice, true);// Place bid with ETH
marketplace.placeBid{value: bidAmount}(nftAddress, tokenId);- Fork the repository
- Create your feature branch:
git checkout -b feature/my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature/my-new-feature - Submit a pull request
The contracts implement several gas optimization techniques:
- Efficient storage packing
- Minimal storage operations
- Use of unchecked blocks where safe
- Optimal struct packing
All error conditions are handled with custom errors for better gas efficiency and clearer error messages:
error NFTMarketplace__PriceMustBeAboveZero();
error NFTMarketplace__NotApprovedForMarketplace();
error NFTMarketplace__AlreadyListed();
// ... etcKey events are emitted for all important state changes:
event ItemListed(address indexed seller, address indexed nftAddress, uint256 indexed tokenId, uint256 price, bool isAuction);
event ItemBought(address indexed buyer, address indexed nftAddress, uint256 indexed tokenId, uint256 price);
// ... etcFor support, please open an issue in the repository.
// Layout of Contract: // version // imports // errors // interfaces, libraries, contracts // Type declarations // State variables // Events // Modifiers // Functions
// Layout of Functions: // constructor // receive function (if exists) // fallback function (if exists) // external // public // internal // private // view & pure functions