Blockchain Development (iii) writing and debugging the first Ethereum smart contract __ Blockchain

Source: Internet
Author: User
Tags manual browser cache advantage

Lihe September 10, 2016

I. Introduction to intelligent Contract IDE

Currently, Ethereum supports three languages to write smart contracts,

solidity: Similar to JavaScript, this is the official Ethereum recommendation language and the most popular smart contract language. For specific usage, take the solidity document, address: https://solidity.readthedocs.io/en/latest/

Serpent: Python-like style, document address: Https://github.com/ethereum/wiki/wiki/Serpent

LLL: A lisp-like style that has now been terminated.

According to different habits can choose different high-level language, the most popular is solidity. All of the intelligent contracts are written in solidity language.

There are several common Ides that can now write smart contracts:

Mix: Early ethereum main development IDE, can support the intelligent contract and Dapp writing, debugging, deployment, full graphical interface, but with the original host Gavin Wood left, slowly marginalized, eventually stopped development, the whole team to remix project, For future reasons, it is not recommended to study mix.

Remix: Is the original Mix team's new work, currently only the simple debug function on-line, the future can focus on.

browser-solidity: This project is the Intelligent contract browser version of the development environment, can support in the browser directly development, debugging and compilation, for beginners, can quickly get started, do not need to install, very convenient, direct access to address use: https:// ethereum.github.io/browser-solidity/, this article uses this IDE for development.

Ethereum Studio: A third-party company developed the Enterprise Edition of smart contracts online IDE, powerful, free to use, can be used as a tool for enterprise development, access address: https://live.ether.camp/

Visual Studio: Yes, Microsoft's vs 2015, Microsoft has integrated Ethereum's smart contract authoring capabilities, you can see the importance of the ethereum.

II. Preparation of the first smart contract

1. Intelligent Contract Grammar Learning method

The syntax and examples of smart contracts can be viewed at Solidity's documentation site http://solidity.readthedocs.io/en/latest/, which basically read these online documents and have mastered them, leaving only the practice of writing code.

2. Example Contract Code

First, let's give an example code, which will explain the writing and debugging of the smart contract in this code example.

-------------------------------------------------------------------------------------------

Contract Votelihe {

struct Candidate {

UINT Votecount;

String name;

}

struct Voter {

BOOL voted;

}

Mapping (address = voter) public voters;

Candidate[] public candidates;

function Votelihe () {

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);

}

}

--------------------------------------------------------------------------------------------------

The code creates a voting program that polls two candidates Lihe and Dandan, each with only one chance to vote, and finally feedback Lihe and Dandan's results. Each function is described as follows:

function Votelihe (): constructor, smart contract runs only once

function Vote_candidate (): Vote for the candidate, and each voter can vote only one vote

function GetCount (): Returns the number of votes currently in the candidate

3. Writing smart contracts using the IDE

First we open the main functions of Browser-solidity,ide as follows:

Copy the sample code to the code-editing box on the left, and the IDE automatically detects the syntax error and displays it in the window on the right, as shown in the following illustration:


Can see, prompt has undeclared object, is in 14 rows of error, it is obvious that I a structure object candidates mistakenly write as Candidates2, modify can verify pass.

Note that in the browser to write code, he is automatically saved in the local browser cache, as long as clear the browser cache, the code will not be lost.

Third, debugging the first smart contract

Currently Browser-solidity has two common debugging methods, one is to use local virtual Machine debug mode, one is connected to the local private chain for debugging.

1. Local virtual machine Debug mode

Local virtual machine debugging, is not connected to any one node, in memory virtual out of an Ethereum node for debugging, the advantage is fast, simple configuration, the disadvantage is because it is only virtual debugging, may finally put on the real blockchain node to run the smart contract will be different from the expected results.

First, in the Debug environment settings, select the JavaScript VM to set the local virtual debug mode, as shown below:

After the setup 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 into memory, and debug, if the deployment succeeds, the function of the smart contract will be run button and parameter input box, then you can debug your smart contract, as shown below:

After running the function, the corresponding transaction data will appear, which can complete the whole intelligent contract debugging.

If you want to gradually debug the smart contract, then select the bug icon, switch to the step-by-Step debugging interface, you can realize the single-stepping operation of the smart contract, note that the single step here is not the code but the smart contract compiled opcode, as shown below.

2. Connect to local private chain debug

Connect to the local private chain debugging, is through the RPC interface, connected to the local Ethereum node, the actual deployment and debugging of intelligent contracts, the disadvantage is slow, complex configuration, the advantage is to be able to run the intelligent contract, the greatest degree of error prevention, about 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, and by default will give a connection address of http://localhost:8545, if you configure the private chain RPC port modified, remember to change to the corresponding port, as shown below:

Then, to switch to the account status bar, the available accounts displayed at this time, should be the private chain you deployed in the account, if not, indicating that the private chain is not successfully connected. There are two possible reasons, one is that the port provided by the private chain is accessed with HTTP, and the Browser-solidity Web Access address is HTTPS, The solution is to change the browser-solidity access address to the address of the HTTP protocol http://ethereum.github.io/browser-solidity/, and the second is that the system time is not synchronized with the network, Sync with the time Sync feature that comes with your Windows system. Third , if the Error:account is locked error occurs when connecting to the local private chain debug, it is because geth needs to unlock the local Ethereum account and enter Personal.unlockaccount in the Geth console (' Account Address ').

Iv. Other common smart contract resources

Here are some examples of web sites to refer to some mature code to facilitate rapid iterative learning, common examples of web sites 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 3 common development frameworks:

Truffle: Manual Address http://truffle.readthedocs.io/en/latest/

Ethereum is currently a popular development framework for the truffle, which is more prevalent in this framework.

Dapple: Manual Address http://dapple.readthedocs.io/en/master/

This development framework was seen on the Gitter chart, with few people feeling

Meteor: Manual Address Https://github.com/ethereum/wiki/wiki/Dapp-using-Meteor

This development framework is officially recommended by Ethereum and is written into the official Ethereum wiki, which is worth learning, and of course, ethereum officials often change direction, and later, they may be different.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.