Voting system using Solidity

3 min read 2 months ago
Published on Apr 29, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Introduction

This tutorial will guide you through creating a simple voting system using Solidity, the programming language for Ethereum smart contracts. By the end of this tutorial, you'll understand the basic structure of a voting contract, how to deploy it, and how to interact with it. This project is a practical introduction to blockchain technology and smart contracts, which are increasingly relevant in today's digital landscape.

Step 1: Setting Up Your Development Environment

Before writing any code, you'll need to set up your development environment.

  • Install Node.js: Download and install Node.js from the official website.
  • Install Truffle: Use npm to install Truffle, a development framework for Ethereum.
    npm install -g truffle
    
  • Install Ganache: Download Ganache, a personal blockchain for Ethereum development, to test your contracts locally.

Step 2: Create a New Truffle Project

Now, let's create a new Truffle project.

  • Open a terminal and create a new directory for your project:
    mkdir VotingSystem
    cd VotingSystem
    
  • Initialize a new Truffle project:
    truffle init
    

Step 3: Write the Voting Contract

Next, you'll create the smart contract that defines the voting system.

  • Navigate to the contracts folder in your project directory.
  • Create a new file named Voting.sol and open it in a code editor.
  • Add the following code to define the voting contract:
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    contract Voting {
        struct Candidate {
            uint id;
            string name;
            uint voteCount;
        }
    
        mapping(uint => Candidate) public candidates;
        mapping(address => bool) public voters;
        uint public candidatesCount;
    
        constructor() {
            addCandidate("Alice");
            addCandidate("Bob");
        }
    
        function addCandidate(string memory _name) private {
            candidatesCount++;
            candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
        }
    
        function vote(uint _candidateId) public {
            require(!voters[msg.sender], "You have already voted.");
            require(_candidateId > 0 && _candidateId <= candidatesCount, "Invalid candidate ID.");
    
            voters[msg.sender] = true;
            candidates[_candidateId].voteCount++;
        }
    }
    

Step 4: Compile the Contract

After writing the contract, you'll need to compile it.

  • In the terminal, run:
    truffle compile
    

Step 5: Deploy the Contract

Next, you need to deploy the contract to your local blockchain.

  • Create a new migration file in the migrations folder named 2_deploy_voting.js and add the following code:
    const Voting = artifacts.require("Voting");
    
    module.exports = function (deployer) {
        deployer.deploy(Voting);
    };
    
  • Deploy the contract to Ganache:
    truffle migrate
    

Step 6: Interact with the Contract

Now that your contract is deployed, you can interact with it.

  • Open the Truffle console:
    truffle console
    
  • Get an instance of the contract:
    let voting = await Voting.deployed();
    
  • Check candidates:
    let candidate1 = await voting.candidates(1);
    let candidate2 = await voting.candidates(2);
    
  • Vote for a candidate:
    await voting.vote(1); // Votes for the first candidate
    

Conclusion

You have successfully created a simple voting system using Solidity. You set up your environment, wrote and deployed a smart contract, and interacted with it. This foundational knowledge can serve as a stepping stone for more complex projects in blockchain development. Next, consider exploring features like event logging, user interfaces, or deploying on a test network for further learning.