目前最為快捷、迅速的上手ethereum開發的方法是使用:testrpc和truffle這兩種工具。其中必須先行安裝Node。js和npm

1 安裝 node。js 和 npm

Node。js 下載安裝包,npm 在過程中同步安裝。

檢查命令Node。js和npm是否安裝成功:若安裝成功返回版本號

node -v

npm -v

2 安裝truffle和testrpc

$ npm install -g truffle

ethereumjs/testrpc 下載testrpc,進入所在目錄。

$ sudo npm install -g ethereumjs-testrpc

3 選擇合適的開發IDE

如同所有的程式語言一樣,初學開發者總是會被多如牛毛的開發工具和開發框架所困惑。這種選擇本身很痛苦,在選擇的過程中也令人沮喪。開發一個Ethereum平臺上的分散式應用程式Dapp,我嘗試和評估了很多工具也經歷了相似的痛苦和迷茫。最終比較推薦如下的一套開發組合:

A hackable text editor for the 21st Century 整合開發IDE Atom Editor

ethereum/mist Ethereum錢包

solc Solidity Command Line Interface (CLI) 編譯器

iTerm2 - macOS Terminal Replacement 終端軟體,其他系統自行選擇

4 初始化開發環境

新建一個myproject目錄

$ mkdir myproject

$ cd myproject

$ truffle init

truffle 會自動安裝一個樣例工程,我們只需在此基礎上進行開發,極大地提升了工作效率。這個樣例工程是MetaCoin,一個簡單的輔幣合約contract。待testrpc執行後,你可在終端中輸入`truffle compile` 首先編譯樣本合約, `truffle migrate` 部署合約到testrpc節點。

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

注意:MacOS的使用者,truffle會被。DS_Store 檔案所迷惑報錯,請刪除這些隱藏檔案。

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

在另一個終端視窗開啟testrpc

$ testrpc

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

成功啟動後,testrpc模擬生成一個私有blockchain網路和testrpc節點,並建立十個賬戶供開發者使用。

5 即時瀏覽器編譯器

Remix - Solidity IDE 從Remix裡面可以提供很多參考資料和資訊,例如此transaction消耗的gas

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

6 永恆的helloworld工程

首先建立新的工程

$ truffle create contract helloworld

這樣在contracts目錄下,新建了helloworld。sol檔案,在此檔案中輸入程式碼。

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

定義一個balance變數,在建構函式中賦值

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

編譯truffle complie,生成helloworld。json檔案

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

第二步是migration

‘truffle migrate’ migration是指truffle將工程中多個contract按照相關的順序相互連結和部署deploy的過程,各種contract彼此之間有依賴關係(dependency)。不僅如此,truffle的migration功能能夠判斷在整條blockchain上,這個contract是否之前曾被部署過。

首先編輯 2_deploy_contracts。js 檔案

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

var helloworld = artifacts。require(“。/helloworld。sol”);

deployer。deploy(helloworld);

此處deployer。link(ConvertLib, MetaCoin),稍作解釋:

truffle裡面包含兩種contract:

ConvertLib 屬於library contract, 在網路中會被無數的其他contract呼叫的contract。這種設計的原因是,每種操作在blockchain上都是昂貴的需要支付真金白銀。類似此種被多次呼叫的contract你並不希望他出現在成千上百個其他contract內部。reference lib

第二種contract,即普通傳統smart contract

link方法就是將contract進行連線。

輸入命令

$ truffle migrate ——reset

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

第三 truffle console

新建一個終端視窗

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

$ helloworld。deployed()

輸出一系列contract的配置和引數

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

第四:獲取balance:

$ var hw

$ helloworld。deployed()。then(function(instance){hw = instance})

$ hw。balance。call()。then(console。log())

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

我們可以清楚看到結果是1000,這裡的獲取balance屬於read-only指令

有兩種方式與blockchain發生互動:

Read-only指令或方法:這些方法並不改變blockchain狀態。 Query / call 發出問詢獲取某個值或者狀態,執行純計算指令由你的計算機負責完成,與blockchain無關,這些指令僅讀取狀態,執行計算,返回結果,由本地計算機執行完成無需消耗gas,定義時加constant

Transactional 方法: 這種方法在contract中改變blockchain狀態或者發生資金流動,必須釋出transaction到blockchain,有整個Eethereum Virtual Machine 執行並消耗gas。Update the state of blockchain。

第五:改變balance的值,狀態

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

建立一個deposit函式,重新compile 和 migrate, 務必加上——reset引數

$ truffle migrate ——reset

獲取賬戶資訊:accounts[0] 賬戶擁有999631 ether

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

給這個helloworld 本身增加500 balance

$ helloworld。deployed()。then(function(instance) {return instance。deposit(500)})。then(console。log());

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

使用EthereumJS TestRPC和truffle搭建ethereum開發環境——Helloworld

此交易完成:有一個交易的hash碼,同時helloworld本身變成1500 balance。

非常明顯此交易改變了blockchain的狀態,因此必然消耗了gas,因此我們看到accounts[0]的ether變為999604。消耗999631 - 999604 = 27

到此為止,我們完成了一個簡單的Helloworld,謝謝。

Reference:

1。

https://www。

youtube。com/watch?

v=8jI1TuEaTro

2。

https://

medium。com/zeppelin-blo

g/the-hitchhikers-guide-to-smart-contracts-in-ethereum-848f08001f05

3。

https://

medium。com/@ConsenSys/a

-101-noob-intro-to-programming-smart-contracts-on-ethereum-695d15c1dab4

4。 TruffleFramework。com - Truffle Framework

5。 Problem with Truffle Console: Cannot read property ‘call’ of undefined