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.
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'sMerkleProofhashes internal nodes askeccak256(abi.encode(a, b))over twobytes32values — 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 withInvalidProof. -
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 exceedmaxPerWallet— verified by a test that fills the cap across several allowlist txs and confirms the next one reverts. -
minted <= maxSupplyalways. Every mint path re-checkstotalMinted + 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 explicitproceedscounter.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 assertsowner.balance + feeRecipient.balance + proceeds == totalPaidand that the contract's on-chain balance always equalsproceeds. -
Reentrancy-safe.
allowlistMint,allowlistMintWithAllocation,publicMint, andwithdrawarenonReentrantand follow checks-effects-interactions.ownerMintisonlyOwnerand not guarded, so_issueadvancestotalMintedbefore the_safeMintloop — a malicious receiver that re-entersownerMintfrom itsonERC721Receivedhook already sees the final supply and cannot mint past the cap. Both cases have regression tests. -
Reveal, then lock. The owner can update
baseURIto reveal art, then calllockBaseURI()to make it permanent; any latersetBaseURIreverts. -
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.
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 atallowlistPrice, exact ETH required. - Public mint (
publicMint) — same cap / supply / exact-payment rules atpublicPrice. - Owner mint (
ownerMint) — free team reserve, bounded bymaxSupply, no per-wallet cap.
v2 additions.
- Allocation-in-leaf allowlist (
allowlistMintWithAllocation) — the leaf commits to a per-walletallocation(keccak256(abi.encodePacked(msg.sender, allocation))), bounding that wallet's allowlist mints to its committed allocation in addition to the globalmaxPerWalletandmaxSupply. 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 —
setBaseURIemitsBatchMetadataUpdate(1, totalMinted)on reveal so marketplaces refresh metadata, andsupportsInterfacereports0x49064906.
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.
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 accountedproceeds.
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.
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();forge build
forge test
MIT. This is unaudited software provided as-is; review it and test on a testnet before any mainnet deployment.