Skip to content

zzzuhaibmohd/vote_dapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Vote DApp - Solana Voting Application

A decentralized voting application built on Solana using Anchor framework. Implements token-based voting where users purchase tokens with SOL, register proposals, and cast votes.

Program ID: 3tSg7dX3NRgNsCyFL4fGw6RWzjhWUUeSCvveBVaZ8PB5


πŸ—οΈ Architecture

System Overview

Solana Blockchain
    β”‚
    β”œβ”€β”€ Vote DApp Program (Anchor)
    β”‚   β”œβ”€β”€ lib.rs (9 instruction handlers)
    β”‚   β”œβ”€β”€ contexts.rs (Account validation)
    β”‚   β”œβ”€β”€ state.rs (Data structures)
    β”‚   β”œβ”€β”€ errors.rs (Error handling)
    β”‚   └── events.rs (Event emissions)
    β”‚
    └── External Programs (via CPI)
        β”œβ”€β”€ System Program (SOL transfers)
        β”œβ”€β”€ Token Program (SPL tokens)
        └── Associated Token Program

Key PDAs

  • treasury_config - Treasury configuration
  • x_mint - Voting token mint
  • sol_vault - Receives SOL from purchases
  • proposal_{id} - One per proposal
  • voter_{pubkey} - One per voter
  • vote_{pubkey}_{id} - One per vote
  • winner - Winning proposal tracker

πŸ“ Project Structure

vote_dapp/
β”œβ”€β”€ programs/vote_dapp/src/
β”‚   β”œβ”€β”€ lib.rs          # Instruction handlers
β”‚   β”œβ”€β”€ contexts.rs      # Account contexts
β”‚   β”œβ”€β”€ state.rs         # On-chain data
β”‚   β”œβ”€β”€ errors.rs        # Custom errors
β”‚   └── events.rs        # Events
β”œβ”€β”€ tests/
β”‚   └── vote_dapp.ts     # Test suite
└── Anchor.toml          # Config

🎯 Core Features

  1. Token Purchase - Buy voting tokens with SOL
  2. Voter Registration - Register as eligible voter
  3. Proposal Creation - Create proposals with token deposit
  4. Voting - Cast yes/no votes with token commitment
  5. Winner Selection - Automatic winner after deadline
  6. Account Management - Rent recovery via account closure

πŸ’‘ Key Technical Learnings

1. Cross-Program Invocation (CPI)

Used for SOL transfers and token operations:

system_program::transfer(cpi_context, amount)?;

transfer(transfer_ctx, token_amount)?;

2. Program Derived Addresses (PDAs)

Deterministic accounts without private keys:

// Single instance
seeds = [b"treasury_config"], bump

// Per-user
seeds = [b"voter", authority.key().as_ref()], bump

// Per-entity
seeds = [b"proposal", proposal_id.to_le_bytes().as_ref()], bump

3. Anchor Constraints

Declarative security checks:

constraint=treasury_config_account.x_mint == x_mint.key()
constraint=authority.key() == treasury_config_account.authority @ VoteError::UnauthorizedAccess

4. Signer Seeds

PDAs need signer seeds when signing transactions:

let signer_seeds = &[&[b"sol_vault", &[bump]][..]];
CpiContext::new_with_signer(program, accounts, signer_seeds)

5. Account Initialization

Different patterns for different needs:

  • init - Always creates new account
  • init_if_needed - Creates only if doesn't exist
  • Manual checks - Custom validation logic

6. String Size Constraints

#[max_len(64)]
pub proposal_info: String

7. Overflow Protection

proposal_count.checked_add(1)
    .ok_or(VoteError::ProposalCounterOverflow)?;

8. Rent Recovery

#[account(..., close = destination)]
pub proposal_account: Account<'info, Proposal>

πŸ”„ Main Workflows

Initialization

Authority β†’ initialize() β†’ Create PDAs β†’ Treasury ready

Token Purchase

Buyer β†’ buy_tokens() β†’ Transfer SOL β†’ Mint tokens β†’ Tokens received

Voting

Voter β†’ register_voter() β†’ proposal_to_vote() β†’ Transfer tokens β†’ Vote recorded

Winner Selection

Authority β†’ pick_winner() β†’ Check deadline β†’ Update winner account

πŸ› οΈ Technology Stack

  • Solana Blockchain - High-performance blockchain
  • Anchor Framework v0.32.1 - Solana development framework
  • Rust - Program language
  • SPL Token - Token standard
  • TypeScript - Tests & client

πŸš€ Quick Start

Prerequisites

  • Rust, Solana CLI, Anchor CLI, Node.js

Commands

anchor build    # Build program
anchor test     # Run tests
anchor deploy   # Deploy to localnet

πŸ§ͺ Unit Tests

image

πŸ“ Notes for Recruiters

This project demonstrates:

βœ… Solana Expertise - Anchor framework, PDAs, CPI patterns
βœ… Security - Validation, overflow protection, authority checks
βœ… Code Quality - Modular architecture, type safety
βœ… Testing - Comprehensive test coverage


Built with Anchor Framework on Solana

About

A Voting Dapp written in Rust and Typescript to understand some important concepts related to Solana

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors