Solidity in Action: Building & Understanding a Voting Smart Contract

Cover Image for Solidity in Action: Building & Understanding a Voting Smart Contract
Posted
3 min read
Table of Contents

    Embark on an immersive journey through the riveting landscape of blockchain development with Solidity. In this blog post, we’re not just going to talk about the possibilities; we’re going to build them. Let’s dive into creating a basic voting smart contract, and don’t miss the little challenge at the end that you can submit via email for feedback!

    A Glimpse into Smart Contracts with Solidity

    In the world of Ethereum, smart contracts are like the modern alchemists turning digital interactions into gold—transparent, secure, and trustless. Solidity is the wand they wield, and today you’ll learn a simple spell of your own.

    Crafting a Voting Contract

    Imagine a digital world where your vote is as secure as your digital currency. We’re going to create a basic voting system using Solidity. This contract will allow users to create a poll, cast votes, and tally them securely on the Ethereum blockchain.

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.4;
    
    contract Voting {
        struct Candidate {
            string name;
            uint voteCount;
        }
    
        struct Poll {
            string question;
            Candidate[] candidates;
            bool ended;
        }
    
        Poll[] public polls;
    
        function createPoll(string memory _question, string[] memory _candidates) public {
            Poll storage newPoll = polls.push();
            newPoll.question = _question;
            
            for (uint i = 0; i < _candidates.length; i++) {
                newPoll.candidates.push(Candidate({
                    name: _candidates[i],
                    voteCount: 0
                }));
            }
        }
    
        function vote(uint _pollIndex, uint _candidateIndex) public {
            Poll storage p = polls[_pollIndex];
            require(!p.ended, "Poll has ended.");
            p.candidates[_candidateIndex].voteCount += 1;
        }
    
        function endPoll(uint _pollIndex) public {
            Poll storage p = polls[_pollIndex];
            require(!p.ended, "Poll has already ended.");
            p.ended = true;
        }
    }

    Breaking it down:

    • Structs: Used to represent a candidate and a poll.
    • Array of Polls: This array holds all polls created.
    • createPoll: This function allows the creation of a new poll with candidates.
    • vote: Lets a user vote for a candidate in a specific poll.
    • endPoll: Marks the poll as ended, preventing further voting.

    Solidity Challenge Exercise

    Here’s a little challenge: Add a function to count the votes for each candidate in a poll. Email your solution at hi@soliditybits.com, and we’ll discuss the best approaches in the next post!

    Wrap-Up

    We just touched upon the transformative power of smart contracts in a democratic process. As you can see, Solidity is more than a language; it’s a tool for fair and transparent governance in the digital age.

    Remember, the blockchain is immutable, so your votes, once cast, are etched in the digital ledger for eternity. Dive into this code, play around with it, and stay tuned for more adventures in the realm of Ethereum development!

    Until next time, keep weaving the digital fabric of the future with Solidity. Happy coding, explorers!

    Did you catch these already?

    See all posts
    Cover Image for Harnessing the Power of Blockchain and AI Together

    Harnessing the Power of Blockchain and AI Together

    3 min read

    Welcome back to our blog, where we explore the cutting-edge intersections of technology. Today, we’re diving into the fascinating convergence of blockchain technology and artificial intelligence (AI). This combination is poised to revolutionize sectors from finance to healthcare, offering unprecedented opportunities and presenting unique challenges. The Synergy of Blockchain and AI Blockchain and AI are […]

    Cover Image for Blockchain and the Environment: Addressing Sustainability

    Blockchain and the Environment: Addressing Sustainability

    3 min read

    As blockchain technology becomes increasingly prevalent across various sectors, its environmental impact, especially in terms of energy consumption, has come under scrutiny. This blog explores the ecological footprint of blockchain and discusses sustainable practices and technologies that are shaping a greener future for this industry. Understanding the Impact Blockchain, particularly networks like Bitcoin that use […]