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

Blockchain Hello World-- based on the voting Dap of Etay Square

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Guide road sign etherfang block chain Dapp etaifang hello world environment

Windows 10 64bit

Refer to the blog

Youclavier-- Dapp course on voting in Tai Fong

Background

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

The steps refer to my other piece of blog, install nvm install node 9.11.1 and switch environment nvm install 9.11.1nvm use 9.11.1 to create a new working directory, and direct to this path on the command line to install ganche-cli, web3, solcnpm install ganache-clinpm install web3@0.20.1npm install solc@0.4.21 / /. If there is no version of the original blog, a version higher than 0.4 will be installed, which will cause subsequent compilation of smart contract 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-clinode_modules\ .bin\ ganache-cli

Create a Smart Contract using Solidity, named: Voting.solpragma 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个帐号,如下 >

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 the smart contract has been compiled successfully

8. 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 place it in the working root directory.

Cdn is not available for some reason, so download the source file directly, with the following link

Web3.js 0.20.6 create the index.html file in the root directory and paste the following code. You need to replace it with the addressDAppVoting ApplicationCandidateVotesJamesNorahJones of the smart contract deployed by yourself in step 8 at the screenshot mark.

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

Servers

Wechat

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

12
Report