pragma solidity ^0.4.22;
contract Voting{
mapping (bytes32 => uint8)public votesReceived;
bytes32[] public candidateList;
constructor(bytes32[] candidateNames)public{
candidateList = candidateNames;
}
function totalVotesFor(bytes32 candidate) view public returns(uint8){
require(validCandidate(candidate));
return votesReceived[candidate];
}
function voteForCandidate(bytes32 candidate) public {
require(validCandidate(candidate));
votesReceived[candidate] += 1;
}
function validCandidate(bytes32 candidate)view public returns(bool){
for(uint i = 0;i
Voting DApp
A Simple Voting Application
Candidate
Votes
Alice
Bob
Cary
Vote
注意
var web3 = new Web3(new Web3.providers.HttpProvider("Http://localhost:8545"));
var abi = JSON.parse('[{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"validCandidate","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint8"}],"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":"voteForCandidate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]');
// var contractAddr = "0x3577f4d4902f4766753056468bd08ef68df6c623";
var VotingContract = new web3.eth.Contract(abi);
// var contractInstance =new web3.eth.Contract(abi,contractAddr);
// contractInstance = VotingContract.at(contractAddr);
var contractInstance = VotingContract.at('0x3577f4d4902f4766753056468bd08ef68df6c623');
var candidates = {
"Alice":"candidate-1",
"Bob":"candidate-2",
"Cary":"candidate-3"
};
function voteForCandidate(){
let candidateName = $("#candidate").val();
try{
contractInstance.voteForCandidate(candidateName),{from:web3.eth.accounts[0]},(err,res)=>{
if(err)
console.log("Error:",err);
else{
let div_id = candidates[candidateName];
let count = contractInstance.totalVotesFor(candidateName).toString();
$("#" + id).html(count);
}
}
}catch(err){
}
};
$(document).ready(function(){
candidateNames = Object.keys(candidates);
for(let i=0;i
var http = require('http');
var fs = require('fs');
var url = require('url');
http.createServer(function(req,res){
var pathName = url.parse(req.url).pathname;
console.log("Request for:"+pathName + "received.");
fs.readFile(pathName.substr(1),function(err,data){
if(err){
console.log(err);
res.writeHead(400,{'Content-Type':"text/html"});
}else{
res.writeHead(200,{'Content-Type':"text/html"});
res.write(data.toString());
}
res.end();
});
}).listen(8888);