Block chain Hello World - Dapp vote Ethernet-based Square

signpost

  • Ethernet Square
  • Block chain
  • Dapp
  • Ethernet Square hello world

surroundings

windows 10 64bit

Reference blog

youclavier - Ethernet Tutorial Square vote Dapp

background

IPFS + Ethereum prepared to take over a project to learn about Ethereum, and try to complete a Hello World.

step

  1. I refer to another piece of blog, install nvm
  2. Installation switching node 9.11.1 and environment
    nvm install 9.11.1
    nvm use 9.11.1
  3. Create a new working directory, and command-line index to the path
  4. Installation ganche-cli, web3, solc
    npm install ganache-cli
    npm install [email protected]
    npm install [email protected]       //此处原博客没有版本,会安装高于0.4的版本,会导致后续编译smart contract编译失败 

    When the ganache-cli and web3 installation, because the version of the tutorial problem occurs error, but does not affect.

  5. Start ganache-cli
    node_modules\.bin\ganache-cli

    After startup output as ganache-cli

  6. Use Solidity create Smart Contract, named: 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个帐号,如下

Block chain Hello World - Dapp vote Ethernet-based Square

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

全部完成会得到如下截图的输出,表示smart contract编译成功
Block chain Hello World - Dapp vote Ethernet-based Square
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. Created in the root directory index.html file, and paste the following code, you need to cut icon provenance, replaced with step 8 of their deployed 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)

Guess you like

Origin blog.51cto.com/13852791/2438063