Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

🏛️ DAO Governance Contract

A comprehensive decentralized autonomous organization (DAO) governance system built with ink! v6. This contract enables community-driven decision making through proposal creation, voting, and execution mechanisms.

Build Status Test Results

📋 Overview

This governance contract implements a complete DAO system with:

  • Proposal Management: Create, vote on, and execute governance proposals
  • Voting System: Multi-option voting with customizable parameters
  • Quorum Requirements: Configurable participation thresholds
  • Execution Delays: Time-locked execution for security
  • Voter Registration: Permission-based voting participation
  • Treasury Management: Fund allocation and spending controls

🏗️ Architecture

Core Components

  • lib.rs - Main contract module and exports
  • governance.rs - Core governance logic and state management
  • types.rs - Data structures and enums
  • errors.rs - Custom error definitions
  • events.rs - Event definitions for logging
  • tests.rs - Comprehensive test suite

Data Structures

pub struct Proposal {
    pub id: u32,
    pub title: String,
    pub description: String,
    pub proposal_type: ProposalType,        // Treasury, Governance, Technical, Other
    pub governance_params: GovernanceParameters,
    pub voting_options: VotingOptions,      // Custom voting choices
    pub proposer: H160,
    pub created_at: u32,                    // Block number
    pub voting_end: u32,                    // Block number
    pub execution_time: u32,                // Block number
    pub status: ProposalStatus,             // Active, Passed, Rejected, Executed, Expired
    pub vote_counts: Vec<u128>,             // Votes per option
    pub total_voters: u32,
}

🚀 Features

Proposal Management

  • Create proposals with custom titles and descriptions
  • Support for different proposal types (Treasury, Governance, Technical, Other)
  • Configurable governance parameters (voting period, quorum, execution delay)
  • Custom voting options for each proposal

Voting System

  • Multi-option voting support
  • One vote per registered voter per proposal
  • Real-time vote counting and tracking
  • Vote weight system (currently 1:1, extensible)

Security Features

  • Voter registration requirement
  • Quorum-based decision making
  • Time-locked execution delays
  • Comprehensive error handling
  • Event logging for transparency

Governance Parameters

  • Voting Periods: 3, 7, 14, or 30 days
  • Quorum Thresholds: 5%, 10%, 20%, or 25%
  • Execution Delays: Immediate, 1, 2, or 7 days

🛠️ Usage

Building the Contract

# Development build
cargo contract build

# Production build
cargo contract build --release

Deploying

# Deploy to local node
cargo contract instantiate --constructor new --suri //Alice --skip-confirm

# Deploy to testnet
cargo contract instantiate --constructor new --suri <your-seed> --url wss://rococo-contracts-rpc.polkadot.io

Key Functions

Proposal Management

// Create a new proposal
create_proposal(
    title: String,
    description: String,
    proposal_type: ProposalType,
    governance_parameters: GovernanceParameters,
    voting_options: VotingOptions
) -> Result<u32>

// Get proposal details
get_proposal(proposal_id: u32) -> Option<Proposal>

// Get all proposal IDs
get_all_proposal_ids() -> Vec<u32>

Voting System

// Vote on a proposal
vote(proposal_id: u32, option_index: u32) -> Result<()>

// Get user's vote
get_user_vote(proposal_id: u32, user: H160) -> Option<Vote>

// Check quorum status
has_reached_quorum(proposal_id: u32) -> Option<bool>

// Get detailed results
get_detailed_results(proposal_id: u32) -> Option<Vec<(String, u128)>>

Voter Management

// Register as voter
register_voter() -> Result<()>

// Check voter status
is_voter(account: H160) -> bool

// Get total voters
get_total_voters() -> u32

Status Management

// Update proposal status (when voting ends)
update_proposal_status(proposal_id: u32) -> Result<()>

// Execute passed proposal
execute_proposal(proposal_id: u32) -> Result<()>

🧪 Testing

Unit Tests

cargo test

Integration Tests

cargo test --features e2e-tests

Test Coverage

The contract includes comprehensive tests covering:

  • Proposal creation and management
  • Voting mechanisms
  • Voter registration
  • Status transitions
  • Error conditions
  • Edge cases

📊 Events

The contract emits events for all major operations:

// Proposal lifecycle
ProposalCreated { proposal_id, proposer, title, proposal_type }
ProposalStatusUpdated { proposal_id, old_status, new_status }
ProposalExecuted { proposal_id, status }

// Voting
VoteCast { proposal_id, voter, option_index, option_text, weight }

// Voter management
VoterRegistered { voter }

🔧 Configuration

Cargo.toml

[dependencies]
ink = { git = "https://github.com/use-ink/ink", tag = "v6.0.0-alpha.4", version = "6.0.0-alpha.4", default-features = false, features = ["unstable-hostfn"] }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"] }

🌐 Frontend Integration

This contract is designed to work with the Governance DApp, which provides:

  • Real-time proposal viewing
  • Interactive voting interface
  • Voter registration
  • Status tracking
  • Results visualization

🔒 Security Considerations

Implemented Security Features

  • Access Control: Voter registration requirement
  • Input Validation: Comprehensive parameter checking
  • Safe Arithmetic: Overflow/underflow protection
  • Time Locks: Execution delays for security
  • Quorum Requirements: Prevents minority rule
  • Event Logging: Full transparency

Best Practices

  • All state changes are logged via events
  • Comprehensive error handling
  • Input validation on all public functions
  • Safe arithmetic operations
  • Clear separation of concerns

📈 Performance

Storage Optimization

  • Efficient mapping-based storage
  • Minimal data duplication
  • Optimized struct packing

Gas Optimization

  • Batch operations where possible
  • Minimal storage writes
  • Efficient data structures

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

📄 License

This project is provided as-is for educational purposes. Check the main repository for licensing information.

🆘 Support

For issues and questions:

  • Create an issue in the repository
  • Check the main README for troubleshooting
  • Review the ink! documentation

🦀 Built with Rust + ink! v6.0.0-alpha.4

This governance contract demonstrates advanced ink! smart contract development patterns and serves as a foundation for building decentralized autonomous organizations.