Ethereum - Getting Started with Blockchain Development

This tutorial runs on Ubuntu 16.04

Environment configuration

Get to know the following nouns:

Ethereum: The underlying system that can implement smart contracts and open source on the blockchain.

testrpc: An Ethereum environment that uses memory emulation locally for development and debugging.

geth: The full name is go-ethereum, which is an Ethereum client, written in go language, and is currently the most commonly used Ethereum client, corresponding to the real Ethereum environment.

truffle: The most popular development framework on Ethereum.

solc: solidity compiler.

Node.js: is a JavaScript runtime environment based on the Chrome V8 engine.

web3

Install nodejs and npm (make sure nodejs is above 6.x)

curl -sLhttps://deb.nodesource.com/setup_6.x | sudo -E bash –

(If you are prompted that curl is not installed, execute sudo apt-get install curl to install curl before executing the previous command)

sudo apt-get install nodejs

安装 furrow 、 furrow-cli

sudo npm install -g solc

sudo npm install -g solc-cli

install truffle

sudo npm install -g truffle

install testrpc

sudo npm install -g ethereumjs-testrpc

install geth

If you need to use the solc compiler in the geth console, the previous installation of solc is not enough, you also need to install the solc binary package with the following command.

sudo add-apt-repositoryppa:ethereum/ethereum

sudo apt-get update

sudo apt-get install solc

then install geth

sudo apt-get installsoftware-properties-common

sudo apt-get install ethereum

getting Started

1. View truffle version information

truffle version

2. Test the testrpc environment

testrpc

Returns ten available accounts (Available Accounts: 0-9)

and ten private keys (Private Keys: 0-9)

Additional information on HD Wallet (Mnemonic and Base HD Path)

Finally, it also prompts to listen to the local port 8545.

Press Ctrl+C to exit testrpc.

3. Create a programming directory and initialize truffle

mkdir ethtest

cd ethtest

truffle init

Looking at the prompt, three commands are given, which will be used later. Execute truffle --help to see more commands:

Let's take a look at what's new in the ethtest folder at this time:

There are three more folders, contracts, migrations, and test, and two files, truffle-config.js and truffle.js. As an explanation, contracts is the folder where contracts are placed. When you open them, you will find that a contract, Migrations.sol, has already been placed. Migrations is the file for the migration operation. When you open it, you will find a file named 1_initial_migration.js. The specific content will be explained in detail later. ;test is the folder where the test script is placed; truffle.js is the configuration file of truffle, the specific content will be described in detail later; finally, about truffle-config.js, it is actually the configuration file of truffle, but it is to avoid the windows system The name changed due to naming conflicts has no effect under Ubuntu and can be deleted.

4. Write the contract

cd contracts

nano SimpleStorage.sol

Use the SimpleStorage contract in the official documentation , note that the file name is the same as the contract name, and the file extension is .sol.

pragmasolidity ^ 0.4.0;

contractSimpleStorage {

    uint storedData;

    function set(uint x) {

        storedData = x;

    }

    function get() constant returns (uint) {

        return storedData;

    }

}

5. Compile the contract

truffle compile

This directive will only compile the contract files that have been modified since the last successful compilation to reduce unnecessary compilation. Set the "--all" option to force compilation of all files.

After the compilation is completed, there will be an additional build folder in the ethtest directory, and there is a compiled SimpleStorage.json file under ethtest/build/contracts.

6. Migrate (or deploy) contracts

Note that open another terminal and execute testrpc in it to make it listen to port 8545, otherwise the commands that use the network later will not be executed normally, and the following error will occur:

truffle migrate

Error below error:

This is a network configuration problem. Open truffle.js:

module.exports= {

  // See<http://truffleframework.com/docs/advanced/configuration>

  // to customize your Truffle configuration!

};

It can be seen that there is no configuration information in it. Visit the website in the comment and give a variety of configurations to choose from. First, use the default configuration to write into truffle.js to complete the configuration:

module.exports= {

  networks: {

    development: {

      host: "localhost",

      port: 8545,

      network_id: "*" // Match anynetwork id

    }

  }

};

In this way, when executing commands such as truffle migrate, the development network under the networks network configuration item will be used by default, and the above error will no longer occur.

To talk about the migration file in detail, open 1_initial_migration.js:

varMigrations = artifacts.require("./Migrations.sol");

 

module.exports= function(deployer) {

  deployer.deploy(Migrations);

};

This is the initial migration file, corresponding to the contract Migrations.sol generated by truffle init.

If we want to deploy a new contract, create a new file, such as SimpleStorage_deploy.js, with the following content:

varSimpleStorage = artifacts.require("./SimpleStorage.sol");

 

module.exports= function(deployer) {

  deployer.deploy(SimpleStorage);

};

        For syntax issues, see: http://truffleframework.com/docs/getting_started/migrations .

        To deploy it to the live network, first modify truffle.js to:

module.exports= {

  networks: {

    development: {

      host: "localhost",

      port: 8545,

      network_id: "*" // Match anynetwork id

    },

    live: {

      host: "localhost",

      port: 8545,

      network_id: "*",

      gas:3000000

    }

  }

};

Execute again:

        trufflemigrate --network live

The contract will be deployed to the live network.

        Similar to compiling, this operation will only deploy contracts with new or modified migration files.

7. Console debugging

truffle console –network live

into the console. Commands are the same as commands, but truffle is not needed at this time. Pay attention to the specified network, otherwise there will be an error that the contract is not deployed.

Take a look at the contract deployed on the network:

Replace the contract with SS and enter it in a short code, pay attention to the syntax:

var SS=SimpleStorage.at(“0x1e7e7282e7f1279ddb24c493243ac891d320e59a”)

Next, you can call the functions in the contract. There are two functions set and get in SimpleStorage:

Press Ctrl+C to exit the console.

8. Run the test script

truffle test

Error below error:

This is because the development network has no gas parameter set.

implement:

        truffletest --network live

No more errors are reported, because the live network has set the gas parameter. However, there is no test script placed in the test folder at this time, so the prompt is as follows:

9.         Set up the server

truffle serve

"Serving static assets in ./build on port 8080" means " Serving static assets in ./build on port 8080 ".

The following error occurs:

Not yet resolved.

10.     Force recompile and deploy all contracts

truffle migrate --reset--network live

Note the specified network.

11.     Clear the network and deployed contracts

truffle networks--clean

12.     Integrate nodejs [Reference: Truffle3.0 integrates NodeJS and fully runs through http://www.jianshu.com/p/eac99bb0a9bc ]

npm init

In the initialization process, there are some prompts with parentheses, just press the prompts in the parentheses to input.

Install the Truffle contract abstraction layer runtime environment:

Install the web3 environment:


Guess you like

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