A comprehensive Clarity smart contract for decentralized proposal creation, voting, and execution on the Stacks blockchain.
This smart contract enables a democratic governance system where users can create proposals, vote on them using weighted voting power, and execute approved proposals. The system includes robust security measures, input validation, and proper stake management.
- Create proposals with title, description, and custom duration
- Automatic unique proposal ID generation
- Stake requirement (1 STX) to prevent spam submissions
- Duration validation (1-100 days)
- User-based voting power allocation
- Prevention of double voting
- Real-time vote counting (yes/no)
- 51% threshold for proposal approval
- Active: Open for voting within time limit
- Passed/Rejected: Based on 51% threshold
- Executed: Finalized proposals with stake return
- Contract owner administrative privileges
- Comprehensive input validation
- Emergency proposal cancellation
- Protection against common attack vectors
Creates a new proposal with the specified parameters.
- Requirements: Valid voting power, 1 STX stake
- Returns: Proposal ID
- Validation: Title/description length, duration bounds
Cast a vote on an active proposal.
- Parameters:
proposal-id: Target proposal IDsupport: true (yes) or false (no)
- Requirements: Active proposal, no previous vote, voting power > 0
Finalize and execute a proposal after voting period.
- Action: Determines pass/fail based on votes
- Stake Return: Returns stake for passed proposals
Owner only - Set voting power for users.
- Range: 1 to 1,000,000
- Access: Contract owner only
Owner only - Emergency cancellation of active proposals.
get-proposal(proposal-id)- Get proposal detailsget-proposal-count()- Total proposals createdhas-voted(proposal-id, voter)- Check voting statusget-vote(proposal-id, voter)- Get user's vote detailsget-voting-power(user)- Get user's voting powerget-user-proposals(user)- Get proposals created by useris-proposal-active(proposal-id)- Check if proposal is active
| Parameter | Value | Description |
|---|---|---|
| MIN_PROPOSAL_DURATION | 144 blocks | ~1 day minimum |
| MAX_PROPOSAL_DURATION | 14,400 blocks | ~100 days maximum |
| MIN_STAKE | 1,000,000 microSTX | 1 STX stake requirement |
| VOTING_THRESHOLD | 51% | Approval threshold |
| MAX_VOTING_POWER | 1,000,000 | Maximum user voting power |
| Code | Constant | Description |
|---|---|---|
| u100 | ERR-NOT-AUTHORIZED | Insufficient permissions |
| u101 | ERR-PROPOSAL-NOT-FOUND | Invalid proposal ID |
| u102 | ERR-ALREADY-VOTED | User already voted |
| u103 | ERR-PROPOSAL-EXPIRED | Proposal voting ended |
| u104 | ERR-INVALID-DURATION | Duration out of bounds |
| u105 | ERR-INSUFFICIENT-STAKE | Stake amount too low |
| u106 | ERR-PROPOSAL-ACTIVE | Proposal still active |
| u107 | ERR-INVALID-STATUS | Invalid proposal status |
| u108 | ERR-INVALID-INPUT | Invalid input parameters |
;; Create a proposal (requires 1 STX stake)
(contract-call? .proposal-system create-proposal
"Increase block rewards"
"Proposal to increase mining rewards by 10%"
u1440) ;; 10 days
;; Vote on proposal ID 1
(contract-call? .proposal-system vote u1 true) ;; Vote yes
;; Execute proposal after voting period
(contract-call? .proposal-system execute-proposal u1)- Input Validation: All user inputs validated before processing
- Stake Protection: Economic incentive against spam
- Access Control: Owner-only administrative functions
- Double Vote Prevention: Users can only vote once per proposal
- Time-based Expiration: Automatic proposal expiration
- Deploy contract to Stacks blockchain
- Set initial voting power for users via
set-voting-power - Users must have STX for proposal stakes
- Contract owner maintains administrative privileges
- Efficient data structures with minimal storage operations
- Read-only functions for gas-free queries
- Optimized for under 300 lines of code
- Strategic use of Clarity built-ins for performance