Smart contract development environment construction and Hello World contract

If you have no concept of Ethereum smart contract development (this article will assume you already know these concepts), it is recommended to read the primer first .
Just like learning any programming language first, the first program to get started is Hello World. Today, let's start with building the Ethereum smart contract development environment step by step, and explain how to write the Hello World of smart contracts.

Development environment setup

Solidity installation

It is strongly recommended that novices use Browser-Solidity for development.
Browser-Solidity is a browser-based Solidity, so you don't need to install Solidity. The Hello World tutorial in this article will also be based on Browser-Solidity.

If you want to install it yourself, please refer to the Solidity installation guide .

geth install

The installation command under Mac is as follows: Reference for other platforms: geth official installation guide

brew tap ethereum/ethereum
brew install ethereum

brew is a package management tool under Mac, similar to apt-get in Ubuntu

After installation, just start the geth console.

startup environment

As mentioned in the introductory article, geth is an Ethereum client, and now use geth to start an Ethereum (developer) network node.

geth --datadir testNet --dev console 2>> test.log

After executing the naming, you will enter the geth console, and the cursor will stop at a right arrow, like this:

Command parameter description:
--dev  enables the developer network (mode), the developer network will use the POA consensus, a developer account will be pre-allocated by default and mining will be automatically enabled.
The parameter after --datadir  is the block data and key storage directory.
After entering the command for the first time, it will create a new testNet directory in the current directory to store the data.
The console  enters the console
2 >> test.log  means to output the console log to the test.log file

For better understanding, it is recommended to open a new command line terminal to display the log in real time:

tail -f test.log

prepare account

Deploying a smart contract requires an external account. Let's take a look at the assigned developer account first. Use the following command to view the account in the console:

> eth.accounts

After pressing Enter, return an account array with a default account, such as:

Accounts can also be viewed using personal.listAccounts,

To take a look at the balance in the account again, use the following command:

> eth.getBalance(eth.accounts[0])

eth.accounts[0] represents the first account in the account list.
After pressing Enter, you can see a large amount of balance, such as:
1.15792089237316195423570985008687907853269... e+77

The developer account has too much balance. If you use this account to deploy the contract, you will not be able to see the balance change. In order to better experience the complete process, choose to create a new account here.

create Account

Create an account with the following command:

> personal.newAccount("TinyXiong")

TinyXiong is the password of the new account. After pressing Enter, it will return to a new account.

At this point we look at the account list:

> eth.accounts

You can see that the account array contains two accounts, and the new account is at the second (index 1) position.

Now look at the account balance:

> eth.getBalance(eth.accounts[1])
0

After the carriage return, the returned value is 0, and the new account is 0. The result is:

Transfer money to new account

We know that an account without a balance cannot deploy the contract, so we will transfer 1 ETH from the default account to the new account, and use the following command (please use the account corresponding to your own eth.accounts output):

eth.sendTransaction({from: '0xb0ebe17ef0e96b5c525709c0a1ede347c66bd391', to: '0xf280facfd60d61f6fd3f88c9dee4fb90d0e11dfc', value: web3.toWei(1, "ether")})

In the opened tail -f test.log log terminal, you can see the mining records at the same time.
Check the balance of the new account again, you can have 1 ether in the new account

Unlock account

Before deploying the contract, you need to unlock the account (just like entering a password for bank transfers), use the following command:

personal.unlockAccount(eth.accounts[1],"TinyXiong");

"TinyXiong" is the password when the account was created. After the account is
unlocked successfully, the account is ready, and the next step is to write the contract code.

Write contract code

Now let's start writing the first smart contract code, the solidity code is as follows:

pragma solidity ^0.4.18;
contract hello {
    string greeting;
    
    function hello(string _greeting) public {
        greeting = _greeting;
    }

    function say() constant public returns (string) {
        return greeting;
    }
}

In a simple explanation, we define a contract named hello, save a string when the contract is initialized (we will pass in hello world), and return the string each time say is called.
Write (copy) this code to Browser-Solidity , if there is no error, click Details to get the deployment code, such as:

Find the WEB3DEPLOY part in the pop-up dialog box, click copy, paste it into the editor, and modify the initialization string to hello world.

When solidity was writing the blog post (2017/11/24), the version was 0.4.18. Solidity is developing very fast, and solidity versions may not be compatible. You can select the corresponding compiler version in the Settings of Browser-Solidity. .
Browser-Solidity is constantly being updated, and the screenshots may not be the same as the interface you see.

deploy contract

The code generated by Browser-Solidity, copied to the editor, and the modified code is as follows:

var _greeting = "Hello World" ;
var helloContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var hello = helloContract.new(
   _greeting,
   {
     from: web3.eth.accounts[1], 
     data: '0x6060604052341561000f57600080fd5b6040516102b83803806102b8833981016040528080518201919050508060009080519060200190610041929190610048565b50506100ed565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061008957805160ff19168380011785556100b7565b828001600101855582156100b7579182015b828111156100b657825182559160200191906001019061009b565b5b5090506100c491906100c8565b5090565b6100ea91905b808211156100e65760008160009055506001016100ce565b5090565b90565b6101bc806100fc6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063954ab4b214610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc61017c565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101725780601f1061014757610100808354040283529160200191610172565b820191906000526020600020905b81548152906001019060200180831161015557829003601f168201915b5050505050905090565b6020604051908101604052806000815250905600a165627a7a723058204a5577bb3ad30e02f7a3bdd90eedcc682700d67fc8ed6604d38bb739c0655df90029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 });

Line 1: Modify the string to Hello World
Line 2: Modify the contract variable name
Line 3: Modify the contract instance variable name, and then you can directly use the instance to call the function.
Line 6: Modify the deployment account to be the new account index, that is, use the new account to deploy the contract.
Line 8: The gas fee to be paid, the IDE has already estimated it for us.
Line 9: Set the deployment callback function.

The copy will be in the geth console. After pressing Enter, you will see output such as:

Contract mined! address: 0x79544078dcd9d560ec3f6eff0af42a9fc84c7d19 transactionHash: 0xe2caab22102e93434888a0b8013a7ae7e804b132e4a8bfd2318356f6cf0480b3

Indicates that the contract has been deployed successfully.

In the opened tail -f test.log log terminal, you can see the mining records at the same time

Now let's check the balance of the new account:

> eth.getBalance(eth.accounts[1])

Is it less than the balance of the previous transfer?

run the contract

> hello.say()
"Hello World"

Output Hello World, our first contract, Hello World, runs successfully.

This article will be updated with the upgrade of geth and solidity language versions, check the original link of this article: https://learnblockchain.cn/2017/11/24/init-env/

The significance of the first contract is more important to experience the smart contract development process. For beginners, you can choose to give up some details first. After the development process is completed, you can increase your confidence in the next step of learning.

Original text: https://www.cnblogs.com/tinyxiong/p/7898599.html

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

If you want to learn Ethereum DApp development efficiently , you can visit the most popular online interactive tutorials provided by Huizhi.com:

1.  An introductory tutorial for Ethereum DApps for blockchain newbies
2.  Blockchain +IPFS+Node.js+ MongoDB+Express decentralized Ethereum e-commerce application development practice

3. Other more content can also visit this Ethereum blog .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326035853&siteId=291194637