Skip to content

Commit d1b955f

Browse files
committed
new functions for smart contract
1 parent ed97e75 commit d1b955f

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

.eslintrc.json

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
11
{
2-
"extends": "airbnb-base"
2+
"env": {
3+
"browser": true,
4+
"es6": true,
5+
"jquery": true
6+
},
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"sourceType": "module"
10+
},
11+
"rules": {
12+
"indent": [
13+
"error",
14+
2
15+
],
16+
"linebreak-style": [
17+
"error",
18+
"unix"
19+
],
20+
"quotes": [
21+
"error",
22+
"double"
23+
],
24+
"semi": [
25+
"error",
26+
"never"
27+
]
28+
}
329
}

contracts/Voting.sol

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ contract Voting {
1010
// Think of this as a hash table, with the key as a uint and value of the struct Candidate
1111
mapping (uint => Candidate) candidates;
1212

13-
function newCandidate(bytes32 name) public returns (uint candidateID) {
13+
function addCandidate(bytes32 name) public returns (uint candidateID) {
1414
// candidateID is the return variable
1515
candidateID = numCandidates++;
1616
// Create new Candidate Struct with name and saves it to storage.
1717
candidates[candidateID] = Candidate(name,0);
1818
}
1919

20+
function vote(uint candidateID) public {
21+
candidates[candidateID].numOfVotes++;
22+
}
2023

24+
function totalVotes(uint candidateID) view public returns (uint) {
25+
return candidates[candidateID].numOfVotes;
26+
}
2127
}

src/js/app.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
import "../stylesheets/app.css";
2-
31
// Import libraries we need.
42
import { default as Web3} from 'web3';
53
import { default as contract } from 'truffle-contract'
64

7-
import voting_artifacts from '../../build/contracts/Voting.json'
8-
9-
var Voting = contract(voting_artifacts);
5+
import votingArtifacts from '../../build/contracts/Voting.json'
106

7+
var Voting = contract(voting_artifacts)
118

129

13-
14-
$( document ).ready(function() {
10+
$(document).ready(function() {
1511
// Is there an injected web3 instance?
1612
if (typeof web3 !== 'undefined') {
1713
console.warn("Using web3 detected from external source like Metamask")
1814
// If there is a web3 instance(in Mist/Metamask), then we use its provider to create our web3object
19-
window.web3 = new Web3(web3.currentProvider);
15+
window.web3 = new Web3(web3.currentProvider)
2016
} else {
21-
console.warn("No web3 detected. Falling back to http://localhost:7545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
17+
console.warn("No web3 detected. Falling back to http://localhost:7545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask")
2218
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
23-
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
19+
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))
2420
}
2521

26-
Voting.setProvider(web3.currentProvider);
27-
28-
});
22+
Voting.setProvider(web3.currentProvider)
23+
})

0 commit comments

Comments
 (0)