Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dkk3675 committed Apr 4, 2023
1 parent 996ec5f commit 6aee792
Show file tree
Hide file tree
Showing 40 changed files with 8,378 additions and 1,143 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

/.env
2 changes: 2 additions & 0 deletions election-contract/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

/.env
175 changes: 120 additions & 55 deletions election-contract/contract/Election.sol
Original file line number Diff line number Diff line change
@@ -1,77 +1,142 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.4.17;

contract Election{

address private admin;
contract Election {
address public admin;

mapping(uint => Contestant) public contestants;
uint public electionNumber;

// mapping(address => bool) public voters;
mapping(address => Voter) private voters;
mapping(uint => mapping(uint => Contestant)) public contestants;
mapping(uint => Contestant[]) public details;

// uint voter counter;
uint public voterCount;
// mapping(address => bool) public voters;
mapping(uint => mapping(string => Voter)) private voters;
string[] private users;

// uint contestant counter;
uint public contestantCount;
uint public contestantCount;
uint public totalVotes;

enum PHASE{ reg, voting, done }
PHASE public state;
enum PHASE {
reg,
voting,
done,
nothing
}
PHASE public state;

struct Contestant{
uint id;
string name;
uint voteCount;
string party;
uint age;
string qualification;
}
struct Contestant {
string name;
uint voteCount;
string party;
uint age;
string qualification;
}

struct Voter{
bool hasVoted;
uint vote;
bool isRegistered;
}
struct Voter {
bool hasVoted;
uint vote;
}

function Election() public{
admin = msg.sender;
state = PHASE.reg;
}
function Election() public {
admin = msg.sender;
state = PHASE.nothing;
electionNumber = 0;
}

function changeState(PHASE x) onlyAdmin public{
require(x > state);
function changeState(PHASE x) public onlyAdmin {
if(x == PHASE.voting){
require(contestantCount > 0);
}
state = x;
}

function addContestant(string memory _name , string memory _party , uint _age , string memory _qualification) public onlyAdmin validState(PHASE.reg){
contestantCount++;
contestants[contestantCount] = Contestant(contestantCount,_name,0,_party,_age,_qualification);
}
function search(string memory current_aadhar) public view returns (bool){
for(uint i=0;i<users.length;i++){
if(keccak256(current_aadhar) == keccak256(users[i])){
return false;
}
}
return true;
}

function voterRegisteration(address user) public onlyAdmin validState(PHASE.reg){
voterCount++;
voters[user].isRegistered = true;
}
function hasVoted(string memory aadhar) public view returns (bool){
return voters[electionNumber][aadhar].hasVoted;
}

function reset() public payable onlyAdmin {
changeState(PHASE.nothing);
electionNumber++;
users = new string[](0);
contestantCount = 0;
totalVotes = 0;
admin.transfer(this.balance);
}

function vote(uint _contestantId) public validState(PHASE.voting){
function addContestant(
string memory aadhar,
string memory _name,
string memory _party,
uint _age,
string memory _qualification
) public payable validState(PHASE.reg) {
require(msg.value == 0.1 ether);
contestantCount++;
contestants[electionNumber][contestantCount] = Contestant(
_name,
0,
_party,
_age,
_qualification
);
voterRegistration(aadhar);
}

function voterRegistration(
string memory aadhar
) public validState(PHASE.reg) {
users.push(aadhar);
voters[electionNumber][aadhar].hasVoted = false;
}

function vote(uint _contestantId,string memory aadhar) public validState(PHASE.voting) {
require(contestantCount > 0);
require(voters[msg.sender].isRegistered);
require(!voters[msg.sender].hasVoted);
require(_contestantId > 0 && _contestantId <= contestantCount);
contestants[_contestantId].voteCount++;
voters[msg.sender].hasVoted = true;
voters[msg.sender].vote = _contestantId;
}
require(!voters[electionNumber][aadhar].hasVoted);
contestants[electionNumber][_contestantId].voteCount++;
voters[electionNumber][aadhar].hasVoted = true;
voters[electionNumber][aadhar].vote = _contestantId;
totalVotes++;
}

modifier onlyAdmin(){
require(msg.sender == admin);
_;
}

modifier validState(PHASE x){
require(state == x);
_;
function getResults() public onlyAdmin{
changeState(PHASE.done);
uint maxVotes = contestants[electionNumber][1].voteCount;
for(uint i = 2;i <= contestantCount;i++){
if(contestants[electionNumber][i].voteCount > maxVotes){
maxVotes = contestants[electionNumber][i].voteCount;
}
}
for(i = 1;i <= contestantCount;i++){
if(contestants[electionNumber][i].voteCount == maxVotes){
details[electionNumber].push(contestants[electionNumber][i]);
}
}
}

function voterCount() public view returns (uint){
return users.length;
}

function detailCount() public view returns (uint){
return details[electionNumber].length;
}

modifier onlyAdmin() {
require(msg.sender == admin);
_;
}

modifier validState(PHASE x) {
require(state == x);
_;
}
}
19 changes: 0 additions & 19 deletions election-contract/contract/Migrations.sol

This file was deleted.

7 changes: 4 additions & 3 deletions election-contract/deploy.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const HDWalletProvider = require("@truffle/hdwallet-provider");
const Web3 = require("web3");
const contract = require("./compile");
require('dotenv');
// console.log(interface)

const provider = new HDWalletProvider(
"genre snake large cloth rare basic confirm tiger bread unable police pill",
"https://goerli.infura.io/v3/68bef7a42aaa4bd88ccb0fd4065ca789"
process.env.PERSONAL_KEY,
process.env.INFURA_API_KEY
);
const web3 = new Web3(provider);

Expand All @@ -16,7 +17,7 @@ const deploy = async () => {

const result = await new web3.eth.Contract(JSON.parse(contract.interface))
.deploy({ data: contract.bytecode })
.send({ gas: "1000000", from: accounts[0] });
.send({ gas: "5000000", from: accounts[0] });

// console.log(result);
console.log("Contract deployed to", result.options.address);
Expand Down
1 change: 1 addition & 0 deletions election-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"dependencies": {
"@truffle/hdwallet-provider": "^2.1.8",
"dotenv": "^16.0.3",
"solc": "0.4.17",
"web3": "^1.8.2"
},
Expand Down
5 changes: 5 additions & 0 deletions election-contract/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,11 @@ dom-walk@^0.1.0:
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.2.tgz#0c548bef048f4d1f2a97249002236060daa3fd84"
integrity sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w==

dotenv@^16.0.3:
version "16.0.3"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.3.tgz#115aec42bac5053db3c456db30cc243a5a836a07"
integrity sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==

ecc-jsbn@~0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9"
Expand Down
Loading

0 comments on commit 6aee792

Please sign in to comment.