Stx-Raffle Smart Contract #1
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Raffle Smart Contract - Detailed Description
Overview
This Clarity smart contract implements a comprehensive raffle system on the Stacks blockchain, designed for fair and transparent prize distribution. The contract manages the entire raffle lifecycle from participant entry to winner selection and prize disbursement, with built-in security measures and administrative controls.
Core Architecture
The contract operates on a single-raffle-at-a-time model, where each raffle has a unique ID and maintains complete isolation from previous raffles. The system uses a combination of data variables for current state management and data maps for historical record-keeping and participant tracking.
State Management:
Functional Components
Entry System
The entry mechanism requires participants to pay a configurable STX fee to join the active raffle. The contract enforces strict validation rules: users can only enter once per raffle, payments must match the exact entry fee, and the raffle must be active with available slots. All entry fees are accumulated in the contract balance to form the prize pool.
Winner Selection Algorithm
Winner selection employs a pseudo-random algorithm that combines multiple blockchain variables for unpredictability:
This approach ensures that winners cannot be predicted in advance while maintaining deterministic reproducibility for verification.
Prize Distribution
Upon winner selection, the contract automatically calculates and distributes funds:
Administrative Controls
The contract owner (deployer address) has exclusive access to critical functions:
Security Architecture
Access Control
The contract implements strict role-based access control with owner verification on all administrative functions. The owner address is immutably set at deployment and cannot be changed, ensuring consistent control authority.
State Validation
Comprehensive validation prevents common attack vectors:
Data Integrity
The contract maintains complete audit trails through immutable historical records stored in maps. Each completed raffle generates a permanent record including winner, prize amount, participant count, and timestamp for full transparency.
Technical Implementation Details
Random Number Generation
The randomness source combines block-height entropy with hash-based unpredictability. While not cryptographically perfect, this approach provides sufficient randomness for fair lottery systems within the constraints of deterministic blockchain execution.
Memory Management
Participant lists are implemented as bounded Clarity lists with a maximum of 100 entries, ensuring predictable gas costs and preventing unbounded growth attacks. User entry mappings are automatically cleared between raffles to prevent memory bloat.
Error Handling
The contract defines specific error codes for all failure scenarios, enabling precise debugging and user feedback. All operations use Clarity's built-in error propagation to ensure transaction rollback on any failure condition.
Economic Model
The contract operates on a simple economic model where participants contribute entry fees to create a prize pool, with the majority going to the winner and a small percentage retained by the contract owner for operational costs. The fee structure is transparent and adjustable within reasonable bounds to maintain fairness while enabling sustainable operation.
This design creates aligned incentives: participants get fair chances at significant prizes, winners receive the majority of the pool, and operators are compensated for providing the service infrastructure.