基于以太坊的区块链HelloWorld是怎样的-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

基于以太坊的区块链HelloWorld是怎样的

本篇文章给大家分享的是有关基于以太坊的区块链Hello World是怎样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联公司是一家集网站建设,桂阳企业网站建设,桂阳品牌网站建设,网站定制,桂阳网站建设报价,网络营销,网络优化,桂阳网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

环境

windows 10 64bit

背景

准备接手一个IPFS+Ethereum的项目,先学习一下Ethereum,并尝试完成一个Hello World。

步骤

  1. 参考我另一片blog, 安装nvm

  2. 安装node 9.11.1 并切换环境

nvm install 9.11.1
nvm use 9.11.1
  1. 创建一个新的工作目录,并在命令行索引到该路径

  2. 安装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时,由于教程版本问题会出现报错,但是不影响。

  1. 启动ganache-cli

node_modules\.bin\ganache-cli

基于以太坊的区块链Hello World是怎样的 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;
   }}
  1. 启动node交互控制台,依次输入以下命令

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

输入以上最后一条命令后会获取Ganache创建的10个帐号,如下

基于以太坊的区块链Hello World是怎样的

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

全部完成会得到如下截图的输出,表示smart contract编译成功 基于以太坊的区块链Hello World是怎样的 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



	
		DApp
		
		
	
	
		

Voting Application

Candidate Votes James Norah Jones
Vote 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); } });

基于以太坊的区块链Hello World是怎样的

  1. 在浏览器打开index.html,输入Candidate中的人名后,点击Vote即可投票,投票后效果如下 基于以太坊的区块链Hello World是怎样的 每次点击投票,也都会生成一个新的区块,效果如下。 基于以太坊的区块链Hello World是怎样的

以上步骤就完成了一个基于Ethereum的投票Dapp的完整搭建流程,整合个补全后步骤应该不会有坑的可以顺利搭建完成。

就像“hello world”的字面意思一样,0-1的过程是最艰难的,但是开了头,剩下的1-n也就会顺畅不少。

以上就是基于以太坊的区块链Hello World是怎样的,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


本文题目:基于以太坊的区块链HelloWorld是怎样的
链接地址:http://kswsj.cn/article/jjscch.html

其他资讯