In this sixth step of building a 3D NFT Marketplace on Stacks, weβre going to implement the core smart contract of the marketplace: the market.clar
.
This contract allows users to list their 3D avatar NFTs for sale, either for STX or fungible tokens, and supports royalty payments, whitelisting, and basic listing logic.
You can explore the full code here: π market.clar
π§ What does the market.clar
do?
The contract provides the following key features:
- Whitelist control for allowed NFT and payment contracts
- NFT listings, with optional takers and expirations
- Sale fulfillment using STX or fungible tokens
- Royalty support via optional
get-royalty-info
- Events for listings, sales, and cancellations
π§© Contract Structure
β Traits
The contract uses the nft-trait
and sip-010
FT trait to ensure compatibility with the 4V4 avatar NFTs and token payments.
(use-trait nft-trait 'SP...nft-trait.nft-trait)
(use-trait ft-trait 'SP...sip-010-trait-ft-standard.sip-010-trait)
π§± Data Model
listing-nonce
: ID counter for new listingslistings
: map that stores listing info per IDwhitelisted-asset-contracts
: which NFTs and FTs are allowed
π€ Listing Assets
Users can list NFTs using:
(list-asset <nft-contract> {
taker: (optional principal),
token-id: uint,
expiry: uint,
price: uint,
payment-asset-contract: (optional principal)
})
This transfers the NFT to the contract, stores the listing, and emits an event.
π³ Fulfilling Listings
Buyers can purchase NFTs by calling:
(fulfil-listing-stx <listing-id> <nft-contract>)
Or for FT tokens:
(fulfil-listing-ft <listing-id> <nft-contract> <ft-contract>)
The listing is validated, royalty (if any) is paid, the NFT is transferred, and the payment is sent.
β Cancelling Listings
If the seller changes their mind, they can cancel the listing:
(cancel-listing <listing-id> <nft-contract>)
The NFT is returned to the maker and the listing is removed.
π Admin Features
The deployer can whitelist contracts to control what assets can be traded:
(set-whitelisted <contract> <bool>)
β How to Deploy the Contract
The contract deployment is similar to the previous steps, you just need to add the market.clar
file to the contracts
folder, update the Clarinet.toml
file and deploy it.