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

sequenceDiagram
  autonumber
  actor Seller
  actor Buyer
  participant M as Marketplace
  participant NFT as ERC-721
  Seller->>M: listNft(nft, id, price)
  M-->>M: listings[nft][id] = Listing
  M-->>Seller: emit NftListed(seller, nft, id, price)

  Buyer->>M: buyNft(nft, id) { value: price }
  M-->>M: delete listings[nft][id]
  M->>NFT: safeTransferFrom(seller → buyer)
  M-->>Seller: ETH transfer
  M-->>Buyer: emit NftSold(buyer, seller, nft, id, price)

  Seller->>M: cancelListing(nft, id)
  M-->>M: delete listings[nft][id]
  M-->>Seller: emit CanceledListing(nft, id)

Security

  • ReentrancyGuard — all three state-changing functions are guarded by OpenZeppelin’s nonReentrant modifier.
  • Checks-Effects-Interactions — buyNft 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%