Skip to content

About

NFT collection minter + factory: phased allowlist/public mint, ERC2981 royalties, reveal lock, bounded protocol fee

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

NFT Launchpad

An ERC-721 collection minter and factory for creators running a phased drop: a Merkle-gated allowlist phase, then a public phase, with a team reserve, ERC-2981 royalties, and revealable metadata that can be permanently locked. Built on OpenZeppelin v5, Solidity 0.8.26.

Why this over a generic minter

The edge is in the parts that usually go wrong. Each claim below is enforced in code and covered by a test; none rely on off-chain trust.

  • The allowlist leaf is second-preimage-safe by construction. A leaf is keccak256(abi.encodePacked(msg.sender)) (a 20-byte preimage) or, for the allocation path, keccak256(abi.encodePacked(msg.sender, allocation)) (20 + 32 = 52 bytes). OpenZeppelin's MerkleProof hashes internal nodes as keccak256(abi.encode(a, b)) over two bytes32 values — a fixed 64-byte preimage. Because 20 and 52 can never equal 64, no internal node can be replayed as a leaf. There is no way to forge membership by presenting an intermediate hash.

  • A stolen proof is useless to anyone else. Both allowlist entry points build the leaf from msg.sender, not from a caller-supplied address. A leaked proof only mints for the wallet it was issued to; presenting someone else's proof reverts with InvalidProof.

  • The per-wallet cap holds across allowlist + public + multiple transactions. A single mintedBy[wallet] counter gates every paid mint on both phases, checked on each call. Splitting a purchase across transactions or hopping phases cannot exceed maxPerWallet — verified by a test that fills the cap across several allowlist txs and confirms the next one reverts.

  • minted <= maxSupply always. Every mint path re-checks totalMinted + quantity <= maxSupply. A fuzzed invariant asserts the cap is never crossed across randomized sequences of mints and withdrawals.

  • Proceeds are conserved: owner + fee == paid. ETH held is tracked in an explicit proceeds counter. withdraw() splits only accounted proceeds — a bounded protocol fee to the factory's fee recipient, the remainder to the owner — and zeroes the counter first. A fuzzed invariant asserts owner.balance + feeRecipient.balance + proceeds == totalPaid and that the contract's on-chain balance always equals proceeds.

  • Reentrancy-safe. allowlistMint, allowlistMintWithAllocation, publicMint, and withdraw are nonReentrant and follow checks-effects-interactions. ownerMint is onlyOwner and not guarded, so _issue advances totalMinted before the _safeMint loop — a malicious receiver that re-enters ownerMint from its onERC721Received hook already sees the final supply and cannot mint past the cap. Both cases have regression tests.

  • Reveal, then lock. The owner can update baseURI to reveal art, then call lockBaseURI() to make it permanent; any later setBaseURI reverts.

  • No admin path to funds beyond accounted proceeds. The owner can move phases, reveal, and withdraw, but withdrawal only ever pays out the tracked proceeds. There is no function that mints ETH to the owner or drains more than was paid in.

What it does

Phases. Closed -> Allowlist -> Public, advanced only by the collection owner via setPhase. Minting in the wrong phase reverts with WrongPhase.

  • Allowlist mint (allowlistMint) — Merkle-gated at allowlistPrice, exact ETH required.
  • Public mint (publicMint) — same cap / supply / exact-payment rules at publicPrice.
  • Owner mint (ownerMint) — free team reserve, bounded by maxSupply, no per-wallet cap.

v2 additions.

  • Allocation-in-leaf allowlist (allowlistMintWithAllocation) — the leaf commits to a per-wallet allocation (keccak256(abi.encodePacked(msg.sender, allocation))), bounding that wallet's allowlist mints to its committed allocation in addition to the global maxPerWallet and maxSupply. It uses the same immutable root; a collection is configured with one leaf format, and the non-matching entry point simply fails proof verification.
  • ERC-4906 — setBaseURI emits BatchMetadataUpdate(1, totalMinted) on reveal so marketplaces refresh metadata, and supportsInterface reports 0x49064906.

Royalties. ERC-2981 default receiver and bps, set at construction.

Fee. Each collection is deployed by the factory wired to a protocolFeeBps (capped at 10%) and a feeRecipient. On withdraw(), that bps cut of proceeds routes to the fee recipient, the rest to the owner. The factory can also charge an optional flat creation fee (capped at 1 ETH). Fee parameters are stamped into each collection at deploy time and are immutable thereafter — later factory changes do not affect existing collections.

Security & testing

60 tests pass under forge test, across five suites: phase gating, allowlist proof validity and per-wallet caps, the allocation path (valid, over-allocation, forged allocation, non-allowlisted, global-cap interaction, cross-format rejection), public supply/cap/exact-payment, royalties, reveal + lock, exact withdraw split, ERC-4906 emission and interface, reentrancy on mint and withdraw, receiver-reentrancy on the unguarded ownerMint path, and factory deploy/index/fee wiring. The in-test Merkle tree uses the same leaf hashing as the contract.

Three of those are fuzzed invariants (forge invariant runs, runs = 64, depth = 200):

  • invariant_totalMintedWithinMaxSupply — totalMinted <= maxSupply.
  • invariant_ethConservation — owner.balance + feeRecipient.balance + proceeds == totalPaid.
  • invariant_balanceEqualsProceeds — the contract's ETH balance equals its accounted proceeds.

Gas, from forge test --gas-report in this repo (single-item mints; medians are pushed up by the invariant handler's large batches):

Function Min gas
allowlistMint 30,127
allowlistMintWithAllocation 30,413
publicMint 28,698
withdraw 23,573

The v2 pass was a manual review of every path plus targeted regression tests for the highest-risk cases (leaf forgery, cap-splitting, receiver reentrancy on ownerMint); no mutation testing or formal verification was run.

Usage

import {NftLaunchpadFactory} from "src/NftLaunchpadFactory.sol";
import {NftCollection} from "src/NftCollection.sol";

// 1. Deploy the factory: owner, protocol fee (bps, <= 1000), fee recipient, flat creation fee.
NftLaunchpadFactory factory = new NftLaunchpadFactory(admin, 250, feeRecipient, 0.1 ether);

// 2. A creator deploys a collection (pays the flat creation fee as msg.value).
NftCollection.MintConfig memory cfg = NftCollection.MintConfig({
    allowlistPrice: 0.05 ether,
    publicPrice: 0.08 ether,
    allowlistMerkleRoot: root,          // keccak256(abi.encodePacked(addr)) leaves
    maxPerWallet: 3
});
address collection = factory.createCollection{value: 0.1 ether}(
    "My Collection", "MYC",
    10_000,                              // maxSupply
    royaltyReceiver, 500,                // ERC-2981 receiver, 5% royalty
    "ipfs://placeholder/",               // pre-reveal base URI
    cfg
);
NftCollection nft = NftCollection(collection);

// 3. The creator (collection owner) opens the allowlist phase.
nft.setPhase(NftCollection.Phase.Allowlist);

// 4. An allowlisted wallet mints with its proof (exact ETH = allowlistPrice * quantity).
nft.allowlistMint{value: 0.05 ether}(1, proof);
// or, with a per-wallet allocation committed in the leaf:
nft.allowlistMintWithAllocation{value: 0.10 ether}(2, 3, allocProof);

// 5. Owner moves to public; anyone mints at publicPrice.
nft.setPhase(NftCollection.Phase.Public);
nft.publicMint{value: 0.08 ether}(1);

// 6. Reveal, then optionally lock, then withdraw the proceeds split.
nft.setBaseURI("ipfs://revealed/");
nft.lockBaseURI();
nft.withdraw();

Build

forge build
forge test

License

MIT. This is unaudited software provided as-is; review it and test on a testnet before any mainnet deployment.

About

NFT collection minter + factory: phased allowlist/public mint, ERC2981 royalties, reveal lock, bounded protocol fee

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages