区块链以太坊学习四:DAPP投票-简易

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 < candidateList.length; i++) {
if (candidateList[i] == candidate) {
 return true; 

}
}
return false;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Voting DApp</title>
<link
href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/boot
strap.min.css' rel='stylesheet' type='text/css'>
</head>
<body class="container">
<h1>A Simple Voting Application</h1>
<div class="table-responsive">
 <table class="table table-bordered">
 <thead>
<tr>
 <th>Candidate</th>
 <th>Votes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td id="candidate-1"></td>
 </tr>
 <tr>
 <td>Bob</td>
 <td id="candidate-2"></td>
</tr>
 <tr>
 <td>Cary</td>
 <td id="candidate-3"></td>
 </tr>
</tbody>
</table>
</div>
<input type="text" id="candidate" />
<a href="#" onclick="voteForCandidate()" class="btn
btn-primary">Vote</a>
</body>
<script
src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.mi
n.js">
</script>

web3 = new Web3(new
Web3.providers.HttpProvider("http://localhost:8545"));
abi = JSON.parse('[{"constant":false,…}]')
VotingContract = web3.eth.contract(abi);
contractInstance =
VotingContract.at('0x329f5c190380ebcf640a90d06eb1db2d68503a53'
);
candidates = {"Alice": "candidate-1", "Bob": "candidate-2",
"Cary": "candidate-3"};
function voteForCandidate(candidate) {
candidateName = $("#candidate").val();
try {
contractInstance.voteForCandidate(candidateName,
{from: web3.eth.accounts[0]},
function() {
let div_id = candidates[candidateName];
$("#"+div_id).html(
contractInstance.totalVotesFor
.call(candidateName)
.toString());
}
);
} catch (err) {
}
}
$(document).ready(function() {
candidateNames = Object.keys(candidates);
for (var i = 0; i < candidateNames.length; i++) {
let name = candidateNames[i];
let val = contractInstance.totalVotesFor
.call(name).toString()
$("#"+candidates[name]).html(val);
}
});
In the node console> Web3 = require('web3')
> web3 = new Web3(new
Web3.providers.HttpProvider("http://localhost:8545"));
> web3.eth.accounts
['0x5c252a0c0475f9711b56ab160a1999729eccce97'
'0x353d310bed379b2d1df3b727645e200997016ba3']
> code = fs.readFileSync('Voting.sol').toString()
> solc = require('solc')
> compiledCode = solc.compile(code)
Execute this in your node console:
> abiDefinition =
JSON.parse(compiledCode.contracts[':Voting'].interface)
> VotingContract = web3.eth.contract(abiDefinition)
> byteCode = compiledCode.contracts[':Voting'].bytecode
> deployedContract =
VotingContract.new(['Alice','Bob','Cary'],{data: byteCode, from:
web3.eth.accounts[0], gas: 4700000})
> deployedContract.address
'0x0396d2b97871144f75ba9a9c8ae12bf6c019f610'
// Your address will be different
> contractInstance = VotingContract.at(deployedContract.address)
In your node console:
> contractInstance.totalVotesFor.call('Rama')
{ [String: '0'] s: 1, e: 0, c: [ 0 ] }
> contractInstance.voteForCandidate('Rama', {from:
web3.eth.accounts[0]})
'0xdedc7ae544c3dde74ab5a0b07422c5a51b5240603d31074f5b75c0ebc78
6bf53'
> contractInstance.voteForCandidate('Rama', {from:
web3.eth.accounts[0]})
'0x02c054d238038d68b65d55770fabfca592a5cf6590229ab91bbe7cd72da
46de9'
> contractInstance.voteForCandidate('Rama', {from:
web3.eth.accounts[0]})
'0x3da069a09577514f2baaa11bc3015a16edf26aad28dffbcd126bde2e71f
2b76f'
>
contractInstance.totalVotesFor.call('Rama').toLocaleString()'3
'

 

In the node console> Web3 = require('web3') > web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); > web3.eth.accounts ['0x5c252a0c0475f9711b56ab160a1999729eccce97' '0x353d310bed379b2d1df3b727645e200997016ba3'] > code = fs.readFileSync('Voting.sol').toString() > solc = require('solc') > compiledCode = solc.compile(code)
posted @ 2022-09-14 14:38  caolingyi  阅读(82)  评论(0编辑  收藏  举报