Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eth/node_modules
34 changes: 34 additions & 0 deletions eth/Voting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const Web3 = require("web3");
const fs = require("fs");

const URL = "http://localhost:8545";
const web3 = new Web3(new Web3.providers.HttpProvider(URL));

// account obtained from the local testnet (ganache)
var voter;
web3.eth.getAccounts().then(acc => (voter = acc[0]));

// ABI, and the bytecode to be deployed on the network
const abi = JSON.parse(fs.readFileSync("Voting_sol_Voting.abi").toString());
const bytecode = fs.readFileSync("Voting_sol_Voting.bin").toString();

// Getting an instance of the contract
const deployedContract = new web3.eth.Contract(abi);

//The address of the deployed contract
console.log(deployedContract.options.address)

// to be called from the frontend to vote for a candidate
const voteForCandidate = candidate => {
deployedContract.methods
.incrementVote(web3.utils.asciiToHex(candidate)) // converting from string to bytes32 format as per contract
.send({ from: voter })
.then(res => {
deployedContract.methods
.totalVotesSecured(web3.utils.asciiToHex(candidate))
.call()
.then(res1 => {
console.log(res1); // number of votes
});
});
};
39 changes: 39 additions & 0 deletions eth/Voting.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pragma solidity >= 0.4.0 < 0.6.0;

contract Voting {

// list of candidates
bytes32[] public candidateList;

constructor(bytes32[] memory candidateNames) public{
candidateList = candidateNames;
}

// mapping for candidates to number of votes (bytes32 is used instead of string for less gas consumption)
mapping (bytes32 => uint256) public votesReceived;



// check if a candidate is valid or not
function IsCandidateValid(bytes32 candidate) view public returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if(candidateList[i] == candidate) {
return true;
}
}

return false;
}

// Add vote to the candidate
function incrementVote(bytes32 candidate) public {
require(IsCandidateValid(candidate));
votesReceived[candidate] += 1;
}

// get the number of votes
function totalVotesSecured(bytes32 candidate) view public returns (uint256) {
require(IsCandidateValid(candidate));
return votesReceived[candidate];
}
}
1 change: 1 addition & 0 deletions eth/Voting_sol_Voting.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesSecured","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"IsCandidateValid","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"incrementVote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
1 change: 1 addition & 0 deletions eth/Voting_sol_Voting.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561001057600080fd5b506040516103ff3803806103ff8339810180604052602081101561003357600080fd5b81019080805164010000000081111561004b57600080fd5b8281019050602081018481111561006157600080fd5b815185602082028301116401000000008211171561007e57600080fd5b5050929190505050806000908051906020019061009c9291906100a3565b5050610115565b8280548282559060005260206000209081019282156100df579160200282015b828111156100de5782518255916020019190600101906100c3565b5b5090506100ec91906100f0565b5090565b61011291905b8082111561010e5760008160009055506001016100f6565b5090565b90565b6102db806101246000396000f3fe608060405234801561001057600080fd5b5060043610610074576000357c0100000000000000000000000000000000000000000000000000000000900480631778c116146100795780637021939f146100bb57806376a3633f146100fd578063b13c744b14610143578063b13dbbd514610185575b600080fd5b6100a56004803603602081101561008f57600080fd5b81019080803590602001909291905050506101b3565b6040518082815260200191505060405180910390f35b6100e7600480360360208110156100d157600080fd5b81019080803590602001909291905050506101e4565b6040518082815260200191505060405180910390f35b6101296004803603602081101561011357600080fd5b81019080803590602001909291905050506101fc565b604051808215151515815260200191505060405180910390f35b61016f6004803603602081101561015957600080fd5b8101908080359060200190929190505050610254565b6040518082815260200191505060405180910390f35b6101b16004803603602081101561019b57600080fd5b8101908080359060200190929190505050610277565b005b60006101be826101fc565b15156101c957600080fd5b60016000838152602001908152602001600020549050919050565b60016020528060005260406000206000915090505481565b600080600090505b600080549050811015610249578260008281548110151561022157fe5b9060005260206000200154141561023c57600191505061024f565b8080600101915050610204565b50600090505b919050565b60008181548110151561026357fe5b906000526020600020016000915090505481565b610280816101fc565b151561028b57600080fd5b6001806000838152602001908152602001600020600082825401925050819055505056fea165627a7a72305820c79934d2250f7854b5eeb344bfb1bd331dfd718a68dee65627d269f4e28f54dc0029
Loading