1. Introduction to Smart Contract IDECurrently, Ethereum supports three languages for writing smart contracts: Solidity: Similar to JavaScript, this is the official recommended language of Ethereum and the most popular smart contract language. For specific usage, please refer to the Solidity documentation at: https://solidity.readthedocs.io/en/latest/ Serpent: Similar to Python style, document address: https://github.com/ethereum/wiki/wiki/Serpent LLL: Lisp-like style, currently discontinued. You can choose different high-level languages according to different habits. The most popular one is Solidity. All smart contracts in this article are written in Solidity. There are several common IDEs that can write smart contracts: Mix: It was the main development IDE for Ethereum in the early days. It supports the writing, debugging, and deployment of smart contracts and DAPPs with a full graphical interface. However, with the departure of the original host Gavin Wood, it was gradually marginalized and eventually stopped developing. The entire team turned to the Remix project. For future considerations, it is not recommended to learn Mix. Remix: It is a new work from the original Mix team. Currently, only the simple Debug function is online. You can pay special attention to it in the future. browser-solidity: This project is a development environment for the smart contract browser version. It can support direct development, debugging and compilation in the browser. For beginners, it can be quickly started without installation, which is very convenient. You can directly access the address: https://ethereum.github.io/browser-solidity/. This article uses this IDE for development. Ethereum Studio: An enterprise version of the smart contract online IDE developed by a third-party company. It is powerful and free to use. It can be used as a tool for enterprise-level development. Visit the address: https://live.ether.camp/ Visual Studio 2015: Yes, it is Microsoft's VS 2015. Microsoft has integrated Ethereum's smart contract writing function, which shows Microsoft's emphasis on Ethereum. 2. Writing the first smart contract1. How to learn smart contract syntax The syntax and examples of smart contracts can be viewed on Solidity’s documentation website http://solidity.readthedocs.io/en/latest/. Basically, after reading these online documents, you have mastered them, and the rest is just practicing writing code. 2. Sample contract code First, we will give a sample code, and then use this code as an example to explain the writing and debugging of smart contracts. ——————————————————————————————- contract Vote struct Candidate { uint votecount; string name; } struct Voter { bool voted; } mapping(address => Voter) public voters; Candidate[] public candidates; function Voter() { candidates.push(Candidate({ name: "lihe", votecount: 0 })); candidates.push(Candidate({ name: "dandan", votecount: 0 })); } function Vote_candidate(uint8 numCandidate) { if(voters[msg.sender].voted ||numCandidate>candidates.length)return; candidates[numCandidate].votecount+=1; voters[msg.sender].voted=true; } function Getcount() returns(string,uint,string,uint){ return(candidates[0].name,candidates[0].votecount,candidates[1].name,candidates[1].votecount); } } ————————————————————————————————– This code creates a voting program to vote for two candidates, lihe and dandan. Each person has only one chance to vote, and finally feedbacks the voting results of lihe and dandan. The functions are described as follows: function Votelihe(): Constructor, the smart contract only runs once function Vote_candidate(): Vote for candidates. Each voter can only cast one vote. function Getcount(): Returns the number of votes for the current candidate 3. Use IDE to write smart contracts First, we open browser-solidity. The main functions of the IDE are as follows: Copy the sample code to the code editing box on the left, and the IDE will automatically detect syntax errors and display them in the window on the right, as shown below: As you can see, there is an undeclared object, which is an error on line 14. Obviously, I mistakenly wrote a structure object candidates as candidates2. I can pass the verification by modifying it. Please note that when you write code in the browser, it is automatically saved in the local browser cache. As long as you clear the browser cache, the code will not be lost. 3. Debugging the first smart contractCurrently, there are two commonly used debugging methods for browser-solidity. One is to use the local virtual machine debugging mode, and the other is to connect to the local private chain for debugging. 1. Local virtual machine debugging mode Local virtual machine debugging means not connecting to any node and creating a virtual Ethereum node in the memory for debugging. The advantages are fast speed and simple configuration. The disadvantage is that because it is only virtual debugging, the results of running the smart contract on the real blockchain node may be different from the expected results. First, in the DEBUG environment settings, select JavaScript VM to set the local virtual debugging mode, as shown below: After the setting is successful, you can see the list of available accounts in the account status bar, as shown below After the smart contract code is written, click the "Create" button to deploy the smart contract to the memory and debug it. If the deployment is successful, the smart contract function run button and parameter input box will appear, and then you can debug your smart contract, as shown below: After running the function, the corresponding transaction data will appear, and the entire smart contract debugging can be completed. If you want to debug the smart contract step by step, select the bug icon and switch to the step-by-step debugging interface to run the smart contract step by step. Note that the single-step running here does not refer to the code but to the OPCODE compiled from the smart contract, as shown in the figure below. 2. Connect to the local private chain for debugging Connecting to the local private chain for debugging means connecting to the local Ethereum node through the RPC interface to actually deploy and debug the smart contract. The disadvantage is that it is slow and has complex configuration. The advantage is that it can actually run the smart contract and prevent errors to the greatest extent. For the configuration of the private chain, please refer to my original article "Blockchain Development (I) Building a Private Chain Environment Based on Ethereum". First, in the DEBUG environment settings, select Web3 Provider to set the local virtual debugging mode. By default, a connection address of http://localhost:8545 will be given. If the private chain RPC port you configured is modified, remember to change it to the corresponding port, as shown below: Then, switch to the account status bar. The available accounts displayed at this time should all be the accounts in the private chain you deployed. If not, it means that the private chain has not been successfully connected. There are two possible reasons. First, the port provided by the private chain is accessed using http, while the web access address of browser-solidity is https. The solution is to change the browser-solidity access address to the http protocol address http://ethereum.github.io/browser-solidity/; second, the system time is not synchronized with the network. Use the time synchronization function that comes with the Windows system to synchronize it. 4. Other common smart contract resourcesThe following example websites refer to some mature codes to facilitate rapid iterative learning. Common example websites are as follows: https://github.com/ethereum/wiki/wiki/Solidity-Collections http://ether.fund/contracts/ https://github.com/chriseth/solidity-examples https://github.com/ethereum/dapp-bin https://github.com/fivedogit/solidity-baby-steps http://dapps.ethercasts.com http://ether.fund/contracts There are three commonly used development frameworks: Truffle: Instruction manual http://truffle.readthedocs.io/en/latest/ The manual for Truffle, a popular development framework for Ethereum. This framework is quite popular. Dapple: Instruction manual http://dapple.readthedocs.io/en/master/ I saw this development framework on the gitter chart. I don't think many people use it. Let's observe it first. Meteor: Instructions address: https://github.com/ethereum/wiki/wiki/Dapp-using-Meteor This development framework is officially recommended by Ethereum and is included in the official wiki of Ethereum. It is worth learning. Of course, the official Ethereum often changes direction, so it is possible that it will change to something else in the future. |
<<: iOS 10 adds Circle Pay feature, is Apple paving the way for Bitcoin?
>>: Coin Zone Trends: Bitcoin Price Trends Based on Big Data This Week (2016-09-13)
If the lifeline is forked, it is not a good thing...
For a person, the lifeline is actually a part tha...
Central banks around the world are eager to issue...
What does long fingers represent? Thumb length Th...
Bitpush terminal data shows that Bitcoin rebounde...
Ears can be big or small, soft or hard, and even ...
There are many relationships in life that we need...
1. What is the significance of the large miner te...
Bitcoin House News October 29 CoinDesk news Russi...
Good or bad fortune will affect a person's li...
What kind of man should I marry? 1. A good man wi...
When we watch costume dramas, if there is a chara...
Your belly button can tell whether you are destin...
The poor appearance is innate and difficult to ch...
Many times, there will be some special conditions...