A secure and flexible ballot system implemented on the Stacks blockchain using Clarity smart contract language.
This smart contract enables decentralized ballot creation and management with weighted participant influence, customizable ballot durations, and automated result calculation. It supports up to 10 choices per ballot and includes comprehensive access controls.
- Flexible Ballot Creation: Create ballots with custom subjects, descriptions, and up to 10 choices
- Weighted Participation: Assign different influence levels to participants (default: 1)
- Time-Based Ballots: Set custom duration using block heights
- Threshold Requirements: Minimum participation requirements for valid results
- Automated Results: Automatic winner determination when ballots conclude
- Access Control: Role-based permissions for ballot management
- Duplicate Prevention: Ensures one ballot per participant per ballot
Creates a new ballot with specified parameters.
(launch-ballot subject details choices duration threshold-count)subject: Ballot title (max 256 characters)details: Detailed description (max 1024 characters)choices: List of up to 10 choice options (max 64 characters each)duration: Ballot duration in blocksthreshold-count: Minimum total ballots required for validity
Allows participants to cast their ballot for a specific choice.
(cast-ballot ballot-id choice-idx)ballot-id: Unique identifier of the ballotchoice-idx: Index of the chosen option (0-9)
Finalizes a ballot and calculates results after the ballot period ends.
(conclude-ballot ballot-id)Sets the ballot influence/weight for a participant (admin only).
(set-participant-influence participant influence)Cancels an active ballot (initiator or admin only).
(cancel-ballot ballot-id)Transfers system administration rights (current admin only).
(transfer-admin-rights new-admin)get-ballot-counter(): Returns total number of ballots createdget-ballot-info(ballot-id): Retrieves complete ballot informationget-participant-ballot(ballot-id, participant): Gets participant's ballot choiceget-choice-tally(ballot-id, choice-idx): Returns tally for specific choiceget-participant-influence(participant): Gets participant's ballot weightget-ballot-outcome(ballot-id): Retrieves finalized ballot resultshas-participant-voted(ballot-id, participant): Checks if participant has votedis-ballot-active(ballot-id): Checks if ballot is currently accepting ballots
Stores complete ballot information including subject, details, choices, timing, and status.
Records each participant's ballot choice with ballot ID and participant address.
Maintains running tallies for each choice option in each ballot.
Stores custom influence weights for participants (default: 1).
Stores finalized results including winner, tallies, and total participation.
ERR-ACCESS-DENIED (100): Insufficient permissionsERR-DUPLICATE-BALLOT (101): Participant already votedERR-BALLOT-NOT-EXISTS (102): Ballot ID not foundERR-BALLOT-EXPIRED (103): Ballot period has endedERR-BALLOT-STILL-ACTIVE (104): Cannot conclude active ballotERR-INVALID-CHOICE (105): Choice index out of rangeERR-FORBIDDEN-CALLER (106): Unauthorized function callerERR-THRESHOLD-NOT-REACHED (107): Minimum participation not metERR-NO-CHOICES-PROVIDED (108): Empty choices list providedERR-INVALID-INFLUENCE (109): Invalid participant influence value
;; Launch a ballot
(contract-call? .ballot-system launch-ballot
u"Should we upgrade the protocol?"
u"Vote on whether to implement the new protocol upgrade"
(list u"Yes" u"No" u"Abstain")
u1000 ;; 1000 blocks duration
u10) ;; minimum 10 total ballots
;; Cast a ballot (choice 0 = "Yes")
(contract-call? .ballot-system cast-ballot u0 u0)
;; Conclude ballot after period ends
(contract-call? .ballot-system conclude-ballot u0)- Single Ballot Per Participant: Prevents duplicate ballots through mapping checks
- Time-Based Validation: Ballots only accept participation during active periods
- Access Control: Admin-only functions for system management
- Threshold Validation: Ensures minimum participation for legitimate results
- Input Validation: Comprehensive error handling and parameter validation