From e8e0f2bf6ea4f73511d2b9f96504c5e3dfd9509e Mon Sep 17 00:00:00 2001 From: Otaiki1 Date: Mon, 17 Apr 2023 23:43:01 +0100 Subject: [PATCH 1/4] refac: fix contract logic --- README.md | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 84cf7eb..4e1fa8c 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,18 @@ ## Table Of Contents: -- [Creating a Simple Voting DApp on Celo using Solidity and Truffle](#creating-a-simple-voting-dapp-on-celo-using-solidity-and-truffle) -- [Table Of Contents](#table-of-contents) -- [Introduction](#introduction) -- [Setup Environment](#1-setup-environment) -- [Smart Contract](#2-smart-contract) -- [Migration](#3-migration) -- [Celo Network Configuration](#4-celo-network-configuration) -- [Deploy Contract to Celo Network](#5-deploy-contract-to-celo-network) -- [Build the User Interface](#6-build-the-user-interface) -- [Add Authentication](#7-add-authentication) -- [Authentication and Transaction Signing using DappKit](#8-authentication-and-transaction-signing-using-dappkit) -- [Display Vote Counts](#9-display-vote-counts) -- [Conclusion](#conclusion) +- [Creating a Simple Voting DApp on Celo using Solidity and Truffle-](#creating-a-simple-voting-dapp-on-celo-using-solidity-and-truffle-) + - [Table Of Contents:](#table-of-contents) + - [Introduction:](#introduction) + - [Step 1: Setup Environment-](#step-1-setup-environment-) + - [Step 2: Smart Contract-](#step-2-smart-contract-) + - [Step 3: Migration-](#step-3-migration-) + - [Step 4: Celo Network Configuration-](#step-4-celo-network-configuration-) + - [Step 5: Deploy Contract to Celo Network-](#step-5-deploy-contract-to-celo-network-) + - [Step 6: Build the User Interface-](#step-6-build-the-user-interface-) + - [Step 7: Add Authentication-](#step-7-add-authentication-) + - [Step 9: Display Vote Counts-](#step-9-display-vote-counts-) + - [Conclusion:](#conclusion) ## Introduction: @@ -60,8 +59,6 @@ contract Voting { // Function to vote for a candidate function voteForCandidate(bytes32 candidate) public { - // Require that the candidate has received at least one vote before - require(votes[candidate] > 0, "Invalid candidate"); // Increment the vote count for the candidate votes[candidate] += 1; From 2aa95626426ce5f3d354cf89702b7a3980723b90 Mon Sep 17 00:00:00 2001 From: Otaiki1 Date: Mon, 17 Apr 2023 23:46:27 +0100 Subject: [PATCH 2/4] refac: add checks to prevent double Voting --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 4e1fa8c..5d871d3 100644 --- a/README.md +++ b/README.md @@ -56,10 +56,16 @@ pragma solidity ^0.8.0; contract Voting { // Define a mapping to store candidate votes with candidate names as keys and vote counts as values mapping (bytes32 => uint256) public votes; + mapping(address => bool) public hasVoted; // Function to vote for a candidate function voteForCandidate(bytes32 candidate) public { + //check to ensure voter doesnt vote twice + require(hasVoted[msg.sender], "Already Voted"); + //update voter status + hasVoted[msg.sender] = true; + // Increment the vote count for the candidate votes[candidate] += 1; } From 41cc902b49b4650fc48a7e54ec9e9c387e4bbbe1 Mon Sep 17 00:00:00 2001 From: Otaiki1 Date: Mon, 17 Apr 2023 23:48:22 +0100 Subject: [PATCH 3/4] chore: update explainer --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5d871d3..2dbcbbe 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ contract Voting { //update voter status hasVoted[msg.sender] = true; - + // Increment the vote count for the candidate votes[candidate] += 1; } @@ -83,7 +83,7 @@ contract Voting { ``` -We defined votes mapping in this contract that will keep track of how many votes each contender has earned. VoteForCandidate and totalVotesFor are two additional functions that we define. Users can vote for their preferred candidate using the voteForCandidate function, and the totalVotesFor function gives the total number of votes cast for a specific candidate. +We defined votes mapping in this contract that will keep track of how many votes each contender has earned, while the hasVoted mapping tracks if a voter has voted before. VoteForCandidate and totalVotesFor are two additional functions that we define. Users can vote for their preferred candidate using the voteForCandidate function,it checks to ensure voters dont vote twice, and the totalVotesFor function gives the total number of votes cast for a specific candidate. ## Step 3: Migration- From ac89835230b2bb734ce26302d127f4a6ddfc7430 Mon Sep 17 00:00:00 2001 From: Otaiki1 Date: Tue, 18 Apr 2023 00:01:07 +0100 Subject: [PATCH 4/4] chore: update second part of contract code --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 2dbcbbe..d4f94ae 100644 --- a/README.md +++ b/README.md @@ -389,6 +389,8 @@ pragma solidity >=0.4.22 <0.9.0; contract Voting { string[] public candidates; // Array to store the list of candidates mapping (string => uint256) public votes; // Mapping to store the votes count for each candidate + mapping(address => bool) public hasVoted; //Mapping to ensure voter doesnt vote twice + constructor() { candidates = ["Candidate 1", "Candidate 2", "Candidate 3"]; // Constructor to initialize the candidates array with initial candidates @@ -396,6 +398,13 @@ contract Voting { function vote(string memory candidate) public { require(validCandidate(candidate)); // Function to cast a vote for a valid candidate + + //check to ensure voter doesnt vote twice + require(hasVoted[msg.sender], "Already Voted"); + + //update voter status + hasVoted[msg.sender] = true; + votes[candidate] += 1; // Increment the vote count for the candidate }