Truffle 4.0, Geth 1.7.2, TESTRPC build intelligent contract on private chain __truffle

Source: Internet
Author: User
1, what is truffle.

Truffle is the most popular development framework, capable of compiling and deploying smart contracts locally, and the mission is to make development easier.

Truffle requires Ethernet client support and needs to support the standard JSON RPC APIs. 2, suitable for truffle development of the client

There are many Ethernet square clients to choose from. We recommend using different clients for development and deployment.

  applicable to the development of the client Etherumjs TESTRPC

  applicable to officially released clients Geth (Go-ethereum)

When developing a truffle based application, it is recommended that you use ETHEREUMJS testrpc. It is a complete block chain in memory that only exists on the device you are developing. Instead of waiting for the default block time, you can quickly validate your new code and give you instant feedback when there is an error, as opposed to GETH,TESTRPC, which returns in real time when the transaction is executed, rather than wait for the defaults. It is also a powerful client that supports automated testing. Truffle makes full use of its characteristics to speed up the test running time by nearly 90%.

   3, truffle source code address

Https://github.com/trufflesuite/truffle

   4, how to install.

For the next example, we will use truffle to connect Geth and TESTRPC to test the deployment of the smart contract, first we install truffle, Geth, testrpc separately.

   4.1, installation Go-ethereum 1.7.2

Go-ethereum Installation Reference This article: using Go-ethereum 1.7.2 to build the private chain of the etheric square

   4.2, installation Truffle 4.0

Dependent environment:
* Nodejs 5.0+
* Windows,linux, or Mac OS X

The installation is simple:

NPM install-g truffle@4.0.0

To view the installed version:

➜/users/lion >truffle version
truffle v4.0.0 core:4.0.0 solidity
(v0.4.18)

   4.3. Installation Testrpc

➜/users/lion >npm install-g ethereumjs-testrpc
/usr/local/bin/testrpc-> Ethereumjs-testrpc/build/cli.node.js
+ ethereumjs-testrpc@6.0.1
added 1 package and updated 2 packages in 10.648s

   5, the use of truffle for the development of intelligent contracts 5.1, initialization of a truffle project

By using the Truffle init command, you can initialize a default Ethernet square currency contract project, which we can quickly learn by using this project:

➜/users/lion/my_project/_eth >mkdir test_truffle
➜/users/lion/my_project/_eth >cd test_truffle
Users/lion/my_project/_eth/test_truffle >truffle init

downloading
...
unpacking ... Setting up ...
Unbox successful. sweet!

Commands:

  Compile:        truffle Compile
  Migrate:        truffle Migrate
  test contracts:truffle test

Upon completion, you will have the following directory:
* Contracts Intelligent Contract Catalogue
* Migrations Publish Script Directory
* Test Files Stored
* Truffle.js Truffle configuration file

   5.2. Compile the contract

To compile the contract, use the truffle compile command to compile the original code into an Ethernet-approved bytecode:

➜/users/lion/my_project/_eth/test_truffle >truffle compile
compiling./contracts/migrations.sol
... Writing artifacts to./build/contracts

Truffle only compiles files that have been modified since the last compilation by default to reduce unnecessary compilation. If you want to compile all files, you can use the--compile-all option

Truffle compile--compile-all

Truffle the contract name and file name that you want to define match exactly, and this match is case-sensitive, which means that the case is consistent. It is recommended to capitalize each opening letter.

Dependencies between files, you can use import to make a reference between contracts, truffle will compile the contract in the correct order, and automatically associate the library when needed. For example:

Import "./anothercontract.sol";

   5.3, create a Hello mshk.top contract and compile

Create a new Hello_mshk_top.sol file in the contracts directory with the following code:

pragma solidity ^0.4.17;

Contract Hello_mshk_top {

  //say Hello mshk.top
  function say () Public pure Returns (string) {return
    "Hello MSH K.top ";
  }

  Print name
  function print (string name) public pure Returns (string) {return
    name;
  }
}

There are two methods in the code: The Say () method is to output a paragraph of text Hello Mshk.top;print (string name) method is output incoming content.

Edit the Migrations/1_initial_migration.js deployment script to set the Hello_mshk_top.sol file we just created into the release profile, as follows:

var migrations = Artifacts.require ("./migrations.sol");
var hello_mshk_top = Artifacts.require ("./hello_mshk_top.sol");
Module.exports = function (deployer) {
  deployer.deploy (migrations);
  Deployer.deploy (hello_mshk_top);
};

The project is compiled using the truffle compile command, and the compiled files are placed in the./build/contracts directory:

➜/users/lion/my_project/_eth/test_truffle >truffle compile
compiling./contracts/hello_mshk_top.sol ...
compiling .../contracts/migrations.sol
. Writing artifacts to./build/contracts

Hello_mshk_top.sol the compiled file is./build/contracts/hello_mshk_top.json, later in the deployment to Geth, we will use.

   6, the deployment of intelligent contracts

Edit the Truffle.js configuration file and set the location where we will deploy the smart contract later, as follows:

Module.exports = {
    networks: {
        development: {
          host: "localhost",
          port:8545,
          network_id: "*"
        }
    }
};

Next, we will use the smart contract above to carry out the deployment test in TESTRPC and Geth respectively.

Truffle Smart Contract Project deployment, using the following command:

Truffle migrate

This command will execute all JS files in the Migrations directory. If the previous truffle migrate command, execute again, will only deploy the new JS file, if no new JS file, will not play any role. If you use the--reset parameter, the deployment of all scripts is performed again.

If you are deploying to a specified network, you can use the--network parameter, for example:

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.