Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NFT Marketplace

A decentralized marketplace for ERC-721 NFTs built with Solidity, OpenZeppelin, and Foundry.
The marketplace enables NFT owners to list their assets for sale, buyers to purchase listed NFTs, and sellers to cancel active listings in a secure, trustless manner.

Solidity Foundry OpenZeppelin License


Features

  • ERC-721 Marketplace
  • List NFTs for sale
  • Buy listed NFTs
  • Cancel active listings
  • Ownership validation
  • Marketplace approval verification
  • Secure NFT transfers
  • Event emission for marketplace actions
  • Comprehensive unit testing with Foundry

Architecture

                     ┌─────────────────────────┐
                     │      NFT Owner          │
                     └──────────┬──────────────┘
                                │ approve()
                                ▼
                     ┌─────────────────────────┐
                     │    ERC721 Collection    │
                     └──────────┬──────────────┘
                                │
                                │ transferFrom()
                                ▼
                 ┌────────────────────────────────┐
                 │        NFT Marketplace         │
                 │────────────────────────────────│
                 │                                │
                 │ Listing[]                      │
                 │                                │
                 │ • seller                       │
                 │ • nftAddress                   │
                 │ • tokenId                      │
                 │ • price                        │
                 │                                │
                 └───────┬─────────────┬──────────┘
                         │             │
              listNFT()  │             │ buyNFT()
                         │             │
                         ▼             ▼
                  Listing Stored   ETH Payment
                         │             │
                         └──────┬──────┘
                                ▼
                     NFT transferred to buyer

Marketplace Workflow

Seller
   │
   │
   ├── Mint NFT
   │
   ├── Approve Marketplace
   │
   ├── List NFT
   │
   ▼
Marketplace
   │
   ├── Validate owner
   ├── Validate approval
   ├── Store listing
   │
   ▼
Buyer
   │
   ├── Buy NFT
   │
   ▼
Marketplace
   │
   ├── Validate payment
   ├── Transfer NFT
   ├── Transfer ETH
   ├── Remove listing
   │
   ▼
Transaction Completed

Project Structure

.
├── src/
│   ├── NFTMarketplace.sol
├── test/
│   └── NFTMarketplace.t.sol
├── lib/
├── foundry.toml
└── README.md

Technology Stack

  • Solidity
  • Foundry
  • Forge
  • Anvil
  • Cast
  • OpenZeppelin ERC721

Smart Contract Design

Listing Structure

struct Listing {
    address seller;
    address nftAddress;
    uint256 tokenId;
    uint256 price;
}

The marketplace stores all active NFT listings using this structure.


Core Functions

List NFT

Lists an NFT for sale.

Requirements

  • Caller must own the NFT
  • Marketplace must be approved
  • Price must be greater than zero
  • NFT cannot already be listed

Example

listNFT(
    nftAddress,
    tokenId,
    price
);

Buy NFT

Purchases a listed NFT.

Requirements

  • Listing must exist
  • Buyer must send enough ETH
  • NFT ownership is transferred
  • Seller receives payment
  • Listing is removed

Example

buyNFT{value: 1 ether}(
    nftAddress,
    tokenId
);

Cancel Listing

Removes an NFT from sale.

Requirements

  • Only the seller can cancel
  • Listing must exist

Example

cancelListing(
    nftAddress,
    tokenId
);

Events

event NFTListed(
    address indexed seller,
    address indexed nftAddress,
    uint256 indexed tokenId,
    uint256 price
);

event NFTSold(
    address indexed buyer,
    address indexed nftAddress,
    uint256 indexed tokenId,
    uint256 price
);

event ListingCancelled(
    address indexed seller,
    address indexed nftAddress,
    uint256 indexed tokenId
);

Installation

Clone the repository

git clone https://github.com/<username>/nft-marketplace.git

cd nft-marketplace

Install dependencies

forge install

Compile

forge build

Running Tests

Run every test

forge test

Verbose output

forge test -vvvv

Gas report

forge test --gas-report

Coverage

forge coverage

Deployment

Start a local blockchain

anvil

Deploy

forge script script/DeployNFTMarketplace.s.sol \
    --rpc-url http://127.0.0.1:8545 \
    --private-key <PRIVATE_KEY> \
    --broadcast

Test Coverage

The project includes unit tests for:

  • Contract deployment
  • NFT listing
  • Duplicate listings
  • Invalid price
  • Owner validation
  • Marketplace approval
  • Successful purchase
  • Incorrect payment
  • Seller payment
  • NFT ownership transfer
  • Cancel listing
  • Unauthorized cancellation
  • Revert conditions
  • Event emission

Security Considerations

The marketplace validates:

  • NFT ownership
  • Marketplace approval
  • Existing listings
  • Correct payment amount
  • Authorized cancellation
  • Secure ERC721 transfers
  • State updates before external interactions

Future Improvements

  • Marketplace fees
  • Royalties (ERC-2981)
  • Offers & bidding
  • Auctions
  • Collection whitelisting
  • Batch listings
  • Purchase with ERC20
  • Signature-based listings
  • Upgradeable contracts
  • EIP-712 support
  • Off-chain order book

Useful Commands

Build

forge build

Run tests

forge test

Gas report

forge test --gas-report

Coverage

forge coverage

Format

forge fmt

Clean artifacts

forge clean

Start local node

anvil

Interact with contracts

cast

License

Distributed under the MIT License.


Acknowledgements

  • OpenZeppelin Contracts
  • Foundry
  • Ethereum Foundation

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages