区块链 Hello World -- 基于以太坊的投票Dap
admin
2023-01-20 02:00:06
0

指路牌

  • 以太坊
  • 区块链
  • 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 web3@0.20.1
    npm install solc@0.4.21       //此处原博客没有版本,会安装高于0.4的版本,会导致后续编译smart contract编译失败 

    在安装了ganache-cli与web3时,由于教程版本问题会出现报错,但是不影响。

  5. 启动ganache-cli
    node_modules\.bin\ganache-cli

    区块链 Hello World -- 基于以太坊的投票Dap

  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 -- 基于以太坊的投票Dap

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

全部完成会得到如下截图的输出,表示smart contract编译成功
区块链 Hello World -- 基于以太坊的投票Dap
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
    
    <!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">
    <h2>Voting Application</h2>
    <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>



![更换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)

相关内容

热门资讯

终于明白“潮汕掌上娱.真的有挂... 终于明白“潮汕掌上娱.真的有挂吗?”果然有透视挂您好,潮汕掌上娱这个游戏其实有挂的,确实是有挂的,需...
重磅消息“新海贝之城拼三张.是... 有 亲,根据资深记者爆料新海贝之城拼三张是可以开挂的,确实有挂(咨询软件...
今日重磅消息“七千在线.有挂吗... 有 亲,根据资深记者爆料七千在线是可以开挂的,确实有挂(咨询软件无需打开...
终于了解“情怀古诗词.是不是有... 终于了解“情怀古诗词.是不是有挂?”其实是有挂您好,情怀古诗词这个游戏其实有挂的,确实是有挂的,需要...
玩家最新攻略“欢又聊红包.怎么... 家人们!今天小编来为大家解答欢又聊红包透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买...
终于懂了“微信十三水.怎么开挂... 终于懂了“微信十三水.怎么开挂?”必胜开挂神器您好,微信十三水这个游戏其实有挂的,确实是有挂的,需要...
玩家分享攻略“天下棋牌.开挂器... 网上科普关于“天下棋牌有没有挂”话题很是火热,小编也是针对天下棋牌作*弊开挂的方法以及开挂对应的知识...
【第一资讯】“一喜棋牌.怎么开... 家人们!今天小编来为大家解答一喜棋牌透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里买很...
美国布朗大学撤回第二起枪击报告... 当地时间12月13日,美国布朗大学更新消息称,在第一起枪击事件发生地以东的州长街(Governor ...
今日重大消息“闽悦麻将.是不是... 今日重大消息“闽悦麻将.是不是有挂?”必胜开挂神器您好,闽悦麻将这个游戏其实有挂的,确实是有挂的,需...