A robust and secure raffle system built on the Stacks blockchain using Clarity smart contracts. This contract enables fair and transparent raffles with automated winner selection and prize distribution.
- Secure Entry System: Players pay an entry fee in STX to participate
- Fair Winner Selection: Cryptographically secure random winner selection using block data
- Automated Prize Distribution: Winners receive their prize automatically upon drawing
- Owner Fee Structure: Configurable owner fee (default 5%, max 20%)
- Raffle History: Complete tracking of past raffles and winners
- Emergency Controls: Owner can close raffles and issue refunds if needed
- Anti-Spam Protection: Users can only enter once per raffle
- Entry Fee: Configurable per raffle (minimum 1 microSTX)
- Max Participants: Up to 100 participants per raffle
- Owner Fee: 5% default, adjustable up to 20%
- Prize Pool: Total entries minus owner fee
start-new-raffle(entry-fee, max-participants)- Create a new raffledraw-winner()- Select winner and distribute prizesemergency-close-raffle()- Close active rafflerefund-participants()- Refund all participants (emergency use)update-owner-fee(percentage)- Adjust owner fee percentage
enter-raffle()- Enter the current active raffleget-raffle-info()- View current raffle detailshas-user-entered(user)- Check if user has entered
get-participants(raffle-id)- List all participantsget-participant-count(raffle-id)- Number of participantsget-raffle-history(raffle-id)- Historical raffle dataget-contract-balance()- Current contract STX balance
- Raffle Creation: Contract owner starts a new raffle with specified entry fee and participant limit
- Entry Phase: Users pay the entry fee to join the raffle (one entry per user)
- Winner Selection: Owner calls
draw-winner()which uses blockchain data for randomness - Prize Distribution: Winner receives the prize pool (total fees minus owner commission)
- History Recording: Raffle results are permanently stored on-chain
- Owner-Only Controls: Critical functions restricted to contract deployer
- Duplicate Entry Prevention: Users cannot enter the same raffle twice
- State Validation: Comprehensive checks prevent invalid operations
- Automatic Cleanup: User entries cleared between raffles
- Error Handling: Detailed error codes for all failure scenarios
u100- Owner-only function called by non-owneru101- Resource not foundu102- User already entered this raffleu103- Raffle is closed/inactiveu104- Raffle is already activeu105- Insufficient payment providedu106- No participants in raffleu107- Winner already drawnu108- STX transfer failedu109- Invalid amount specified
;; Owner starts a raffle: 1 STX entry fee, 50 max participants
(contract-call? .raffle start-new-raffle u1000000 u50)
;; User enters raffle (pays 1 STX)
(contract-call? .raffle enter-raffle)
;; Owner draws winner when ready
(contract-call? .raffle draw-winner)
;; Check raffle results
(contract-call? .raffle get-raffle-info)- Contract owner is set to the transaction sender at deployment
- Initial raffle ID starts at 0
- No raffle is active upon deployment
- Owner fee percentage defaults to 5%
This contract implements best practices for smart contract security, including proper access controls, state management, and error handling. The random number generation uses block height and hash data, providing sufficient unpredictability for fair winner selection.