區塊鏈 Hello World -- 基於以太坊的投票Dapp

指路牌

  • 以太坊
  • 區塊鏈
  • Dapp
  • 以太坊hello world

環境

windows 10 64bit

參考博客

youclavier -- 以太坊投票Dapp教程

背景

準備接手一個IPFS+Ethereum的項目,先學習一下Ethereum,並嘗試完成一個Hello World。

步驟

  1. 參考我另一片blog, 安裝nvm
  2. 安裝node 9.11.1 並切換環境
    nvm install 9.11.1
    nvm use 9.11.1
  3. 創建一個新的工作目錄,並在命令行索引到該路徑
  4. 安裝ganche-cli、web3、solc
    npm install ganache-cli
    npm install [email protected]
    npm install [email protected]       //此處原博客沒有版本,會安裝高於0.4的版本,會導致後續編譯smart contract編譯失敗 

    在安裝了ganache-cli與web3時,由於教程版本問題會出現報錯,但是不影響。

  5. 啓動ganache-cli
    node_modules\.bin\ganache-cli

    ganache-cli啓動後輸出如下

  6. 使用Solidity創建Smart Contract,命名爲:Voting.sol
    
    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;
}}

7. 啓動node交互控制檯,依次輸入以下命令

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
web3.eth.accounts


 輸入以上最後一條命令後會獲取Ganache創建的10個帳號,如下

區塊鏈 Hello World -- 基於以太坊的投票Dapp

> code = fs.readFileSync('Voting.sol').toString()
> solc = require('solc')
> compiledCode = solc.compile(code)

全部完成會得到如下截圖的輸出,表示smart contract編譯成功
區塊鏈 Hello World -- 基於以太坊的投票Dapp
8.部署smart contract

> 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,記下來後續會用到

contractInstance = VotingContract.at(deployedContract.address)
  1. 下載web3.js文件,下載後放在工作根目錄下。
    由cdn不知什麼原因不可用,所以直接下載源文件,鏈接如下
    web3.js 0.20.6
  2. 在根目錄下創建index.html文件,並粘貼以下代碼,需要在截圖標出處,更換成第8步自己部署的smart contract的address
    
    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;DApp&lt;/title&gt;
    &lt;link href='https://fonts.googleapis.com/css?family=Open Sans:400,700' rel='stylesheet' type='text/css'&gt;
    &lt;link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet' type='text/css'&gt;
    &lt;/head&gt;
    &lt;body class="container"&gt;
    &lt;h1&gt;Voting Application&lt;/h1&gt;
    &lt;div class="table-responsive"&gt;
    &lt;table class="table table-bordered"&gt;
    &lt;thead&gt;
    &lt;tr&gt;
    &lt;th&gt;Candidate&lt;/th&gt;
    &lt;th&gt;Votes&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
    &lt;tr&gt;
    &lt;td&gt;James&lt;/td&gt;
    &lt;td id="candidate-1"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;Norah&lt;/td&gt;
    &lt;td id="candidate-2"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;Jones&lt;/td&gt;
    &lt;td id="candidate-3"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tbody&gt;
    &lt;/table&gt;
    &lt;/div&gt;
    &lt;input type="text" id="candidate" /&gt;
    &lt;a href="#" onclick="voteForCandidate()" class="btn btn-primary"&gt;Vote&lt;/a&gt;
    &lt;/body&gt;
<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>


![更換address](https://user-images.githubusercontent.com/37465243/64078738-f976e400-cd10-11e9-82ef-20468a8e6ba5.png)

11. 在瀏覽器打開index.html,輸入Candidate中的人名後,點擊Vote即可投票,投票後效果如下
![](https://user-images.githubusercontent.com/37465243/64078736-f8de4d80-cd10-11e9-9e1a-173c812f2264.png)
每次點擊投票,也都會生成一個新的區塊,效果如下。
![](https://user-images.githubusercontent.com/37465243/64078737-f8de4d80-cd10-11e9-874c-d3e8570432fb.png)

## 後記
以上步驟就完成了一個基於Ethereum的投票Dapp的完整搭建流程,整合個補全後步驟應該不會有坑的可以順利搭建完成。

就像“hello world”的字面意思一樣,0-1的過程是最艱難的,但是開了頭,剩下的1-n也就會順暢不少。

####
***要獲取更多Haytham原創文章,請關注公衆號"許聚龍":***
![我的微信公衆號](https://user-images.githubusercontent.com/37465243/63688227-5b2ede00-c839-11e9-9aa9-2b461444f463.png)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章