NFT-Collection

View on GitHub


NFT-Collection is a trustless NFT marketplace built in Solidity. Any ERC-721 token can be listed, bought, or delisted — the contract holds no funds or NFTs at any point. Payment flows directly from buyer to seller and the NFT transfers atomically in the same transaction.

Built using Foundry with Solidity 0.8.34, leveraging OpenZeppelin’s ERC-721, ReentrancyGuard, and Ownable contracts.

Architecture

NFT-Collection Contract Architecture

Functions

FunctionAccessDescription
listNft(nftAddress, tokenId, price)PublicList an ERC-721 token for sale at a given price
buyNft(nftAddress, tokenId)PublicBuy a listed NFT by sending the exact price in ETH
cancelListing(nftAddress, tokenId)SellerRemove an active listing

Flow

User ──► listNft()       ── listings[nft][id] = Listing ──► NftListed(seller, nft, id, price)
User ──► buyNft()        ── delete listing ──► safeTransferFrom ──► ETH to seller ──► NftSold(...)
User ──► cancelListing() ── delete listing ──────────────────────────────────────────► CanceledListing(nft, id)

Security

  • ReentrancyGuard — all three state-changing functions are guarded by OpenZeppelin’s nonReentrant modifier.
  • Checks-Effects-InteractionsbuyNft deletes the listing from storage before any external calls, providing a second layer of reentrancy protection.
  • Ownable — deployer is set as owner via Ownable(msg.sender) for future administrative functions.

Test Coverage

File% Lines% Statements% Branches% Funcs
src/NFTCollection.sol100%100%91.67%100%
test/NFTCollection.t.sol100%100%100%100%