How to Build NFT Marketplace Smart Contracts
An NFT marketplace runs on a small set of contracts for minting, listing, escrow and royalties. Here is what each contract does and how to build them safely.
On this page
Short answer
NFT marketplace smart contracts are the on-chain programs that mint tokens, list them for sale, hold payment in escrow until the trade completes, and split the sale price between the seller, the marketplace and the creator. They replace the trusted middleman with code that anyone can read and verify.
What are NFT marketplace smart contracts?
NFT marketplace smart contracts are the programs that make buying and selling NFTs work without a central company holding the assets. When a seller lists an NFT, a contract records the price and the terms. When a buyer pays, the contract moves the payment and the token at the same time, so neither side has to trust the other.
The marketplace itself is usually a front end and a database that shows listings and search results. The contracts are the part that actually moves value, and they are what users and auditors judge. A marketplace with weak contracts can lose user funds even if its website looks polished.
Most marketplaces use a small set of contracts rather than one large one. Splitting the work makes each contract easier to test, easier to audit and easier to upgrade, since a bug in the auction logic does not touch the minting logic. That split is the first design decision a team makes. The wider build is described in how to develop an NFT marketplace.
Which contracts does a marketplace need?
A marketplace needs contracts for the token itself, the listing, the payment and the creator's share. Each one has a single job, and together they cover the full trade.
| Contract | What it does | Key risk |
|---|---|---|
| Token contract | Mints and owns the NFTs | Unlimited minting |
| Listing contract | Records price, seller and expiry | Stale or fake listings |
| Escrow contract | Holds payment until the trade settles | Funds stuck on failure |
| Royalty contract | Splits the sale price to the creator | Unpaid or wrong shares |
| Auction contract | Takes bids and settles the winner | Last-second bid games |
The token contract is usually an existing standard rather than custom code, which is the safest choice. The listing and escrow contracts are where most of the custom logic lives, because they define how a sale is agreed and how the money moves. Royalties are the trickiest part, because the standard for enforcing them on chain is still settling.
Teams should decide early whether royalties are enforced by the contract or honoured by the front end.
Which token standards should you use?
Most marketplaces use ERC-721 for unique items and ERC-1155 for editions and game items, and the choice shapes the contracts you write. ERC-721 gives each token its own identity, which fits art and collectibles where every item is different. ERC-1155 lets one contract hold many token types and supports batch transfers, which fits game items and limited editions.
Using a well-tested standard library matters more than the choice between the two. Libraries such as OpenZeppelin provide token contracts that have been reviewed by many teams, and building on them avoids the most common bugs in minting, transfers and approvals. Writing a token contract from scratch is rarely worth the risk.
Two details deserve care. First, the metadata that describes each token usually lives off chain, so the contract should point to a stable location and the team should plan what happens if that location changes. Second, approvals matter: a marketplace contract should only be able to move a token the owner has approved, and the approval should be limited to that marketplace rather than open-ended.
How do you build the marketplace contracts?
You build the marketplace contracts by defining the trade flow, writing the contracts one at a time, testing each one, and then testing the whole flow together before any real money moves.
- Map the trade flow. Write down each step of a sale, a bid and a cancellation, including what happens when a step fails halfway.
- Start with the token contract. Use a standard library, set the minting rules, and limit who can mint.
- Write the listing contract. Record the seller, the token, the price and the expiry, and let the seller cancel a listing at any time.
- Write the escrow and settlement logic. Hold the buyer's payment, move the token, then release the payment in one atomic step.
- Add royalties and fees. Split the payment between seller, creator and marketplace in the same transaction as the sale.
- Test the whole flow. Run buys, bids, cancellations and failures on a test network, including attempts to cheat the contracts.
- Audit before launch. Get an independent review of the contracts and fix the findings before real funds are involved.
The escrow step is where most marketplace bugs live, because it handles money and tokens together. The rule is simple: the token and the payment must move in the same transaction, or not at all. If a sale can leave the buyer with the token but the seller unpaid, the contract is broken. The general approach is covered in smart contract development.
How do royalties and fees work in the contracts?
Royalties and fees are splits of the sale price that the settlement contract applies when a trade completes. A typical sale divides the price three ways: the seller keeps most of it, the creator receives a royalty share, and the marketplace keeps a fee. The contract calculates each share and sends them in the same transaction as the token transfer.
Royalties are the hard part. The original royalty standard, EIP-2981, only tells a marketplace what royalty a creator asks for; it does not force the marketplace to pay it. Some marketplaces enforce royalties in their contracts by blocking transfers that skip the royalty payment, while others honour them only in their own front end, which means a sale through another marketplace can skip them.
Teams should be honest with creators about this. If the marketplace cannot enforce royalties on chain, say so, and explain what it does enforce. Fees are simpler, since the marketplace controls its own fee, but the fee should be visible before the buyer confirms, and changing it should require a clear announcement rather than a quiet contract update.
How do you keep marketplace contracts safe?
You keep marketplace contracts safe by limiting what each contract can do, testing attacks as well as normal use, and getting an independent audit before launch. Security is a process, not a single check.
- Use standard libraries. Token and access-control code from a reviewed library is safer than code written from scratch.
- Follow the checks-effects pattern. Update the contract's records before sending money or tokens, so a re-entrant call cannot repeat the payment.
- Limit approvals. The marketplace should only move tokens the owner approved, and approvals should expire or be revocable.
- Control admin powers. The owner role should be able to pause the marketplace in an emergency, but not to move user funds.
- Test attacks. Try to buy a listing twice, cancel after a sale, bid after expiry, and drain the escrow. Each test should fail safely.
- Audit and monitor. Get an independent audit, fix the findings, and watch the contracts after launch for unusual activity.
Re-entrancy is the classic marketplace bug: a buyer contract that calls back into the marketplace during a sale and gets paid twice. The checks-effects pattern and a re-entrancy guard together prevent it, and both are cheap to add. An audit is the last line of defence, not the first, so fix the basics before paying for one. What a smart contract audit covers explains what reviewers look for.
Example (hypothetical): a first listing and sale
Example (hypothetical): an artist mints 10 editions of a digital artwork as ERC-1155 tokens and lists 5 of them at 0.2 ETH each, keeping 5 in reserve. The marketplace fee is 2.5 percent and the creator royalty is 10 percent of the sale price.
A buyer purchases one edition for 0.2 ETH. The settlement contract takes the payment and splits it: the royalty is 0.02 ETH to the artist, the fee is 0.005 ETH to the marketplace, and the seller keeps 0.175 ETH. The three shares add to 0.2 ETH, and the token moves to the buyer in the same transaction.
The buyer later resells the same edition for 0.4 ETH. The royalty of 0.04 ETH goes to the artist again, the fee is 0.01 ETH, and the seller keeps 0.35 ETH. The artist earns from both sales without doing anything, which is the point of the royalty split. The team checks each share on the explorer before launch, because a wrong split is a bug users notice fast.
What are the common mistakes?
The most common mistakes in marketplace contracts are writing token code from scratch, letting money and tokens move separately, and launching without an audit.
- Custom token contracts. A hand-written token contract usually has bugs a standard library already fixed.
- Separate payment and transfer. If the token moves in one transaction and the payment in another, one side can be left unpaid.
- Unlimited minting. A mint function with no cap or no access control lets anyone create new tokens.
- Open approvals. Approving the marketplace to move all of a user's tokens, forever, is a risk the user should not have to take.
- Silent fee changes. Changing the marketplace fee without telling users breaks trust and can break integrations.
- No pause switch. A marketplace without a way to stop trading in an emergency cannot react to a bug found after launch.
Key takeaways
- A marketplace is a small set of contracts: token, listing, escrow, royalty and auction
- Use a standard token library instead of writing token code from scratch
- Royalties are requested by a standard but not always enforced on chain, so be honest with creators
- Update records before sending money or tokens to block re-entrancy
- Audit the contracts and test attacks, not just normal sales
Frequently asked questions
Do I need to write my own token contract?
Usually not. Standard libraries provide reviewed ERC-721 and ERC-1155 contracts that cover minting, transfers and approvals. You add your own minting rules on top, such as who can mint and how many tokens exist. Writing a token contract from scratch is rarely worth the risk, since a bug there affects every token the marketplace holds.
What is the difference between ERC-721 and ERC-1155?
ERC-721 gives each token a unique identity, which fits one-of-a-kind art and collectibles. ERC-1155 lets one contract hold many token types and supports batch transfers, which fits game items and limited editions. Many marketplaces support both, using ERC-721 for unique pieces and ERC-1155 for editions.
How do NFT royalties actually work?
A royalty is a share of the resale price paid to the creator. The EIP-2981 standard lets a token say what royalty it asks for, but it does not force a marketplace to pay it. Some marketplaces enforce royalties in their contracts, while others honour them only in their own front end. Check what a marketplace enforces before promising creators a royalty.
How much does it cost to build marketplace contracts?
Costs depend on the team, the chain and the audit. The contract work itself is usually a few weeks for a small team, and an independent audit adds several weeks and a separate fee. Gas costs for users depend on the chain: a layer 2 network is usually far cheaper for buyers and sellers than Ethereum mainnet.
What should an audit of marketplace contracts cover?
An audit should cover the token contract, the listing and escrow logic, the royalty split, access control and re-entrancy. Reviewers also check that a sale cannot leave one side unpaid and that admin powers cannot move user funds. Fix the findings before launch, and re-audit any contract you change afterwards.
Where can I get help building marketplace contracts?
A smart contract development team can design the contracts, write the tests and prepare the audit, while your team builds the front end and the marketplace experience. Look for a team that has shipped marketplace contracts before and can show the audit reports from those projects.