Ethereum Learning (iv)-building an intelligent contract test environment, development, compilation, deployment

Source: Internet
Author: User
Tags json mkdir requires versions install node

Original link: https://yq.aliyun.com/articles/212944#3

Summary: How to write Smart Contracts (smart contract)? (I) Full video tutorial https://v.qq.com/x/page/h0552ba9k8h.html learning objectives Learn about smart contracts simple environment building can use solidity to write Hello World contract deployment and contract engagement Writing smart contracts using solidity language smart contracts on Ethereum need to use solidity language to write smart contracts (smart contract)? I) complete video tutorial https://v.qq.com/x/page/h0552ba9k8h.html Learning Objectives Understanding Smart Contract Simple environment building can use solidity to write Hello World contract deployment and contract engagement use solidity language to compose smart contracts

Smart contracts on Ethereum need to be written in the solidity language. While there are other languages that can be used to compose smart contracts such as serpent (class Python), LLL (class Fortran), it is now seen that all publicly available smart contracts are written using solidity.

Propaganda says that solidity is a JavaScript-like language, and the various development toolchain around solidity are provided using NPM, which is part of the JavaScript ecosystem. But I think solidity is still more like Java or C #. Because unlike JavaScript, solidity and Java or C # are strongly typed (strong type, you need to specify a type when defining a variable), and you also need to specify the type of callbacks (type) when defining functions (function). It also needs to be compiled before it can be executed. These features are not available in JavaScript. Pre-development preparation

This article will be developed using the current most active smart contract development framework, truffle. ENS (Ethereum Name Service) also uses the truffle framework. Other options are embark and so on.

Just like a Web site or app development, developers can test the effect of a program's execution on their own computer (also known as a native) or through a test network before providing a public service, and then deploy to a publicly available network after testing is complete. The same is true of the development zone block chain Smart Contract (program) process. In particular, the operation of all write or read computations on the public chain requires real money (virtual tokens), and depending on the network condition, each operation on the public chain requires a small response time (15 seconds to several minutes), which is a waste of valuable development time ⏳. So in the process of development, we will use the TESTRPC tool to simulate the Ethereum memory block chain test environment required by the Smart contract on the computer.

The TESTRPC also contains a JavaScript version of the Ethereum virtual machine (Ethereum), so you can execute the smart contract in its entirety.

In addition, a HT editor needs to be prepared before development. I am currently using atom with the solidity plugin to develop. Solidity plug-in in addition to support syntax highlighting, but also through the Solium check and prompt for basic grammatical errors, quite convenient. Other editors should also have similar plugins to choose from.



Install the tools you need

First, you must install node. JS on the development machine, and then use the following command to mount the required tools:

$ npm install-g Ethereumjs-testrpc Truffle
liyuechun:~ yuechunli$ npm install-g ethereumjs-testrpc truffle/usr/local/bin/testrpc/usr/local/lib/node_
Modules/ethereumjs-testrpc/build/cli.node.js
/usr/local/bin/truffle-/usr/local/lib/node_modules/ Truffle/build/cli.bundled.js
+ truffle@3.4.9
+ ethereumjs-testrpc@4.1.3
added 1 package and updated 7 Packages in 76.132s
Start Testrpc

You can use the TESTRPC command to start the Ethereum test environment at any time after installation.

liyuechun:~ yuechunli$ testrpc ethereumjs testrpc v4.1.3 (ganache-core:1.1.3) Available Accounts ================== (0) 0XBBD414B340F2255DAB9D923428C97F0B65D9DF81 (1) 0xe9869e3cf29b6fca81762c314df229c7c4fea25e (2) 0xc79e72362a4511b9e499d186654332c4d6f569be (3) 0x9a6f0651907c149d4173c03927144dbbba1473d4 (4) 0X5B13A5D6788752B26DD4E338AAE2E01058EE145E (5) 0xfc7f56d942ad5260be23ecee92a344aba1b7e7d8 (6) 0xc48dc22c6bacd6ade4421ab54f25bc45c1c51142 (7) 0x3fe2b7d4141dd0a456661f77086d055cbaf3b78f (8) 0x567979fed26ca85e9d1b4ac919c840e3fc9857e2 (9) 0xb2eafe245f098eef1c2c1f466d9a8dcd58764c62 Private Keys ============ ====== (0) 947AB78E91133103612CA099D60E6C38CAC5BB769F7F097C82D003CF058500BD (1) 8FFE0BA8DC53E16944A17DDDD3378B5FBA0379CD84DF4E5237B8B46D05B8762F (2) FFE2E04E43E4106B247407656F5233BCC3E0C49730972D0DF9C1D1093375E2EF (3) A20E453DC44C76AACA6A22EFDBB605C2ED9EEA64C11317E683461E11BD105EA7 (4) 4748268FF1B828868DC56D07A1B121B427E1BDEDE5DBB3C14EF1254D9D26B1A5 (5) f9957e68c6d20d38b81604a0509e6c4591478BC754F87D5682564073705FBB46 (6) 34E648B23C0ACE6B2B0893651D87F70BE8496F97ECF6B7B4607B2ACC4E05C9BD (7) D2477CEDEC217E3FB19A5981DAFBC125EF66CCC9DC7DF29301D08A24DA843CF5 (8) d319f85ccd80e55b2e707e05f09662632564c297248f8b96f82ea5eeaeef0851 (9) 88c33ac9f1062b82f9e82f86a0ce307e3bd8fcf683b9751232c2f193f5bdc668 HD Wallet ================== Mnemonic:hire Custom Clinic expect Fury fantasy try dress source spy viable flag Base HD path:m/44 '/60 '/0 '/0/{account_index} Listening on lo calhost:8545

You can see that the TESTRPC automatically establishes 10 accounts (Accounts) and the private key corresponding to each account (private key). There are 100 test ether in each account. Note that TESTRPC is only running in memory, so it will return to a completely new state each time it is re-opened.

When everything is ready, we can start building our first Smart Contract project. Build a project

Open another terminal window and enter the following command to build the project:

Liyuechun:desktop yuechunli$ mkdir smartcontractdemo
liyuechun:desktop yuechunli$ cd smartcontractdemo/
Liyuechun:smartcontractdemo yuechunli$ mkdir HelloWorld
liyuechun:smartcontractdemo yuechunli$ cd helloworld/
liyuechun:helloworld yuechunli$ Truffle init

Downloading project ...
Project initialized.

  Documentation:http://truffleframework.com/docs

Commands:

  compile:truffle Compile
  migrate:truffle Migrate
  test:    truffle test

liyuechun:helloworld yuechunli$ ls
contracts    Migrations    Test        Truffle.js
directory structure:

/contracts: Where the original code of the smart contract is stored, you can see that there are already three Sol files in it, and the Helloworld.sol file we developed is stored here.

/migrations: This is the function truffle use to deploy the smart contract, and later we'll modify the 2_deploy_contracts.js to deploy the Helloworld.sol.
/test: The code to test the smart contract is here to support JS and Sol testing.
The setup document for the truffle.js:Truffle.

New HelloWorld Contract

Create a new Helloworld.sol file under the Contracts folder and, of course, directly under the HelloWorld path, you can simply execute the truffle creation contract HelloWorld command.

Helloworld.sol file contents are as follows:

pragma solidity ^0.4.4;

Contract HelloWorld {
  function SayHello () returns (string) {
    return ("Hello World");
  }
}

explanation

pragma solidity ^0.4.4;

The first line is named the solidity version currently in use, and different versions of solidity may compile different bytecode. ^ Stands for compatible solidity ' 0.4.4 ~ 0.4.9 ' versions.

Contract HelloWorld {
    ...
}

The contract keyword is similar to the more common class in other languages. Because solidity is a language designed for the smart contract (contact), the functionality required to develop a smart contract is built-in after declaring contract. This sentence can also be understood as class HelloWorld extends contract.

function SayHello () returns (string) {
    return ("Hello World");
}

The structure of a function is similar to other programs, but if you have an incoming parameter or callback value, you need to specify the type of the parameter or callback value. compiling

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

Liyuechun:helloworld yuechunli$ LS Contracts migrations Test truffle.js Liyuechun:helloworld yuechunli$ Truff
Le compile compiling./contracts/convertlib.sol ...
Compiling./contracts/helloworld.sol.
Compiling./contracts/metacoin.sol.
Compiling./contracts/migrations.sol.        Writing artifacts to./build/contracts liyuechun:helloworld yuechunli$ LS build contracts migrations test Truffle.js liyuechun:helloworld yuechunli$ cd build/liyuechun:build yuechunli$ ls contracts liyuechun:build YUECHUNL
i$ cd contracts/liyuechun:contracts yuechunli$ ls convertlib.json helloworld.json metacoin.json Migrations.json Liyuechun:contracts yuechunli$ cat Helloworld.json {"Contract_name": "HelloWorld", "ABI": [{"Inputs": [] , "payable": false, "type": "Constructor"}], "Unlinked_binary": "0x60606040523415600e57600080fd5b5b5 b5b603680601e6000396000f30060606040525b600080fd00a165627a7a723058203ee98a767948e9bc08094df4a46ab0361f068b2a559032cf968df5bbf63e91430029 "," Networks ": {}," schema_version ":" 0.0.5 "," Updated_at ":  

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.