Skip to content

About

ERC-721 NFT marketplace with batch minting, royalties and a PostgreSQL event indexer, on a local Hardhat chain.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

NFT Trading Platform

ERC-721 NFT marketplace with batch minting, royalties and a PostgreSQL event indexer, on a local Hardhat chain.

CI

Marketplace demo: connect a wallet, buy an NFT, resell it, then the live feed and analytics, on a local Hardhat chain

The real marketplace UI on a local Hardhat chain, driven through a scripted stand-in for MetaMask. It shows connect, buy, resell, live activity feed and analytics. Silent, 37 seconds. Full quality mp4. How it was recorded is in docs/DEMO_RECORDING.md.

Creators mint single or batch NFTs with IPFS metadata. They list them on a marketplace contract that pays royalties and a platform fee. A PostgreSQL event indexer feeds a REST API and a Socket.IO feed, and both frontends read them. It runs on a local Hardhat chain. Its security notes list a professional audit as the step before any public network.

What is in it

Creator dashboard

  • Single and batch minting, up to 20 NFTs in one transaction. The contract enforces the cap.
  • Image and metadata upload to IPFS through Pinata. It needs a PINATA_JWT. Without one the dashboard shows "IPFS Not Configured" and the upload returns an error.
  • Default royalty of 2.5% or a per-token royalty up to 10%, through ERC-2981.
  • Creator analytics and a list of your creations, with direct listing after minting.

Marketplace

  • Browse, search by token ID or name, and filter by price and status.
  • Live activity feed over WebSocket.
  • Buy, list, cancel and re-price with MetaMask.
  • Analytics charts for volume, price distribution and top traders, plus per-NFT price history. Recharts draws them.

Remix web client (web-remix/)

  • A second client for the same API, built on React Router framework mode (what Remix is now) in strict TypeScript.
  • Loaders read the API and filter by search text and price range from the URL, so the listings page works with JavaScript off.
  • The NFT page answers 404 for an unknown token and 503 when the backend is unreachable.
  • A "watch this NFT" form posts to an action that validates on the server and returns field errors.
  • It runs and is tested against the local Docker stack with sample data. Run and test steps: docs/WEB_REMIX.md.

Contracts (contracts/)

  • GameNFT.sol: ERC-721 with URI storage and ERC-2981 royalties on OpenZeppelin 5.4.0, owner-only minting.
  • Marketplace.sol: listing, buying, cancelling and price updates. It takes a 2.5% platform fee capped at 10%, pays out royalties and refunds overpayment. A reentrancy guard covers buyNFT and withdrawFees.
  • 62 passing Hardhat tests: 27 for GameNFT and 35 for Marketplace.

Backend (backend/)

  • An event indexer that reads contract events into PostgreSQL. Every chain read is retried with backoff, so one dropped RPC request does not skip a block range.
  • Analytics responses are cached in memory for 15 seconds. The cache clears whenever the indexer processes new events, and a burst of identical requests shares one database query.
  • 25 REST endpoints under /api (6 NFT, 10 marketplace, 9 analytics) plus /health.
  • A Socket.IO server with room-based broadcasting.
  • 5 PostgreSQL tables and 10 indexes (backend/migrations/001_nft_tables.sql), read and written through Prisma 7 (see Data layer).
  • CI runs 18 backend unit tests, 74 database tests against a real PostgreSQL, a TypeScript typecheck and a build. The unit tests cover the cache, the retry helper and the price range check.

Quick start

You need Node.js 22.12 or newer, Docker Desktop and PowerShell. A Pinata account is optional and only needed for IPFS uploads.

git clone https://github.com/Exalt24/NFT-Trading.git
cd NFT-Trading
.\scripts\docker-up.ps1

The script builds the images and starts PostgreSQL and a Hardhat node. It deploys the contracts and copies the fresh ABIs into both frontends and the backend. It writes a root .env with the deployed contract addresses, which docker-compose reads. Then it starts the backend and both frontends and runs the database migration. The first run takes a few minutes. Running it again wipes the containers and the database volume and starts from a fresh chain.

Service URL
Marketplace http://localhost:3002
Creator dashboard http://localhost:3003
Backend API http://localhost:4001/api
Health check http://localhost:4001/health
Hardhat RPC http://localhost:8546
PostgreSQL localhost:55432

The ports differ from the usual defaults (3000, 3001, 4000, 5432, 8545). That keeps the stack from colliding with other projects. Inside Docker the standard ports are used.

To get IPFS uploads in the creator dashboard, put PINATA_JWT= and PINATA_GATEWAY= in the root .env (see .env.docker.example) before running the script. The dashboard reads the token as VITE_PINATA_JWT, which ends up in the browser bundle. Use a throwaway key meant for local testing and never commit the file.

Seed demo data

An empty marketplace has nothing to browse, so scripts/seed-demo.ts fills it:

cd scripts
npm install
npx tsx seed-demo.ts

It mints 13 generated-art NFTs from Hardhat account #0. It lists 8 of them at prices from 0.15 to 2.0 ETH, and account #1 buys three. Without a PINATA_JWT it embeds each NFT's metadata and SVG art in the token URI as data: URIs. That needs no external account and no network, and the backend indexer decodes those. With PINATA_JWT set in your shell it uploads to IPFS instead. Run it once per fresh stack, since docker-up.ps1 resets the chain. RPC_URL, NFT_CONTRACT_ADDRESS and MARKETPLACE_CONTRACT_ADDRESS override its defaults (http://localhost:8546 and the two addresses a fresh Hardhat node produces).

The full stack is supported through Docker only. Running the four services by hand needs the same ABI copy, address wiring and migration steps. It has not been re-verified, so it is not documented here.

Using it

MetaMask setup and the buy, list and mint steps are in docs/USING.md.

Project structure

Contracts, backend, the two Vite frontends, the Remix client, tests, scripts, docs and Docker files each have their own folder. The full tree with a note per folder is in docs/STRUCTURE.md.

Tech stack

Layer Technology
Contracts Solidity 0.8.30, Hardhat 3.0.7, OpenZeppelin 5.4.0, ethers.js 6.15
Backend Node.js 22, Express 5.1, PostgreSQL 18, Prisma 7, Socket.IO 4.8, Pinata SDK
Frontend React 19.2, Vite 7.1, Tailwind CSS 4.1, Recharts 3.3
Remix client React Router 8.5 framework mode, Tailwind CSS 4, Playwright
Language TypeScript 5.9
Container Docker

Data layer

The backend reaches PostgreSQL through Prisma 7 (backend/prisma/schema.prisma). The tables are still built by backend/migrations/001_nft_tables.sql (npm run migrate). Migrations stay in that SQL file, and npm run db:drift fails when the schema file and the migrated database disagree. Service methods keep their signatures and snake_case row types, so the routes did not change.

Of the 36 data-access methods (counted 2026-10-22), 18 use Prisma Client only and 4 mix it with raw SQL. The other 14 are raw, written as $queryRaw or $executeRaw tagged templates with bound parameters. Raw stays for four reasons: text wei prices that need a numeric cast, SQL Prisma has no syntax for, INSERT ... ON CONFLICT, and the multi-statement migration runner. They are listed in docs/DATA_LAYER.md.

docker compose up -d --wait postgres      # PostgreSQL 18 on host port 55432
cd backend
npm install
$env:DATABASE_URL = "postgresql://postgres:postgres@localhost:55432/nft_marketplace"
npm run migrate                           # apply the SQL migration
npm run db:generate                       # build the Prisma Client
npm run db:drift                          # schema file against the database
npm test                                  # 18 unit tests, no database needed
npm run test:db                           # 74 tests on a rebuilt _test database
npm run test:db:mutation                  # breaks 15 guards one at a time, expects red each time

On 2026-10-22, after the port, the typecheck was clean. 18 unit tests and 74 database tests passed (74 in 5.5 seconds), and 15 of 15 mutations were caught. CI runs them against a Postgres service. Each database test file opens with positive controls. On 2026-10-22 the floor price, price range filter, marketplace most expensive sales and price distribution moved to numeric comparison. 10 ETH no longer sorts below 9 ETH. Known limits are in docs/DATA_LAYER.md.

Testing

Backend unit tests and typecheck

cd backend
npm install
npm test            # 18 tests: the TTL cache (expiry, shared computation, failures are not cached), the retry helper and the price range check
npm run typecheck   # tsc --noEmit
npm run build

These need no database or chain. The database tests are described under Data layer. The indexer and the REST routes are covered by the integration scripts below, which need the Docker stack.

Contract tests

cd contracts
npm install
npx hardhat test

On 2026-10-11 this printed 62 passing (run inside the Hardhat container): 27 in GameNFT.test.ts and 35 in Marketplace.test.ts. Add --gas-report for gas figures, or --grep "Should allow claiming" to run one test.

Integration scripts

.\scripts\docker-up.ps1
cd scripts
npm run test-all

test-all runs four scripts from tests/integration/ against the running stack. The mint flow covers contract, IPFS, backend and API. The marketplace flow covers list, buy and ownership transfer. The WebSocket flow covers room subscriptions and event broadcasting. The analytics flow covers platform statistics. They are scripts, not a test-framework suite. The four scripts hold about 43 assert calls between them, and the end-to-end script adds about 18 more. All of them need the running Docker stack. The mint flow uploads to IPFS, so it expects Pinata to be configured. These were not re-run when the demo was recorded.

tests/e2e/full-stack.test.ts (npx tsx tests/e2e/full-stack.test.ts) runs a longer lifecycle across mints, listings, purchases, analytics and WebSocket events.

Stack verification

npx tsx scripts/verify-stack.ts

This checks the PostgreSQL container and schema, the backend health endpoint, the deployed contract addresses and that both frontends answer over HTTP.

Everyday commands

Start, stop, logs, backend restart, ABI copy and a psql shell are in docs/COMMANDS.md. The steps to run after editing a contract are there too.

Troubleshooting

The fixes for the common failures are in docs/TROUBLESHOOTING.md. They cover services that will not start, backend not responding, contracts not deployed, MetaMask rejecting transactions after a restart and IPFS upload failing.

Security status

What the code does:

  • Contracts use OpenZeppelin libraries, owner-only minting and fee updates, and input checks. The checks cover zero price, zero address, empty URI, batch size, and royalty and fee caps. A reentrancy guard covers buyNFT and withdrawFees. Solidity 0.8.30 gives overflow checks.
  • The backend sends every value to PostgreSQL as a bound parameter, through Prisma Client or a tagged $queryRaw template. One query used to paste the day count into its SQL text, and now binds it.

Open items:

  • Rate limiting for the REST API and the WebSocket server.
  • CORS defaults to * unless CORS_ORIGIN is set.
  • The creator dashboard receives the Pinata token as a browser-side variable, so anyone who can open the dashboard can read it.
  • The contracts are written for test accounts and a local chain, and an audit comes before any public network.

docs/SECURITY.md has the full checklist, including the open items. Get the contracts audited before putting them on a public network, and keep private keys and real environment files out of the repository.

Documentation

Next steps

  • A shared store for the cache if the backend runs as several instances (today it is in memory in one process).
  • Replaying a block range whose chain reads still fail after three attempts (today it is logged and skipped).
  • A public deployment on a testnet or mainnet.
  • Rate limiting.
  • A contract audit.
  • Frontend unit tests (the integration scripts run against the whole Docker stack).
  • Auctions, offers and bids, collections, multi-chain support and lazy minting.

License

MIT. See LICENSE.

About

ERC-721 NFT marketplace with batch minting, royalties and a PostgreSQL event indexer, on a local Hardhat chain.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages