Skip to content

Uchechukwu-Ekezie/ink-governance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Treasury Governance Smart Contract

A comprehensive treasury governance smart contract built with Ink! v6 for Polkadot/Substrate chains.

📁 Project Structure

The contract is modularized into separate files for better organization and maintainability:

governance/
├── lib.rs          # Main contract implementation
├── errors.rs       # Error types and Result type alias
├── events.rs       # Contract events
├── structs.rs      # Data structures (enums and structs)
└── Cargo.toml      # Project dependencies

File Descriptions

lib.rs

Main contract file that includes:

  • Contract storage structure
  • Constructor and initialization
  • All message functions (create_proposal, vote, execute_proposal, etc.)
  • Query functions
  • Unit tests

The file uses include! macro to inline the separate module files as required by Ink! v6.

errors.rs

Contains all custom error types:

  • Error enum with all possible contract errors
  • Result<T> type alias for cleaner error handling

events.rs

Defines all contract events:

  • ProposalCreated - Emitted when a new proposal is created
  • VoteCast - Emitted when a vote is cast
  • ProposalExecuted - Emitted when a proposal is executed
  • ProposalStatusChanged - Emitted when proposal status changes
  • ProposalCancelled - Emitted when a proposal is cancelled

structs.rs

Contains all data structures:

  • Enums: ProposalType, VotingPeriod, QuorumThreshold, ExecutionDelay, ProposalStatus
  • Structs: GovernanceParameters, VotingOptions, VoteChoice, Proposal, Vote
  • Helper methods for conversions (e.g., to_blocks(), to_percentage())

🔧 Technical Details

Ink! Version

This contract uses Ink! v6 with H160 address type (Ethereum-style addresses).

Key Features

  1. Proposal Management

    • Create proposals with custom governance parameters
    • Support for different proposal types (Treasury, Governance, Technical, Other)
    • Flexible voting options (1-10 options per proposal)
  2. Voting System

    • One vote per address
    • Prevention of double voting
    • Configurable voting periods (3, 7, 14, or 30 days)
    • Real-time vote counting
  3. Governance Parameters

    • Voting Period: Duration for voting on proposals
    • Quorum Threshold: Minimum participation required (5%, 10%, 20%, or 25%)
    • Execution Delay: Time delay before execution (Immediate, 1, 2, or 7 days)
  4. Proposal Execution

    • Automatic status updates based on quorum and voting results
    • Tie detection (proposals rejected on tie)
    • Execution delay enforcement
  5. Voter Registration

    • Register voters for accurate quorum calculations
    • Track total number of registered voters

🚀 Building and Testing

Prerequisites

  • Rust toolchain
  • cargo-contract CLI tool

Build

cargo contract build --release

Test

cargo test

Deploy

cargo contract instantiate --suri //Alice

📊 Contract Functions

Constructor

  • new() - Initialize the contract

Voter Management

  • register_voter() - Register as a voter
  • get_total_voters() - Get total registered voters
  • is_registered_voter(account) - Check if an address is registered

Proposal Management

  • create_proposal(...) - Create a new proposal
  • cancel_proposal(proposal_id) - Cancel an active proposal (owner/proposer only)
  • get_proposal(proposal_id) - Get proposal details
  • get_all_proposal_ids() - Get all proposal IDs

Voting

  • vote(proposal_id, option_index) - Cast a vote
  • get_user_vote(proposal_id, user) - Get a user's vote
  • update_proposal_status(proposal_id) - Update proposal status after voting ends

Execution

  • execute_proposal(proposal_id) - Execute a passed proposal

Query Functions

  • get_stats() - Get contract statistics
  • has_reached_quorum(proposal_id) - Check if quorum is reached
  • get_proposal_results(proposal_id) - Get vote counts and quorum status
  • get_voting_options(proposal_id) - Get voting options
  • get_detailed_results(proposal_id) - Get results with option names
  • get_winning_option(proposal_id) - Get winning option and vote count
  • get_owner() - Get contract owner address

🔐 Security Features

  • Authorization Checks: Only owner or proposer can cancel proposals
  • Double Voting Prevention: Each address can only vote once per proposal
  • Overflow Protection: Uses checked arithmetic for all calculations
  • Status Validation: Ensures proposals are in correct state before actions
  • Time-based Controls: Enforces voting periods and execution delays

📝 Usage Example

// 1. Register as a voter
contract.register_voter()?;

// 2. Create a proposal
let governance_params = GovernanceParameters {
   voting_period: VotingPeriod::SevenDays,
   quorum_threshold: QuorumThreshold::Ten,
   execution_delay: ExecutionDelay::OneDay,
};

let voting_options = VotingOptions {
   options: vec![
       String::from("Approve"),
       String::from("Reject"),
   ],
};

let proposal_id = contract.create_proposal(
   String::from("Treasury Allocation"),
   String::from("Allocate 100 tokens for development"),
   ProposalType::Treasury,
   governance_params,
   voting_options,
)?;

// 3. Vote on the proposal
contract.vote(proposal_id, 0)?; // Vote for option 0 (Approve)

// 4. Update proposal status (after voting period)
contract.update_proposal_status(proposal_id)?;

// 5. Execute proposal (after execution delay)
contract.execute_proposal(proposal_id)?;

📄 License

This project is licensed under MIT License.

🤝 Contributing

Contributions are welcome! Please ensure all tests pass before submitting a PR.

🔗 Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages