Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the block chain Hello World based on Etay Square?

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What this article shares to you is about the block chain Hello World based on Etay Square. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

Environment

Windows 10 64bit

Background

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

Steps

Refer to my other piece of blog and install nvm

Install node 9.11.1 and switch environments

Nvm install 9.11.1nvm use 9.11.1

Create a new working directory and index to that path on the command line

Install ganche-cli, web3, solc

Npm install ganache-clinpm install web3@0.20.1npm install solc@0.4.21 / / there is no version of the original blog here, and a version higher than 0.4 will be installed, which will cause subsequent compilation and smart contract compilation to fail.

When ganache-cli and web3 are installed, errors will occur due to problems with the version of the tutorial, but it will not be affected.

Start ganache-cli

Node_modules\ .bin\ ganache-cli

6. Create a Smart Contract using Solidity and name it: 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; }} 启动node交互控制台,依次输入以下命令 >

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

Entering the last command above will get the 10 accounts created by Ganache, as follows

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

When all is completed, you will get the output of the following screenshot, indicating that smart contract compiled successfully. Deploy 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

The address will be obtained at this time, and write down that it will be used later.

> contractInstance = VotingContract.at (deployedContract.address)

Download the web3.js file and put it in the working root directory after download.

Cdn is not available for some reason, so download the source file directly and link to the following web3.js 0.20.6

Create the index.html file in the root directory and paste the following code. At the screenshot mark, replace it with the address of the smart contract deployed by yourself in step 8.

DApp Voting Application Candidate Votes James Norah Jones

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report