区块链 | Distributed Ledger Technology区块链大学区块链实践专题

<new version> TruffleDapp_

2018-09-13  本文已影响108人  Olly_Zhang
This article is a summary of how I deploy my first metacoin smart contract to ethereum in the dev env, and all the issues I have met in the deployment are also marked down.

Getting started

Basic tools we need -

NPM is a package manager for Node.js packages or modules.

Ganachecli: a Nodejs based Ethereum client for testing and development. it runs locally and mimics a full ethererum client but is much faster making it idea for testing and development.

Truffle: a development env, testing framework, and asset pipeline for ethereum. Basically, it helps you delpoy your contracts to the blockchain, and hoop your front-end up to your deployed contracts

MetaMask: an Ethereum light client and Chrome extension that lets users interact with the blockchain without downloading the whole blockchain. In other words, it allows you to interact with dApps in an ordinary Chrome browser.

OS preparation

Create a vm of ubuntu-16.04.4 from VMware workstation, connect it with Putty.

Install environment

1. install nodejs 5+ (npm)

$ npm install -g web3

2. install truffle

$ npm install -g truffle

To verify it installed, type truffle list in the terminal to list all truffle commands.

3.Init project

$mkdir metacoin

$cd metacoin

Using truffle Dapp framework to init a project

$truffle unbox webpack

Main components in the directory: app/---the entry file to your Dapp including javascript used to talk with smart contract, css file for frontend  and html. contracts/---three contracts by default, metacoin.sol; migration.sol; ConvertLab.sol. node_modules/ ---required modules.

4. project deployment

Before smart contract deployed, we need to install an ethereum client as your local personal blockchain, here i use the test dev env of ganache-cli(new version of testrpc);

$npm install -g ganache-cli

$ganache-cli -h <local IP>

Ganachecli default port: 8545.

5. $truffle migrate

If there is error warning you unable to connect to enthereum client, you need open truffle.js to replace the default port of 7545 with 8545.

Make sure the port and host ganache run in the same with $ganache-cli then smart contract could successfully deploy to the blockchain

6. Dapp development

$npm run dev

7. Open http://localhost:8080, you should be able to see

8. Add Metamask to chrome

It lets you run Ethereum stuff in a browser without a node.

choose the private network localhost:8545

import private keys to generate all the 10 accounts in Metamask.

Now you should be able to send metacoin to any accounts in MetaMask

Here takes an example of voting App

How an Ethereum Dapp looks like at a high level:

上一篇下一篇

猜你喜欢

热点阅读