If you don’t like to waste time setting up your development environment, you can use this online tutorial from Wisdom:

  • Ethereum DApp combat development introduction
  • Decentralized e-commerce DApp combat development

First, install the DApp development environment

1.1 installation Node. Js

We are using the official long-supported 8.10.0LTS version. Click this link to download the 32-bit installer. The 32-bit installer is available for both 32-bit and 64-bit systems. If you are sure your system is 64-bit, you can also download the 64-bit package. Download and install directly. Once installed, a console window opens and Node is ready to use:

C: \ Users \ hubwiz > node - v v8.10.0Copy the code

1.2 installation Geth

Download the 64-bit or 32-bit Geth installer and install. After the installation is complete, open a console and run the following command to verify the installation:

C:\Users\hubwiz> geth version
Geth
Version: 1.8.3-stable
Copy the code

1.3 Installing the Solidity compiler

Solidity is the programming language for developing Ethereum smart contracts. For those not familiar, check out the Ethereum Solidity Development Language introduction.

C:\Users\hubwiz> npm install –g solc
Copy the code

After the installation is complete, run commands to verify the installation

C: \ Users \ hubwiz > solcjs version 0.40.2 + commit. 3155 dd80 Emscripten. ClangCopy the code

1.4 installation web3

The Web3 installation process uses Git, so you need to install the Windows Version of the Git command line first. Download a 64-bit or 32-bit Git installer and continue installing Web3 after a local installation.

C:\Users\hubwiz> npm install –g [email protected]
Copy the code

Installation verification:

C: \ Users \ hubwiz > node - p'require("web3")'{[Function: Web3] will: {... }}Copy the code

1.5 Installing the Truffle Framework

Execute the following command to install the Truffle framework:

C:\Users\hubwiz> npm install –g truffle
Copy the code

Verify installation:

C: Users\hubwiz> truffle. CMD version truffle v4.1.3 (core 4.1.3)Copy the code

1.6 installation webpack

Run the following command to install webpack:

C:\Users\hubwiz> npm install –g [email protected]
Copy the code

Verify the installation

C: \ Users \ hubwiz > 3.11.0 webpack - vCopy the code

Two, run the private chain node

2.1 Genesis Block configuration

Create a node directory node1 and create the genesis Block configuration file for the private chain in it:

C:\Users\hubwiz> mkdir node1
C:\Users\hubwiz> cd node1
C:\Users\hubwiz\node1> notepad private.json
Copy the code

Then edit the following:

{
    "config": {
        "chainId": 7878,
        "homesteadBlock": 0."eip155Block": 0."eip158Block": 0}."difficulty": "200"."gasLimit": "2100000"."alloc": {
        "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
        "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000"}}}Copy the code
  • config.chainIdTo declare the ethereum network number, select a number greater than 10.
  • difficultyUsed to declare the difficulty of mining, the smaller the value, the lower the difficulty, and the faster the block.

2.2 Initializing a Private Chain Node

Execute geth init to initialize the private link node:

C:\Users\hubwiz\node1> geth --datadir .\data init private.json
Copy the code

This creates a data directory under the current directory to hold block data and account information:

C:\Users\hubwiz\node1> dir
data private.json
Copy the code

You can write the above commands to a script init.cmd to avoid typing too many memorized items each time. The content of the document is as follows:

geth --datadir .\data init private.json
Copy the code

When the next node is deployed, this script can be executed directly for initialization. For example, on another machine:

C:\Users\hubwiz\node1> init.cmd
Copy the code

2.3 Starting a Private Link Node

Start the node from the specified private link data directory and set a different network number:

C:\Users\hubwiz\node1> geth --rpc --datadir .\data --networkid 7878 console
Copy the code

Also, you can use a script console. CMD to simplify the input when starting a node. The file reads as follows:

Geth -- RPC \ -- rpcAddr 0.0.0.0 \ -- rpccorsDomain"*" \
      --datadir ./data \
      --networkid 7878 \
      console
Copy the code
  • rpcaddrParameter used to declare the node RPC API listening address, set to 0.0.0.0 can access the API from other machines;
  • rpccorsdomainParameters are intended to address the security limitations of Web3 making cross-domain calls from the browser. To start the node later, simply execute this script directly:
C:\Users\hubwiz\node1> console.cmd
Copy the code

2.4 Account Management

2.4.1 Viewing the Account List

On the GETH console, use the accounts property of the ETH object to view the current account list:

> eth.accounts
[]
Copy the code

Because we haven’t created the account yet, the list is still empty.

2.4.2 Creating an Account

On the geth console, create a newAccount using the newAccount() method of the personal object with the password of your choice:

> personal.newAccount('78787878')
0xd8bcf1324d566cbec5d3b67e6e14485b06a41d49
Copy the code

The output is the newly created account address (public key), and your output will not be the same as in the example above. Geth is saved to the keystore file in the data directory. Remember your password and use it later.

2.4.3 Querying the Account Balance

On the Geth console, use the getBalance() method of the personal object to get the balance of the specified account as the account address:

> eth.getBalance(eth.accounts[0])
0
Copy the code

Or directly enter the account address:

> eth.getBalance('0xd8bcf1324d566cbec5d3b67e6e14485b06a41d49')
0
Copy the code

The newly created account has a balance of 0.

2.4.4 dig

An account without money can do nothing but mine to earn some money. To start mining, execute the start() method on the Miner object on the GEth console:

> miner.start(1)
Copy the code

After a few minutes, check your account balance:

> the eth. The getBalance (eth. Accounts [0]) 2.695 e+21Copy the code

A lot of money, 2695ETH, the current market value of nearly 5 million YUAN, ha. Execute the stop() method on the Miner object to stop mining:

> miner.stop()
Copy the code

2.4.5 Unlocking an Account

An unlocked account is required to deploy the contract. Unlock the specified account using the personal object’s unlockAccount() method on the Geth console with the account address and the account password (the password specified when the account was created) :

> eth.unlockAccount(eth.accounts[0],'78787878')
true
Copy the code

Build a sample project

3.1 Creating a DApp Project

Execute the following command to create and access the project directory:

C:\Users\hubwiz> mkdir demo
C:\Users\hubwiz> cd demo
Copy the code

Then initialize the skeleton structure with the WebPack template:

C:\Users\ Hubwiz \demo> truffle. CMD unbox webpack Downloading... Unpacking... Setting up... Unbox successful. Sweet!Copy the code

3.2 Installing the NPM package the project depends on

Run the following command to install the NMP package:

C:\Users\hubwiz\demo$ npm install
Copy the code

3.3 Modifying Truffle Configurations

If you use the graphical version of Ganache, there is no need to modify the truffle.js configuration file. Otherwise, in truffle. Js, change port to 8545 because ganache-cli listens on port 8545:

module.exports = {
  networks:{
    development: {
      port: 8545
    }
  }
}
Copy the code

3.4 Starting a Node

Execute the following command to start the node emulator to deploy the contract and execute the transaction:

C:\Users\hubwiz\node1> console.cmd
Copy the code

Note: In order to deploy the contract on the node, don’t forget to unlock the account after starting GEth:

> personal.unlockAcount(eth.accounts[0],'78787878')
true
Copy the code

3.5 Compiling contracts

Execute the following command to compile the project contract:

C:\Users\hubwiz\demo> truffle.cmd compile
Copy the code

3.6 Deployment Contract

Execute the following command to deploy the contract:

C:\Users\hubwiz\demo> truffle.cmd migrate
Copy the code

If you forgot to unlock your account on the Geth console, you will see the following error:

Error: authentication needed: password or unlock
Copy the code

If the account has been unlocked correctly, you should see that the deployment process stops in the following state:

Replacing Migrations... ... 0 x3088762a5bc9...Copy the code

This is because Truffle is waiting for the deployment transaction to submit, but we haven’t started mining in the private chain yet. Now switch back to the GETH terminal window to view the status of the trading pool:

> txpool.status
{
  pending:1,
  queued:0
}
Copy the code

Sure enough, there was a pending deal! Just start digging:

> miner.start(1)
Copy the code

Wait a moment to check the status of the trading pool:

> txpool.status
{
  pending:0,
  queued:0
}
Copy the code

The transaction has been successfully submitted. We can stop mining because it takes too much CPU:

> miner.stop()
Copy the code

Now switch back to the Truffle terminal and the deployment process executes correctly.

3.7 start DApp

Run the following command to start the DApp:

C:\Users\hubwiz\demo> npm run dev
Copy the code

Just visit http://localhost:8080 in your browser

If you want your DApp to be accessible from another machine, modify package.json:

{
  scripts:{
    "dev": "Webpack dev - server - host 0.0.0.0." "}}Copy the code

The tutorials

  • Ethereum DApp development environment -Ubuntu platform
  • Ethereum DApp development environment setup – Windows
  • Ubuntu Ethereum private chain construction tutorial
  • Ethereum development introductory free tutorial