Skip to content

Commit

Permalink
Added original project
Browse files Browse the repository at this point in the history
  • Loading branch information
varadet committed Mar 30, 2019
1 parent 78febef commit c0067b0
Show file tree
Hide file tree
Showing 18 changed files with 9,695 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"env": {
"browser": true,
"es6": true,
"jquery": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"never"
]
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
.vscode/
build/
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Boilerplate with Truffle and webpack for dApps built on Ethereum

This boilerplate was made for this [blog post](https://medium.freecodecamp.org/developing-an-ethereum-decentralized-voting-application-a99de24992d9).
### Dependencies:
- [Nodejs 5.0+](https://nodejs.org/en/)
- [Truffle](https://github.com/trufflesuite/truffle)
- [Ganache](http://truffleframework.com/ganache/)

## Setup
```
npm install -g truffle
git clone https://github.com/tko22/truffle-webpack-boilerplate.git
cd truffle-webpack-boilerplate
npm install
npm run dev
```

23 changes: 23 additions & 0 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity ^0.4.2;

contract Migrations {
address public owner;
uint public last_completed_migration;

modifier restricted() {
if (msg.sender == owner) _;
}

function Migrations() {
owner = msg.sender;
}

function setCompleted(uint completed) restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
72 changes: 72 additions & 0 deletions contracts/Voters.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
pragma solidity ^0.4.18;
// written for Solidity version 0.4.18 and above that doesnt break functionality

contract Voting {
event AddedCandidate(uint candidateID);

// describes a Voter, which has an id and the ID of the candidate they voted for
struct Voter {
bytes32 uid; // bytes32 type are basically strings
uint candidateIDVote;
}
// describes a Candidate
struct Candidate {
bytes32 name;
bytes32 party;
bool doesExist;
}

// These state variables are used keep track of the number of Candidates/Voters and used to as a way to index them
uint numCandidates = 0; // declares a state variable - number Of Candidates
uint numVoters;


// Think of these as a hash table
mapping (uint => Candidate) candidates;
mapping (uint => Voter) voters;
mapping (bytes32 => bool) public voted;

constructor() public {
addCandidate("Sandesh Chatarmal","BJP");
addCandidate("Ajay Khaple","Congress");
addCandidate("Jaydeep Patil","Shivsena");
}
function addCandidate(bytes32 name, bytes32 party) public {
uint candidateID = numCandidates++;
// Create new Candidate Struct with name and saves it to storage.
candidates[candidateID] = Candidate(name,party,true);
AddedCandidate(candidateID);
}

function vote(bytes32 uid, uint candidateID) public {

if (candidates[candidateID].doesExist == true) {
uint voterID = numVoters++; //voterID is the return variable
voters[voterID] = Voter(uid,candidateID);
voted[uid] = true;
}
}


// finds the total amount of votes for a specific candidate by looping through voters
function totalVotes(uint candidateID) view public returns (uint) {
uint numOfVotes = 0; // we will return this
for (uint i = 0; i < numVoters; i++) {
if (voters[i].candidateIDVote == candidateID) {
numOfVotes++;
}
}
return numOfVotes;
}

function getNumOfCandidates() public view returns(uint) {
return numCandidates;
}

function getNumOfVoters() public view returns(uint) {
return numVoters;
}
function getCandidate(uint candidateID) public view returns (uint,bytes32, bytes32) {
return (candidateID,candidates[candidateID].name,candidates[candidateID].party);
}
}
5 changes: 5 additions & 0 deletions migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var Migrations = artifacts.require("./Migrations.sol")

module.exports = function(deployer) {
deployer.deploy(Migrations)
}
5 changes: 5 additions & 0 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var Voting = artifacts.require("Voting")

module.exports = function(deployer) {
deployer.deploy(Voting).then(()=>console.warn(Voting.address)).then(()=>Voting.deployed()).then(_instance => console.log(_instance.address))
}
Loading

0 comments on commit c0067b0

Please sign in to comment.