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

How to develop Hello World with Intelligent contract in Ethernet Square

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the relevant knowledge of "how to develop Hello World with intelligent contract in Ethernet Square". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to develop Hello World with intelligent contract in Ethernet Square" can help you solve the problem.

I. Solidity language

Solidity is an intelligent contract high-level language that runs on top of the Ethereum Virtual Machine (EVM:Ethereum Virtual Machine).

The syntax of Solidity is similar to Javascript, it is an object-oriented language, and the various development tool chains around Solidity are provided using npm, which belongs to the Javascript ecosystem.

II. Editor

I am currently using Atom with the solidity (linter-solium) plug-in to develop.

III. Truffle framework

Truffle is a development framework for the Solidity language based on Ethernet Square, which itself is based on Javascript.

IV. Tool installation

1. Install Node.js and NPM

2. Install the Truffle framework and run the command on the terminal:

$sudo npm install-g truffle

Install Truffle

3. Install Truffle client EtherumJS TestRPC:

$sudo npm install-g ganache-cli

Install TestRPC

5. Start TestRPC

Use the following command to start the Ethernet Square test environment:

$ganache-cli

Start the ethernet test environment

You can see that after startup, 10 accounts (Accounts) are automatically established, corresponding to the private key (Private Key) of each account. There are 100 Ether for testing in each account. Note that the Ethernet Square test environment only runs in memory, so it returns to a new state each time it is reopened.

VI. Create a project

Reopen a terminal window and run the following command to create the project:

$mkdir SmartContractProject

$cd SmartContractProject/

$mkdir HelloWorld

$cd HelloWorld/

$truffle init

Initialize Truffle

HelloWorld project catalogue

QQ20180116-160844@2x.png

Directory structure:

Contracts/:Truffle default address for storing contract documents

Migrations/: stores the released script file

Test/: stores test files for test applications and contracts

Configuration files for truffle.js and truffle-config.js:Truffle.

VII. New HelloWorld contract

Create a new HelloWorld.sol file under the contracts folder:

$cd contracts/

$truffle create contract HelloWorld

New HelloWorld contract

QQ20180116-163158@2x.png

The HelloWorld.sol file is as follows:

HelloWorld.sol

Explanation:

Pragma solidity ^ 0.4.4

The first line specifies the version of solidity currently in use, and different versions of solidity may compile different bytecode. ^ stands for versions that are compatible with solidity 0.4.4-0.4.9.

Function HelloWorld () {/ / constructor}}

The contract keyword is similar to class, which is more common in other languages. Because solidity is a language designed for smart contracts (Contact), the functions needed to develop smart contracts are built in after the declaration of contract. This sentence can also be understood as class HelloWorld extends Contract.

The structure of the function is similar to that of other programs, but if you have an incoming parameter or return value, you need to specify the type of parameter or return value (type).

Add a new method function sayHello () returns (string) {return ("Hello World");} to the contract

Add a new method

IX. Compilation contract

Now by executing the truffle compile command, we can compile the HelloWorld.sol source code into Ethereum bytecode:

$cd..

$truffle compile

Compilation contract

[note that a warning appears]

Modify the method in the HelloWorld.sol file again:

Pragma solidity ^ 0.4.4X contract HelloWorld {function HelloWorld () public {/ / constructor} function sayHello () public pure returns (string) {return ("HelloWorld");}}

Modify the method in the contract

Rerun the command after saving:

$truffle compile

Recompile the contract

After the command runs successfully, there will be an extra directory of build, as follows:

Build directory

You will see the HelloWorld.json file under the build/contracts folder under the HelloWorld folder:

HelloWorld.json

10. Modify the contents of truffle.js file:

Add the following to the truffle.js file and save:

Networks: {development: {host: "localhost", port:8545, network_id: "*" / / match any network id}}

Modify the truffle.js file

11. Deployment contract

Create the migration file in the migrations directory:

$cd migrations/

$truffle create migration 2_deploy_helloworld

Create a migration file

Migrations directory

1516095208_2_deploy_helloworld.js file content

Modify the file name and contents as follows:

Var HelloWorld = artifacts.require (". / HelloWorld.sol"); module.exports = function (deployer) {deployer.deploy (HelloWorld);}

Modify the contents of the migration file

Use the artifacts.require statement to get the contract ready for deployment.

Deploy the contract to the blockchain using the deployer.deploy statement.

Here HelloWorld is the name of the contract, not the file name.

So you can use this syntax to read any contract in any .sol file.

Now execute the truffle migrate command:

$truffle migrate

Deployment contract

After successful deployment, you will see the following changes in the terminal window that launches TestRPC:

Terminal window change of TestRPC

12. Interact with the contract

Truffle provides command-line tools. After executing the truffle console command, you can use Javascript to interact with the contract you just deployed:

$cd..

$truffle console

$HelloWorld.deployed () .then (instance = > contract = instance)

Interact with contract 1

Interact with the contract 3

Calling contract.sayHello () directly here will get the same result. Truffle-contract provides the use of call () to read read-only (read only) data, so there is no need to provide gas. So if we encounter an operation that needs to write data to the block chain, we cannot use the call statement.

In this way, we have written and deployed the first smart contract, and verified that the contract does work.

XIII. Add new methods

We add another echo method to HelloWorld.sol. The echo method accepts the input of a parameter and returns the transmitted parameter `:

Function echo (string name) public pure returns (string) {return name;}

Add a new method

Since the contract content has been updated, we need to recompile it first, deploy the compilation result to testrpc, and then see the result through truffle console execution.

$truffle compile

$truffle migrate-reset

Transplant reset

$truffle console

$let contract

HelloWorld.deployed () .then (instance = > contract = instance)

Interact with the contract again 1

Interact with the contract again 2

One thing to note is that if we still use the $truffle migrate command this time, we will get the following information:

$truffle migrateUsing network 'development'.Network up to date.

Truffle will tell you that all the contracts on the network are up-to-date, but in fact the new methods just added to the program have not been updated to the memory block chain. To update the deployed programs on the memory block chain, you need to rewrite the script in migrations. Fortunately, it doesn't matter how the memory block chain we developed is modified. Testrpc, you can use the truffle migrate-- reset command to redeploy directly on the testrpc.

This is the end of the content about "how to develop Hello World with intelligent contracts in Ethernet Square". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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