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 development method of Etay Square?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the development method of Etai Square". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what is the development method of Etai Square"!

1. Set up the development environment

We use a simulated memory block chain (ganache) instead of the real block chain for development. In Chapter 2 of this tutorial, we will interact with the real blockchain. Here are the steps to install ganache, web3js, and then start a test chain on linux. The installation process is the same on macOS.

You can see that ganache-cli automatically creates 10 test accounts, each with 100 (fictitious) ethers pre-allocated

If you need a more detailed installation tutorial for the development environment, you can refer to the following article:

Setting up the development environment of windows Yitai Fang

Setting up the development environment of linux/ubuntu Yitai Fang

two。 A simple voting contract

We will use the solidity programming language to write our contract. If you are familiar with object-oriented programming, learning to write solidity contracts should be a piece of cake. We will write a contract object that contains a constructor to initialize the number of candidates. There are two methods for contract objects:

Returns the total number of votes obtained by the candidate

Increase the number of votes cast by candidates.

Note: the constructor is called only once when you deploy the contract to the blockchain. Unlike every deployment of your code in the online world overwrites the old code, the deployed code remains the same on the block chain. For example, if you update your contract and deploy again, the old contract will still be on the block chain, the data it stores will not be affected, and the new deployment will create a new instance contract.

Here is the code for the voting contract:

Pragma solidity ^ 0.4.18; / / We have to specify what version of compiler this code will compile with contract Voting {/ * mapping field below is equivalent to an associative array or hash. The key of the mapping is candidate name stored as type bytes32 and value is an unsigned integer to store the vote count * / mapping (bytes32 = > uint8) public votesReceived; / * Solidity doesn't let you pass in an array of strings in the constructor (yet). We will use an array of bytes32 instead to store the list of candidates * / bytes32 [] public candidateList; / * This is the constructor which will be called once when you deploy the contract to the blockchain. When we deploy the contract, we will pass an array of candidates who will be contesting in the election * / function Voting (bytes32 [] candidateNames) public {candidateList = candidateNames;} / / This function returns the total votes a candidate has received so far function totalVotesFor (bytes32 candidate) view public returns (uint8) {require (validCandidate (candidate)); return votesReceived [candidate] } / / This function increments the vote count for the specified candidate. This / / is equivalent to casting a vote 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; } } 复制上面的代码,在hello_world_voting目录下创建一个Voting.sol文件。现在让我们来编译代码并将其部署到ganache的区块链上. 为了编译solidity代码,我们需要安装名字为solc的npm模块 ~/hello_world_voting$ npm install solc 我们将在node控制台中使用这个库来编译我们的合约。在上一篇文章中我们提到,web3js是一个让我们可以通过rpc访问区块链的库。我们将使用该库来部署我们的应用程序并与之交互。 首先,在命令行中断运行node命令进入node控制台,初始化solc和文本对象。下面的所有代码片段都需要在node控制台中键入 ~/hello_world_voting$ node >

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

To ensure that the web3 object is initialized and that the blockchain is accessible, let's try querying all the accounts on the blockchain. You should see the following results:

> web3.eth.accounts ['0x9c02f5c68e02390a3ab81f63341edc1ba5dbb39e', '0x7d920be073e92a590dc47e4ccea2f28db3f218cc', '0xf8a9c7c65c4d1c0c21b06c06ee5da80bd8f074a9', '0x9d8ee8c3d4f8b1e08803da274bdaff80c2204fc6', '0x26bb5d139aa7bdb1380af0e1e8f98147ef4c406a', '0x622e557aad13c36459fac83240f25ae91882127c', '0xbf8b1630d5640e272f33653e83092ce33d302fd2', '0xe37a3157cb3081ea7a96ba9f9e942c72cf7ad87b', '0x175dae81345f36775db285d368f0b1d49f61b2f8', '0xc26bda5f3370bdd46e7c84bdb909aead4d8f35f3']

Load the code from voting.sol, save it in a string variable, and start compiling

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

When your code compiles and prints the contents of the contract object (output in the node console), there are two fields that are important and need to be understood:

CompiledCode.contracts [': Voting']. Bytecode: the bytecode obtained after compilation of the Voting.sol source code. This is the code that will be deployed to blockchain.

CompiledCode.contracts [': Voting'] .interface: the contract interface or template (called ABI) tells the user which methods the contract contains. You need these definitions of ABI because you will always need to interact with contracts in the future.

Thank you for your reading. the above is the content of "what is the development method of Etai Square". After the study of this article, I believe you have a deeper understanding of what is the development method of Etai Fang. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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