A robust Clarity smart contract for peer-to-peer game server hosting on the Stacks blockchain. This contract enables users to host game servers, rent server capacity, and build a decentralized gaming infrastructure with built-in ratings and payments.
This smart contract creates a decentralized marketplace where:
- Hosts can register and monetize their game servers
- Renters can pay to use server capacity on-demand
- Platform takes a configurable fee for facilitating transactions
- Community can rate servers to ensure quality
- Register Servers: List your game server with custom pricing and capacity
- Dynamic Pricing: Update server prices anytime
- Status Management: Set servers to active, inactive, or maintenance mode
- Earnings Tracking: Monitor total earnings from all rentals
- Capacity Control: Define max players and total capacity slots
- Flexible Rentals: Rent server slots for custom durations (in blocks)
- Slot-Based System: Reserve exactly the number of slots you need
- Active Rental Management: End rentals early to free up capacity
- Rating System: Rate servers (1-5 stars) after using them
- Automated Payments: Instant payment splitting between host and platform
- Fee Management: Configurable platform fee (max 20%)
- Reputation System: Community-driven server ratings
- Capacity Protection: Prevents overbooking with real-time tracking
(register-server (name (string-ascii 50)) (description (string-ascii 200))
(price-per-hour uint) (max-players uint) (total-capacity uint))Register a new game server with pricing and capacity details.
Parameters:
name: Server name (1-50 characters)description: Server description (1-200 characters)price-per-hour: Price in microSTX per blockmax-players: Maximum players per rental sessiontotal-capacity: Total concurrent slots available
Returns: Server ID on success
(rent-server (server-id uint) (duration-blocks uint) (slots uint))Rent server capacity for a specified duration.
Parameters:
server-id: ID of the server to rentduration-blocks: Rental duration (max 52,560 blocks ≈ 1 year)slots: Number of player slots to reserve
Payment: Automatically transfers STX to contract, then to host (minus platform fee)
Returns: Rental ID on success
(end-rental (rental-id uint))Terminate an active rental and free up server capacity.
Only callable by: Original renter
(rate-server (server-id uint) (rating uint))Rate a server you've previously rented.
Parameters:
server-id: Server to raterating: 1-5 stars
Requirements:
- Must have rented the server before
- Can only rate once per server
(update-server-status (server-id uint) (new-status uint))Change server status (active=1, inactive=2, maintenance=3).
Only callable by: Server host
(update-server-price (server-id uint) (new-price uint))Update server rental price.
Only callable by: Server host
(set-platform-fee (new-fee uint))Update platform fee percentage (max 20%).
Only callable by: Contract owner
(withdraw-contract-balance)Withdraw accumulated platform fees.
Only callable by: Contract owner
(get-server (server-id uint))Retrieve complete server information.
(get-rental (rental-id uint))Get rental details including dates and payment info.
(get-server-rating (server-id uint))Calculate average server rating.
(get-host-earnings (host principal))Get total lifetime earnings for a host.
(get-platform-fee)Get current platform fee percentage.
(is-server-available (server-id uint))Check if server has available capacity.
- Renter calls
rent-serverwith STX payment - Contract receives full payment
- Platform fee calculated (default 5%)
- Host receives payment minus fee immediately
- Platform fee remains in contract for owner withdrawal
Example:
- Rental cost: 1000 STX
- Platform fee (5%): 50 STX
- Host receives: 950 STX
- ✅ Input Validation: All user inputs validated before processing
- ✅ Authorization Checks: Only authorized users can modify data
- ✅ Capacity Protection: Prevents overbooking with real-time checks
- ✅ Payment Safety: Atomic transfers with failure rollback
- ✅ Rate Limiting: One rating per user per server
- ✅ Bounds Checking: Duration and rating limits enforced
| Code | Constant | Description |
|---|---|---|
| u100 | err-owner-only |
Only contract owner can perform action |
| u101 | err-not-found |
Server or rental not found |
| u102 | err-unauthorized |
Caller not authorized |
| u103 | err-invalid-price |
Price must be greater than 0 |
| u104 | err-insufficient-funds |
Insufficient STX for rental |
| u105 | err-server-inactive |
Server not active |
| u106 | err-already-exists |
Resource already exists |
| u107 | err-invalid-capacity |
Invalid capacity value |
| u108 | err-server-full |
No available slots |
| u109 | err-not-renter |
Must have rented to perform action |
| u110 | err-already-rated |
Already rated this server |
| u111 | err-invalid-duration |
Duration out of valid range |
| u112 | err-invalid-rating |
Rating must be 1-5 |
Host registers Minecraft server with 20 player capacity, renters pay per block for slots.
Rent entire server capacity for tournament duration with guaranteed performance.
Small communities rent affordable slots on shared servers with hourly billing.
Build a reputation-based marketplace where quality hosts earn premium pricing.
- Language: Clarity (Stacks Blockchain)
- Lines of Code: 297
- Max Duration: 52,560 blocks (~1 year)
- Max Platform Fee: 20%
- Rating Scale: 1-5 stars
- String Limits: Name (50 chars), Description (200 chars)
- Deploy contract to Stacks blockchain
- Contract deployer becomes owner with fee withdrawal rights
- Initial platform fee set to 5%
- Ready for hosts to register servers
- Set competitive pricing based on server specs
- Maintain server status accurately
- Respond to maintenance needs promptly
- Build reputation through quality service
- Check server ratings before renting
- Rent appropriate duration to avoid waste
- Rate servers to help community
- End rentals when done to free capacity
- Keep platform fees reasonable (5-10%)
- Monitor for malicious behavior
- Regular balance withdrawals
- Community engagement for trust
- No refund mechanism (rent responsibly)
- Last-write-wins for concurrent updates
- No server performance guarantees (off-chain)
- Rating cannot be changed after submission
- Duration paid upfront (no partial refunds)