الدرس رقم 2

Implementing Voter Registration and Vote Casting

In this lesson, we will continue developing our decentralized voting system by implementing the functionalities for voter registration and vote casting. We'll start by expanding our Voter contract and then dive into the details of Solidity's mappings and arrays.

Expanding the Voter Contract

We will add a proposal system and an option for registered voters to vote. For this, we’ll need an array to store the proposals and a new function for voting.

In the Voter contract, add the following lines of code:

Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Voter {
    struct Person {
        bool voted;  
        uint vote;   
    }

    struct Proposal {
        string name;   
        uint voteCount; 
    }

    Proposal[] public proposals;
    mapping(address => Person) public voters;

    function registerVoter() public {
        voters[msg.sender].voted = false;
    }

    function addProposal(string memory _name) public {
        proposals.push(Proposal(_name, 0));
    }

    function vote(uint _proposal) public { 
     require(_proposal < proposals.length, "Invalid proposal index."); // This is the added check 

     Person storage sender = voters[msg.sender]; 
     require(!sender.voted, "Already voted."); 
     sender.voted = true; 
     sender.vote = _proposal; 

     proposals[_proposal].voteCount += 1; 
    }
}

Understanding the Code

Let’s break down the new code:

  1. Proposal Struct: We’ve added a new struct, Proposal, that contains the name of the proposal and the count of votes it has received.

  2. Proposals Array: The proposals array holds all the proposals in the voting system.

  3. Add Proposal Function: The addProposal function allows us to add a new proposal to the proposals array. The proposal name is passed as a parameter, and the vote count is initially set to zero.

  4. Vote Function: The vote function allows a registered voter to cast their vote. It takes the index of a proposal as a parameter and increases the proposal’s vote count by one. It also checks if the voter has already voted and updates the voter’s status.

Compiling and Testing

Now that we have added more features to our contract, it’s time to compile and test it. In the Remix IDE, click on the Solidity compiler icon on the left sidebar and then click on the “Compile” button.

To test your contract, go to the “Deploy & Run Transactions” tab (the one below the Solidity compiler icon) and click on the “Deploy” button. Once the contract is deployed, you can interact with it by calling its functions from this tab. Try registering a voter, adding a proposal, and casting a vote to see how it works.

In the next lesson, we will implement the functionality to tally the votes and announce the result of the voting process. Until then, feel free to explore and experiment with the contract. Solidity offers many more features that can make your contract more robust and secure. Happy coding!

إخلاء المسؤولية
* ينطوي الاستثمار في العملات الرقمية على مخاطر كبيرة. فيرجى المتابعة بحذر. ولا تهدف الدورة التدريبية إلى تقديم المشورة الاستثمارية.
* تم إنشاء الدورة التدريبية من قبل المؤلف الذي انضم إلى مركز التعلّم في Gate. ويُرجى العلم أنّ أي رأي يشاركه المؤلف لا يمثّل مركز التعلّم في Gate.
الكتالوج
الدرس رقم 2

Implementing Voter Registration and Vote Casting

In this lesson, we will continue developing our decentralized voting system by implementing the functionalities for voter registration and vote casting. We'll start by expanding our Voter contract and then dive into the details of Solidity's mappings and arrays.

Expanding the Voter Contract

We will add a proposal system and an option for registered voters to vote. For this, we’ll need an array to store the proposals and a new function for voting.

In the Voter contract, add the following lines of code:

Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Voter {
    struct Person {
        bool voted;  
        uint vote;   
    }

    struct Proposal {
        string name;   
        uint voteCount; 
    }

    Proposal[] public proposals;
    mapping(address => Person) public voters;

    function registerVoter() public {
        voters[msg.sender].voted = false;
    }

    function addProposal(string memory _name) public {
        proposals.push(Proposal(_name, 0));
    }

    function vote(uint _proposal) public { 
     require(_proposal < proposals.length, "Invalid proposal index."); // This is the added check 

     Person storage sender = voters[msg.sender]; 
     require(!sender.voted, "Already voted."); 
     sender.voted = true; 
     sender.vote = _proposal; 

     proposals[_proposal].voteCount += 1; 
    }
}

Understanding the Code

Let’s break down the new code:

  1. Proposal Struct: We’ve added a new struct, Proposal, that contains the name of the proposal and the count of votes it has received.

  2. Proposals Array: The proposals array holds all the proposals in the voting system.

  3. Add Proposal Function: The addProposal function allows us to add a new proposal to the proposals array. The proposal name is passed as a parameter, and the vote count is initially set to zero.

  4. Vote Function: The vote function allows a registered voter to cast their vote. It takes the index of a proposal as a parameter and increases the proposal’s vote count by one. It also checks if the voter has already voted and updates the voter’s status.

Compiling and Testing

Now that we have added more features to our contract, it’s time to compile and test it. In the Remix IDE, click on the Solidity compiler icon on the left sidebar and then click on the “Compile” button.

To test your contract, go to the “Deploy & Run Transactions” tab (the one below the Solidity compiler icon) and click on the “Deploy” button. Once the contract is deployed, you can interact with it by calling its functions from this tab. Try registering a voter, adding a proposal, and casting a vote to see how it works.

In the next lesson, we will implement the functionality to tally the votes and announce the result of the voting process. Until then, feel free to explore and experiment with the contract. Solidity offers many more features that can make your contract more robust and secure. Happy coding!

إخلاء المسؤولية
* ينطوي الاستثمار في العملات الرقمية على مخاطر كبيرة. فيرجى المتابعة بحذر. ولا تهدف الدورة التدريبية إلى تقديم المشورة الاستثمارية.
* تم إنشاء الدورة التدريبية من قبل المؤلف الذي انضم إلى مركز التعلّم في Gate. ويُرجى العلم أنّ أي رأي يشاركه المؤلف لا يمثّل مركز التعلّم في Gate.