windows 10 64bitjavascript
youclavier -- 以太坊投票Dapp教程css
準備接手一個IPFS+Ethereum的項目,先學習一下Ethereum,並嘗試完成一個Hello World。html
nvm install 9.11.1
nvm use 9.11.1
複製代碼
npm install ganache-cli
npm install web3@0.20.1
npm install solc@0.4.21 //此處原博客沒有版本,會安裝高於0.4的版本,會致使後續編譯smart contract編譯失敗
複製代碼
在安裝了ganache-cli與web3時,因爲教程版本問題會出現報錯,可是不影響。java
node_modules\.bin\ganache-cli
複製代碼
pragma solidity ^0.4.18;
contract Voting {
mapping (bytes32 => uint8) public votesReceived;
bytes32[] public candidateList;
function Voting(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;
}}
複製代碼
> Web3 = require('web3')
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
> web3.eth.accounts
複製代碼
輸入以上最後一條命令後會獲取Ganache建立的10個賬號,以下node
> code = fs.readFileSync('Voting.sol').toString()
> solc = require('solc')
> compiledCode = solc.compile(code)
複製代碼
所有完成會獲得以下截圖的輸出,表示smart contract編譯成功 jquery
> abi = JSON.parse(compiledCode.contracts[':Voting'].interface)
> VotingContract = web3.eth.contract(abi)
> byteCode = compiledCode.contracts[':Voting'].bytecode
> deployedContract = VotingContract.new(['James', 'Norah', 'Jones'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
> deployedContract.address
複製代碼
此時會獲取address,記下來後續會用到web
> contractInstance = VotingContract.at(deployedContract.address)
複製代碼
由cdn不知什麼緣由不可用,因此直接下載源文件,連接以下 web3.js 0.20.6npm
<!DOCTYPE html>
<html>
<head>
<title>DApp</title>
<link href='https://fonts.googleapis.com/css?family=Open Sans:400,700' rel='stylesheet' type='text/css'>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
</head>
<body class="container">
<h1>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>James</td>
<td id="candidate-1"></td>
</tr>
<tr>
<td>Norah</td>
<td id="candidate-2"></td>
</tr>
<tr>
<td>Jones</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="web3.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script language="javascript" type="text/javascript">
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
abi = JSON.parse('[{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"validCandidate","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"x","type":"bytes32"}],"name":"bytes32ToString","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"voteForCandidate","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"contractOwner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"type":"constructor"}]')
VotingContract = web3.eth.contract(abi);
contractInstance = VotingContract.at('0x47f49b300eb86d972f91f103913376fb0a8e52e7');
candidates = {"James": "candidate-1", "Norah": "candidate-2", "Jones": "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);
}
});
</script>
</html>
複製代碼
以上步驟就完成了一個基於Ethereum的投票Dapp的完整搭建流程,整合個補全後步驟應該不會有坑的能夠順利搭建完成。bootstrap
就像「hello world」的字面意思同樣,0-1的過程是最艱難的,可是開了頭,剩下的1-n也就會順暢很多。windows
要獲取更多Haytham原創文章,請關注公衆號"許聚龍":