NFT Royalty Distribution System
A robust Clarity smart contract for managing NFT sales with automatic royalty distribution on the Stacks blockchain.
Overview
This smart contract enables NFT creators to receive ongoing royalties from secondary sales while providing a complete marketplace infrastructure with sale tracking, royalty management, and secure fund distribution.
Features
Core Functionality
- NFT Minting: Create NFTs with customizable royalty percentages (0-10%)
- Automatic Royalty Distribution: Royalties are automatically calculated and credited on each sale
- Sale Management: Complete transaction history with timestamps and pricing data
- Royalty Withdrawal: Creators can withdraw accumulated royalties at any time
- Transfer & Burn: Standard NFT transfer and burning capabilities
Advanced Features
- Multiple Recipients: Support for adding multiple royalty recipients per NFT
- Platform Fees: Configurable platform fee system (default 2.5%)
- Dynamic Updates: Creators can update royalty percentages post-mint
- Sale History: Complete audit trail of all transactions
Contract Functions
Public Functions
mint-nft
(mint-nft (token-id uint) (royalty-percentage uint))
Mint a new NFT with specified royalty percentage (max 1000 = 10%).
sell-nft
(sell-nft (token-id uint) (buyer principal) (price uint))
Sell an NFT with automatic royalty and platform fee distribution.
transfer-nft
(transfer-nft (token-id uint) (recipient principal))
Transfer NFT ownership to another user.
withdraw-royalties
(withdraw-royalties)
Withdraw accumulated royalties from sales.
add-royalty-recipient
(add-royalty-recipient (token-id uint) (recipient principal) (share uint))
Add additional royalty recipients for an NFT (creator only).
update-royalty-percentage
(update-royalty-percentage (token-id uint) (new-percentage uint))
Update royalty percentage for an NFT (creator only, max 10%).
(update-platform-fee (new-fee uint))
Update platform fee percentage (contract owner only, max 10%).
burn-nft
(burn-nft (token-id uint))
Permanently burn an NFT (owner only).
Read-Only Functions
get-nft-metadata: Retrieve NFT metadata including creator, royalty %, and sales stats
get-nft-owner: Get current owner of an NFT
get-royalty-recipient: Check royalty recipient share for an NFT
get-user-balance: View accumulated royalty balance for a user
get-platform-fee: Get current platform fee percentage
calculate-royalty: Calculate royalty amount for a given price
get-sale-history: Retrieve details of a specific sale
Error Codes
| Code |
Constant |
Description |
| u100 |
err-owner-only |
Operation requires contract owner |
| u101 |
err-not-found |
NFT not found |
| u102 |
err-unauthorized |
Caller not authorized |
| u103 |
err-invalid-percentage |
Percentage exceeds maximum |
| u104 |
err-already-exists |
NFT already minted |
| u105 |
err-insufficient-funds |
Insufficient balance |
| u106 |
err-invalid-recipient |
Invalid recipient address |
| u107 |
err-transfer-failed |
STX transfer failed |
Usage Example
Minting an NFT
;; Mint NFT with 5% royalty
(contract-call? .nft-royalty mint-nft u1 u500)
Selling an NFT
;; Sell NFT #1 to buyer for 1000 STX
(contract-call? .nft-royalty sell-nft u1 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM u1000000000)
Withdrawing Royalties
;; Withdraw accumulated royalties
(contract-call? .nft-royalty withdraw-royalties)
Royalty Calculation
Royalties are calculated using basis points (10000 = 100%):
Royalty Amount = (Sale Price × Royalty Percentage) / 10000
Platform Fee = (Sale Price × Platform Fee Percentage) / 10000
Seller Receives = Sale Price - Royalty Amount - Platform Fee
Example: For a 1000 STX sale with 5% royalty and 2.5% platform fee:
- Royalty: 50 STX (credited to creator’s balance)
- Platform Fee: 25 STX (sent to contract owner)
- Seller: 925 STX (sent to seller)
Security Features
- ✅ Ownership verification on all sensitive operations
- ✅ Comprehensive input validation
- ✅ Safe STX transfer handling with error propagation
- ✅ Duplicate prevention for minting
- ✅ Balance overflow protection
- ✅ Percentage caps to prevent excessive fees
Technical Details
- Language: Clarity
- Blockchain: Stacks
- Total Lines: 277
- Max Royalty: 10% (1000 basis points)
- Max Platform Fee: 10% (1000 basis points)
- Default Platform Fee: 2.5% (250 basis points)
Data Storage
The contract maintains five primary data structures:
- nft-metadata: Creator, royalty %, and sales statistics
- nft-owners: Current ownership mapping
- royalty-recipients: Multiple recipient configurations
- user-royalty-balance: Accumulated royalty balances
- sale-history: Complete transaction audit trail
Deployment
Deploy this contract to the Stacks blockchain using:
clarinet deploy
Testing
Test the contract using Clarinet:
clarinet test
评论